From 66633f4344e9902e273296a863e3b3535e65857d Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 7 Feb 2019 11:40:12 +0100 Subject: [PATCH] Adding some more configuration options --- etc/create-vmware-template.ini.default | 16 ++++ lib/cr_vmware_tpl/config.py | 110 +++++++++++++++++++++---- 2 files changed, 108 insertions(+), 18 deletions(-) diff --git a/etc/create-vmware-template.ini.default b/etc/create-vmware-template.ini.default index b5feff6..3545369 100644 --- a/etc/create-vmware-template.ini.default +++ b/etc/create-vmware-template.ini.default @@ -34,6 +34,22 @@ ;mac_address = 00:16:3e:54:ab:2b +[Timeouts] + +; General timeout in seconds for create_vm, poweron_vm and shutdown_vm +; if the particular timeouts are not configured +;max_wait_for_general = 15 + +; Timeout in seconds for creating a VM in VSphere +;max_wait_for_create_vm = 15 + +; Timeout in seconds for powering on a VM in VSphere +;max_wait_for_poweron_vm = 15 + +; Timeout in seconds for shutting down a VM in VSphere +;max_wait_for_shutdown_vm = 15 + +; Timeout in seconds for waiting of completing the installation of the template VM ;max_wait_for_finish_install = 3600 diff --git a/lib/cr_vmware_tpl/config.py b/lib/cr_vmware_tpl/config.py index d53750e..de72e35 100644 --- a/lib/cr_vmware_tpl/config.py +++ b/lib/cr_vmware_tpl/config.py @@ -17,7 +17,7 @@ import re # Own modules from fb_tools.config import ConfigError, BaseConfiguration -__version__ = '1.0.0' +__version__ = '1.1.1' LOG = logging.getLogger(__name__) @@ -49,11 +49,14 @@ class CrTplConfiguration(BaseConfiguration): default_ram_mb = 4 * 1024 default_network = '192.168.88.0_23' default_mac_address = '00:16:3e:54:ab:2b' + default_max_wait_for_general = 15 default_max_wait_for_finish_install = 60 * 60 default_max_nr_templates_stay = 4 default_vmware_cfg_version = 'vmx-13' default_os_version = 'oracleLinux7_64Guest' + min_max_wait_for_finish_general = 2 min_max_wait_for_finish_install = 3 * 60 + max_max_wait_for_finish_general = 60 * 60 max_max_wait_for_finish_install = 24 * 60 * 60 limit_max_nr_templates_stay = 100 @@ -78,6 +81,10 @@ class CrTplConfiguration(BaseConfiguration): self.ram_mb = self.default_ram_mb self.network = self.default_network self.mac_address = self.default_mac_address + self.max_wait_for_general = self.default_max_wait_for_general + self.max_wait_for_create_vm = None + self.max_wait_for_poweron_vm = None + self.max_wait_for_shutdown_vm = None self.max_wait_for_finish_install = self.default_max_wait_for_finish_install self.max_nr_templates_stay = self.default_max_nr_templates_stay self.vmware_cfg_version = self.default_vmware_cfg_version @@ -155,6 +162,9 @@ class CrTplConfiguration(BaseConfiguration): if section_name.lower() == 'template': self._eval_config_template(config, section_name) return + if section_name.lower() == 'timeouts': + self._eval_config_timeouts(config, section_name) + return if self.verbose > 1: LOG.debug("Unhandled configuration section {!r}.".format(section_name)) @@ -249,28 +259,92 @@ class CrTplConfiguration(BaseConfiguration): self.vmware_cfg_version = value.strip() elif key.lower() == 'os_version': self.os_version = value.strip() - elif key.lower() == 'max_wait_for_finish_install': - v = float(value) - if v < self.min_max_wait_for_finish_install: - LOG.error(( - "Value {val} for max_wait_for_finish_install is less than " - "{minval}, using {default} seconds.").format( - val=v, minval=self.min_max_wait_for_finish_install, - default=self.default_max_wait_for_finish_install)) - elif v > self.max_max_wait_for_finish_install: - LOG.error(( - "Value {val} for max_wait_for_finish_install is greater than " - "{maxval}, using {default} seconds.").format( - val=v, maxval=self.max_max_wait_for_finish_install, - default=self.default_max_wait_for_finish_install)) - else: - self.max_wait_for_finish_install = v + + self._eval_config_timeouts(config, section_name) return + # ------------------------------------------------------------------------- + def _eval_timeout_value(self, prop_name, value, min_val, max_val, default_val): + + if self.verbose > 2: + LOG.debug("Checking value {v!r} for {p} ...".format(v=value, p=prop_name)) + if self.verbose > 3: + LOG.debug(( + "Minimal value: {min_val}, maximum value: {max_val}, " + "default value: {def_val}.").format( + min_val=min_val, max_val=max_val, def_val=default_val)) + + v = float(value) + + if v < min_val: + msg = ( + "Value {val} for {prop} is less than {min_val}, " + "using {def_val} seconds.").format(val=v, min_val=min_val, def_val=default_val) + LOG.error(msg) + return -# ============================================================================= + if v < min_val: + msg = ( + "Value {val} for {prop} is greater than {max_val}, " + "using {def_val} seconds.").format(val=v, max_val=max_val, def_val=default_val) + LOG.error(msg) + return + + if self.verbose > 2: + msg = "Setting timeout {p!r} to {v:0.1f} seconds.".format(p=prop_name, v=v) + LOG.debug(msg) + setattr(self, prop_name, v) + # ------------------------------------------------------------------------- + def _eval_config_timeouts(self, config, section_name): + + if self.verbose > 1: + LOG.debug("Checking config section {!r} for timeouts ...".format(section_name)) + + for (key, value) in config.items(section_name): + if key.lower() == 'max_wait_for_general': + self._eval_timeout_value( + prop_name='max_wait_for_general', value=value, + min_val=self.min_max_wait_for_finish_general, + max_val=self.max_max_wait_for_finish_general, + default_val=self.default_max_wait_for_general) + + for (key, value) in config.items(section_name): + if key.lower() == 'max_wait_for_create_vm': + self._eval_timeout_value( + prop_name='max_wait_for_create_vm', value=value, + min_val=self.min_max_wait_for_finish_general, + max_val=self.max_max_wait_for_finish_general, + default_val=self.max_wait_for_general) + elif key.lower() == 'max_wait_for_poweron_vm': + self._eval_timeout_value( + prop_name='max_wait_for_poweron_vm', value=value, + min_val=self.min_max_wait_for_finish_general, + max_val=self.max_max_wait_for_finish_general, + default_val=self.max_wait_for_general) + elif key.lower() == 'max_wait_for_shutdown_vm': + self._eval_timeout_value( + prop_name='max_wait_for_shutdown_vm', value=value, + min_val=self.min_max_wait_for_finish_general, + max_val=self.max_max_wait_for_finish_general, + default_val=self.max_wait_for_general) + elif key.lower() == 'max_wait_for_finish_install': + self._eval_timeout_value( + prop_name='max_wait_for_finish_install', value=value, + min_val=self.min_max_wait_for_finish_install, + max_val=self.max_max_wait_for_finish_install, + default_val=self.default_max_wait_for_finish_install) + + if self.max_wait_for_create_vm is None: + self.max_wait_for_create_vm = self.max_wait_for_general + if self.max_wait_for_poweron_vm is None: + self.max_wait_for_poweron_vm = self.max_wait_for_general + if self.max_wait_for_shutdown_vm is None: + self.max_wait_for_shutdown_vm = self.max_wait_for_general + + +# ============================================================================= if __name__ == "__main__": pass -- 2.39.5