From 2f7e56eef6af66e33354ed91dca0bc45675606e8 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 13 Apr 2018 14:34:22 +0200 Subject: [PATCH] Defining command line parameters --- lib/cr_vmware_tpl/app.py | 34 ++++++++++++++++++++++++++++++++-- lib/cr_vmware_tpl/handler.py | 6 ++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/cr_vmware_tpl/app.py b/lib/cr_vmware_tpl/app.py index 6bc5f28..157d1f3 100644 --- a/lib/cr_vmware_tpl/app.py +++ b/lib/cr_vmware_tpl/app.py @@ -34,7 +34,7 @@ from .config import CrTplConfiguration from .handler import ExpectedHandlerError, CrTplHandler -__version__ = '0.3.4' +__version__ = '0.4.1' LOG = logging.getLogger(__name__) @@ -406,6 +406,8 @@ class CrTplApplication(PpBaseObject): self.config.password = getpass.getpass(prompt=prompt) self.handler.config = self.config + if self.args.rotate: + self.handler.rotate_only = True self.handler.initialized = True self.initialized = True @@ -612,7 +614,7 @@ class CrTplApplication(PpBaseObject): vmware_group.add_argument( '-F', '--folder', dest='folder', help="Folder in vSphere, where to create the template (Default: {!r}).".format( - CrTplConfiguration.default_template_vm) + CrTplConfiguration.default_folder) ) vmware_group.add_argument( @@ -629,6 +631,21 @@ class CrTplApplication(PpBaseObject): "(Default: {!r}).").format(CrTplConfiguration.default_template_name) ) + vmware_group.add_argument( + '-N', '--number', '--number-templates', dest='number', + type=int, metavar='INT', + help=( + "Maximum number of templates to stay in templates folder (" + "1 <= x < 100, Default: {}).".format( + CrTplConfiguration.default_max_nr_templates_stay)) + ) + + vmware_group.add_argument( + '-R', '--rotate', '--rotate-only', dest="rotate", action='store_true', + help="Execute rortation of existing templates only, don't create a new one." + ) + + # ------------------------------------------------------------------------- def _perform_arg_parser(self): """ @@ -678,6 +695,19 @@ class CrTplApplication(PpBaseObject): if self.args.template: 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 + # ------------------------------------------------------------------------- def _init_env(self): """ diff --git a/lib/cr_vmware_tpl/handler.py b/lib/cr_vmware_tpl/handler.py index 4777a70..9fff9ef 100644 --- a/lib/cr_vmware_tpl/handler.py +++ b/lib/cr_vmware_tpl/handler.py @@ -171,7 +171,7 @@ class CrTplHandler(PpBaseObject): self.ssh_user = 'root' self.ssh_timeout = 30 self.max_wait_for_shutdown = 600 - self.rotate_only = True + self.rotate_only = False if initialized: self.initialized = True @@ -211,7 +211,9 @@ class CrTplHandler(PpBaseObject): self.ensure_vm_folder() self.check_for_temp_tpl_vm() self.select_data_store() - if not self.rotate_only: + if self.rotate_only: + LOG.warn("Only executing of template rotating.") + else: self.check_network() self.create_vm() self.tpl_vm = self.get_temp_tpl_vm() -- 2.39.5