From 3d8ce448b861bdb67dc3379c236ca659b835c29e Mon Sep 17 00:00:00 2001 From: d11wtq Date: Sun, 8 Jun 2014 09:48:59 +0000 Subject: [PATCH] Spike: Re-use existing containers for `fig run` Signed-off-by: Chris Corbyn --- fig/cli/main.py | 15 ++++++++++++--- fig/project.py | 7 ++----- fig/service.py | 15 +++++++++++---- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/fig/cli/main.py b/fig/cli/main.py index 594a20aee..a107a3fb0 100644 --- a/fig/cli/main.py +++ b/fig/cli/main.py @@ -222,7 +222,8 @@ class TopLevelCommand(Command): if not options['--no-links']: self.project.up( service_names=service.get_linked_names(), - start_links=True + start_links=True, + keep_old=True ) tty = True @@ -301,7 +302,9 @@ class TopLevelCommand(Command): If there are existing containers for a service, `fig up` will stop and recreate them (preserving mounted volumes with volumes-from), - so that changes in `fig.yml` are picked up. + so that changes in `fig.yml` are picked up. If you do not want existing + containers to be recreated, `fig up --keep-old` will re-use existing + containers. Usage: up [options] [SERVICE...] @@ -309,13 +312,19 @@ class TopLevelCommand(Command): -d Detached mode: Run containers in the background, print new container names. --no-links Don't start linked services. + --keep-old If containers already exist, don't recreate them. """ detached = options['-d'] start_links = not options['--no-links'] + keep_old = options['--keep-old'] service_names = options['SERVICE'] - to_attach = self.project.up(service_names=service_names, start_links=start_links) + to_attach = self.project.up( + service_names=service_names, + start_links=start_links, + keep_old=keep_old + ) if not detached: print("Attaching to", list_containers(to_attach)) diff --git a/fig/project.py b/fig/project.py index ac1477edc..51461cd37 100644 --- a/fig/project.py +++ b/fig/project.py @@ -138,11 +138,11 @@ class Project(object): else: log.info('%s uses an image, skipping' % service.name) - def up(self, service_names=None, start_links=True): + def up(self, service_names=None, start_links=True, keep_old=False): new_containers = [] for service in self.get_services(service_names, include_links=start_links): - for (_, new) in service.recreate_containers(): + for (_, new) in service.recreate_containers(keep_old): new_containers.append(new) return new_containers @@ -159,9 +159,6 @@ class Project(object): return l def _prepend_with_links(self, acc, service): - if service in acc: - return acc - linked_names = service.get_linked_names() if len(linked_names) > 0: diff --git a/fig/service.py b/fig/service.py index 748b13e02..cd9cbf832 100644 --- a/fig/service.py +++ b/fig/service.py @@ -76,9 +76,7 @@ class Service(object): def start(self, **options): for c in self.containers(stopped=True): - if not c.is_running: - log.info("Starting %s..." % c.name) - self.start_container(c, **options) + self.start_container_if_stopped(c, **options) def stop(self, **options): for c in self.containers(): @@ -156,7 +154,7 @@ class Service(object): return Container.create(self.client, **container_options) raise - def recreate_containers(self, **override_options): + def recreate_containers(self, keep_old=False, **override_options): """ If a container for this service doesn't exist, create and start one. If there are any, stop them, create+start new ones, and remove the old containers. @@ -168,6 +166,8 @@ class Service(object): container = self.create_container(**override_options) self.start_container(container) return [(None, container)] + elif keep_old: + return [(None, self.start_container_if_stopped(c)) for c in containers] else: tuples = [] @@ -201,6 +201,13 @@ class Service(object): return (intermediate_container, new_container) + def start_container_if_stopped(self, container, **options): + if container.is_running: + return container + else: + log.info("Starting %s..." % container.name) + return self.start_container(container, **options) + def start_container(self, container=None, volumes_from=None, **override_options): if container is None: container = self.create_container(**override_options)