From: Frank Brehm Date: Mon, 6 Sep 2021 15:56:02 +0000 (+0200) Subject: Pre-Defining of VSphere configurations X-Git-Tag: 1.5.2^2~14 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=83b4ddacf3ef9f7a56d4f36ecf1c3bfc25f6fdfb;p=pixelpark%2Fcreate-terraform.git Pre-Defining of VSphere configurations --- diff --git a/lib/cr_tf/config.py b/lib/cr_tf/config.py index cd6f144..6dc93ac 100644 --- a/lib/cr_tf/config.py +++ b/lib/cr_tf/config.py @@ -32,7 +32,7 @@ from .errors import CrTfConfigError from .xlate import XLATOR -__version__ = '1.4.1' +__version__ = '1.5.0' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -380,20 +380,27 @@ class VsphereConfig(FbBaseObject): # ------------------------------------------------------------------------- def is_valid(self, raise_on_error=False): + name = '<{}>'.format(_('unknown')) + if self.name: + name = self.name + if self.verbose > 1: + LOG.debug(_("Checking validity of {o}-object {n!r} ...").format( + o=self.__class__.__name__, n=name)) + error_lst = [] - attribs = ('name', 'host', 'user', 'password') - for attrib in attribs: + mandatory_attribs = ('name', 'host', 'dc', 'cluster') + requested_attribs = ('user', 'password') + + for attrib in mandatory_attribs: cur_val = getattr(self, attrib, None) if not cur_val: - name = '<{}>'.format(_('unknown')) - if self.name: - name = self.name msg = _("Attribute {a!r} of the {o}-object {n!r} is not set.").format( a=attrib, o=self.__class__.__name__, n=name) error_lst.append(msg) if not raise_on_error: LOG.error(msg) + if error_lst: if raise_on_error: nr = len(error_lst) @@ -403,6 +410,16 @@ class VsphereConfig(FbBaseObject): msg = msg.format(nr) + '\n * ' + '\n * '.join(error_lst) raise CrTfConfigError(msg) return False + + for attrib in requested_attribs: + cur_val = getattr(self, attrib, None) + if not cur_val: + msg = _( + "Attribute {a!r} of the {o}-object {n!r} is not set, it " + "will be requestet during this script and on starting terraform.").format( + a=attrib, o=self.__class__.__name__, n=name) + LOG.warn(msg) + return True # ============================================================================= @@ -418,6 +435,22 @@ class CrTfConfiguration(BaseConfiguration): default_pdns_api_path_prefix = DEFAULT_PDNS_API_PREFIX default_pdns_api_timeout = DEFAULT_PDNS_API_PORT default_pdns_comment_account = 'provisioning' + + default_vsphere_defs = { + 'live': { + 'host': 'vcs01.ppbrln.internal', + 'port': 443, + 'dc': 'vmcc', + 'cluster': 'vmcc-l105-01', + }, + 'test': { + 'host': 'test-vcsa01.pixelpark.net', + 'port': 443, + 'dc': 'test-vmcc', + 'cluster': 'test-vmcc-l105-01', + }, + } + default_min_root_size_gb = 32.0 default_tz_name = 'Europe/Berlin' default_guest_id = "oracleLinux7_64Guest" @@ -444,6 +477,13 @@ class CrTfConfiguration(BaseConfiguration): max_pdns_api_timeout = 3600 + re_excl_ds = re.compile(r'^\s*excluded?[-_]datastores?\s*$', re.IGNORECASE) + re_split_ds = re.compile(r'[,;\s]+') + re_template = re.compile(r'^\s*template(?:[-_\.]?name)?\s*$', re.IGNORECASE) + re_min_root_size = re.compile( + r'^\s*min[-_\.]?root[-_\.]?size(?:[-_\.]?gb)\s*$', re.IGNORECASE) + re_guest_id = re.compile(r'^\s*guest[-_]?id\s*$', re.IGNORECASE) + # ------------------------------------------------------------------------- def __init__( self, appname=None, verbose=0, version=__version__, base_dir=None, simulate=False, @@ -652,6 +692,34 @@ class CrTfConfiguration(BaseConfiguration): raise ValueError(msg) self._root_max_size = val + # ------------------------------------------------------------------------- + def init_vsphere_defaults(self): + + for vname in self.default_vsphere_defs.keys(): + + vs_data = self.default_vsphere_defs[vname] + + params = { + 'appname': self.appname, + 'verbose': self.verbose, + 'base_dir': self.base_dir, + 'name': vname, + 'host': vs_data['host'], + 'port': vs_data['port'], + 'dc': vs_data['dc'], + 'cluster': vs_data['cluster'], + } + + if self.verbose > 2: + msg = _("Creating a {}-object with parameters:").format('VsphereConfig') + msg += '\n' + pp(params) + LOG.debug(msg) + vsphere = VsphereConfig(**params) + if self.verbose > 2: + LOG.debug(_("Created object:") + '\n' + pp(vsphere.as_dict())) + + self.vsphere[vname] = vsphere + # ------------------------------------------------------------------------- def as_dict(self, short=True, show_secrets=False): """ @@ -767,12 +835,13 @@ class CrTfConfiguration(BaseConfiguration): LOG.debug(_("Checking config section {s!r} ({n}) ...").format( s=section_name, n=vsphere_name)) - re_excl_ds = re.compile(r'^\s*excluded?[-_]datastores?\s*$', re.IGNORECASE) - re_split_ds = re.compile(r'[,;\s]+') - re_template = re.compile(r'^\s*template(?:[-_\.]?name)?\s*$', re.IGNORECASE) - re_min_root_size = re.compile( - r'^\s*min[-_\.]?root[-_\.]?size(?:[-_\.]?gb)\s*$', re.IGNORECASE) - re_guest_id = re.compile(r'^\s*guest[-_]?id\s*$', re.IGNORECASE) + if vsphere_name in self.vsphere: + self.eval_config_existing_vsphere(config, section_name, vsphere_name) + else: + self.eval_config_new_vsphere(config, section_name, vsphere_name) + + # ------------------------------------------------------------------------- + def eval_config_new_vsphere(self, config, section_name, vsphere_name): params = { 'appname': self.appname, @@ -795,15 +864,15 @@ class CrTfConfiguration(BaseConfiguration): params['dc'] = value.strip() elif key.lower() == 'cluster' and value.strip(): params['cluster'] = value.strip() - elif re_template.search(key) and value.strip(): + elif self.re_template.search(key) and value.strip(): params['template_name'] = value.strip() - elif re_excl_ds.search(key) and value.strip(): + elif self.re_excl_ds.search(key) and value.strip(): datastores = re_split_ds.split(value.strip()) params['excluded_ds'] = datastores - elif re_min_root_size.search(key) and value.strip(): + elif self.re_min_root_size.search(key) and value.strip(): params['min_root_size_gb'] = value - elif re_guest_id.search(key) and value.strip(): - params['guest_id'] = datastores + elif self.re_guest_id.search(key) and value.strip(): + params['guest_id'] = value.strip() else: msg = _( "Unknown configuration parameter {k!r} with value {v!r} for VSPhere {n!r} " @@ -824,6 +893,49 @@ class CrTfConfiguration(BaseConfiguration): return + # ------------------------------------------------------------------------- + def eval_config_existing_vsphere(self, config, section_name, vsphere_name): + + vsphere = self.vsphere[vsphere_name] + + for (key, value) in config.items(section_name): + + if key.lower() == 'host' and value.strip(): + vsphere.host = value.strip() + elif key.lower() == 'port': + vsphere.port = value + elif key.lower() == 'user' and value.strip(): + vsphere.user = value.strip() + elif key.lower() == 'password': + vsphere.password = value + elif key.lower() == 'dc' and value.strip(): + vsphere.dc = value.strip() + elif key.lower() == 'cluster' and value.strip(): + vsphere.cluster = value.strip() + elif self.re_template.search(key) and value.strip(): + vsphere.template_name = value.strip() + elif self.re_excl_ds.search(key) and value.strip(): + datastores = re_split_ds.split(value.strip()) + vsphere.datastores = datastores + elif self.re_min_root_size.search(key) and value.strip(): + vsphere.min_root_size_gb = value.strip() + elif self.re_guest_id.search(key) and value.strip(): + vsphere.guest_id = value.strip() + else: + msg = _( + "Unknown configuration parameter {k!r} with value {v!r} for VSPhere {n!r} " + "found.").format(k=key, v=value, n=vsphere_name) + LOG.warning(msg) + + if self.verbose > 2: + LOG.debug(_("Updated object:") + '\n' + pp(vsphere.as_dict())) + + vsphere.is_valid(raise_on_error=True) + + self.vsphere[vsphere_name] = vsphere + + return + # ------------------------------------------------------------------------- def eval_config_pdns(self, config, section): diff --git a/lib/cr_tf/handler.py b/lib/cr_tf/handler.py index 909eb43..ea51bcc 100644 --- a/lib/cr_tf/handler.py +++ b/lib/cr_tf/handler.py @@ -270,7 +270,7 @@ class CreateTerraformHandler(BaseHandler): if config: self.config = config if self.verbose >= 1: - msg = _("Giveen configurations:") + '\n' + pp(self.config.as_dict()) + msg = _("Given configuration:") + '\n' + pp(self.config.as_dict()) LOG.debug(msg) self.script_dir = self.base_dir.joinpath('postinstall-scripts')