Remove project.create_containers(), revamp project.recreate_containers()

`recreate_containers` now returns two lists of old+new containers, along
with their services.
This commit is contained in:
Aanand Prasad 2014-01-15 13:17:39 +00:00
parent bdc6b47e1f
commit f5f9357736
3 changed files with 22 additions and 15 deletions

View File

@ -79,20 +79,22 @@ class Project(object):
unsorted = [self.get_service(name) for name in service_names]
return [s for s in self.services if s in unsorted]
def create_containers(self, service_names=None):
"""
For each service, creates a container if there are none.
"""
for service in self.get_services(service_names):
if len(service.containers(stopped=True)) == 0:
service.create_container()
def recreate_containers(self, service_names):
def recreate_containers(self, service_names=None):
"""
For each service, create or recreate their containers.
Returns a tuple with two lists. The first is a list of
(service, old_container) tuples; the second is a list
of (service, new_container) tuples.
"""
old = []
new = []
for service in self.get_services(service_names):
service.recreate_containers()
(s_old, s_new) = service.recreate_containers()
old += [(service, container) for container in s_old]
new += [(service, container) for container in s_new]
return (old, new)
def start(self, service_names=None, **options):
for service in self.get_services(service_names):

View File

@ -80,7 +80,7 @@ class Service(object):
"""
old_containers = self.containers(stopped=True)
if len(old_containers) == 0:
return [self.create_container(**override_options)]
return ([], [self.create_container(**override_options)])
else:
new_containers = []
for old_container in old_containers:

View File

@ -42,17 +42,22 @@ class ProjectTest(DockerClientTestCase):
project = Project('test', [web], self.client)
self.assertEqual(project.get_service('web'), web)
def test_create_containers(self):
def test_recreate_containers(self):
web = self.create_service('web')
db = self.create_service('db')
project = Project('test', [web, db], self.client)
project.create_containers(service_names=['web'])
old_web_container = web.create_container()
self.assertEqual(len(web.containers(stopped=True)), 1)
self.assertEqual(len(db.containers(stopped=True)), 0)
project.create_containers()
self.assertEqual(len(web.containers(stopped=True)), 1)
(old, new) = project.recreate_containers()
self.assertEqual(old, [(web, old_web_container)])
self.assertEqual(len(new), 2)
self.assertEqual(new[0][0], web)
self.assertEqual(new[1][0], db)
self.assertEqual(len(web.containers(stopped=True)), 2)
self.assertEqual(len(db.containers(stopped=True)), 1)
def test_start_stop_kill_remove(self):