Scaling down removes containers

Squashed version of #162.
Closes #121.
This commit is contained in:
sebastianneubauer 2014-03-21 19:34:19 +01:00 committed by Aanand Prasad
parent 401ea4e7a8
commit 352ad7a38c
4 changed files with 60 additions and 8 deletions

View File

@ -298,20 +298,13 @@ class TopLevelCommand(Command):
""" """
detached = options['-d'] detached = options['-d']
(old, new) = self.project.recreate_containers(service_names=options['SERVICE']) new = self.project.up(service_names=options['SERVICE'])
if not detached: if not detached:
to_attach = [c for (s, c) in new] to_attach = [c for (s, c) in new]
print("Attaching to", list_containers(to_attach)) print("Attaching to", list_containers(to_attach))
log_printer = LogPrinter(to_attach, attach_params={"logs": True}) log_printer = LogPrinter(to_attach, attach_params={"logs": True})
for (service, container) in new:
service.start_container(container)
for (service, container) in old:
container.remove()
if not detached:
try: try:
log_printer.run() log_printer.run()
finally: finally:

View File

@ -137,6 +137,17 @@ class Project(object):
else: else:
log.info('%s uses an image, skipping' % service.name) log.info('%s uses an image, skipping' % service.name)
def up(self, service_names=None):
(old, new) = self.recreate_containers(service_names=service_names)
for (service, container) in new:
service.start_container(container)
for (service, container) in old:
container.remove()
return new
def remove_stopped(self, service_names=None, **options): def remove_stopped(self, service_names=None, **options):
for service in self.get_services(service_names): for service in self.get_services(service_names):
service.remove_stopped(**options) service.remove_stopped(**options)

View File

@ -85,6 +85,14 @@ class Service(object):
c.kill(**options) c.kill(**options)
def scale(self, desired_num): def scale(self, desired_num):
"""
Adjusts the number of containers to the specified number and ensures they are running.
- creates containers until there are at least `desired_num`
- stops containers until there are at most `desired_num` running
- starts containers until there are at least `desired_num` running
- removes all stopped containers
"""
if not self.can_be_scaled(): if not self.can_be_scaled():
raise CannotBeScaledError() raise CannotBeScaledError()
@ -117,6 +125,8 @@ class Service(object):
self.start_container(c) self.start_container(c)
running_containers.append(c) running_containers.append(c)
self.remove_stopped()
def remove_stopped(self, **options): def remove_stopped(self, **options):
for c in self.containers(stopped=True): for c in self.containers(stopped=True):

View File

@ -118,3 +118,41 @@ class ProjectTest(DockerClientTestCase):
project.remove_stopped() project.remove_stopped()
self.assertEqual(len(project.containers(stopped=True)), 0) self.assertEqual(len(project.containers(stopped=True)), 0)
def test_project_up(self):
web = self.create_service('web')
db = self.create_service('db')
project = Project('figtest', [web, db], self.client)
project.start()
self.assertEqual(len(project.containers()), 0)
project.up()
self.assertEqual(len(project.containers()), 2)
project.kill()
project.remove_stopped()
def test_unscale_after_restart(self):
web = self.create_service('web')
project = Project('figtest', [web], self.client)
project.start()
service = project.get_service('web')
service.scale(1)
self.assertEqual(len(service.containers()), 1)
service.scale(3)
self.assertEqual(len(service.containers()), 3)
project.up()
service = project.get_service('web')
self.assertEqual(len(service.containers()), 3)
service.scale(1)
self.assertEqual(len(service.containers()), 1)
project.up()
service = project.get_service('web')
self.assertEqual(len(service.containers()), 1)
# does scale=0 ,makes any sense? after recreating at least 1 container is running
service.scale(0)
project.up()
service = project.get_service('web')
self.assertEqual(len(service.containers()), 1)
project.kill()
project.remove_stopped()