From f82c8401c2c1947834cca9bb5cdb90c07df25918 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Wed, 6 Jul 2022 21:46:19 +0200 Subject: [PATCH] Rewrite the docker_image_load module (#406) * Rewrite the docker_image_load module. * Improve error messages. --- changelogs/fragments/406-docker-api.yml | 4 ++++ plugins/modules/docker_image_load.py | 20 +++++++------------ .../targets/docker_image_load/tasks/test.yml | 4 ++-- 3 files changed, 13 insertions(+), 15 deletions(-) create mode 100644 changelogs/fragments/406-docker-api.yml diff --git a/changelogs/fragments/406-docker-api.yml b/changelogs/fragments/406-docker-api.yml new file mode 100644 index 000000000..8621affbc --- /dev/null +++ b/changelogs/fragments/406-docker-api.yml @@ -0,0 +1,4 @@ +major_changes: + - "docker_image_load - 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/406)." diff --git a/plugins/modules/docker_image_load.py b/plugins/modules/docker_image_load.py index 3ace84d3c..119b1559c 100644 --- a/plugins/modules/docker_image_load.py +++ b/plugins/modules/docker_image_load.py @@ -28,14 +28,12 @@ required: true extends_documentation_fragment: -- community.docker.docker -- community.docker.docker.docker_py_2_documentation + - community.docker.docker.api_documentation notes: - Does not support C(check_mode). requirements: - - "L(Docker SDK for Python,https://docker-py.readthedocs.io/en/stable/) >= 2.5.0" - "Docker API >= 1.25" author: @@ -75,7 +73,7 @@ from ansible.module_utils.common.text.converters import to_native -from ansible_collections.community.docker.plugins.module_utils.common import ( +from ansible_collections.community.docker.plugins.module_utils.common_api import ( AnsibleDockerClient, RequestException, ) @@ -84,11 +82,7 @@ is_image_name_id, ) -try: - from docker.errors import DockerException -except ImportError: - # missing Docker SDK for Python handled in module_utils.docker.common - pass +from ansible_collections.community.docker.plugins.module_utils._api.errors import DockerException class ImageManager(DockerBaseClass): @@ -125,7 +119,8 @@ def load_images(self): self.log("Opening image {0}".format(self.path)) with open(self.path, 'rb') as image_tar: self.log("Loading images from {0}".format(self.path)) - for line in self.client.load_image(image_tar): + res = self.client._post(self.client._url("/images/load"), data=image_tar, stream=True) + for line in self.client._stream_helper(res, decode=True): self.log(line, pretty_print=True) self._extract_output_line(line, load_output) except EnvironmentError as exc: @@ -168,7 +163,6 @@ def main(): path=dict(type='path', required=True), ), supports_check_mode=False, - min_docker_version='2.5.0', ) try: @@ -180,10 +174,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()) diff --git a/tests/integration/targets/docker_image_load/tasks/test.yml b/tests/integration/targets/docker_image_load/tasks/test.yml index 2496e5679..fbe9beaaa 100644 --- a/tests/integration/targets/docker_image_load/tasks/test.yml +++ b/tests/integration/targets/docker_image_load/tasks/test.yml @@ -28,7 +28,7 @@ force_kill: yes with_items: "{{ cnames }}" - when: docker_py_version is version('2.5.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 tests!" - when: not(docker_py_version is version('2.5.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)