diff options
Diffstat (limited to 'contrib/inventory/docker.py')
-rwxr-xr-x | contrib/inventory/docker.py | 46 |
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)) |