Add support for legacy registries to pull_image().

Since we now have several exceptions to the assumption that the
result of the pull would be on the last status line returned by
docker-py's pull(), I've changed the function so that it looks
through the status lines and returns what if finds on it.
Despite the repeated `break`s, the code seems simpler and a little
more coherent like this. From what I've checked using
`https://github.com/jlafon/ansible-profile`, the execution time is
mostly the same.
This commit is contained in:
Manuel Tiago Pereira 2015-10-28 11:35:14 +00:00 committed by Matt Clay
parent eba767902c
commit 057cbacf8a

View file

@ -135,7 +135,7 @@ options:
version_added: "2.0" version_added: "2.0"
log_opt: log_opt:
description: description:
- Additional options to pass to the logging driver selected above. See Docker `log-driver - Additional options to pass to the logging driver selected above. See Docker `log-driver
<https://docs.docker.com/reference/logging/overview/>` documentation for more information. <https://docs.docker.com/reference/logging/overview/>` documentation for more information.
Requires docker >=1.7.0. Requires docker >=1.7.0.
required: false required: false
@ -1486,24 +1486,20 @@ class DockerManager(object):
self.module.fail_json(msg="failed to login to the remote registry, check your username/password.", error=repr(e)) self.module.fail_json(msg="failed to login to the remote registry, check your username/password.", error=repr(e))
try: try:
changes = list(self.client.pull(image, tag=tag, stream=True, **extra_params)) changes = list(self.client.pull(image, tag=tag, stream=True, **extra_params))
try: pull_success = False
last = changes[-1] for change in changes:
# seems Docker 1.8 puts an empty dict at the end of the status = json.loads(change).get('status', '')
# stream; catch that and get the previous instead if status.startswith('Status: Image is up to date for'):
# https://github.com/ansible/ansible-modules-core/issues/2043 # Image is already up to date. Don't increment the counter.
if last.strip() == '{}': pull_success = True
last = changes[-2] break
except IndexError: elif (status.startswith('Status: Downloaded newer image for') or
last = '{}' status.startswith('Download complete')):
status = json.loads(last).get('status', '') # Image was updated. Increment the pull counter.
if status.startswith('Status: Image is up to date for'): self.increment_counter('pulled')
# Image is already up to date. Don't increment the counter. pull_success = True
pass break
elif (status.startswith('Status: Downloaded newer image for') or if not pull_success:
status.startswith('Download complete')):
# Image was updated. Increment the pull counter.
self.increment_counter('pulled')
else:
# Unrecognized status string. # Unrecognized status string.
self.module.fail_json(msg="Unrecognized status from pull.", status=status, changes=changes) self.module.fail_json(msg="Unrecognized status from pull.", status=status, changes=changes)
except Exception as e: except Exception as e: