From 62bba1684b91397f5deec3dad37a60eb37c1646e Mon Sep 17 00:00:00 2001 From: Cameron Maske Date: Sat, 18 Jan 2014 14:11:25 +0000 Subject: [PATCH] Updated recreate_containers to attempt to base intermediate container's the previous container's image. Added in additional functionality to reset any entrypoints for the intermediate container and pull/retry handling if the image does not exist. Updated test coverage to check if an container is recreated with an entrypoint it is handled correctly. --- fig/container.py | 6 +++++- fig/service.py | 16 +++++++++------- tests/service_test.py | 11 ++++++++--- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/fig/container.py b/fig/container.py index da10ec7ae..76f2d29e1 100644 --- a/fig/container.py +++ b/fig/container.py @@ -3,7 +3,7 @@ from __future__ import absolute_import class Container(object): """ - Represents a Docker container, constructed from the output of + Represents a Docker container, constructed from the output of GET /containers/:id:/json. """ def __init__(self, client, dictionary, has_been_inspected=False): @@ -38,6 +38,10 @@ class Container(object): def id(self): return self.dictionary['ID'] + @property + def image(self): + return self.dictionary['Image'] + @property def short_id(self): return self.id[:10] diff --git a/fig/service.py b/fig/service.py index f96794267..730e3a1c9 100644 --- a/fig/service.py +++ b/fig/service.py @@ -141,12 +141,14 @@ class Service(object): if container.is_running: container.stop(timeout=1) - intermediate_container = Container.create( - self.client, - image='ubuntu', - command='echo', - volumes_from=container.id, - ) + intermediate_container_options = { + 'image': container.image, + 'command': 'echo', + 'volumes_from': container.id, + 'entrypoint': None + } + intermediate_container = self.create_container( + one_off=True, **intermediate_container_options) intermediate_container.start() intermediate_container.wait() container.remove() @@ -212,7 +214,7 @@ class Service(object): return links def _get_container_options(self, override_options, one_off=False): - keys = ['image', 'command', 'hostname', 'user', 'detach', 'stdin_open', 'tty', 'mem_limit', 'ports', 'environment', 'dns', 'volumes', 'volumes_from'] + keys = ['image', 'command', 'hostname', 'user', 'detach', 'stdin_open', 'tty', 'mem_limit', 'ports', 'environment', 'dns', 'volumes', 'volumes_from', 'entrypoint'] container_options = dict((k, self.options[k]) for k in keys if k in self.options) container_options.update(override_options) diff --git a/tests/service_test.py b/tests/service_test.py index ff7b24160..e4183a02b 100644 --- a/tests/service_test.py +++ b/tests/service_test.py @@ -110,8 +110,9 @@ class ServiceTest(DockerClientTestCase): self.assertIn('/var/db', container.inspect()['Volumes']) def test_recreate_containers(self): - service = self.create_service('db', environment={'FOO': '1'}, volumes=['/var/db']) + service = self.create_service('db', environment={'FOO': '1'}, volumes=['/var/db'], entrypoint=['ps']) old_container = service.create_container() + self.assertEqual(old_container.dictionary['Config']['Entrypoint'], ['ps']) self.assertEqual(old_container.dictionary['Config']['Env'], ['FOO=1']) self.assertEqual(old_container.name, 'figtest_db_1') service.start_container(old_container) @@ -120,11 +121,15 @@ class ServiceTest(DockerClientTestCase): num_containers_before = len(self.client.containers(all=True)) service.options['environment']['FOO'] = '2' - (old, new) = service.recreate_containers() - self.assertEqual(len(old), 1) + (intermediate, new) = service.recreate_containers() + self.assertEqual(len(intermediate), 1) self.assertEqual(len(new), 1) new_container = new[0] + intermediate_container = intermediate[0] + self.assertEqual(intermediate_container.dictionary['Config']['Entrypoint'], None) + + self.assertEqual(new_container.dictionary['Config']['Entrypoint'], ['ps']) self.assertEqual(new_container.dictionary['Config']['Env'], ['FOO=2']) self.assertEqual(new_container.name, 'figtest_db_1') service.start_container(new_container)