-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcfsi.py
executable file
·129 lines (98 loc) · 4.09 KB
/
cfsi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
""" Simple CLI for CFSI. Requires Docker and Docker-Compose """
import subprocess
from typing import Callable, Dict, List
from cfsi.cli import CFSICLIParser, generate_container_name
# noinspection PyPep8Naming
class CFSI_CLI:
def __init__(self):
parser = CFSICLIParser()
self.__actions, self.__optional_args = parser.parse()
self.__action_map = self.__get_action_map()
self.__methods = [self.__action_map[action.name] for action in self.__actions]
self.__run()
exit(0)
def __get_action_map(self) -> Dict[str, Callable]:
""" Dict that maps actions to methods """
return {"build": self.__build, "start": self.__start, "init": self.__initialize,
"stop": self.__stop, "clean": self.__clean, "index": self.__index,
"mask": self.__mask, "mosaic": self.__mosaic, "deploy": self.__deploy,
"destroy": self.__destroy, "log": self.__log, "console": self.__console}
def __run(self):
try:
for method in self.__methods:
method()
except Exception as err: # TODO: custom exceptions
raise err
def __build(self):
self.__run_command("docker-compose", "build", "--no-cache")
def __start(self):
self.__run_command("docker-compose", "up", "-d", "db")
def __initialize(self):
name = generate_container_name("CFSI-init")
self.__wait_for_db(name)
command = self.__generate_compose_run_command(name) + [
"odc", "cfsi/scripts/setup/odc_init.sh"]
self.__run_command(*command)
def __stop(self):
self.__run_command("docker-compose", "down")
def __clean(self):
self.__run_command("docker-compose", "down", "--volumes")
def __console(self):
name = generate_container_name("CFSI-console")
self.__wait_for_db(name)
command = self.__generate_compose_run_command(name) + ["odc", "bash"]
self.__run_command(*command)
def __index(self):
name = generate_container_name("CFSI-index")
self.__wait_for_db(name)
command = self.__generate_compose_run_command(name) + [
"odc", "python3", "-m", "cfsi.scripts.index.s2_index"]
self.__run_command(*command)
def __mask(self):
name = generate_container_name("CFSI-mask")
command = self.__generate_compose_run_command(name) + [
"odc", "python3", "-m", "cfsi.scripts.process.create_masks"]
self.__run_command(*command)
def __mosaic(self):
name = generate_container_name("CFSI-mosaic")
command = self.__generate_compose_run_command(name) + [
"odc", "python3", "-m", "cfsi.scripts.process.create_mosaics"]
self.__run_command(*command)
def __deploy(self):
self.__run_command("terraform", "apply")
def __destroy(self):
self.__run_command("terraform", "destroy")
def __log(self):
self.__run_command("docker-compose", "logs", "-f")
@staticmethod
def __run_command(*command: str):
try:
command_list = [part for part in command]
subprocess.run(command_list, check=True)
except subprocess.CalledProcessError as err:
print('\n', err)
def __generate_compose_run_command(self, name: str) -> List[str]:
""" Generates a base for docker-compose run commands """
base = ["docker-compose", "run", "--name", name, "--rm"]
if self.__optional_args.detach and "waiter" not in name:
base[-1] = "-d"
return base
def __wait_for_db(self, name: str = ""):
if name:
name += "_waiter"
else:
name = generate_container_name("CFSI-waiter")
command = self.__generate_compose_run_command(name) + [
"odc", "cfsi/utils/wait-for-it.sh", "db:5432"]
self.__run_command(*command)
if __name__ == "__main__":
try:
CFSI_CLI()
exit(0)
except KeyboardInterrupt:
print("Aborted")
exit(1)
except Exception as error:
print("Unhandled exception: ", error)
exit(1)