docker_*: report more warnings (#53710)
* More warnings. * Add changelog. * Improve docstring.
This commit is contained in:
parent
35e7fb776a
commit
fbbab7429e
4 changed files with 20 additions and 2 deletions
changelogs/fragments
lib/ansible
3
changelogs/fragments/53710-docker-warnings.yml
Normal file
3
changelogs/fragments/53710-docker-warnings.yml
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
bugfixes:
|
||||||
|
- "docker_network - now returns warnings from docker daemon on network creation."
|
||||||
|
- "docker_swarm_service - now returns warnings from docker daemon on service creation."
|
|
@ -24,6 +24,7 @@ from distutils.version import LooseVersion
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
||||||
from ansible.module_utils.common._collections_compat import Mapping, Sequence
|
from ansible.module_utils.common._collections_compat import Mapping, Sequence
|
||||||
|
from ansible.module_utils.six import string_types
|
||||||
from ansible.module_utils.six.moves.urllib.parse import urlparse
|
from ansible.module_utils.six.moves.urllib.parse import urlparse
|
||||||
from ansible.module_utils.parsing.convert_bool import BOOLEANS_TRUE, BOOLEANS_FALSE
|
from ansible.module_utils.parsing.convert_bool import BOOLEANS_TRUE, BOOLEANS_FALSE
|
||||||
|
|
||||||
|
@ -664,6 +665,15 @@ class AnsibleDockerClient(Client):
|
||||||
def report_warnings(self, result, warnings_key=None):
|
def report_warnings(self, result, warnings_key=None):
|
||||||
'''
|
'''
|
||||||
Checks result of client operation for warnings, and if present, outputs them.
|
Checks result of client operation for warnings, and if present, outputs them.
|
||||||
|
|
||||||
|
warnings_key should be a list of keys used to crawl the result dictionary.
|
||||||
|
For example, if warnings_key == ['a', 'b'], the function will consider
|
||||||
|
result['a']['b'] if these keys exist. If the result is a non-empty string, it
|
||||||
|
will be reported as a warning. If the result is a list, every entry will be
|
||||||
|
reported as a warning.
|
||||||
|
|
||||||
|
In most cases (if warnings are returned at all), warnings_key should be
|
||||||
|
['Warnings'] or ['Warning']. The default value (if not specified) is ['Warnings'].
|
||||||
'''
|
'''
|
||||||
if warnings_key is None:
|
if warnings_key is None:
|
||||||
warnings_key = ['Warnings']
|
warnings_key = ['Warnings']
|
||||||
|
@ -674,6 +684,8 @@ class AnsibleDockerClient(Client):
|
||||||
if isinstance(result, Sequence):
|
if isinstance(result, Sequence):
|
||||||
for warning in result:
|
for warning in result:
|
||||||
self.module.warn('Docker warning: {0}'.format(warning))
|
self.module.warn('Docker warning: {0}'.format(warning))
|
||||||
|
elif isinstance(result, string_types) and result:
|
||||||
|
self.module.warn('Docker warning: {0}'.format(result))
|
||||||
|
|
||||||
|
|
||||||
def compare_dict_allow_more_present(av, bv):
|
def compare_dict_allow_more_present(av, bv):
|
||||||
|
|
|
@ -496,7 +496,7 @@ class DockerNetworkManager(object):
|
||||||
|
|
||||||
if not self.check_mode:
|
if not self.check_mode:
|
||||||
resp = self.client.create_network(self.parameters.network_name, **params)
|
resp = self.client.create_network(self.parameters.network_name, **params)
|
||||||
|
self.client.report_warnings(resp, ['Warning'])
|
||||||
self.existing_network = self.client.get_network(id=resp['Id'])
|
self.existing_network = self.client.get_network(id=resp['Id'])
|
||||||
self.results['actions'].append("Created network %s with driver %s" % (self.parameters.network_name, self.parameters.driver))
|
self.results['actions'].append("Created network %s with driver %s" % (self.parameters.network_name, self.parameters.driver))
|
||||||
self.results['changed'] = True
|
self.results['changed'] = True
|
||||||
|
|
|
@ -2255,10 +2255,13 @@ class DockerServiceManager(object):
|
||||||
name=name,
|
name=name,
|
||||||
**service_data
|
**service_data
|
||||||
)
|
)
|
||||||
|
# Unfortunately, docker-py f***ed up and doesn't return the structure
|
||||||
|
# the corresponding API call returns, which would include a list of warnings.
|
||||||
|
|
||||||
def create_service(self, name, service):
|
def create_service(self, name, service):
|
||||||
service_data = service.build_docker_service(self.get_networks_names_ids())
|
service_data = service.build_docker_service(self.get_networks_names_ids())
|
||||||
self.client.create_service(name=name, **service_data)
|
result = self.client.create_service(name=name, **service_data)
|
||||||
|
self.client.report_warnings(result, ['Warning'])
|
||||||
|
|
||||||
def remove_service(self, name):
|
def remove_service(self, name):
|
||||||
self.client.remove_service(name)
|
self.client.remove_service(name)
|
||||||
|
|
Loading…
Add table
Reference in a new issue