From 0917074a4200b87de3cf31ce10b348a0ea200b11 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 25 Sep 2019 15:15:13 +0200 Subject: [PATCH] Detection for existence of configuration file. --- lib/cr_tf/__init__.py | 4 +++- lib/cr_tf/app.py | 23 ++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/cr_tf/__init__.py b/lib/cr_tf/__init__.py index 91409dd..72c7bad 100644 --- a/lib/cr_tf/__init__.py +++ b/lib/cr_tf/__init__.py @@ -1,11 +1,13 @@ #!/bin/env python3 # -*- coding: utf-8 -*- -__version__ = '1.3.0' +__version__ = '1.3.1' MIN_VERSION_TERRAFORM = '0.12.0' MAX_VERSION_TERRAFORM = '0.12.99' MIN_VERSION_VSPHERE_PROVIDER = '1.11.0' +CFGFILE_BASENAME = 'create-terraform.ini' + # vim: ts=4 et list diff --git a/lib/cr_tf/app.py b/lib/cr_tf/app.py index 358ef23..1aeba33 100644 --- a/lib/cr_tf/app.py +++ b/lib/cr_tf/app.py @@ -22,6 +22,7 @@ from pathlib import Path # Own modules from . import __version__ as __pkg_version__ +from . import CFGFILE_BASENAME from fb_tools.common import pp @@ -42,7 +43,7 @@ from .xlate import __base_dir__ as __xlate_base_dir__ from .xlate import __mo_file__ as __xlate_mo_file__ from .xlate import XLATOR, LOCALE_DIR, DOMAIN -__version__ = '1.0.4' +__version__ = '1.0.5' LOG = logging.getLogger(__name__) SIGNAL_NAMES = { @@ -168,6 +169,7 @@ class CrTfApplication(BaseApplication): self.yaml_file = None self.config = None self.handler = None + self._cfg_dir = None self._cfg_file = None desc = _( @@ -179,6 +181,12 @@ class CrTfApplication(BaseApplication): description=desc, terminal_has_colors=terminal_has_colors, initialized=False, ) + # ------------------------------------------------------------------------- + @property + def cfg_dir(self): + """Directory of the configuration file.""" + return self._cfg_dir + # ------------------------------------------------------------------------- @property def cfg_file(self): @@ -202,8 +210,20 @@ class CrTfApplication(BaseApplication): self.init_logging() + self._cfg_dir = self.base_dir.joinpath('etc') + self._cfg_file = self.cfg_dir.joinpath(CFGFILE_BASENAME) + self.perform_arg_parser() + if not self.cfg_file.exists(): + default_conf_file = self.cfg_dir.joinpath(CFGFILE_BASENAME + '.default') + msg = (_( + "Configuration file {f!r} does not exists. Please copy {d!r} to {f!r} and " + "fill out all necessary entries, e.g. the passwords and API keys.").format( + f=str(self.cfg_file), d=str(default_conf_file))) + LOG.error(msg) + self.exit(1) + self.config = CrTfConfiguration( appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, config_file=self.cfg_file) @@ -286,6 +306,7 @@ class CrTfApplication(BaseApplication): res = super(CrTfApplication, self).as_dict(short=short) + res['cfg_dir'] = self.cfg_dir res['cfg_file'] = self.cfg_file res['__pkg_version__'] = __pkg_version__ res['config'] = None -- 2.39.5