]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Changing some methods for multi VSPhere support
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 1 Oct 2019 13:48:11 +0000 (15:48 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 1 Oct 2019 13:48:11 +0000 (15:48 +0200)
lib/cr_tf/config.py
lib/cr_tf/handler.py

index 95dc80603bb653dc21f01da639c35e284b1e0373..ce8b092b52712b4e86c1954de5dc6a4c3c041d3d 100644 (file)
@@ -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:
index 4fe5fbe44254cbb5a8e97feccd72f70197599dab..6c4e5aeb8b282f162107441591b57a809fd0efc3 100644 (file)
@@ -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):