forked from wmo-im/wis2box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wis2box-ctl.py
executable file
·209 lines (180 loc) · 6.96 KB
/
wis2box-ctl.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
###############################################################################
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
###############################################################################
import argparse
import os
import subprocess
DOCKER_COMPOSE_ARGS = """
--file docker/docker-compose.yml
--file docker/docker-compose.override.yml
--file docker/docker-compose.monitoring.yml
--env-file dev.env
--project-name wis2box_project
"""
parser = argparse.ArgumentParser(
description='manage a compposition of docker containers to implement a wis 2 box',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument(
'--simulate',
dest='simulate',
action='store_true',
help='simulate execution by printing action rather than executing')
commands = [
'build',
'config',
'down',
'execute',
'lint',
'logs',
'login',
'prune',
'restart',
'start',
'start-dev',
'status',
'stop',
'up',
'update',
]
parser.add_argument('command',
choices=commands,
help="""
- config: validate and view Docker configuration
- build [containers]: build all services
- start [containers]: start system
- start-dev [containers]: start system in local development mode
- login [container]: login to the container (default: wis2box)
- login-root [container]: login to the container as root
- stop: stop [container] system
- update: update Docker images
- prune: cleanup dangling containers and images
- restart [containers]: restart one or all containers
- status [containers|-a]: view status of wis2box containers
- lint: run PEP8 checks against local Python code
""")
parser.add_argument('args', nargs=argparse.REMAINDER)
args = parser.parse_args()
def split(value: str) -> list:
"""
Splits string and returns as list
:param value: required, string. bash command.
:returns: list. List of separated arguments.
"""
return value.split()
def walk_path(path: str) -> list:
"""
Walks os directory path collecting all CSV files.
:param path: required, string. os directory.
:returns: list. List of csv filepaths.
"""
file_list = []
for root, _, files in os.walk(path, topdown=False):
for name in files:
if name.endswith('.py'):
file_list.append(os.path.join(root, name))
return file_list
def run(args, cmd, asciiPipe=False) -> str:
if args.simulate:
if asciiPipe:
print(f"simulation: {' '.join(cmd)} >/tmp/temp_buffer$$.txt")
else:
print(f"simulation: {' '.join(cmd)}")
return '`cat /tmp/temp_buffer$$.txt`'
else:
if asciiPipe:
return subprocess.run(
cmd, stdout=subprocess.PIPE).stdout.decode('ascii')
else:
subprocess.run(cmd)
return None
def make(args) -> None:
"""
Serves as pseudo Makefile using Python subprocesses.
:param command: required, string. Make command.
:returns: None.
"""
# if you selected a bunch of them, default to all
containers = "" if not args.args else ' '.join(args.args)
# if there can be only one, default to wisbox
container = "wis2box" if not args.args else ' '.join(args.args)
if args.command == "config":
run(args, split(f'docker compose {DOCKER_COMPOSE_ARGS} config'))
elif args.command == "build":
run(args, split(
f'docker compose {DOCKER_COMPOSE_ARGS} build {containers}'))
elif args.command in ["up", "start", "start-dev"]:
run(args, split(
'docker plugin install grafana/loki-docker-driver:latest --alias loki --grant-all-permissions > /dev/null 2>&1'))
run(args, split(
'docker plugin enable loki'))
if containers:
run(args, split(f"docker compose {DOCKER_COMPOSE_ARGS} start {containers}"))
else:
if args.command == 'start-dev':
run(args, split(f'docker compose {DOCKER_COMPOSE_ARGS} --file docker/docker compose.dev.yml up'))
else:
run(args, split(f'docker compose {DOCKER_COMPOSE_ARGS} up -d'))
elif args.command == "execute":
run(args, ['docker', 'exec', '-i', 'wis2box', 'sh', '-c', containers])
elif args.command == "login":
run(args, split(f'docker exec -it {container} /bin/bash'))
elif args.command == "login-root":
run(args, split(f'docker exec -u -0 -it {container} /bin/bash'))
elif args.command == "logs":
run(args, split(
f'docker compose {DOCKER_COMPOSE_ARGS} logs --follow {containers}'))
elif args.command in ["stop", "down"]:
if containers:
run(args, split(f"docker compose {DOCKER_COMPOSE_ARGS} {containers}"))
else:
run(args, split(
f'docker compose {DOCKER_COMPOSE_ARGS} down --remove-orphans {containers}'))
elif args.command == "update":
run(args, split(f'docker compose {DOCKER_COMPOSE_ARGS} pull'))
elif args.command == "prune":
run(args, split('docker builder prune -f'))
run(args, split('docker container prune -f'))
run(args, split('docker volume prune -f'))
_ = run(args,
split('docker images --filter dangling=true -q --no-trunc'),
asciiPipe=True)
run(args, split(f'docker rmi {_}'))
_ = run(args, split('docker ps -a -q'), asciiPipe=True)
run(args, split(f'docker rm {_}'))
elif args.command == "restart":
if containers:
run(args, split(
f'docker compose {DOCKER_COMPOSE_ARGS} stop {containers}'))
run(args, split(
f'docker compose {DOCKER_COMPOSE_ARGS} start {containers}'))
else:
run(args, split(
f'docker compose {DOCKER_COMPOSE_ARGS} down --remove-orphans'))
run(args, split(
f'docker compose {DOCKER_COMPOSE_ARGS} up -d'))
elif args.command == "status":
run(args, split(
f'docker compose {DOCKER_COMPOSE_ARGS} ps {containers}'))
elif args.command == "lint":
files = walk_path(".")
run(args, ('python3', '-m', 'flake8', *files))
if __name__ == "__main__":
make(args)