diff --git a/fig/cli/main.py b/fig/cli/main.py index 5109a9983..732b4d29e 100644 --- a/fig/cli/main.py +++ b/fig/cli/main.py @@ -91,6 +91,7 @@ class TopLevelCommand(Command): scale Set number of containers for a service start Start services stop Stop services + restart Restart services up Create and start containers """ @@ -336,6 +337,14 @@ class TopLevelCommand(Command): """ project.stop(service_names=options['SERVICE']) + def restart(self, project, options): + """ + Restart running containers. + + Usage: restart [SERVICE...] + """ + project.restart(service_names=options['SERVICE']) + def up(self, project, options): """ Build, (re)create, start and attach to containers for a service. diff --git a/fig/container.py b/fig/container.py index 9d869dc41..71f5baa2f 100644 --- a/fig/container.py +++ b/fig/container.py @@ -123,6 +123,9 @@ class Container(object): def kill(self): return self.client.kill(self.id) + def restart(self): + return self.client.restart(self.id) + def remove(self, **options): return self.client.remove_container(self.id, **options) diff --git a/fig/project.py b/fig/project.py index 870a17f0c..c224c5967 100644 --- a/fig/project.py +++ b/fig/project.py @@ -156,6 +156,10 @@ class Project(object): for service in reversed(self.get_services(service_names)): service.kill(**options) + def restart(self, service_names=None, **options): + for service in self.get_services(service_names): + service.restart(**options) + def build(self, service_names=None, no_cache=False): for service in self.get_services(service_names): if service.can_be_built(): diff --git a/fig/service.py b/fig/service.py index 8036bec6e..29ad34874 100644 --- a/fig/service.py +++ b/fig/service.py @@ -108,6 +108,11 @@ class Service(object): log.info("Killing %s..." % c.name) c.kill(**options) + def restart(self, **options): + for c in self.containers(): + log.info("Restarting %s..." % c.name) + c.restart(**options) + def scale(self, desired_num): """ Adjusts the number of containers to the specified number and ensures they are running. diff --git a/tests/integration/cli_test.py b/tests/integration/cli_test.py index 4880a2576..3c0f2aa7f 100644 --- a/tests/integration/cli_test.py +++ b/tests/integration/cli_test.py @@ -194,6 +194,22 @@ class CLITestCase(DockerClientTestCase): self.command.dispatch(['rm', '--force'], None) self.assertEqual(len(service.containers(stopped=True)), 0) + def test_restart(self): + service = self.project.get_service('simple') + container = service.create_container() + service.start_container(container) + started_at = container.dictionary['State']['StartedAt'] + self.command.dispatch(['restart'], None) + container.inspect() + self.assertNotEqual( + container.dictionary['State']['FinishedAt'], + '0001-01-01T00:00:00Z', + ) + self.assertNotEqual( + container.dictionary['State']['StartedAt'], + started_at, + ) + def test_scale(self): project = self.project