|
@@ -0,0 +1,89 @@
|
|
1
|
+#!/usr/bin/python3
|
|
2
|
+
|
|
3
|
+import argparse
|
|
4
|
+import inspect
|
|
5
|
+import json
|
|
6
|
+import subprocess
|
|
7
|
+import sys
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+def inspect_container(container):
|
|
11
|
+ output = subprocess.check_output(['docker', 'inspect', '--type=container', container])
|
|
12
|
+ return json.loads(output.decode('utf-8'))[0]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+def handle_binds(container, config):
|
|
16
|
+ if container['HostConfig']['Binds']:
|
|
17
|
+ config['args'].extend(['--volume=%s' % bind for bind in container['HostConfig']['Binds']])
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+def handle_image(container, config):
|
|
21
|
+ config['image'] = container['Config']['Image']
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+def handle_name(container, config):
|
|
25
|
+ # Trim the leading / off the name. They're equivalent from docker's point of view, but having
|
|
26
|
+ # the plain name looks nicer from a human point of view.
|
|
27
|
+ config['args'].append('--name=%s' % container['Name'][1:])
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+def handle_network_mode(container, config):
|
|
31
|
+ network = container['HostConfig']['NetworkMode']
|
|
32
|
+ if network != 'default':
|
|
33
|
+ config['args'].append('--net=%s' % network)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+def handle_ports(container, config):
|
|
37
|
+ ports = container['HostConfig']['PortBindings']
|
|
38
|
+ if ports:
|
|
39
|
+ for port, bindings in ports.items():
|
|
40
|
+ for binding in bindings:
|
|
41
|
+ if binding['HostIp']:
|
|
42
|
+ config['args'].append('-p=%s:%s:%s' % (binding['HostIp'], binding['HostPort'], port))
|
|
43
|
+ elif binding['HostPort']:
|
|
44
|
+ config['args'].append('-p=%s:%s' % (binding['HostPort'], port))
|
|
45
|
+ else:
|
|
46
|
+ config['args'].append('-p=%s' % port)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+def handle_restart(container, config):
|
|
50
|
+ policy = container['HostConfig']['RestartPolicy']
|
|
51
|
+ if policy and policy['Name'] != 'no':
|
|
52
|
+ arg = '--restart=%s' % policy['Name']
|
|
53
|
+ if policy['MaximumRetryCount'] > 0:
|
|
54
|
+ arg += ':%s' % policy['MaximumRetryCount']
|
|
55
|
+ config['args'].append(arg)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+def handle_volumes_from(container, config):
|
|
59
|
+ if container['HostConfig']['VolumesFrom']:
|
|
60
|
+ config['args'].extend(['--volumes-from=%s' % cont for cont in container['HostConfig']['VolumesFrom']])
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+def functions():
|
|
64
|
+ return [m for m in inspect.getmembers(sys.modules[__name__]) if inspect.isfunction(m[1])]
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+def handlers():
|
|
68
|
+ return [func for (name, func) in functions() if name.startswith('handle_')]
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+def main():
|
|
72
|
+ parser = argparse.ArgumentParser(description='Reruns docker containers with different parameters.')
|
|
73
|
+ parser.add_argument('container', type=str, help='The container to rerun')
|
|
74
|
+ parser.add_argument('-d', '--dry-run', action='store_true', help='Don\'t actually re-run the container, just print what would happen.')
|
|
75
|
+ args = parser.parse_args()
|
|
76
|
+ container = inspect_container(args.container)
|
|
77
|
+
|
|
78
|
+ docker_config = {'args': ['-d'], 'image': ''}
|
|
79
|
+ for handler in handlers():
|
|
80
|
+ handler(container, docker_config)
|
|
81
|
+
|
|
82
|
+ if args.dry_run:
|
|
83
|
+ print('docker stop %s' % args.container)
|
|
84
|
+ print('docker rm %s' % args.container)
|
|
85
|
+ print('docker run %s %s' % (' '.join(docker_config['args']), docker_config['image']))
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+if __name__ == "__main__":
|
|
89
|
+ main()
|