-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-dup.py
404 lines (318 loc) · 11.6 KB
/
docker-dup.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/python3
# imports
from argparse import ArgumentParser
from os.path import isfile
from subprocess import call
from yaml import dump, load
# constants
ext = '.yml'
settings_path = 'settings' + ext
compose_path = 'docker-compose' + ext
# create settings if it doesn't exist
if not isfile(settings_path):
# create dict
new_settings = {
'version': '3.7',
'image_dir': './',
'variables': {
'subnet': '10.0'
},
'services': None,
'networks': None,
'scripts': None
}
# write to the file
with open(settings_path, 'w') as new_settings_file:
dump(new_settings, new_settings_file, default_flow_style=False)
# run command function
def run(args):
# get args
if args.config_file.endswith(ext):
config_path = args.config_file
else:
config_path = args.config_file + ext
num = args.num
# check if config file exists
if not isfile(config_path):
print(f'Error: config_file {config_path} does not exist.')
exit(1)
# check num
if num < 1 or num > 99:
print('Error: num must be 1-99.')
exit(1)
# load settings
with open(settings_path, 'r') as settings_file:
settings = load(settings_file)
# load config file
with open(config_path, 'r') as config_file:
config = load(config_file)
# combine services
services = {}
if settings['services']:
services.update(settings['services'])
if config['services']:
services.update(config['services'])
# combine scripts
scripts = []
if settings['scripts']:
scripts.extend(settings['scripts'])
if config['scripts']:
scripts.extend(config['scripts'])
# start compose dict
compose = {
'version': settings['version'],
'services': {},
'networks': {}
}
# start replacements dict
reps = {
'num': '',
'num-l': '',
'name': '',
'name-n': ''
}
if settings['variables']:
reps.update(settings['variables'])
# iterate over services
for name, service in services.items():
# if service has codes
if '%' in name:
# get name and codes
split = name.split('%')
codes = split[0]
new_name = reps['name'] = split[1]
# parse codes and make service dicts
first = others = {}
if 'b' in codes:
first = {
'build': settings['image_dir'] + new_name,
'image': new_name,
**service
}
if 'd' in codes:
others = {
'image': new_name,
'depends_on': [new_name + '-1'],
**service
}
# iterate if d code
if 'd' in codes:
for i in range(1, num + 1):
# set replacements
reps['num'] = str(i)
reps['num-l'] = str(i).zfill(2)
reps['name-n'] = f'{new_name}-{str(i)}'
# if first instance and b code
if i == 1 and 'b' in codes:
compose['services'][reps['name-n']] = rep(first, reps)
# if other instances
else:
if 'b' in codes:
compose['services'][reps['name-n']] = rep(others, reps)
else:
compose['services'][reps['name-n']] = rep(service, reps)
# if one instance
else:
compose['services'][new_name] = rep(first, reps)
# if no codes
else:
reps['name'] = name
compose['services'][name] = rep(service, reps)
# iterate over networks
if settings['networks']:
for name, network in settings['networks'].items():
# check if network has codes
if '%' in name:
# get name and codes
split = name.split('%')
codes = split[0]
new_name = reps['name'] = split[1]
# iterate if d code
if 'd' in codes:
for i in range(1, num + 1):
# set replacements
reps['num'] = str(i)
reps['num-l'] = str(i).zfill(2)
reps['name-n'] = f'{new_name}-{str(i)}'
# add network
compose['networks'][reps['name-n']] = rep(network, reps)
# no codes
else:
reps['name'] = name
compose['networks'][name] = rep(network, reps)
# write compose dict to file
with open(compose_path, 'w') as compose_file:
# write num as comment
compose_file.write(f'#{num}\n')
# write scripts as comments
for script in scripts:
compose_file.write(f'#{script}\n')
# write yaml
dump(compose, compose_file, default_flow_style=False)
# remove previous instances
if not args.restart:
stop(None)
# run compose
command = 'DOCKER_CLIENT_TIMEOUT=600 COMPOSE_HTTP_TIMEOUT=600 docker-compose up'
if args.build:
command += ' --build'
if args.detach:
command += ' -d'
call(command, shell=True)
# runi command function
def runi(args):
# remove previous instances
if not args.restart:
stop(None)
# build if option
if args.build:
call(f'docker build {args.image_name} -t {args.image_name}', shell=True)
# run the image
call(f'docker run {args.options} {args.image_name}', shell=True)
# stop command function
def stop(args):
# stop all containers
call('docker stop $(docker ps -a -q)', shell=True)
# remove all containers
call('docker rm $(docker ps -a -q)', shell=True)
# remove unused networks
call('docker network prune -f', shell=True)
# ex command function
def ex(args):
# read compose file
with open(compose_path, 'r') as compose_file:
compose = compose_file.read()
# get num
num = int(compose.splitlines()[0][1])
# load variables
with open(settings_path, 'r') as settings_file:
variables = load(settings_file)['variables']
# start replacements dict
reps = {
'num': '',
'num-l': ''
}
if variables:
reps.update(variables)
# get scripts
scripts = []
for line in compose.splitlines()[1:]:
# if comment
if line[0] == '#':
# if code
if '%' in line[1:]:
# get script and codes
split = line[1:].split('%')
codes = split[0]
script = split[1]
# iterate if d code
if 'd' in codes:
for i in range(1, num + 1):
# set replacements
reps['num'] = str(i)
reps['num-l'] = str(i).zfill(2)
# add script with replacements
scripts.append(rep(script, reps, False))
# if no code
else:
scripts.append(line[1:])
# stop after last comment
else:
break
# run scripts
for script in scripts:
call(script, shell=True)
# clear command function
def clear(args):
# remove all images
call('docker rmi -f $(docker images -q -a)', shell=True)
# mkc command function
def mkc(args):
# get args
if args.config_file.endswith(ext):
config_path = args.config_file
else:
config_path = args.config_file + ext
# create config dict
config = {
'services': None,
'scripts': None
}
# write the dict
with open(config_path + ext, 'w') as config_file:
dump(config, config_file, default_flow_style=False)
# mki command function
def mki(args):
# get image directory from settings
with open(settings_path, 'r') as settings_file:
image_dir = load(settings_file)['image_dir']
# make directory for image
call(f'mkdir {image_dir}{args.image_name}', shell=True)
# make blank Dockerfile
call(f'touch {image_dir}{args.image_name}/Dockerfile', shell=True)
# shell command function
def shell(args):
# get the shell
call(f'docker exec -it {args.container_name} bash', shell=True)
# replacement function
def rep(i, reps, yaml=True):
# convert dict to yaml string if yaml
if yaml:
s = dump(i, default_flow_style=False)
else:
s = i
# do each replacement
for key, value in reps.items():
s = s.replace(f'_{key}_', value)
# return as dict if yaml
if yaml:
return load(s)
else:
return s
# base parser
parser = ArgumentParser(
description='docker-dup - '
'Run multiple instances of a set of Docker containers on different networks via docker-compose. '
f'Settings located in ./{settings_path}, which is created automatically if it does not exist.')
subparsers = parser.add_subparsers(metavar='command', dest='command')
subparsers.required = True
# run subparser
parser_run = subparsers.add_parser('run', help=f'run {ext} config file via docker-compose up')
parser_run.add_argument('config_file', help=f'path to {ext} file with or without the extension')
parser_run.add_argument('num', help='number of instances to create (1-99)', type=int)
parser_run.add_argument('-b', '--build', action='store_true', help='build images before running')
parser_run.add_argument('-d', '--detach', action='store_true', help='run in background')
parser_run.add_argument('-r', '--restart', action='store_true',
help='do not stop and remove all containers and networks before running')
parser_run.set_defaults(func=run)
# runi subparser
parser_runi = subparsers.add_parser('runi', help='run an image as a single container')
parser_runi.add_argument('image_name', help='path of directory image Dockerfile')
parser_runi.add_argument('-o', '--options', help='optional arguments to pass to docker run', type=str, default='')
parser_runi.add_argument('-b', '--build', action='store_true', help='build image before running')
parser_runi.add_argument('-r', '--restart', action='store_true',
help='do not stop and remove all containers and networks before running')
parser_runi.set_defaults(func=runi)
# script subparser
parser_ex = subparsers.add_parser('ex', help='execute scripts for currently running containers')
parser_ex.set_defaults(func=ex)
# stop subparser
parser_stop = subparsers.add_parser('stop', help='stop and remove all containers and networks')
parser_stop.set_defaults(func=stop)
# clear subparser
parser_clear = subparsers.add_parser('clear', help='remove all images with --force')
parser_clear.set_defaults(func=clear)
# mkc subparser
parser_mkc = subparsers.add_parser('mkc', help=f'create {ext} config file template')
parser_mkc.add_argument('config_file', help=f'path to {ext} file with or without the extension')
parser_mkc.set_defaults(func=mkc)
# mki subparser
parser_mki = subparsers.add_parser('mki', help='create docker image template in image directory')
parser_mki.add_argument('image_name', help='name of folder to create the image in')
# shell subparser
parser_shell = subparsers.add_parser('shell', help='get a shell to a running container')
parser_shell.add_argument('container_name', help='name of container, append -# if multiple instances')
parser_shell.set_defaults(func=shell)
# call respective function on args
arguments = parser.parse_args()
arguments.func(arguments)