Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.9] docker_image: fix handling of image IDs #73817

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,5 @@
bugfixes:
- "docker_image - prevent module failure when removing image that is removed between inspection and removal (https://github.com/ansible-collections/community.docker/pull/87)."
- "docker_image - prevent module failure when removing non-existant image by ID (https://github.com/ansible-collections/community.docker/pull/87)."
- "docker_image_info - prevent module failure when image vanishes between listing and inspection (https://github.com/ansible-collections/community.docker/pull/87)."
- "docker_image_info - prevent module failure when querying non-existant image by ID (https://github.com/ansible-collections/community.docker/pull/87)."
10 changes: 9 additions & 1 deletion lib/ansible/module_utils/docker/common.py
Expand Up @@ -636,14 +636,17 @@ def find_image(self, name, tag):
if len(images) == 1:
try:
inspection = self.inspect_image(images[0]['Id'])
except NotFound:
self.log("Image %s:%s not found." % (name, tag))
return None
except Exception as exc:
self.fail("Error inspecting image %s:%s - %s" % (name, tag, str(exc)))
return inspection

self.log("Image %s:%s not found." % (name, tag))
return None

def find_image_by_id(self, image_id):
def find_image_by_id(self, image_id, accept_missing_image=False):
'''
Lookup an image (by ID) and return the inspection results.
'''
Expand All @@ -653,6 +656,11 @@ def find_image_by_id(self, image_id):
self.log("Find image %s (by ID)" % image_id)
try:
inspection = self.inspect_image(image_id)
except NotFound as exc:
if not accept_missing_image:
self.fail("Error inspecting image ID %s - %s" % (image_id, str(exc)))
self.log("Image %s not found." % image_id)
return None
except Exception as exc:
self.fail("Error inspecting image ID %s - %s" % (image_id, str(exc)))
return inspection
Expand Down
9 changes: 7 additions & 2 deletions lib/ansible/modules/cloud/docker/docker_image.py
Expand Up @@ -451,7 +451,7 @@
else:
from docker.auth.auth import resolve_repository_name
from docker.utils.utils import parse_repository_tag
from docker.errors import DockerException
from docker.errors import DockerException, NotFound
except ImportError:
# missing Docker SDK for Python handled in module_utils.docker.common
pass
Expand Down Expand Up @@ -557,6 +557,8 @@ def present(self):
self.client.fail('Cannot find the image %s locally.' % name)
if not self.check_mode and image and image['Id'] == self.results['image']['Id']:
self.results['changed'] = False
else:
self.results['image'] = image

if self.archive_path:
self.archive_image(self.name, self.tag)
Expand All @@ -574,7 +576,7 @@ def absent(self):
'''
name = self.name
if is_image_name_id(name):
image = self.client.find_image_by_id(name)
image = self.client.find_image_by_id(name, accept_missing_image=True)
else:
image = self.client.find_image(name, self.tag)
if self.tag:
Expand All @@ -583,6 +585,9 @@ def absent(self):
if not self.check_mode:
try:
self.client.remove_image(name, force=self.force_absent)
except NotFound:
# If the image vanished while we were trying to remove it, don't fail
pass
except Exception as exc:
self.fail("Error removing image %s - %s" % (name, str(exc)))

Expand Down
6 changes: 4 additions & 2 deletions lib/ansible/modules/cloud/docker/docker_image_info.py
Expand Up @@ -172,7 +172,7 @@

try:
from docker import utils
from docker.errors import DockerException
from docker.errors import DockerException, NotFound
except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass
Expand Down Expand Up @@ -220,7 +220,7 @@ def get_facts(self):
for name in names:
if is_image_name_id(name):
self.log('Fetching image %s (ID)' % (name))
image = self.client.find_image_by_id(name)
image = self.client.find_image_by_id(name, accept_missing_image=True)
else:
repository, tag = utils.parse_repository_tag(name)
if not tag:
Expand All @@ -237,6 +237,8 @@ def get_all_images(self):
for image in images:
try:
inspection = self.client.inspect_image(image['Id'])
except NotFound:
pass
except Exception as exc:
self.fail("Error inspecting image %s - %s" % (image['Id'], str(exc)))
results.append(inspection)
Expand Down