]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Reorganized ensuring of VM folder for templates
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 22 Mar 2018 11:25:35 +0000 (12:25 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 22 Mar 2018 11:25:35 +0000 (12:25 +0100)
lib/cr_vmware_tpl/handler.py

index bb36a3f5b100ca773d95db5c215e7e67fc9c40be..9c6ba504d4770338d29547ca13fc47e45a7a4a08 100644 (file)
@@ -28,7 +28,7 @@ from .obj import PpBaseObject
 
 from .config import CrTplConfiguration
 
-__version__ = '0.2.1'
+__version__ = '0.2.2'
 LOG = logging.getLogger(__name__)
 
 
@@ -66,6 +66,8 @@ class CrTplHandler(PpBaseObject):
     A handler class for creating a vSphere template.
     """
 
+    max_depth = 10
+
     # -------------------------------------------------------------------------
     def __init__(
         self, appname=None, verbose=0, version=__version__, base_dir=None,
@@ -81,6 +83,7 @@ class CrTplHandler(PpBaseObject):
 
         self.config = config
         self.server_instance = None
+        self.tpl_vm_folder = None
 
         if initialized:
             self.initialized = True
@@ -116,8 +119,11 @@ class CrTplHandler(PpBaseObject):
                 user=self.config.vsphere_user)
 
         try:
-            self.create_folder()
+            self.ensure_vm_folder()
+            #self.list_vms()
         finally:
+            LOG.debug("Disconnecting from vSphere host {h}:{p} ...".format(
+                h=self.config.vsphere_host, p=self.config.vsphere_port))
             Disconnect(self.server_instance)
 
     # -------------------------------------------------------------------------
@@ -146,21 +152,77 @@ class CrTplHandler(PpBaseObject):
             t=ftype, f=folder_name, d=self.config.dc))
 
     # -------------------------------------------------------------------------
-    def create_folder(self):
+    def get_tpl_folder(self, vm_folder=None):
+
+        if not vm_folder:
+            content = self.server_instance.RetrieveContent()
+            dc = self.get_obj(content, [vim.Datacenter], self.config.dc)
+            vm_folder = dc.vmFolder
+
+        for child in vm_folder.childEntity:
+            if not isinstance(child, vim.Folder):
+                continue
+            if self.verbose > 3:
+                LOG.debug("Found vim.Folder {!r} in root VM folder.".format(child.name))
+            if child.name == self.config.folder:
+                LOG.info("VM-Folder {!r} already exists.".format(self.config.folder))
+                return child
+
+        return None
+
+    # -------------------------------------------------------------------------
+    def ensure_vm_folder(self):
 
         content = self.server_instance.RetrieveContent()
         dc = self.get_obj(content, [vim.Datacenter], self.config.dc)
 
-        if self.get_obj(content, [vim.Folder], self.config.folder):
-            LOG.info("vSphere folder {f!r} in data center {d!r} already exists.".format(
-                f=self.config.folder, d=self.config.dc))
+        tpl_vm_folder = self.get_tpl_folder(dc.vmFolder)
+        if tpl_vm_folder:
+            self.tpl_vm_folder = tpl_vm_folder
             return True
 
-        self._create_folder(dc.hostFolder, self.config.folder, 'host')
+#        if self.get_obj(content, [vim.Folder], self.config.folder):
+#            LOG.info("vSphere folder {f!r} in data center {d!r} already exists.".format(
+#                f=self.config.folder, d=self.config.dc))
+#            return True
+
+        #self._create_folder(dc.hostFolder, self.config.folder, 'host')
         self._create_folder(dc.vmFolder, self.config.folder, 'VM')
 
+        self.tpl_vm_folder = self.get_tpl_folder()
+
         return True
 
+    # -------------------------------------------------------------------------
+    def list_vms(self):
+
+        content = self.server_instance.RetrieveContent()
+
+        for child in content.rootFolder.childEntity:
+            if hasattr(child, 'vmFolder'):
+                datacenter = child
+                vmfolder = datacenter.vmFolder
+                for vm in vmfolder.childEntity:
+                    self._printvminfo(vm)
+
+    # -------------------------------------------------------------------------
+    def _printvminfo(self, vm, depth=1):
+
+        if hasattr(vm, 'childEntity'):
+            if depth > self.max_depth:
+                return
+            for child in vm.childEntity:
+                self._printvminfo(child, depth + 1)
+            return
+
+        if hasattr(vm, 'summary'):
+            summary = vm.summary
+            vm_type = 'Virtual Machine'
+            if summary.config.template:
+                vm_type = 'Template'
+            LOG.info("Found {t} {n!r}.".format(t=vm_type, n=summary.config.name))
+        return
+
 # =============================================================================
 
 if __name__ == "__main__":