From e6730ae0a05d8d988b1c423715265386b6b019a4 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 1 Oct 2019 15:48:11 +0200 Subject: [PATCH] Changing some methods for multi VSPhere support --- lib/cr_tf/config.py | 5 ++-- lib/cr_tf/handler.py | 69 ++++++++++++++++++++++++++++++-------------- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/lib/cr_tf/config.py b/lib/cr_tf/config.py index 95dc806..ce8b092 100644 --- a/lib/cr_tf/config.py +++ b/lib/cr_tf/config.py @@ -34,7 +34,7 @@ from .errors import CrTfConfigError from .xlate import XLATOR -__version__ = '1.3.3' +__version__ = '1.3.4' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -72,6 +72,7 @@ class VsphereConfig(FbBaseObject): self._min_root_size_gb = self.default_min_root_size_gb self._guest_id = self.default_guest_id self.excluded_ds = [] + self.used_templates = [] super(VsphereConfig, self).__init__( appname=appname, verbose=verbose, version=version, @@ -250,7 +251,7 @@ class VsphereConfig(FbBaseObject): if value is None: self._template_name = self.default_template_name return - val = str(value).strip() + val = str(value).strip().lower() if val == '': self._template_name = self.default_template_name else: diff --git a/lib/cr_tf/handler.py b/lib/cr_tf/handler.py index 4fe5fbe..6c4e5ae 100644 --- a/lib/cr_tf/handler.py +++ b/lib/cr_tf/handler.py @@ -59,7 +59,7 @@ from .terraform.disk import TerraformDisk from .xlate import XLATOR -__version__ = '2.9.4' +__version__ = '2.9.5' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -692,6 +692,7 @@ class CreateTerraformHandler(BaseHandler): self.incr_verbosity() for vname in self.vsphere: + LOG.debug(_("Searching for clusters in VSPhere {!r} ...").format(vname)) self.vsphere[vname].get_clusters() LOG.info(_("Finished step {!r}.").format('vmw-clusters')) @@ -705,6 +706,7 @@ class CreateTerraformHandler(BaseHandler): self.incr_verbosity() for vname in self.vsphere: + LOG.debug(_("Searching for datastores in VSPhere {!r} ...").format(vname)) self.vsphere[vname].get_datastores() LOG.info(_("Finished step {!r}.").format('vmw-datastores')) @@ -718,6 +720,7 @@ class CreateTerraformHandler(BaseHandler): self.incr_verbosity() for vname in self.vsphere: + LOG.debug(_("Searching for datastore clusters in VSPhere {!r} ...").format(vname)) self.vsphere[vname].get_ds_clusters() LOG.info(_("Finished step {!r}.").format('vmw-ds-clusters')) @@ -731,6 +734,7 @@ class CreateTerraformHandler(BaseHandler): self.incr_verbosity() for vname in self.vsphere: + LOG.debug(_("Searching for networks in VSPhere {!r} ...").format(vname)) self.vsphere[vname].get_networks() if self.eval_errors: msg = ngettext( @@ -1091,33 +1095,54 @@ class CreateTerraformHandler(BaseHandler): LOG.info(_("Exploring all vSphere templates ...")) - template_names = [] - if self.config.template_name: - template_names.append(self.config.template_name) + for vname in self.vsphere: - for vm in self.vms: - template_name = vm.vm_template - if template_name: - if template_name not in template_names: - template_names.append(template_name) - else: - LOG.error(_("VM {!r} has not template defined.").format(vm.name)) - self.eval_errors += 1 + if vname not in self.vsphere_templates: + self.vsphere_templates[vname] = {} - LOG.debug(_("All vSphere templates to explore:") + "\n" + pp(template_names)) + self.config.vsphere[vname].used_templates = [] - for template_name in template_names: + for vm in self.vms: + template_name = vm.vm_template + if template_name: + if template_name not in self.config.vsphere[vname].used_templates: + self.config.vsphere[vname].used_templates.append(template_name) + else: + LOG.error(_("VM {!r} has not template defined.").format(vm.name)) + self.eval_errors += 1 - vm_info = self.vsphere.get_vm(template_name) - if vm_info: - tname = template_name.lower() - if tname not in self.vsphere_templates: - self.vsphere_templates[tname] = vm_info - else: - self.eval_errors += 1 + msg = _("All {} VSPhere templates to explore:").format(vname) + msg += "\n" + pp(self.config.vsphere[vname].used_templates) + LOG.debug(msg) + + for template_name in self.config.vsphere[vname].used_templates: + + if template_name in self.vsphere_templates[vname]: + continue + + LOG.debug(_("Searching for template {t!r} in VSPhere {v!r} ...").format( + t=template_name, v=vname)) + re_vm = re.compile(r'^' + re.escape(template_name) + r'$', re.IGNORECASE) + vm_list = self.vsphere[vname].get_vms(re_vm, as_obj=True, stop_at_found=True) + if vm_list: + vm = vm_list[0] + tname = vm.name.lower() + if tname not in self.vsphere_templates[vname]: + self.vsphere_templates[vname][template_name] = vm + else: + LOG.error(_("Template {t!r} not found in VSPhere {v!r}.").format( + t=template_name, v=vname)) + self.eval_errors += 1 if self.verbose > 2: - LOG.debug(_("All explored vSphere templates:") + "\n" + pp(self.vsphere_templates)) + msg = _("All explored vSphere templates:") + out_dict = {} + for vname in self.vsphere_templates: + out_dict[vname] = {} + for tname in self.vsphere_templates[vname]: + out_dict[vname][tname] = self.vsphere_templates[vname][tname].as_dict() + msg += "\n" + pp(out_dict) + LOG.debug(msg) # -------------------------------------------------------------------------· def validate_clusters(self): -- 2.39.5