summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authormilo-minderbinder <milo-minderbinder@users.noreply.github.com>2018-10-30 15:39:32 -0400
committeransibot <ansibot@users.noreply.github.com>2018-10-30 15:39:32 -0400
commit77d73808e65287f26a7dab400beda8de9277cbab (patch)
tree399105b68700c4db99338eb26b7124f86ff326d5 /contrib
parent4572d9ccd776b88a2fc4f3f9ae5215962652f37d (diff)
downloadansible-77d73808e65287f26a7dab400beda8de9277cbab.tar.gz
fix setting config with DOCKER_CONFIG_FILE (#23096)
Fixes #23095 Fixed issue preventing a caller from setting a docker configuration file (e.g. docker.yml) by specifing the config file path in the `DOCKER_CONFIG_FILE` environment variable. Previously, the cli argument parser set a default value for the `--config-file` argument which would prevent ever checking the environment variable, regardless of whether or not the `--config-file` argument even specified a valid file. This commit adds a global `DEFAULT_DOCKER_CONFIG_FILE` variable, which points to the current default `docker.yml` config in the contrib/inventory directory. Now, when this script is called from the command line, the config file passed with the cli `--config-file` arg will be given the highest precedence; if it is absent, this script will then check if the `DOCKER_CONFIG_FILE` env var is set, and load the config file specified if possible. If neither the environment variable or cli argument are specified, then the script will attempt to parse the config file `docker.yml` in this script's directory (if present). If either the `DOCKER_CONFIG_FILE` environment variable or the `--config-file` argument are given but point to a nonexistant file, then the script will print an error message and exit with an error code. It is *not* an error condition if the fallback `docker.yml` does not exist.
Diffstat (limited to 'contrib')
-rwxr-xr-xcontrib/inventory/docker.py46
1 files changed, 22 insertions, 24 deletions
diff --git a/contrib/inventory/docker.py b/contrib/inventory/docker.py
index 9e344828b3..93359ea0e0 100755
--- a/contrib/inventory/docker.py
+++ b/contrib/inventory/docker.py
@@ -391,6 +391,7 @@ except ImportError as exc:
class Client:
pass
+DEFAULT_DOCKER_CONFIG_FILE = os.path.splitext(os.path.basename(__file__))[0] + '.yml'
DEFAULT_DOCKER_HOST = 'unix://var/run/docker.sock'
DEFAULT_TLS = False
DEFAULT_TLS_VERIFY = False
@@ -784,29 +785,29 @@ class DockerInventory(object):
def _parse_config_file(self):
config = dict()
- config_path = None
+ config_file = DEFAULT_DOCKER_CONFIG_FILE
if self._args.config_file:
- config_path = self._args.config_file
+ config_file = self._args.config_file
elif self._env_args.config_file:
- config_path = self._env_args.config_file
+ config_file = self._env_args.config_file
- if config_path:
- try:
- config_file = os.path.abspath(config_path)
- # default config path is docker.yml in same directory as this script
- # old behaviour is docker.yml in current directory. Handle both.
- if not os.path.exists(config_file):
- config_file = os.path.abspath(os.path.basename(config_path))
- except:
- config_file = None
-
- if config_file and os.path.exists(config_file):
- with open(config_file) as f:
- try:
- config = yaml.safe_load(f.read())
- except Exception as exc:
- self.fail("Error: parsing %s - %s" % (config_path, str(exc)))
+ config_file = os.path.abspath(config_file)
+
+ if os.path.isfile(config_file):
+ with open(config_file) as f:
+ try:
+ config = yaml.safe_load(f.read())
+ except Exception as exc:
+ self.fail("Error: parsing %s - %s" % (config_file, str(exc)))
+ else:
+ msg = "Error: config file given by {} does not exist - " + config_file
+ if self._args.config_file:
+ self.fail(msg.format('command line argument'))
+ elif self._env_args.config_file:
+ self.fail(msg.format(DOCKER_ENV_ARGS.get('config_file')))
+ else:
+ self.log(msg.format('DEFAULT_DOCKER_CONFIG_FILE'))
return config
def log(self, msg, pretty_print=False):
@@ -831,9 +832,6 @@ class DockerInventory(object):
def _parse_cli_args(self):
# Parse command line arguments
- basename = os.path.splitext(os.path.basename(__file__))[0]
- default_config = os.path.join(os.path.dirname(__file__), basename + '.yml')
-
parser = argparse.ArgumentParser(
description='Return Ansible inventory for one or more Docker hosts.')
parser.add_argument('--list', action='store_true', default=True,
@@ -844,8 +842,8 @@ class DockerInventory(object):
help='Only get information for a specific container.')
parser.add_argument('--pretty', action='store_true', default=False,
help='Pretty print JSON output(default: False)')
- parser.add_argument('--config-file', action='store', default=default_config,
- help="Name of the config file to use. Default is %s" % (default_config))
+ parser.add_argument('--config-file', action='store', default=None,
+ help="Name of the config file to use. Default is %s" % (DEFAULT_DOCKER_CONFIG_FILE))
parser.add_argument('--docker-host', action='store', default=None,
help="The base url or Unix sock path to connect to the docker daemon. Defaults to %s"
% (DEFAULT_DOCKER_HOST))