-
Notifications
You must be signed in to change notification settings - Fork 9
/
start.py
executable file
·143 lines (118 loc) · 4.2 KB
/
start.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
import argparse
import grp
import os
import pathlib
import subprocess
fact_version = os.getenv("FACT_DOCKER_VERSION", "4.0.1")
def create_docker_mount_base_dir_with_correct_permissions(docker_mount_base_dir):
docker_gid = grp.getgrnam("docker").gr_gid
pathlib.Path(docker_mount_base_dir).mkdir(mode=0o770, parents=True, exist_ok=True)
os.chown(docker_mount_base_dir, -1, docker_gid)
def pass_docker_socket_args(args):
docker_gid = grp.getgrnam("docker").gr_gid
docker_socket = os.getenv("DOCKER_HOST", default="/var/run/docker.sock")
return f"""\
--group-add {docker_gid} \
--mount type=bind,source={docker_socket},destination=/var/run/docker.sock \
"""
def mount_relevant_dirs_for_docker_args(docker_mount_base_dir):
return f"""\
--mount type=bind,source={docker_mount_base_dir},destination={docker_mount_base_dir} \
"""
def pull(args):
cmd = f"""docker run \
{pass_docker_socket_args(args)} \
-it \
--rm \
ghcr.io/fkie-cad/fact-core-scripts:{fact_version} pull-containers
"""
subprocess.run(cmd.split())
def initialize_db(args):
cmd = f"""docker run \
-it \
--mount type=bind,source={args.main_cfg_path},destination=/opt/FACT_core/src/config/main.cfg,ro=true \
--rm \
--network {args.network} \
ghcr.io/fkie-cad/fact-core-scripts:{fact_version} \
initialize-db
"""
subprocess.run(cmd.split())
def compose_env(args):
docker_gid = grp.getgrnam("docker").gr_gid
fw_data_dir_gid = os.stat(args.firmware_file_storage_dir).st_gid
create_docker_mount_base_dir_with_correct_permissions(args.docker_mount_base_dir)
print(f"""\
export FACT_DOCKER_DOCKER_GID={docker_gid}
export FACT_DOCKER_DOCKER_MOUNT_BASE_DIR={args.docker_mount_base_dir}
export FACT_DOCKER_FIRMWARE_FILE_STORAGE_DIR={args.firmware_file_storage_dir}
export FACT_DOCKER_FIRMWARE_FILE_STORAGE_DIR_GID={fw_data_dir_gid}
export FACT_DOCKER_FRONTEND_PORT={args.port}
export FACT_DOCKER_MAIN_CFG_PATH={args.main_cfg_path}
export FACT_DOCKER_UWSGI_CONFIG_INI_PATH={args.uwsgi_config_ini_path}
export FACT_DOCKER_VERSION={fact_version}""")
def main():
parser = argparse.ArgumentParser()
parser.set_defaults(func=lambda _: parser.print_usage())
subparsers = parser.add_subparsers()
pull_p = subparsers.add_parser(
"pull",
help="Pull or build all neccessary docker containers required to run FACT.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
pull_p.set_defaults(func=pull)
initialize_db_p = subparsers.add_parser(
"initialize-db",
help="Initialize the postgres database",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
initialize_db_p.set_defaults(func=initialize_db)
initialize_db_p.add_argument(
"--main-cfg-path",
default=f"{os.getcwd()}/main.cfg",
help="Path to main.cfg",
required=False,
)
initialize_db_p.add_argument(
"--network",
help="The docker network that the postgres container runs in",
required=True,
)
compose_env_p = subparsers.add_parser(
"compose-env",
help="Print out sane defaults for FACT_DOCKER_* variables",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
compose_env_p.set_defaults(func=compose_env)
compose_env_p.add_argument(
"--main-cfg-path",
default="$(pwd)/main.cfg",
help="Path to main.cfg",
required=False,
)
compose_env_p.add_argument(
"--uwsgi-config-ini-path",
default="$(pwd)/uwsgi_config.ini",
help="Path to uwsgi_config.ini",
required=False,
)
compose_env_p.add_argument(
"--firmware-file-storage-dir",
help="Path to 'firmware-file-storage-directory'",
required=True,
)
compose_env_p.add_argument(
"--port",
default=5000,
help="The FACT frontend webserver port",
required=False,
)
compose_env_p.add_argument(
"--docker-mount-base-dir",
default="/tmp/fact-docker-mount-base-dir",
help="Has to match docker-mount-base-dir in main.cfg",
)
args = parser.parse_args()
args.func(args)
if __name__ == "__main__":
main()