docker_container: make condition less specific (#48590)

* Code seems to have changed from 409 to 400, so let's not check the code.

* Unpause container before removing it.

* Improve code.

* Same for stop_container.
pull/47027/head^2
Felix Fontein 6 years ago committed by John R Barker
parent 24d7b4a660
commit f3a3f42490

@ -2505,18 +2505,34 @@ class ContainerManager(DockerBaseClass):
self.results['changed'] = True self.results['changed'] = True
response = None response = None
if not self.check_mode: if not self.check_mode:
try: count = 0
response = self.client.remove_container(container_id, v=volume_state, link=link, force=force) while True:
except NotFound as exc: try:
pass response = self.client.remove_container(container_id, v=volume_state, link=link, force=force)
except APIError as exc: except NotFound as exc:
if exc.response.status_code == 409 and ('removal of container ' in exc.explanation and
' is already in progress' in exc.explanation):
pass pass
else: except APIError as exc:
if 'Unpause the container before stopping or killing' in exc.explanation:
# New docker versions do not allow containers to be removed if they are paused
# Make sure we don't end up in an infinite loop
if count == 3:
self.fail("Error removing container %s (tried to unpause three times): %s" % (container_id, str(exc)))
count += 1
# Unpause
try:
self.client.unpause(container=container_id)
except Exception as exc2:
self.fail("Error unpausing container %s for removal: %s" % (container_id, str(exc2)))
# Now try again
continue
if 'removal of container ' in exc.explanation and ' is already in progress' in exc.explanation:
pass
else:
self.fail("Error removing container %s: %s" % (container_id, str(exc)))
except Exception as exc:
self.fail("Error removing container %s: %s" % (container_id, str(exc))) self.fail("Error removing container %s: %s" % (container_id, str(exc)))
except Exception as exc: # We only loop when explicitly requested by 'continue'
self.fail("Error removing container %s: %s" % (container_id, str(exc))) break
return response return response
def container_update(self, container_id, update_parameters): def container_update(self, container_id, update_parameters):
@ -2554,13 +2570,32 @@ class ContainerManager(DockerBaseClass):
self.results['changed'] = True self.results['changed'] = True
response = None response = None
if not self.check_mode: if not self.check_mode:
try: count = 0
if self.parameters.stop_timeout: while True:
response = self.client.stop(container_id, timeout=self.parameters.stop_timeout) try:
else: if self.parameters.stop_timeout:
response = self.client.stop(container_id) response = self.client.stop(container_id, timeout=self.parameters.stop_timeout)
except Exception as exc: else:
self.fail("Error stopping container %s: %s" % (container_id, str(exc))) response = self.client.stop(container_id)
except APIError as exc:
if 'Unpause the container before stopping or killing' in exc.explanation:
# New docker versions do not allow containers to be removed if they are paused
# Make sure we don't end up in an infinite loop
if count == 3:
self.fail("Error removing container %s (tried to unpause three times): %s" % (container_id, str(exc)))
count += 1
# Unpause
try:
self.client.unpause(container=container_id)
except Exception as exc2:
self.fail("Error unpausing container %s for removal: %s" % (container_id, str(exc2)))
# Now try again
continue
self.fail("Error stopping container %s: %s" % (container_id, str(exc)))
except Exception as exc:
self.fail("Error stopping container %s: %s" % (container_id, str(exc)))
# We only loop when explicitly requested by 'continue'
break
return response return response

Loading…
Cancel
Save