diff --git a/plum/cli/main.py b/plum/cli/main.py index 7819df61d..fe03dbd3c 100644 --- a/plum/cli/main.py +++ b/plum/cli/main.py @@ -71,6 +71,8 @@ class TopLevelCommand(Command): run Run a one-off command start Start services stop Stop services + kill Kill containers + rm Remove stopped containers """ def docopt_options(self): @@ -186,6 +188,22 @@ class TopLevelCommand(Command): """ self.project.stop() + def kill(self, options): + """ + Kill all containers + + Usage: kill + """ + self.project.kill() + + def rm(self, options): + """ + Remove all stopped containers + + Usage: rm + """ + self.project.remove_stopped() + def logs(self, options): """ View output from containers diff --git a/plum/project.py b/plum/project.py index 847616441..b9fbaabd4 100644 --- a/plum/project.py +++ b/plum/project.py @@ -74,6 +74,14 @@ class Project(object): for service in self.services: service.stop(**options) + def kill(self, **options): + for service in self.services: + service.kill(**options) + + def remove_stopped(self, **options): + for service in self.services: + service.remove_stopped(**options) + def containers(self, *args, **kwargs): l = [] for service in self.services: diff --git a/plum/service.py b/plum/service.py index 9caccc6ff..fab896f55 100644 --- a/plum/service.py +++ b/plum/service.py @@ -47,6 +47,15 @@ class Service(object): for c in self.containers(): c.stop(**options) + def kill(self, **options): + for c in self.containers(): + c.kill(**options) + + def remove_stopped(self, **options): + for c in self.containers(stopped=True): + if not c.is_running: + c.remove(**options) + def create_container(self, one_off=False, **override_options): """ Create a container for this service. If the image doesn't exist, attempt to pull diff --git a/tests/service_test.py b/tests/service_test.py index 774196302..e3a3e625a 100644 --- a/tests/service_test.py +++ b/tests/service_test.py @@ -73,6 +73,22 @@ class ServiceTest(DockerClientTestCase): self.assertEqual(len(service.containers()), 0) self.assertEqual(len(service.containers(stopped=True)), 1) + def test_kill_remove(self): + service = self.create_service('scalingtest') + + service.start_container() + self.assertEqual(len(service.containers()), 1) + + service.remove_stopped() + self.assertEqual(len(service.containers()), 1) + + service.kill() + self.assertEqual(len(service.containers()), 0) + self.assertEqual(len(service.containers(stopped=True)), 1) + + service.remove_stopped() + self.assertEqual(len(service.containers(stopped=True)), 0) + def test_create_container_with_one_off(self): db = self.create_service('db') container = db.create_container(one_off=True)