Skip to content

Commit

Permalink
Rewrite the docker_image_load module (#406)
Browse files Browse the repository at this point in the history
* Rewrite the docker_image_load module.

* Improve error messages.
  • Loading branch information
felixfontein authored Jul 6, 2022
1 parent e4f3402 commit f82c840
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
4 changes: 4 additions & 0 deletions changelogs/fragments/406-docker-api.yml
Original file line number Diff line number Diff line change
@@ -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)."
20 changes: 7 additions & 13 deletions plugins/modules/docker_image_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
)
Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -168,7 +163,6 @@ def main():
path=dict(type='path', required=True),
),
supports_check_mode=False,
min_docker_version='2.5.0',
)

try:
Expand All @@ -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())


Expand Down
4 changes: 2 additions & 2 deletions tests/integration/targets/docker_image_load/tasks/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit f82c840

Please sign in to comment.