Browse Source

Switch to using Container.

tags/v0.1.1
Chris Smith 7 years ago
parent
commit
9251136071
1 changed files with 28 additions and 30 deletions
  1. 28
    30
      docker-rerun

+ 28
- 30
docker-rerun View File

@@ -19,7 +19,7 @@ import sys
19 19
 class Container(object):
20 20
     """Encapsulates information about a container."""
21 21
 
22
-    def __init__(self, name, container_info, image_info):
22
+    def __init__(self, container_info, image_info):
23 23
         """Creates a new Container.
24 24
 
25 25
         Args:
@@ -27,7 +27,7 @@ class Container(object):
27 27
             container_info (dict): Dictionary describing the container state.
28 28
             image_info (dict): Dictionary describing the image state.
29 29
         """
30
-        self.args = ['--name=%s' % name]
30
+        self.args = ['-d']
31 31
         """The arguments passed to docker to create the container."""
32 32
 
33 33
         self.image = ''
@@ -78,64 +78,62 @@ def docker_inspect(target, what):
78 78
     return json.loads(output.decode('utf-8'))[0]
79 79
 
80 80
 
81
-def handle_binds(container, config):
81
+def handle_binds(container):
82 82
     """Copies the volume bind (--volume/-v) arguments."""
83
-    if container['HostConfig']['Binds']:
84
-        config['args'].extend(['--volume=%s' % bind
85
-                               for bind in container['HostConfig']['Binds']])
83
+    container.add_args_from_list('--volume=%s',
84
+                                 lambda c: c['HostConfig']['Binds'])
86 85
 
87 86
 
88
-def handle_image(container, config):
87
+def handle_image(container):
89 88
     """Copies the image argument."""
90
-    config['image'] = container['Config']['Image']
89
+    container.image = container.info['Config']['Image']
91 90
 
92 91
 
93
-def handle_name(container, config):
92
+def handle_name(container):
94 93
     """Copies the name (--name) argument."""
95 94
     # Trim the leading / off the name. They're equivalent from docker's point
96 95
     # of view, but having the plain name looks nicer from a human point of view.
97
-    config['args'].append('--name=%s' % container['Name'][1:])
96
+    container.args.append('--name=%s' % container.info['Name'][1:])
98 97
 
99 98
 
100
-def handle_network_mode(container, config):
99
+def handle_network_mode(container):
101 100
     """Copies the network mode (--net) argument."""
102
-    network = container['HostConfig']['NetworkMode']
101
+    network = container.info['HostConfig']['NetworkMode']
103 102
     if network != 'default':
104
-        config['args'].append('--net=%s' % network)
103
+        container.args.append('--net=%s' % network)
105 104
 
106 105
 
107
-def handle_ports(container, config):
106
+def handle_ports(container):
108 107
     """Copies the port publication (-p) arguments."""
109
-    ports = container['HostConfig']['PortBindings']
108
+    ports = container.info['HostConfig']['PortBindings']
110 109
     if ports:
111 110
         for port, bindings in ports.items():
112 111
             for binding in bindings:
113 112
                 if binding['HostIp']:
114
-                    config['args'].append('-p=%s:%s:%s' % (binding['HostIp'],
113
+                    container.args.append('-p=%s:%s:%s' % (binding['HostIp'],
115 114
                                                            binding['HostPort'],
116 115
                                                            port))
117 116
                 elif binding['HostPort']:
118
-                    config['args'].append('-p=%s:%s' % (binding['HostPort'],
117
+                    container.args.append('-p=%s:%s' % (binding['HostPort'],
119 118
                                                         port))
120 119
                 else:
121
-                    config['args'].append('-p=%s' % port)
120
+                    container.args.append('-p=%s' % port)
122 121
 
123 122
 
124
-def handle_restart(container, config):
123
+def handle_restart(container):
125 124
     """Copies the restart policy (--restart) argument."""
126
-    policy = container['HostConfig']['RestartPolicy']
125
+    policy = container.info['HostConfig']['RestartPolicy']
127 126
     if policy and policy['Name'] != 'no':
128 127
         arg = '--restart=%s' % policy['Name']
129 128
         if policy['MaximumRetryCount'] > 0:
130 129
             arg += ':%s' % policy['MaximumRetryCount']
131
-        config['args'].append(arg)
130
+        container.args.append(arg)
132 131
 
133 132
 
134
-def handle_volumes_from(container, config):
133
+def handle_volumes_from(container):
135 134
     """Copies the volumes from (--volumes-from) argument."""
136
-    if container['HostConfig']['VolumesFrom']:
137
-        config['args'].extend(['--volumes-from=%s' % cont for
138
-                               cont in container['HostConfig']['VolumesFrom']])
135
+    container.add_args_from_list('--volumes-from=%s',
136
+                                 lambda c: c['HostConfig']['VolumesFrom'])
139 137
 
140 138
 
141 139
 def functions():
@@ -168,17 +166,17 @@ def main():
168 166
                         help='Don\'t actually re-run the container, just ' \
169 167
                              'print what would happen.')
170 168
     args = parser.parse_args()
171
-    container = docker_inspect(args.container, 'container')
169
+    container_info = docker_inspect(args.container, 'container')
170
+    image_info = docker_inspect(container_info['Config']['Image'], 'image')
171
+    container = Container(container_info, image_info)
172 172
 
173
-    docker_config = {'args': ['-d'], 'image': ''}
174 173
     for handler in handlers():
175
-        handler(container, docker_config)
174
+        handler(container)
176 175
 
177
-    docker_config['args'].sort()
178 176
     commands = [
179 177
         ['docker', 'stop', args.container],
180 178
         ['docker', 'rm', args.container],
181
-        ['docker', 'run'] + docker_config['args'] + [docker_config['image']],
179
+        container.command_line(),
182 180
     ]
183 181
 
184 182
     if args.dry_run:

Loading…
Cancel
Save