mirror of
https://github.com/ansible-collections/community.docker.git
synced 2025-12-17 12:28:55 +00:00
Rewrite the docker_host_info module (#403)
* Rewrite the docker_host_info module. * Improve error messages.
This commit is contained in:
parent
37ff980a44
commit
9e168b75cf
4
changelogs/fragments/403-docker-api.yml
Normal file
4
changelogs/fragments/403-docker-api.yml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
major_changes:
|
||||||
|
- "docker_host_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/403)."
|
||||||
@ -94,15 +94,13 @@ options:
|
|||||||
type: bool
|
type: bool
|
||||||
default: no
|
default: no
|
||||||
extends_documentation_fragment:
|
extends_documentation_fragment:
|
||||||
- community.docker.docker
|
- community.docker.docker.api_documentation
|
||||||
- community.docker.docker.docker_py_1_documentation
|
|
||||||
|
|
||||||
|
|
||||||
author:
|
author:
|
||||||
- Piotr Wojciechowski (@WojciechowskiPiotr)
|
- Piotr Wojciechowski (@WojciechowskiPiotr)
|
||||||
|
|
||||||
requirements:
|
requirements:
|
||||||
- "L(Docker SDK for Python,https://docker-py.readthedocs.io/en/stable/) >= 1.10.0"
|
|
||||||
- "Docker API >= 1.25"
|
- "Docker API >= 1.25"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
@ -205,21 +203,17 @@ 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,
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
|
||||||
from docker.errors import DockerException, APIError
|
|
||||||
except ImportError:
|
|
||||||
# Missing Docker SDK for Python handled in ansible.module_utils.docker.common
|
|
||||||
pass
|
|
||||||
|
|
||||||
from ansible_collections.community.docker.plugins.module_utils.util import (
|
from ansible_collections.community.docker.plugins.module_utils.util import (
|
||||||
DockerBaseClass,
|
DockerBaseClass,
|
||||||
clean_dict_booleans_for_docker_api,
|
clean_dict_booleans_for_docker_api,
|
||||||
)
|
)
|
||||||
|
from ansible_collections.community.docker.plugins.module_utils._api.errors import DockerException, APIError
|
||||||
|
from ansible_collections.community.docker.plugins.module_utils._api.utils.utils import convert_filters
|
||||||
|
|
||||||
|
|
||||||
class DockerHostManager(DockerBaseClass):
|
class DockerHostManager(DockerBaseClass):
|
||||||
@ -275,25 +269,37 @@ class DockerHostManager(DockerBaseClass):
|
|||||||
filter_arg['filters'] = filters
|
filter_arg['filters'] = filters
|
||||||
try:
|
try:
|
||||||
if docker_object == 'containers':
|
if docker_object == 'containers':
|
||||||
items = self.client.containers(**filter_arg)
|
params = {
|
||||||
|
'limit': -1,
|
||||||
|
'all': 0,
|
||||||
|
'size': 0,
|
||||||
|
'trunc_cmd': 0,
|
||||||
|
'filters': convert_filters(filters) if filters else None,
|
||||||
|
}
|
||||||
|
items = self.client.get_json("/containers/json", params=params)
|
||||||
elif docker_object == 'networks':
|
elif docker_object == 'networks':
|
||||||
items = self.client.networks(**filter_arg)
|
params = {
|
||||||
|
'filters': convert_filters(filters or {})
|
||||||
|
}
|
||||||
|
items = self.client.get_json("/networks", params=params)
|
||||||
elif docker_object == 'images':
|
elif docker_object == 'images':
|
||||||
items = self.client.images(**filter_arg)
|
params = {
|
||||||
|
'only_ids': 0,
|
||||||
|
'all': 0,
|
||||||
|
'filters': convert_filters(filters) if filters else None,
|
||||||
|
}
|
||||||
|
items = self.client.get_json("/images/json", params=params)
|
||||||
elif docker_object == 'volumes':
|
elif docker_object == 'volumes':
|
||||||
items = self.client.volumes(**filter_arg)
|
params = {
|
||||||
|
'filters': convert_filters(filters) if filters else None,
|
||||||
|
}
|
||||||
|
items = self.client.get_json('/volumes', params=params)
|
||||||
|
items = items['Volumes']
|
||||||
except APIError as exc:
|
except APIError as exc:
|
||||||
self.client.fail("Error inspecting docker host for object '%s': %s" %
|
self.client.fail("Error inspecting docker host for object '%s': %s" % (docker_object, to_native(exc)))
|
||||||
(docker_object, to_native(exc)))
|
|
||||||
|
|
||||||
if self.verbose_output:
|
if self.verbose_output:
|
||||||
if docker_object != 'volumes':
|
|
||||||
return items
|
return items
|
||||||
else:
|
|
||||||
return items['Volumes']
|
|
||||||
|
|
||||||
if docker_object == 'volumes':
|
|
||||||
items = items['Volumes']
|
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
item_record = dict()
|
item_record = dict()
|
||||||
@ -329,16 +335,9 @@ def main():
|
|||||||
verbose_output=dict(type='bool', default=False),
|
verbose_output=dict(type='bool', default=False),
|
||||||
)
|
)
|
||||||
|
|
||||||
option_minimal_versions = dict(
|
|
||||||
network_filters=dict(docker_py_version='2.0.2'),
|
|
||||||
disk_usage=dict(docker_py_version='2.2.0'),
|
|
||||||
)
|
|
||||||
|
|
||||||
client = AnsibleDockerClient(
|
client = AnsibleDockerClient(
|
||||||
argument_spec=argument_spec,
|
argument_spec=argument_spec,
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
min_docker_version='1.10.0',
|
|
||||||
option_minimal_versions=option_minimal_versions,
|
|
||||||
fail_results=dict(
|
fail_results=dict(
|
||||||
can_talk_to_docker=False,
|
can_talk_to_docker=False,
|
||||||
),
|
),
|
||||||
@ -353,10 +352,10 @@ def main():
|
|||||||
DockerHostManager(client, results)
|
DockerHostManager(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())
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@
|
|||||||
####################################################################
|
####################################################################
|
||||||
|
|
||||||
- include_tasks: test_host_info.yml
|
- include_tasks: test_host_info.yml
|
||||||
when: docker_py_version is version('1.10.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_host_info tests!"
|
- fail: msg="Too old docker / docker-py version to run docker_host_info tests!"
|
||||||
when: not(docker_py_version is version('1.10.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)
|
||||||
|
|||||||
@ -224,7 +224,6 @@
|
|||||||
docker_host_info:
|
docker_host_info:
|
||||||
disk_usage: yes
|
disk_usage: yes
|
||||||
register: output
|
register: output
|
||||||
ignore_errors: yes
|
|
||||||
|
|
||||||
- name: assert reading docker host facts when docker is running and get disk usage
|
- name: assert reading docker host facts when docker is running and get disk usage
|
||||||
assert:
|
assert:
|
||||||
@ -236,20 +235,12 @@
|
|||||||
- 'output.images is not defined'
|
- 'output.images is not defined'
|
||||||
- 'output.disk_usage.LayersSize is number'
|
- 'output.disk_usage.LayersSize is number'
|
||||||
- 'output.disk_usage.BuilderSize is not defined'
|
- 'output.disk_usage.BuilderSize is not defined'
|
||||||
when: docker_py_version is version('2.2.0', '>=')
|
|
||||||
- assert:
|
|
||||||
that:
|
|
||||||
- output is failed
|
|
||||||
- "('version is ' ~ docker_py_version ~ ' ') in output.msg"
|
|
||||||
- "'Minimum version required is 2.2.0 ' in output.msg"
|
|
||||||
when: docker_py_version is version('2.2.0', '<')
|
|
||||||
|
|
||||||
- name: Get info on Docker host and get disk usage with verbose output
|
- name: Get info on Docker host and get disk usage with verbose output
|
||||||
docker_host_info:
|
docker_host_info:
|
||||||
disk_usage: yes
|
disk_usage: yes
|
||||||
verbose_output: yes
|
verbose_output: yes
|
||||||
register: output
|
register: output
|
||||||
ignore_errors: yes
|
|
||||||
|
|
||||||
- name: assert reading docker host facts when docker is running and get disk usage with verbose output
|
- name: assert reading docker host facts when docker is running and get disk usage with verbose output
|
||||||
assert:
|
assert:
|
||||||
@ -261,13 +252,6 @@
|
|||||||
- 'output.images is not defined'
|
- 'output.images is not defined'
|
||||||
- 'output.disk_usage.LayersSize is number'
|
- 'output.disk_usage.LayersSize is number'
|
||||||
- 'output.disk_usage.BuilderSize is number'
|
- 'output.disk_usage.BuilderSize is number'
|
||||||
when: docker_py_version is version('2.2.0', '>=')
|
|
||||||
- assert:
|
|
||||||
that:
|
|
||||||
- output is failed
|
|
||||||
- "('version is ' ~ docker_py_version ~ ' ') in output.msg"
|
|
||||||
- "'Minimum version required is 2.2.0 ' in output.msg"
|
|
||||||
when: docker_py_version is version('2.2.0', '<')
|
|
||||||
|
|
||||||
- name: Get info on Docker host, disk usage and get all lists together
|
- name: Get info on Docker host, disk usage and get all lists together
|
||||||
docker_host_info:
|
docker_host_info:
|
||||||
@ -275,7 +259,7 @@
|
|||||||
containers: yes
|
containers: yes
|
||||||
networks: yes
|
networks: yes
|
||||||
images: yes
|
images: yes
|
||||||
disk_usage: "{{ docker_py_version is version('2.2.0', '>=') }}"
|
disk_usage: yes
|
||||||
register: output
|
register: output
|
||||||
|
|
||||||
- name: assert reading docker host facts when docker is running, disk usage and get lists together
|
- name: assert reading docker host facts when docker is running, disk usage and get lists together
|
||||||
@ -290,11 +274,8 @@
|
|||||||
- 'output.volumes[0].Mountpoint is not defined'
|
- 'output.volumes[0].Mountpoint is not defined'
|
||||||
- 'output.images[0].Id is string'
|
- 'output.images[0].Id is string'
|
||||||
- 'output.images[0].ParentId is not defined'
|
- 'output.images[0].ParentId is not defined'
|
||||||
- assert:
|
|
||||||
that:
|
|
||||||
- 'output.disk_usage.LayersSize is number'
|
- 'output.disk_usage.LayersSize is number'
|
||||||
- 'output.disk_usage.BuilderSize is not defined'
|
- 'output.disk_usage.BuilderSize is not defined'
|
||||||
when: docker_py_version is version('2.2.0', '>=')
|
|
||||||
|
|
||||||
- name: Get info on Docker host, disk usage and get all lists together with verbose output
|
- name: Get info on Docker host, disk usage and get all lists together with verbose output
|
||||||
docker_host_info:
|
docker_host_info:
|
||||||
@ -302,7 +283,7 @@
|
|||||||
containers: yes
|
containers: yes
|
||||||
networks: yes
|
networks: yes
|
||||||
images: yes
|
images: yes
|
||||||
disk_usage: "{{ docker_py_version is version('2.2.0', '>=') }}"
|
disk_usage: yes
|
||||||
verbose_output: yes
|
verbose_output: yes
|
||||||
register: output
|
register: output
|
||||||
|
|
||||||
@ -318,11 +299,8 @@
|
|||||||
- 'output.volumes[0].Mountpoint is string'
|
- 'output.volumes[0].Mountpoint is string'
|
||||||
- 'output.images[0].Id is string'
|
- 'output.images[0].Id is string'
|
||||||
- 'output.images[0].ParentId is string'
|
- 'output.images[0].ParentId is string'
|
||||||
- assert:
|
|
||||||
that:
|
|
||||||
- 'output.disk_usage.LayersSize is number'
|
- 'output.disk_usage.LayersSize is number'
|
||||||
- 'output.disk_usage.BuilderSize is number'
|
- 'output.disk_usage.BuilderSize is number'
|
||||||
when: docker_py_version is version('2.2.0', '>=')
|
|
||||||
|
|
||||||
always:
|
always:
|
||||||
- name: Delete container
|
- name: Delete container
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user