Browse Source

Add support for modifiers.

Initially just support --image to change the image.
tags/v0.1.1
Chris Smith 7 years ago
parent
commit
5d4616fb90
3 changed files with 65 additions and 11 deletions
  1. 18
    8
      README.md
  2. 31
    0
      docker-rerun
  3. 16
    3
      test.py

+ 18
- 8
README.md View File

7
 Want to update to a newer image, or add a missing port publication?
7
 Want to update to a newer image, or add a missing port publication?
8
 docker-rerun's got you covered.
8
 docker-rerun's got you covered.
9
 
9
 
10
-## How to use it
10
+## Usage
11
 
11
 
12
 In the most basic usage, you pass in a container name and it will be
12
 In the most basic usage, you pass in a container name and it will be
13
 stopped, deleted and recreated:
13
 stopped, deleted and recreated:
14
 
14
 
15
     $ ./docker-rerun apache
15
     $ ./docker-rerun apache
16
 
16
 
17
+You can also pass additional arguments to modify aspects of the container
18
+when it's rerun. For example, to change the image:
19
+
20
+    $ ./docker-rerun --image nginx:latest webserver
21
+
17
 To check what exactly is going to be performed beforehand, use the --dry-run
22
 To check what exactly is going to be performed beforehand, use the --dry-run
18
 option:
23
 option:
19
 
24
 
24
 
29
 
25
 ## What's supported
30
 ## What's supported
26
 
31
 
27
-At present docker-rerun supports a small number of commonly used arguments:
32
+At present docker-rerun supports copying a number of commonly used arguments:
28
 
33
 
29
     * Commands (trailing arguments)
34
     * Commands (trailing arguments)
30
     * Environment variables (-e/--env)
35
     * Environment variables (-e/--env)
37
     * User switching (-u/--user)
42
     * User switching (-u/--user)
38
     * Volumes (-v/--volume, and --volumes-from)
43
     * Volumes (-v/--volume, and --volumes-from)
39
 
44
 
45
+If a container uses an argument that's not supported yet, it will be silently
46
+dropped when rerunning.
47
+
48
+
49
+The following arguments can be used when executing `docker-rerun` to modify
50
+the resulting container:
51
+
52
+    * `--image <image>` - changes the image that will be used. You can specify
53
+      tags (`name:tag`) or digests (`name@digest`) as with `docker run`.
54
+
40
 ## What's not done yet
55
 ## What's not done yet
41
 
56
 
42
 Many other command line arguments:
57
 Many other command line arguments:
45
     * Permissions and policies
60
     * Permissions and policies
46
     * Advanced networking options
61
     * Advanced networking options
47
 
62
 
48
-Additional options to allow mutating the container config when rerunning.
49
-For example:
50
-
51
-    $ ./docker-rerun --image nginx:1.11.1 nginx
52
-
53
-Should replace the previously used image with the one specified.
63
+More options to allow mutating the container config when rerunning.
54
 
64
 

+ 31
- 0
docker-rerun View File

7
 Each function named `handle_*` handles one configuration option,
7
 Each function named `handle_*` handles one configuration option,
8
 reading the relevant information from the inspect output and adding
8
 reading the relevant information from the inspect output and adding
9
 the relevant command line flags to the config.
9
 the relevant command line flags to the config.
10
+
11
+Each function named `modify_*` allows the user to modify the
12
+configuration in some manner. Modify functions are called twice:
13
+once with an ArgumentParser, and subsequently with the parsed
14
+arguments and the container object.
10
 """
15
 """
11
 
16
 
12
 import argparse
17
 import argparse
205
                                  lambda c: c['HostConfig']['VolumesFrom'])
210
                                  lambda c: c['HostConfig']['VolumesFrom'])
206
 
211
 
207
 
212
 
213
+def modify_image(parser=None, args=None, container=None):
214
+    """Allows the image (name, version, etc) to be modified in one go."""
215
+    if parser:
216
+        parser.add_argument('--image',
217
+                            help='Image to use in place of the original')
218
+    elif args.image:
219
+        container.image = args.image
220
+
221
+
208
 def functions():
222
 def functions():
209
     """Lists all functions defined in this module.
223
     """Lists all functions defined in this module.
210
 
224
 
226
     return [func for (name, func) in functions() if name.startswith('handle_')]
240
     return [func for (name, func) in functions() if name.startswith('handle_')]
227
 
241
 
228
 
242
 
243
+def modifiers():
244
+    """Lists all modifiers defined in this module.
245
+
246
+    Returns:
247
+        list of function: All modifiers (modify_* funcs) in this module.
248
+    """
249
+    return [func for (name, func) in functions() if name.startswith('modify_')]
250
+
251
+
229
 def main():
252
 def main():
230
     """Script entry point."""
253
     """Script entry point."""
231
     parser = argparse.ArgumentParser(description='Reruns docker containers ' \
254
     parser = argparse.ArgumentParser(description='Reruns docker containers ' \
234
     parser.add_argument('-d', '--dry-run', action='store_true',
257
     parser.add_argument('-d', '--dry-run', action='store_true',
235
                         help='Don\'t actually re-run the container, just ' \
258
                         help='Don\'t actually re-run the container, just ' \
236
                              'print what would happen.')
259
                              'print what would happen.')
260
+
261
+    mods = modifiers()
262
+    for mod in mods:
263
+        mod(parser=parser)
264
+
237
     args = parser.parse_args()
265
     args = parser.parse_args()
238
     container_info = docker_inspect(args.container, 'container')
266
     container_info = docker_inspect(args.container, 'container')
239
     image_info = docker_inspect(container_info['Config']['Image'], 'image')
267
     image_info = docker_inspect(container_info['Config']['Image'], 'image')
242
     for handler in handlers():
270
     for handler in handlers():
243
         handler(container)
271
         handler(container)
244
 
272
 
273
+    for mod in mods:
274
+        mod(args=args, container=container)
275
+
245
     commands = [
276
     commands = [
246
         ['docker', 'stop', args.container],
277
         ['docker', 'stop', args.container],
247
         ['docker', 'rm', args.container],
278
         ['docker', 'rm', args.container],

+ 16
- 3
test.py View File

48
     yield check, ['docker', 'run', '--link=testA:bar', '--link=testB', '--name=test123', '-d', 'hello-world']
48
     yield check, ['docker', 'run', '--link=testA:bar', '--link=testB', '--name=test123', '-d', 'hello-world']
49
 
49
 
50
 
50
 
51
+@with_setup(setup, teardown)
52
+def test_modifiers():
53
+    yield (check,
54
+           ['docker', 'run', '--name=test123', '-d', 'hello-world'],
55
+           ['--image', 'tutum/hello-world'],
56
+           ['docker', 'run', '--name=test123', '-d', 'tutum/hello-world'])
57
+    yield (check,
58
+           ['docker', 'run', '--name=test123', '-d', 'hello-world'],
59
+           ['--image', 'hello-world:latest'],
60
+           ['docker', 'run', '--name=test123', '-d', 'hello-world:latest'])
61
+
62
+
51
 @with_setup(setup_each, teardown_each)
63
 @with_setup(setup_each, teardown_each)
52
-def check(command):
64
+def check(command, args=[], expected=None):
53
     _run(command)
65
     _run(command)
54
-    output = subprocess.check_output(['./docker-rerun', '--dry-run', 'test123'])
66
+    output = subprocess.check_output(['./docker-rerun', '--dry-run', 'test123'] + args)
55
     output = output.decode('utf-8').strip().splitlines()
67
     output = output.decode('utf-8').strip().splitlines()
56
-    assert output[3] == ' '.join(command), 'Expected "%s" but got "%s"' % (' '.join(command), output[3])
68
+    expected = ' '.join(expected or command)
69
+    assert output[3] == expected, 'Expected "%s" but got "%s"' % (expected, output[3])
57
 
70
 

Loading…
Cancel
Save