diff options
author | Stefan Hajnoczi <stefanha@gmail.com> | 2016-06-11 17:08:57 +0100 |
---|---|---|
committer | Stefan Hajnoczi <stefanha@gmail.com> | 2016-06-11 17:29:39 +0100 |
commit | 8254e2b54780cbb578247da9092b7c2ea1ea1078 (patch) | |
tree | 1606c2adbaa9beb15c23c2bee7f1bf2d6b77aeb9 | |
parent | 1eb63c7567430e5f5b862b348dc9c3f61a1ede4e (diff) | |
download | ansible-modules-core-8254e2b54780cbb578247da9092b7c2ea1ea1078.tar.gz |
docker_service: make PyYAML requirement explicit
The "Developing Modules" documentation states:
Include a minimum of dependencies if possible. If there are
dependencies, document them at the top of the module file, and have
the module raise JSON error messages when the import fails.
When docker_service runs on a remote host without PyYAML it crashes with
ImportError.
This patch raises a JSON error message when import fails, but only if
the PyYAML module is actually used. It's only needed when the
"definition" parameter is given.
Signed-off-by: Stefan Hajnoczi <stefanha@gmail.com>
-rw-r--r-- | cloud/docker/docker_service.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/cloud/docker/docker_service.py b/cloud/docker/docker_service.py index c45b3dda..527e0fbf 100644 --- a/cloud/docker/docker_service.py +++ b/cloud/docker/docker_service.py @@ -153,6 +153,7 @@ requirements: - "python >= 2.6" - "docker-compose >= 1.7.0" - "Docker API >= 1.20" + - "PyYAML >= 3.11" ''' EXAMPLES = ''' @@ -402,13 +403,19 @@ actions: type: string ''' +HAS_YAML = True +HAS_YAML_EXC = None HAS_COMPOSE = True HAS_COMPOSE_EXC = None MINIMUM_COMPOSE_VERSION = '1.7.0' -import yaml -from distutils.version import LooseVersion +try: + import yaml +except ImportError as exc: + HAS_YAML = False + HAS_YAML_EXC = str(exc) +from distutils.version import LooseVersion from ansible.module_utils.basic import * try: @@ -489,6 +496,9 @@ class ContainerManager(DockerBaseClass): self.log(self.options, pretty_print=True) if self.definition: + if not HAS_YAML: + self.client.fail("Unable to load yaml. Try `pip install PyYAML`. Error: %s" % HAS_YAML_EXC) + if not self.project_name: self.client.fail("Parameter error - project_name required when providing definition.") |