From 5ab23e43f65e37a8614c5a1658e2bff32199b914 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Wed, 7 Apr 2021 07:59:26 +0200 Subject: [PATCH] Improve exception handling. --- .../fragments/121-exception-handling.yml | 2 ++ plugins/modules/docker_compose.py | 24 +++++++++++-------- plugins/modules/docker_config.py | 6 +++-- plugins/modules/docker_container.py | 18 +++++++------- plugins/modules/docker_container_exec.py | 12 ++++++---- plugins/modules/docker_container_info.py | 8 +++++-- plugins/modules/docker_host_info.py | 6 +++-- plugins/modules/docker_image.py | 24 ++++++++++--------- plugins/modules/docker_image_info.py | 10 +++++--- plugins/modules/docker_image_load.py | 15 +++++++----- plugins/modules/docker_login.py | 13 ++++++---- plugins/modules/docker_network.py | 10 +++++--- plugins/modules/docker_network_info.py | 8 +++++-- plugins/modules/docker_node.py | 6 +++-- plugins/modules/docker_node_info.py | 8 +++++-- plugins/modules/docker_plugin.py | 24 ++++++++++--------- plugins/modules/docker_prune.py | 8 +++++-- plugins/modules/docker_secret.py | 6 +++-- plugins/modules/docker_swarm.py | 6 +++-- plugins/modules/docker_swarm_info.py | 6 +++-- plugins/modules/docker_swarm_service.py | 14 ++++++----- plugins/modules/docker_swarm_service_info.py | 8 +++++-- plugins/modules/docker_volume.py | 14 +++++++---- plugins/modules/docker_volume_info.py | 10 +++++--- 24 files changed, 168 insertions(+), 98 deletions(-) create mode 100644 changelogs/fragments/121-exception-handling.yml diff --git a/changelogs/fragments/121-exception-handling.yml b/changelogs/fragments/121-exception-handling.yml new file mode 100644 index 000000000..28eefc400 --- /dev/null +++ b/changelogs/fragments/121-exception-handling.yml @@ -0,0 +1,2 @@ +bugfixes: +- "all modules - use ``to_native`` to convert exceptions to strings (https://github.com/ansible-collections/community.docker/pull/121)." diff --git a/plugins/modules/docker_compose.py b/plugins/modules/docker_compose.py index 823920186..7eaba417e 100644 --- a/plugins/modules/docker_compose.py +++ b/plugins/modules/docker_compose.py @@ -485,6 +485,8 @@ HAS_COMPOSE_EXC = traceback.format_exc() DEFAULT_TIMEOUT = 10 +from ansible.module_utils._text import to_native + from ansible_collections.community.docker.plugins.module_utils.common import ( AnsibleDockerClient, DockerBaseClass, @@ -632,7 +634,7 @@ def __init__(self, client): if not HAS_COMPOSE: self.client.fail("Unable to load docker-compose. Try `pip install docker-compose`. Error: %s" % - HAS_COMPOSE_EXC) + to_native(HAS_COMPOSE_EXC)) if LooseVersion(compose_version) < LooseVersion(MINIMUM_COMPOSE_VERSION): self.client.fail("Found docker-compose version %s. Minimum required version is %s. " @@ -647,7 +649,7 @@ def __init__(self, client): if self.definition: if not HAS_YAML: - self.client.fail("Unable to load yaml. Try `pip install PyYAML`. Error: %s" % HAS_YAML_EXC) + self.client.fail("Unable to load yaml. Try `pip install PyYAML`. Error: %s" % to_native(HAS_YAML_EXC)) if not self.project_name: self.client.fail("Parameter error - project_name required when providing definition.") @@ -660,7 +662,7 @@ def __init__(self, client): with open(compose_file, 'w') as f: f.write(yaml.dump(self.definition, default_flow_style=False)) except Exception as exc: - self.client.fail("Error writing to %s - %s" % (compose_file, str(exc))) + self.client.fail("Error writing to %s - %s" % (compose_file, to_native(exc))) else: if not self.project_src: self.client.fail("Parameter error - project_src required.") @@ -669,7 +671,7 @@ def __init__(self, client): self.log("project_src: %s" % self.project_src) self.project = project_from_options(self.project_src, self.options) except Exception as exc: - self.client.fail("Configuration error - %s" % str(exc)) + self.client.fail("Configuration error - %s" % to_native(exc)) def exec_module(self): result = dict() @@ -886,7 +888,7 @@ def cmd_pull(self): except NoSuchImageError: pass except Exception as exc: - self.client.fail("Error: service image lookup failed - %s" % str(exc)) + self.client.fail("Error: service image lookup failed - %s" % to_native(exc)) out_redir_name, err_redir_name = make_redirection_tempfiles() # pull the image @@ -908,7 +910,7 @@ def cmd_pull(self): if image and image.get('Id'): new_image_id = image['Id'] except NoSuchImageError as exc: - self.client.fail("Error: service image lookup failed after pull - %s" % str(exc)) + self.client.fail("Error: service image lookup failed after pull - %s" % to_native(exc)) if new_image_id != old_image_id: # if a new image was pulled @@ -940,7 +942,7 @@ def cmd_build(self): except NoSuchImageError: pass except Exception as exc: - self.client.fail("Error: service image lookup failed - %s" % str(exc)) + self.client.fail("Error: service image lookup failed - %s" % to_native(exc)) out_redir_name, err_redir_name = make_redirection_tempfiles() # build the image @@ -1099,7 +1101,7 @@ def parse_scale(self, service_name): return int(self.scale[service_name]) except ValueError: self.client.fail("Error scaling %s - expected int, got %s", - service_name, str(type(self.scale[service_name]))) + service_name, to_native(type(self.scale[service_name]))) def main(): @@ -1142,9 +1144,11 @@ def main(): result = ContainerManager(client).exec_module() client.module.exit_json(**result) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_config.py b/plugins/modules/docker_config.py index 3791dda42..748db3a1d 100644 --- a/plugins/modules/docker_config.py +++ b/plugins/modules/docker_config.py @@ -290,9 +290,11 @@ def main(): ConfigManager(client, results)() client.module.exit_json(**results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_container.py b/plugins/modules/docker_container.py index 575183d9d..23ec29871 100644 --- a/plugins/modules/docker_container.py +++ b/plugins/modules/docker_container.py @@ -1416,7 +1416,7 @@ def __init__(self, client): try: setattr(self, param_name, human_to_bytes(client.module.params.get(param_name))) except ValueError as exc: - self.fail("Failed to convert %s to bytes: %s" % (param_name, exc)) + self.fail("Failed to convert %s to bytes: %s" % (param_name, to_native(exc))) self.publish_all_ports = False self.published_ports = self._parse_publish_ports() @@ -1728,7 +1728,7 @@ def get_default_host_ip(self): break except NotFound as nfe: self.client.fail( - "Cannot inspect the network '{0}' to determine the default IP: {1}".format(net['name'], nfe), + "Cannot inspect the network '{0}' to determine the default IP: {1}".format(net['name'], to_native(nfe)), exception=traceback.format_exc() ) return ip @@ -1898,7 +1898,7 @@ def _parse_ulimits(self): try: results.append(Ulimit(**limits)) except ValueError as exc: - self.fail("Error parsing ulimits value %s - %s" % (limit, exc)) + self.fail("Error parsing ulimits value %s - %s" % (limit, to_native(exc))) return results def _parse_sysctls(self): @@ -1935,7 +1935,7 @@ def _parse_log_config(self): try: return LogConfig(**options) except ValueError as exc: - self.fail('Error parsing logging options - %s' % (exc)) + self.fail('Error parsing logging options - %s' % (to_native(exc), )) def _parse_tmpfs(self): ''' @@ -2019,7 +2019,7 @@ def _process_mounts(self): try: mount_dict['tmpfs_size'] = human_to_bytes(mount_dict['tmpfs_size']) except ValueError as exc: - self.fail('Failed to convert tmpfs_size of mount "{0}" to bytes: {1}'.format(target, exc)) + self.fail('Failed to convert tmpfs_size of mount "{0}" to bytes: {1}'.format(target, to_native(exc))) if mount_dict.get('tmpfs_mode') is not None: try: mount_dict['tmpfs_mode'] = int(mount_dict['tmpfs_mode'], 8) @@ -3164,7 +3164,7 @@ def container_kill(self, container_id): else: response = self.client.kill(container_id) except Exception as exc: - self.fail("Error killing container %s: %s" % (container_id, exc)) + self.fail("Error killing container %s: %s" % (container_id, to_native(exc))) return response def container_restart(self, container_id): @@ -3608,9 +3608,11 @@ def main(): cm = ContainerManager(client) client.module.exit_json(**sanitize_result(cm.results)) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_container_exec.py b/plugins/modules/docker_container_exec.py index 27532d8f2..a01b4e1d4 100644 --- a/plugins/modules/docker_container_exec.py +++ b/plugins/modules/docker_container_exec.py @@ -126,7 +126,7 @@ import shlex import traceback -from ansible.module_utils._text import to_text, to_bytes +from ansible.module_utils._text import to_text, to_bytes, to_native from ansible_collections.community.docker.plugins.module_utils.common import ( AnsibleDockerClient, @@ -244,12 +244,14 @@ def main(): client.fail('Could not find container "{0}"'.format(container)) except APIError as e: if e.response and e.response.status_code == 409: - client.fail('The container "{0}" has been paused ({1})'.format(container, e)) - client.fail('An unexpected docker error occurred: {0}'.format(e), exception=traceback.format_exc()) + client.fail('The container "{0}" has been paused ({1})'.format(container, to_native(e))) + client.fail('An unexpected docker error occurred: {0}'.format(to_native(e)), exception=traceback.format_exc()) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_container_info.py b/plugins/modules/docker_container_info.py index 92a735252..fd3d395b3 100644 --- a/plugins/modules/docker_container_info.py +++ b/plugins/modules/docker_container_info.py @@ -104,6 +104,8 @@ import traceback +from ansible.module_utils._text import to_native + try: from docker.errors import DockerException except ImportError: @@ -136,9 +138,11 @@ def main(): container=container, ) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_host_info.py b/plugins/modules/docker_host_info.py index 9007221af..3700a32a8 100644 --- a/plugins/modules/docker_host_info.py +++ b/plugins/modules/docker_host_info.py @@ -334,9 +334,11 @@ def main(): DockerHostManager(client, results) client.module.exit_json(**results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_image.py b/plugins/modules/docker_image.py index af5b704aa..f3bde7a53 100644 --- a/plugins/modules/docker_image.py +++ b/plugins/modules/docker_image.py @@ -501,7 +501,7 @@ def absent(self): # If the image vanished while we were trying to remove it, don't fail pass except Exception as exc: - self.fail("Error removing image %s - %s" % (name, str(exc))) + self.fail("Error removing image %s - %s" % (name, to_native(exc))) self.results['changed'] = True self.results['actions'].append("Removed image %s" % (name)) @@ -536,7 +536,7 @@ def archive_image(self, name, tag): try: saved_image = self.client.get_image(image_name) except Exception as exc: - self.fail("Error getting image %s - %s" % (image_name, str(exc))) + self.fail("Error getting image %s - %s" % (image_name, to_native(exc))) try: with open(self.archive_path, 'wb') as fd: @@ -547,7 +547,7 @@ def archive_image(self, name, tag): for chunk in saved_image.stream(2048, decode_content=False): fd.write(chunk) except Exception as exc: - self.fail("Error writing image archive %s - %s" % (self.archive_path, str(exc))) + self.fail("Error writing image archive %s - %s" % (self.archive_path, to_native(exc))) if image: self.results['image'] = image @@ -590,11 +590,11 @@ def push_image(self, name, tag=None): if re.search('unauthorized', str(exc)): if re.search('authentication required', str(exc)): self.fail("Error pushing image %s/%s:%s - %s. Try logging into %s first." % - (registry, repo_name, tag, str(exc), registry)) + (registry, repo_name, tag, to_native(exc), registry)) else: self.fail("Error pushing image %s/%s:%s - %s. Does the repository exist?" % (registry, repo_name, tag, str(exc))) - self.fail("Error pushing image %s: %s" % (repository, str(exc))) + self.fail("Error pushing image %s: %s" % (repository, to_native(exc))) self.results['image'] = self.client.find_image(name=repository, tag=tag) if not self.results['image']: self.results['image'] = dict() @@ -634,7 +634,7 @@ def tag_image(self, name, tag, repository, push=False): if not tag_status: raise Exception("Tag operation failed.") except Exception as exc: - self.fail("Error: failed to tag image - %s" % str(exc)) + self.fail("Error: failed to tag image - %s" % to_native(exc)) self.results['image'] = self.client.find_image(name=repo, tag=repo_tag) if image and image['Id'] == self.results['image']['Id']: self.results['changed'] = False @@ -763,10 +763,10 @@ def load_image(self): ) except EnvironmentError as exc: if exc.errno == errno.ENOENT: - self.client.fail("Error opening image %s - %s" % (self.load_path, str(exc))) - self.client.fail("Error loading image %s - %s" % (self.name, str(exc)), stdout='\n'.join(load_output)) + self.client.fail("Error opening image %s - %s" % (self.load_path, to_native(exc))) + self.client.fail("Error loading image %s - %s" % (self.name, to_native(exc)), stdout='\n'.join(load_output)) except Exception as exc: - self.client.fail("Error loading image %s - %s" % (self.name, str(exc)), stdout='\n'.join(load_output)) + self.client.fail("Error loading image %s - %s" % (self.name, to_native(exc)), stdout='\n'.join(load_output)) # Collect loaded images if has_output: @@ -908,9 +908,11 @@ def detect_pull_platform(client): ImageManager(client, results) client.module.exit_json(**results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_image_info.py b/plugins/modules/docker_image_info.py index 86c7b093d..a71e76322 100644 --- a/plugins/modules/docker_image_info.py +++ b/plugins/modules/docker_image_info.py @@ -165,6 +165,8 @@ import traceback +from ansible.module_utils._text import to_native + try: from docker import utils from docker.errors import DockerException, NotFound @@ -235,7 +237,7 @@ def get_all_images(self): except NotFound: pass except Exception as exc: - self.fail("Error inspecting image %s - %s" % (image['Id'], str(exc))) + self.fail("Error inspecting image %s - %s" % (image['Id'], to_native(exc))) results.append(inspection) return results @@ -260,9 +262,11 @@ def main(): ImageManager(client, results) client.module.exit_json(**results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_image_load.py b/plugins/modules/docker_image_load.py index 920520b15..ae60304b8 100644 --- a/plugins/modules/docker_image_load.py +++ b/plugins/modules/docker_image_load.py @@ -73,6 +73,8 @@ import errno import traceback +from ansible.module_utils._text import to_native + from ansible_collections.community.docker.plugins.module_utils.common import ( AnsibleDockerClient, DockerBaseClass, @@ -126,10 +128,10 @@ def load_images(self): self._extract_output_line(line, load_output) except EnvironmentError as exc: if exc.errno == errno.ENOENT: - self.client.fail("Error opening archive {0} - {1}".format(self.path, str(exc))) - self.client.fail("Error loading archive {0} - {1}".format(self.path, str(exc)), stdout='\n'.join(load_output)) + self.client.fail("Error opening archive {0} - {1}".format(self.path, to_native(exc))) + self.client.fail("Error loading archive {0} - {1}".format(self.path, to_native(exc)), stdout='\n'.join(load_output)) except Exception as exc: - self.client.fail("Error loading archive {0} - {1}".format(self.path, str(exc)), stdout='\n'.join(load_output)) + self.client.fail("Error loading archive {0} - {1}".format(self.path, to_native(exc)), stdout='\n'.join(load_output)) # Collect loaded images loaded_images = [] @@ -177,10 +179,11 @@ def main(): ImageManager(client, results) client.module.exit_json(**results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk ' - 'to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_login.py b/plugins/modules/docker_login.py index 03d3f2a1a..e7c7c25f9 100644 --- a/plugins/modules/docker_login.py +++ b/plugins/modules/docker_login.py @@ -124,7 +124,8 @@ import os import re import traceback -from ansible.module_utils._text import to_bytes, to_text + +from ansible.module_utils._text import to_bytes, to_text, to_native try: from docker.errors import DockerException @@ -333,7 +334,7 @@ def login(self): dockercfg_path=self.config_path ) except Exception as exc: - self.fail("Logging into %s for user %s failed - %s" % (self.registry_url, self.username, str(exc))) + self.fail("Logging into %s for user %s failed - %s" % (self.registry_url, self.username, to_native(exc))) # If user is already logged in, then response contains password for user if 'password' in response: @@ -351,7 +352,7 @@ def login(self): dockercfg_path=self.config_path ) except Exception as exc: - self.fail("Logging into %s for user %s failed - %s" % (self.registry_url, self.username, str(exc))) + self.fail("Logging into %s for user %s failed - %s" % (self.registry_url, self.username, to_native(exc))) response.pop('password', None) self.results['login_result'] = response @@ -477,9 +478,11 @@ def main(): del results['actions'] client.module.exit_json(**results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_network.py b/plugins/modules/docker_network.py index d2ecf39ce..29984bb42 100644 --- a/plugins/modules/docker_network.py +++ b/plugins/modules/docker_network.py @@ -254,6 +254,8 @@ from distutils.version import LooseVersion +from ansible.module_utils._text import to_native + from ansible_collections.community.docker.plugins.module_utils.common import ( AnsibleDockerClient, DockerBaseClass, @@ -370,7 +372,7 @@ def __init__(self, client): for ipam_config in self.parameters.ipam_config: validate_cidr(ipam_config['subnet']) except ValueError as e: - self.client.fail(str(e)) + self.client.fail(to_native(e)) if self.parameters.driver_options: self.parameters.driver_options = clean_dict_booleans_for_docker_api(self.parameters.driver_options) @@ -663,9 +665,11 @@ def main(): cm = DockerNetworkManager(client) client.module.exit_json(**cm.results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_network_info.py b/plugins/modules/docker_network_info.py index 491ebf8b8..404a4567a 100644 --- a/plugins/modules/docker_network_info.py +++ b/plugins/modules/docker_network_info.py @@ -100,6 +100,8 @@ import traceback +from ansible.module_utils._text import to_native + try: from docker.errors import DockerException except ImportError: @@ -132,9 +134,11 @@ def main(): network=network, ) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_node.py b/plugins/modules/docker_node.py index d73b2d705..ef2c16864 100644 --- a/plugins/modules/docker_node.py +++ b/plugins/modules/docker_node.py @@ -285,9 +285,11 @@ def main(): SwarmNodeManager(client, results) client.module.exit_json(**results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_node_info.py b/plugins/modules/docker_node_info.py index d541588c3..ff324b008 100644 --- a/plugins/modules/docker_node_info.py +++ b/plugins/modules/docker_node_info.py @@ -87,6 +87,8 @@ import traceback +from ansible.module_utils._text import to_native + from ansible_collections.community.docker.plugins.module_utils.common import ( RequestException, ) @@ -147,9 +149,11 @@ def main(): nodes=nodes, ) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_plugin.py b/plugins/modules/docker_plugin.py index 0d0580617..c08bb16c4 100644 --- a/plugins/modules/docker_plugin.py +++ b/plugins/modules/docker_plugin.py @@ -108,6 +108,8 @@ import traceback +from ansible.module_utils._text import to_native + try: from docker.errors import APIError, NotFound, DockerException from docker import DockerClient @@ -122,8 +124,6 @@ RequestException ) -from ansible.module_utils.six import text_type - class TaskParameters(DockerBaseClass): def __init__(self, client): @@ -189,7 +189,7 @@ def get_existing_plugin(self): except NotFound: return None except APIError as e: - self.client.fail(text_type(e)) + self.client.fail(to_native(e)) if plugin is None: return None @@ -227,7 +227,7 @@ def install_plugin(self): try: self.existing_plugin = self.dclient.plugins.install(self.parameters.plugin_name, None) except APIError as e: - self.client.fail(text_type(e)) + self.client.fail(to_native(e)) self.results['actions'].append("Installed plugin %s" % self.parameters.plugin_name) self.results['changed'] = True @@ -239,7 +239,7 @@ def remove_plugin(self): try: self.existing_plugin.remove(force) except APIError as e: - self.client.fail(text_type(e)) + self.client.fail(to_native(e)) self.results['actions'].append("Removed plugin %s" % self.parameters.plugin_name) self.results['changed'] = True @@ -252,7 +252,7 @@ def update_plugin(self): try: self.existing_plugin.configure(prepare_options(self.parameters.plugin_options)) except APIError as e: - self.client.fail(text_type(e)) + self.client.fail(to_native(e)) self.results['actions'].append("Updated plugin %s settings" % self.parameters.plugin_name) self.results['changed'] = True else: @@ -287,7 +287,7 @@ def enable(self): try: self.existing_plugin.enable(timeout) except APIError as e: - self.client.fail(text_type(e)) + self.client.fail(to_native(e)) self.results['actions'].append("Enabled plugin %s" % self.parameters.plugin_name) self.results['changed'] = True else: @@ -296,7 +296,7 @@ def enable(self): try: self.existing_plugin.enable(timeout) except APIError as e: - self.client.fail(text_type(e)) + self.client.fail(to_native(e)) self.results['actions'].append("Enabled plugin %s" % self.parameters.plugin_name) self.results['changed'] = True @@ -307,7 +307,7 @@ def disable(self): try: self.existing_plugin.disable() except APIError as e: - self.client.fail(text_type(e)) + self.client.fail(to_native(e)) self.results['actions'].append("Disable plugin %s" % self.parameters.plugin_name) self.results['changed'] = True else: @@ -334,9 +334,11 @@ def main(): cm = DockerPluginManager(client) client.module.exit_json(**cm.results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_prune.py b/plugins/modules/docker_prune.py index 8fcb40944..150446326 100644 --- a/plugins/modules/docker_prune.py +++ b/plugins/modules/docker_prune.py @@ -178,6 +178,8 @@ import traceback +from ansible.module_utils._text import to_native + try: from docker.errors import DockerException except ImportError: @@ -256,9 +258,11 @@ def main(): client.module.exit_json(**result) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_secret.py b/plugins/modules/docker_secret.py index 57df6e66f..26d6ec792 100644 --- a/plugins/modules/docker_secret.py +++ b/plugins/modules/docker_secret.py @@ -293,9 +293,11 @@ def main(): SecretManager(client, results)() client.module.exit_json(**results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_swarm.py b/plugins/modules/docker_swarm.py index 3a7486a63..d1651c4c7 100644 --- a/plugins/modules/docker_swarm.py +++ b/plugins/modules/docker_swarm.py @@ -663,9 +663,11 @@ def main(): SwarmManager(client, results)() client.module.exit_json(**results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_swarm_info.py b/plugins/modules/docker_swarm_info.py index 095bf7fea..d91c23f34 100644 --- a/plugins/modules/docker_swarm_info.py +++ b/plugins/modules/docker_swarm_info.py @@ -375,9 +375,11 @@ def main(): results.update(client.fail_results) client.module.exit_json(**results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_swarm_service.py b/plugins/modules/docker_swarm_service.py index fa7785629..d640bf372 100644 --- a/plugins/modules/docker_swarm_service.py +++ b/plugins/modules/docker_swarm_service.py @@ -934,7 +934,7 @@ from ansible.module_utils.basic import human_to_bytes from ansible.module_utils.six import string_types -from ansible.module_utils._text import to_text +from ansible.module_utils._text import to_text, to_native try: from docker import types @@ -2419,7 +2419,7 @@ def run(self): except DockerException as e: self.client.fail( 'Error looking for an image named %s: %s' - % (image, e) + % (image, to_native(e)) ) try: @@ -2427,7 +2427,7 @@ def run(self): except Exception as e: self.client.fail( 'Error looking for service named %s: %s' - % (module.params['name'], e) + % (module.params['name'], to_native(e)) ) try: secret_ids = self.get_missing_secret_ids() @@ -2445,7 +2445,7 @@ def run(self): ) except Exception as e: return self.client.fail( - 'Error parsing module parameters: %s' % e + 'Error parsing module parameters: %s' % to_native(e) ) changed = False @@ -2822,9 +2822,11 @@ def main(): client.module.exit_json(**results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_swarm_service_info.py b/plugins/modules/docker_swarm_service_info.py index b1b25caa5..f35eacdf2 100644 --- a/plugins/modules/docker_swarm_service_info.py +++ b/plugins/modules/docker_swarm_service_info.py @@ -62,6 +62,8 @@ import traceback +from ansible.module_utils._text import to_native + try: from docker.errors import DockerException except ImportError: @@ -106,9 +108,11 @@ def main(): exists=bool(service) ) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_volume.py b/plugins/modules/docker_volume.py index e504a5910..a09508be0 100644 --- a/plugins/modules/docker_volume.py +++ b/plugins/modules/docker_volume.py @@ -109,6 +109,8 @@ import traceback +from ansible.module_utils._text import to_native + try: from docker.errors import DockerException, APIError except ImportError: @@ -171,7 +173,7 @@ def get_existing_volume(self): try: volumes = self.client.volumes() except APIError as e: - self.client.fail(text_type(e)) + self.client.fail(to_native(e)) if volumes[u'Volumes'] is None: return None @@ -228,7 +230,7 @@ def create_volume(self): resp = self.client.create_volume(self.parameters.volume_name, **params) self.existing_volume = self.client.inspect_volume(resp['Name']) except APIError as e: - self.client.fail(text_type(e)) + self.client.fail(to_native(e)) self.results['actions'].append("Created volume %s with driver %s" % (self.parameters.volume_name, self.parameters.driver)) self.results['changed'] = True @@ -239,7 +241,7 @@ def remove_volume(self): try: self.client.remove_volume(self.parameters.volume_name) except APIError as e: - self.client.fail(text_type(e)) + self.client.fail(to_native(e)) self.results['actions'].append("Removed volume %s" % self.parameters.volume_name) self.results['changed'] = True @@ -299,9 +301,11 @@ def main(): cm = DockerVolumeManager(client) client.module.exit_json(**cm.results) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__': diff --git a/plugins/modules/docker_volume_info.py b/plugins/modules/docker_volume_info.py index 8e1734c27..0e086d9c4 100644 --- a/plugins/modules/docker_volume_info.py +++ b/plugins/modules/docker_volume_info.py @@ -77,6 +77,8 @@ import traceback +from ansible.module_utils._text import to_native + try: from docker.errors import DockerException, NotFound except ImportError: @@ -95,7 +97,7 @@ def get_existing_volume(client, volume_name): except NotFound as dummy: return None except Exception as exc: - client.fail("Error inspecting volume: %s" % exc) + client.fail("Error inspecting volume: %s" % to_native(exc)) def main(): @@ -119,9 +121,11 @@ def main(): volume=volume, ) except DockerException as e: - client.fail('An unexpected docker error occurred: {0}'.format(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-py tried to talk to the docker daemon: {0}'.format(e), exception=traceback.format_exc()) + client.fail( + 'An unexpected requests error occurred when docker-py tried to talk to the docker daemon: {0}'.format(to_native(e)), + exception=traceback.format_exc()) if __name__ == '__main__':