Fix 'fig up' behaviour

- For each service, creates a container if there are none (stopped OR
  started)
- Attaches to all containers (unless -d is passed)
- Starts all containers
- On ^C, kills all containers (unless -d is passed)
This commit is contained in:
Aanand Prasad 2013-12-31 13:02:08 +00:00
parent 9ed6538693
commit d4f3ed1840
3 changed files with 12 additions and 59 deletions

View File

@ -156,24 +156,20 @@ class TopLevelCommand(Command):
"""
detached = options['-d']
unstarted = self.project.create_containers(service_names=options['SERVICE'])
self.project.create_containers(service_names=options['SERVICE'])
containers = self.project.containers(service_names=options['SERVICE'], stopped=True)
if not detached:
to_attach = self.project.containers(service_names=options['SERVICE']) + [c for (s, c) in unstarted]
print "Attaching to", list_containers(to_attach)
log_printer = LogPrinter(to_attach, attach_params={'logs': True})
print "Attaching to", list_containers(containers)
log_printer = LogPrinter(containers)
for (s, c) in unstarted:
s.start_container(c)
self.project.start(service_names=options['SERVICE'])
if detached:
for (s, c) in unstarted:
print c.name
else:
if not detached:
try:
log_printer.run()
finally:
self.project.kill_and_remove(unstarted)
self.project.kill(service_names=options['SERVICE'])
def start(self, options):
"""

View File

@ -75,19 +75,11 @@ class Project(object):
def create_containers(self, service_names=None):
"""
Returns a list of (service, container) tuples,
one for each service with no running containers.
For each service, creates a container if there are none.
"""
containers = []
for service in self.get_services(service_names):
if len(service.containers()) == 0:
containers.append((service, service.create_container()))
return containers
def kill_and_remove(self, tuples):
for (service, container) in tuples:
container.kill()
container.remove()
if len(service.containers(stopped=True)) == 0:
service.create_container()
def start(self, service_names=None, **options):
for service in self.get_services(service_names):

View File

@ -46,49 +46,14 @@ class ProjectTest(DockerClientTestCase):
db = self.create_service('db')
project = Project('test', [web, db], self.client)
unstarted = project.create_containers(service_names=['web'])
self.assertEqual(len(unstarted), 1)
self.assertEqual(unstarted[0][0], web)
project.create_containers(service_names=['web'])
self.assertEqual(len(web.containers(stopped=True)), 1)
self.assertEqual(len(db.containers(stopped=True)), 0)
unstarted = project.create_containers()
self.assertEqual(len(unstarted), 2)
self.assertEqual(unstarted[0][0], web)
self.assertEqual(unstarted[1][0], db)
project.create_containers()
self.assertEqual(len(web.containers(stopped=True)), 1)
self.assertEqual(len(db.containers(stopped=True)), 1)
def test_up(self):
web = self.create_service('web')
db = self.create_service('db')
other = self.create_service('other')
project = Project('test', [web, db, other], self.client)
web.create_container()
self.assertEqual(len(web.containers()), 0)
self.assertEqual(len(db.containers()), 0)
self.assertEqual(len(web.containers(stopped=True)), 1)
self.assertEqual(len(db.containers(stopped=True)), 0)
unstarted = project.create_containers(service_names=['web', 'db'])
self.assertEqual(len(unstarted), 2)
self.assertEqual(unstarted[0][0], web)
self.assertEqual(unstarted[1][0], db)
self.assertEqual(len(web.containers()), 0)
self.assertEqual(len(db.containers()), 0)
self.assertEqual(len(web.containers(stopped=True)), 2)
self.assertEqual(len(db.containers(stopped=True)), 1)
project.kill_and_remove(unstarted)
self.assertEqual(len(web.containers()), 0)
self.assertEqual(len(db.containers()), 0)
self.assertEqual(len(web.containers(stopped=True)), 1)
self.assertEqual(len(db.containers(stopped=True)), 0)
def test_start_stop_kill_remove(self):
web = self.create_service('web')
db = self.create_service('db')