Rewrite the docker_image_info module (#405)

* Rewrite the docker_image_info module.

* Improve error messages.
This commit is contained in:
Felix Fontein 2022-07-06 21:46:14 +02:00 committed by GitHub
parent 4f2f45b953
commit e4f3402035
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 19 deletions

View File

@ -0,0 +1,4 @@
major_changes:
- "docker_image_info - no longer uses the Docker SDK for Python. It requires ``requests`` to be installed,
and depending on the features used has some more requirements. If the Docker SDK for Python is installed,
these requirements are likely met (https://github.com/ansible-collections/community.docker/pull/405)."

View File

@ -36,12 +36,10 @@ options:
elements: str
extends_documentation_fragment:
- community.docker.docker
- community.docker.docker.docker_py_1_documentation
- community.docker.docker.api_documentation
requirements:
- "L(Docker SDK for Python,https://docker-py.readthedocs.io/en/stable/) >= 1.8.0"
- "Docker API >= 1.25"
author:
@ -167,14 +165,7 @@ import traceback
from ansible.module_utils.common.text.converters import to_native
try:
from docker import utils
from docker.errors import DockerException, NotFound
except ImportError:
# missing Docker SDK for Python handled in ansible.module_utils.docker.common
pass
from ansible_collections.community.docker.plugins.module_utils.common import (
from ansible_collections.community.docker.plugins.module_utils.common_api import (
AnsibleDockerClient,
RequestException,
)
@ -182,6 +173,8 @@ from ansible_collections.community.docker.plugins.module_utils.util import (
DockerBaseClass,
is_image_name_id,
)
from ansible_collections.community.docker.plugins.module_utils._api.errors import DockerException, NotFound
from ansible_collections.community.docker.plugins.module_utils._api.utils.utils import parse_repository_tag
class ImageManager(DockerBaseClass):
@ -221,7 +214,7 @@ class ImageManager(DockerBaseClass):
self.log('Fetching image %s (ID)' % (name))
image = self.client.find_image_by_id(name, accept_missing_image=True)
else:
repository, tag = utils.parse_repository_tag(name)
repository, tag = parse_repository_tag(name)
if not tag:
tag = 'latest'
self.log('Fetching image %s:%s' % (repository, tag))
@ -232,12 +225,16 @@ class ImageManager(DockerBaseClass):
def get_all_images(self):
results = []
images = self.client.images()
params = {
'only_ids': 0,
'all': 0,
}
images = self.client.get_json("/images/json", params=params)
for image in images:
try:
inspection = self.client.inspect_image(image['Id'])
inspection = self.client.get_json('/images/{0}/json', image['Id'])
except NotFound:
pass
inspection = None
except Exception as exc:
self.fail("Error inspecting image %s - %s" % (image['Id'], to_native(exc)))
results.append(inspection)
@ -263,10 +260,10 @@ def main():
ImageManager(client, results)
client.module.exit_json(**results)
except DockerException as e:
client.fail('An unexpected docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
client.fail('An unexpected Docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc())
except RequestException as e:
client.fail(
'An unexpected requests error occurred when Docker SDK for Python tried to talk to the docker daemon: {0}'.format(to_native(e)),
'An unexpected requests error occurred when trying to talk to the Docker daemon: {0}'.format(to_native(e)),
exception=traceback.format_exc())

View File

@ -53,7 +53,7 @@
- "docker_test_image_hello_world in result.images[0].RepoTags"
- "docker_test_image_alpine in result.images[1].RepoTags"
when: docker_py_version is version('1.8.0', '>=') and docker_api_version is version('1.25', '>=')
when: docker_api_version is version('1.25', '>=')
- fail: msg="Too old docker / docker-py version to run docker_image_info tests!"
when: not(docker_py_version is version('1.8.0', '>=') and docker_api_version is version('1.25', '>=')) and (ansible_distribution != 'CentOS' or ansible_distribution_major_version|int > 6)
when: not(docker_api_version is version('1.25', '>=')) and (ansible_distribution != 'CentOS' or ansible_distribution_major_version|int > 6)