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,13 +7,18 @@ containers using the same arguments you used previously.
7 7
 Want to update to a newer image, or add a missing port publication?
8 8
 docker-rerun's got you covered.
9 9
 
10
-## How to use it
10
+## Usage
11 11
 
12 12
 In the most basic usage, you pass in a container name and it will be
13 13
 stopped, deleted and recreated:
14 14
 
15 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 22
 To check what exactly is going to be performed beforehand, use the --dry-run
18 23
 option:
19 24
 
@@ -24,7 +29,7 @@ option:
24 29
 
25 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 34
     * Commands (trailing arguments)
30 35
     * Environment variables (-e/--env)
@@ -37,6 +42,16 @@ At present docker-rerun supports a small number of commonly used arguments:
37 42
     * User switching (-u/--user)
38 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 55
 ## What's not done yet
41 56
 
42 57
 Many other command line arguments:
@@ -45,10 +60,5 @@ Many other command line arguments:
45 60
     * Permissions and policies
46 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,6 +7,11 @@ and reconstructed by looking at the `docker inspect` output.
7 7
 Each function named `handle_*` handles one configuration option,
8 8
 reading the relevant information from the inspect output and adding
9 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 17
 import argparse
@@ -205,6 +210,15 @@ def handle_volumes_from(container):
205 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 222
 def functions():
209 223
     """Lists all functions defined in this module.
210 224
 
@@ -226,6 +240,15 @@ def handlers():
226 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 252
 def main():
230 253
     """Script entry point."""
231 254
     parser = argparse.ArgumentParser(description='Reruns docker containers ' \
@@ -234,6 +257,11 @@ def main():
234 257
     parser.add_argument('-d', '--dry-run', action='store_true',
235 258
                         help='Don\'t actually re-run the container, just ' \
236 259
                              'print what would happen.')
260
+
261
+    mods = modifiers()
262
+    for mod in mods:
263
+        mod(parser=parser)
264
+
237 265
     args = parser.parse_args()
238 266
     container_info = docker_inspect(args.container, 'container')
239 267
     image_info = docker_inspect(container_info['Config']['Image'], 'image')
@@ -242,6 +270,9 @@ def main():
242 270
     for handler in handlers():
243 271
         handler(container)
244 272
 
273
+    for mod in mods:
274
+        mod(args=args, container=container)
275
+
245 276
     commands = [
246 277
         ['docker', 'stop', args.container],
247 278
         ['docker', 'rm', args.container],

+ 16
- 3
test.py View File

@@ -48,10 +48,23 @@ def test_command_matches():
48 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 63
 @with_setup(setup_each, teardown_each)
52
-def check(command):
64
+def check(command, args=[], expected=None):
53 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 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