mirror of
https://github.com/ansible-collections/community.docker.git
synced 2025-12-16 20:08:41 +00:00
Rewrite the docker_image_load module (#406)
* Rewrite the docker_image_load module. * Improve error messages.
This commit is contained in:
parent
e4f3402035
commit
f82c8401c2
4
changelogs/fragments/406-docker-api.yml
Normal file
4
changelogs/fragments/406-docker-api.yml
Normal file
@ -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)."
|
||||||
@ -28,14 +28,12 @@ options:
|
|||||||
required: true
|
required: true
|
||||||
|
|
||||||
extends_documentation_fragment:
|
extends_documentation_fragment:
|
||||||
- community.docker.docker
|
- community.docker.docker.api_documentation
|
||||||
- community.docker.docker.docker_py_2_documentation
|
|
||||||
|
|
||||||
notes:
|
notes:
|
||||||
- Does not support C(check_mode).
|
- Does not support C(check_mode).
|
||||||
|
|
||||||
requirements:
|
requirements:
|
||||||
- "L(Docker SDK for Python,https://docker-py.readthedocs.io/en/stable/) >= 2.5.0"
|
|
||||||
- "Docker API >= 1.25"
|
- "Docker API >= 1.25"
|
||||||
|
|
||||||
author:
|
author:
|
||||||
@ -75,7 +73,7 @@ import traceback
|
|||||||
|
|
||||||
from ansible.module_utils.common.text.converters import to_native
|
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,
|
AnsibleDockerClient,
|
||||||
RequestException,
|
RequestException,
|
||||||
)
|
)
|
||||||
@ -84,11 +82,7 @@ from ansible_collections.community.docker.plugins.module_utils.util import (
|
|||||||
is_image_name_id,
|
is_image_name_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
from ansible_collections.community.docker.plugins.module_utils._api.errors import DockerException
|
||||||
from docker.errors import DockerException
|
|
||||||
except ImportError:
|
|
||||||
# missing Docker SDK for Python handled in module_utils.docker.common
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class ImageManager(DockerBaseClass):
|
class ImageManager(DockerBaseClass):
|
||||||
@ -125,7 +119,8 @@ class ImageManager(DockerBaseClass):
|
|||||||
self.log("Opening image {0}".format(self.path))
|
self.log("Opening image {0}".format(self.path))
|
||||||
with open(self.path, 'rb') as image_tar:
|
with open(self.path, 'rb') as image_tar:
|
||||||
self.log("Loading images from {0}".format(self.path))
|
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.log(line, pretty_print=True)
|
||||||
self._extract_output_line(line, load_output)
|
self._extract_output_line(line, load_output)
|
||||||
except EnvironmentError as exc:
|
except EnvironmentError as exc:
|
||||||
@ -168,7 +163,6 @@ def main():
|
|||||||
path=dict(type='path', required=True),
|
path=dict(type='path', required=True),
|
||||||
),
|
),
|
||||||
supports_check_mode=False,
|
supports_check_mode=False,
|
||||||
min_docker_version='2.5.0',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -180,10 +174,10 @@ def main():
|
|||||||
ImageManager(client, results)
|
ImageManager(client, results)
|
||||||
client.module.exit_json(**results)
|
client.module.exit_json(**results)
|
||||||
except DockerException as e:
|
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:
|
except RequestException as e:
|
||||||
client.fail(
|
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())
|
exception=traceback.format_exc())
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -28,7 +28,7 @@
|
|||||||
force_kill: yes
|
force_kill: yes
|
||||||
with_items: "{{ cnames }}"
|
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!"
|
- 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)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user