docker_container: use restart() API function instead of stop/start sequence (#55894)

* Improve container restart.

* Adjust tests.

* Add changelog.

* Quote options.

* Move tests for restart/recreate options to start/stop tests.

* Fix changelog name.
This commit is contained in:
Felix Fontein 2019-05-03 17:30:39 +02:00 committed by ansibot
parent 93f0112953
commit 95d1564f70
4 changed files with 138 additions and 112 deletions

View file

@ -2351,8 +2351,8 @@ class ContainerManager(DockerBaseClass):
container = self.container_start(container.Id)
elif state == 'started' and self.parameters.restart:
self.diff_tracker.add('running', parameter=True, active=was_running)
self.container_stop(container.Id)
container = self.container_start(container.Id)
self.diff_tracker.add('restarted', parameter=True, active=False)
container = self.container_restart(container.Id)
elif state == 'stopped' and container.running:
self.diff_tracker.add('running', parameter=False, active=was_running)
self.container_stop(container.Id)
@ -2634,6 +2634,19 @@ class ContainerManager(DockerBaseClass):
self.fail("Error killing container %s: %s" % (container_id, exc))
return response
def container_restart(self, container_id):
self.results['actions'].append(dict(restarted=container_id, timeout=self.parameters.stop_timeout))
self.results['changed'] = True
if not self.check_mode:
try:
if self.parameters.stop_timeout:
response = self.client.restart(container_id, timeout=self.parameters.stop_timeout)
else:
response = self.client.restart(container_id)
except Exception as exc:
self.fail("Error restarting container %s: %s" % (container_id, str(exc)))
return self._get_container(container_id)
def container_stop(self, container_id):
if self.parameters.force_kill:
self.container_kill(container_id)