import argparse
import getpass
import argparse
+import pathlib
# Third party modules
from . import __version__ as GLOBAL_VERSION
import fb_tools
+
+from fb_tools.common import pp, caller_search_path
+
from fb_tools.app import BaseApplication
-from fb_tools.errors import FbAppError, ExpectedHandlerError, CommandNotFoundError
-from fb_tools.common import caller_search_path
+from fb_tools.errors import FbAppError, ExpectedHandlerError, CommandNotFoundError
from .config import CrTplConfiguration
setattr(namespace, self.dest, values)
+# =============================================================================
+class CfgFileOptionAction(argparse.Action):
+
+ # -------------------------------------------------------------------------
+ def __init__(self, option_strings, *args, **kwargs):
+
+ super(CfgFileOptionAction, self).__init__(
+ option_strings=option_strings, *args, **kwargs)
+
+ # -------------------------------------------------------------------------
+ def __call__(self, parser, namespace, values, option_string=None):
+
+ if values is None:
+ setattr(namespace, self.dest, None)
+ return
+
+ path = pathlib.Path(values)
+ if not path.exists():
+ msg = "File {!r} does not exists.".format(values)
+ raise argparse.ArgumentError(self, msg)
+ if not path.is_file():
+ msg = "File {!r} is not a regular file.".format(values)
+ raise argparse.ArgumentError(self, msg)
+
+ setattr(namespace, self.dest, path.resolve())
+
+
# =============================================================================
class CrTplApplication(BaseApplication):
"""
which can be used to spawn different virtual machines.
""").strip()
+ self._cfg_file = None
self.config = None
super(CrTplApplication, self).__init__(
self.initialized = True
+ # -------------------------------------------------------------------------
+ @property
+ def cfg_file(self):
+ """Configuration file."""
+ return self._cfg_file
+
# -------------------------------------------------------------------------
def as_dict(self, short=True):
"""
"""
res = super(CrTplApplication, self).as_dict(short=short)
+ res['cfg_file'] = self.cfg_file
return res
self.init_logging()
+ self.perform_arg_parser()
+
self.config = CrTplConfiguration(
- appname=self.appname, verbose=self.verbose, base_dir=self.base_dir)
+ appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
+ config_file=self.cfg_file)
+ #self.config.config_file = self.cfg_file
self.config.read()
if self.config.verbose > self.verbose:
self.verbose = self.config.verbose
self.config.initialized = True
- self.perform_arg_parser()
+ if self.verbose > 3:
+ LOG.debug("Read configuration:\n{}".format(pp(self.config.as_dict())))
+
+ self.perform_arg_parser_vmware()
-# if not self.config.password:
-# prompt = 'Enter password for host {h!r} and user {u!r}: '.format(
-# h=self.config.vsphere_host, u=self.config.vsphere_user)
-# self.config.password = getpass.getpass(prompt=prompt)
+ if not self.config.password:
+ prompt = 'Enter password for host {h!r} and user {u!r}: '.format(
+ h=self.config.vsphere_host, u=self.config.vsphere_user)
+ self.config.password = getpass.getpass(prompt=prompt)
self.handler = CrTplHandler(
appname=self.appname, verbose=self.verbose, base_dir=self.base_dir)
super(CrTplApplication, self).init_arg_parser()
+ default_cfg_file = self.base_dir.joinpath('etc').joinpath(self.appname + '.ini')
+
self.arg_parser.add_argument(
'-A', '--abort', dest='abort', action='store_true',
help="Abort creation of VMWare template after successsful creation of template VM.",
)
+ self.arg_parser.add_argument(
+ '-c', '--config', '--config-file', dest='cfg_file', metavar='FILE',
+ action=CfgFileOptionAction,
+ help="Configuration file (default: {!r})".format(default_cfg_file)
+ )
+
vmware_group = self.arg_parser.add_argument_group('VMWare options')
vmware_group.add_argument(
# -------------------------------------------------------------------------
def perform_arg_parser(self):
+
+ if self.args.cfg_file:
+ self._cfg_file = self.args.cfg_file
+
+ # -------------------------------------------------------------------------
+ def perform_arg_parser_vmware(self):
"""
Public available method to execute some actions after parsing
the command line parameters.
"""
- pass
-
if self.args.host:
self.config.vsphere_host = self.args.host
if self.args.port:
self.config.template_name = self.args.template
if self.args.number is not None:
- v = self.args.number
- if v < 1:
- LOG.error((
- "Wrong number {} of templates to stay in templates folder, "
- "must be greater than zero.").format(v))
- elif v >= 100:
- LOG.error((
- "Wrong number {} of templates to stay in templates folder, "
- "must be less than 100.").format(v))
- else:
- self.config.max_nr_templates_stay = v
+ self.config.max_nr_templates_stay = self.args.number
# -------------------------------------------------------------------------
def _run(self):
# -------------------------------------------------------------------------
def __init__(
self, appname=None, verbose=0, version=__version__, base_dir=None,
- initialized=False):
+ encoding=None, config_dir=None, config_file=None, initialized=False):
self.vsphere_host = self.default_vsphere_host
self.vsphere_port = self.default_vsphere_port
self.excluded_datastores = []
super(CrTplConfiguration, self).__init__(
- appname=appname,
- verbose=verbose,
- version=version,
- base_dir=base_dir,
- initialized=False,
+ appname=appname, verbose=verbose, version=version, base_dir=base_dir,
+ encoding=encoding, config_dir=config_dir, config_file=config_file, initialized=False,
)
# Workaround, bis das Lesen der Config implementiert ist
# -------------------------------------------------------------------------
def _eval_config_vsphere(self, config, section_name):
- if self.verbose > 2:
+ if self.verbose > 1:
LOG.debug("Checking config section {!r} ...".format(section_name))
re_excl_ds = re.compile(r'^\s*excluded?[-_]datastores?\s*$', re.IGNORECASE)
# -------------------------------------------------------------------------
def _eval_config_template(self, config, section_name):
- if self.verbose > 2:
+ if self.verbose > 1:
LOG.debug("Checking config section {!r} ...".format(section_name))
for (key, value) in config.items(section_name):