Refactor recreate_containers() in preparation for smart name-preserving logic

This commit is contained in:
Aanand Prasad 2014-01-15 16:15:46 +00:00
parent 8a0071d9c1
commit 3956d85a8c
1 changed files with 19 additions and 8 deletions

View File

@ -78,19 +78,30 @@ class Service(object):
If a container for this service doesn't exist, create one. If there are
any, stop them and create new ones. Does not remove the old containers.
"""
old_containers = self.containers(stopped=True)
if len(old_containers) == 0:
containers = self.containers(stopped=True)
if len(containers) == 0:
return ([], [self.create_container(**override_options)])
else:
old_containers = []
new_containers = []
for old_container in old_containers:
if old_container.is_running:
old_container.stop(timeout=1)
options = dict(override_options)
options['volumes_from'] = old_container.id
new_containers.append(self.create_container(**options))
for c in containers:
(old_container, new_container) = self.recreate_container(c, **override_options)
old_containers.append(old_container)
new_containers.append(new_container)
return (old_containers, new_containers)
def recreate_container(self, container, **override_options):
if container.is_running:
container.stop(timeout=1)
options = dict(override_options)
options['volumes_from'] = container.id
return (container, self.create_container(**options))
def start_container(self, container=None, **override_options):
if container is None:
container = self.create_container(**override_options)