-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
executable file
·280 lines (211 loc) · 7.67 KB
/
helper.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
#
# Copyright (c) 2021 Peter Schüller, Technische Universität Wien
# MIT License
#
#
# This script helps to build/deploy the AI4Industry pilot
# for the AI4EU Experiments Platform.
# It basically helps with docker commands and compiling protobuf.
#
import logging
import os
import shutil
import subprocess
import sys
# we describe each image and which ports need to be redirected to which ports
# according to the container specification,
# the gRPC server must run at 8061 and the web gui (if exists) at 8062
METADATA = {
'gui': {
'version': '1.0',
'ports': {8061: 8001, 8062: 8000},
},
'skillmatching': {
'version': '1.0',
'ports': {8061: 8002},
},
'planning': {
'version': '1.0',
'ports': {8061: 8003},
},
'timeprediction': {
'version': '1.0',
'ports': {8061: 8004},
},
}
# this is used as prefix for local images and containers
BASENAME = 'ai4eu-ai4industry'
REMOTE_REPO = 'cicd.ai4eu-dev.eu:7444/pilots/ai4industry'
# REMOTE_REPO = 'peterschueller/test'
COMPONENTS = ['gui', 'skillmatching', 'planning', 'timeprediction']
USAGE = '''
Helps to build/deploy the AI4Industry Pilot for the AI4EU Experiments Platform.
Usage for building and pushing to docker registry:
{self} build [component]
{self} tag-and-push [component]
Usage for running locally using docker:
{self} build-protobufs
{self} run {{detached|interactive|shell}} [component]
{self} orchestrate
Usage for maintenance of local runs:
{self} follow [component]
{self} list
{self} kill [component]
where [component] is one of {comps}
'''
def cmd_info(cmd, cwd=None):
if cwd is None:
sys.stderr.write("RUNNING %s\n" % cmd)
else:
sys.stderr.write("IN %s RUNNING %s\n" % (cwd, cmd))
sys.stderr.flush()
class ShowUsage(Exception):
pass
def build_protobufs():
cmd = 'python3 -m grpc_tools.protoc --python_out=. --proto_path=. --grpc_python_out=. *.proto'
for component in COMPONENTS + ['orchestrator']:
logging.info('running in %s', component)
cmd_info(cmd, cwd=component)
subprocess.check_call(cmd, cwd=component, shell=True, stdout=sys.stdout, stderr=sys.stderr)
if 'planning' in COMPONENTS:
dest = 'planning/scripts/'
logging.info("moving 'planning' generated files into '%s'", dest)
for genf in ['planning_pb2.py', 'planning_pb2_grpc.py']:
try:
os.remove(dest + genf)
except FileNotFoundError:
pass # ignore if file already exists
shutil.move('planning/' + genf, dest)
def build(component):
logging.info("building component %s", component)
cmd = "docker build -t {}:{}-{} .".format(
BASENAME, component, METADATA[component]['version'])
cmd_info(cmd, cwd=component)
subprocess.check_call(cmd, cwd=component, shell=True, stdout=sys.stdout, stderr=sys.stderr)
def tag_and_push(component):
logging.info("tagging component %s", component)
x = {
'basename': BASENAME,
'comp': component,
'ver': METADATA[component]['version'],
'repo': REMOTE_REPO
}
cmd = "docker tag {basename}:{comp}-{ver} {repo}:{comp}-{ver}".format(**x)
cmd_info(cmd, cwd=component)
subprocess.check_call(cmd, cwd=component, shell=True, stdout=sys.stdout, stderr=sys.stderr)
logging.info("pushing component %s", component)
cmd = "docker push {repo}:{comp}-{ver}".format(**x)
cmd_info(cmd, cwd=component)
subprocess.check_call(cmd, cwd=component, shell=True, stdout=sys.stdout, stderr=sys.stderr)
print("\u001b[31mpushed %s to repo/path %s with tag %s-%s\u001b[37m" % (component, x['repo'], x['comp'], x['ver']))
def run(mode, component):
if mode not in ['detached', 'interactive', 'shell']:
raise ShowUsage()
x = {
'basename': BASENAME,
'comp': component,
'ver': METADATA[component]['version'],
'ports': ' '.join([
'--publish={ext}:{int}'.format(int=int, ext=ext)
for int, ext in METADATA[component]['ports'].items()
]),
'repo': REMOTE_REPO,
'args': '--rm', # remove image after run
'cmd': ''
}
if mode == 'interactive':
x['args'] += ' -it'
elif mode == 'detached':
x['args'] += ' -d'
elif mode == 'shell':
x['args'] += ' -it'
x['cmd'] = '/bin/bash'
cmd = 'docker run {args} --name {basename}-{comp} {ports} {basename}:{comp}-{ver} {cmd}'.format(**x)
cmd_info(cmd, cwd=component)
subprocess.check_call(cmd, cwd=component, shell=True, stdout=sys.stdout, stderr=sys.stderr)
def kill(component):
cmd = "docker container ls -q --filter name={}-{}".format(BASENAME, component)
cmd_info(cmd)
out = subprocess.check_output(cmd, shell=True)
out = out.decode('utf8').strip()
if out == '':
logging.warning("nothing to kill for %s", component)
return
logging.info("got container %s for component %s", out, component)
cmd = "docker container kill {}".format(out)
cmd_info(cmd)
subprocess.call(cmd, shell=True, stdout=sys.stdout, stderr=sys.stderr)
logging.info("removing container %s", component)
cmd = "docker container rm {}-{}".format(BASENAME, component)
cmd_info(cmd)
# ignore failure!
subprocess.call(cmd, shell=True, stdout=sys.stdout, stderr=sys.stderr)
def follow(component):
cmd = "docker logs -f {}-{}".format(BASENAME, component)
cmd_info(cmd)
subprocess.call(cmd, shell=True, stdout=sys.stdout, stderr=sys.stderr)
def main():
logging.basicConfig(level=logging.INFO)
try:
if len(sys.argv) == 1:
raise ShowUsage()
mode = sys.argv[1]
args = sys.argv[2:]
if mode == 'build-protobufs':
if args != []:
raise ShowUsage()
build_protobufs()
elif mode == 'build':
if len(args) == 0:
for c in COMPONENTS:
build(c)
elif args[0] in COMPONENTS:
build(args[0])
else:
raise ShowUsage()
elif mode == 'tag-and-push':
if len(args) == 0:
for c in COMPONENTS:
tag_and_push(c)
elif args[0] in COMPONENTS:
tag_and_push(args[0])
else:
raise ShowUsage()
elif mode == 'run':
if len(args) == 1:
mode = args[0]
for c in COMPONENTS:
run(args[0], c)
elif len(args) == 2 and args[1] in COMPONENTS:
run(args[0], args[1])
else:
raise ShowUsage()
elif mode == 'kill':
if len(args) == 0:
for c in COMPONENTS:
kill(c)
elif args[0] in COMPONENTS:
kill(args[0])
else:
raise ShowUsage()
elif mode == 'follow':
if len(args) == 1 and args[0] in COMPONENTS:
follow(args[0])
else:
raise ShowUsage()
elif mode == 'list':
subprocess.check_call('docker container list --all --filter name={}*'.format(BASENAME), shell=True, stdout=sys.stdout, stderr=sys.stderr)
elif mode == 'orchestrate':
cmd = 'python orchestrator.py'
cwd = 'orchestrator'
cmd_info(cmd, cwd=cwd)
subprocess.check_call(cmd, cwd=cwd, shell=True, stdout=sys.stdout, stderr=sys.stderr)
except ShowUsage:
logging.error(USAGE.format(
self=sys.argv[0],
comps=' '.join(COMPONENTS)
))
return -1
if __name__ == '__main__':
main()