]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Checking existence of temporary VM
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 22 Mar 2018 12:11:28 +0000 (13:11 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 22 Mar 2018 12:11:28 +0000 (13:11 +0100)
lib/cr_vmware_tpl/app.py
lib/cr_vmware_tpl/handler.py

index 1edc615f73d3b11924487a5f6e6fb5ad068d1065..6ff4b8b402ec0e09e6bbfec23de05454273eb558 100644 (file)
@@ -32,7 +32,7 @@ from .obj import PpBaseObject
 
 from .config import CrTplConfiguration
 
-from .handler import CrTplHandler
+from .handler import TempVmExistsError, CrTplHandler
 
 __version__ = '0.3.1'
 LOG = logging.getLogger(__name__)
@@ -429,7 +429,12 @@ class CrTplApplication(PpBaseObject):
         """
 
         LOG.info("Starting ...")
-        self.handler()
+
+        try:
+            self.handler()
+        except TempVmExistsError as e:
+            self.handle_error(str(e), "Temporary VM")
+            self.exit(5)
 
     # -------------------------------------------------------------------------
     def __call__(self):
index 9c6ba504d4770338d29547ca13fc47e45a7a4a08..60eb9ac6cbe1c4c0c6935515105cfffafdf0ad97 100644 (file)
@@ -28,7 +28,7 @@ from .obj import PpBaseObject
 
 from .config import CrTplConfiguration
 
-__version__ = '0.2.2'
+__version__ = '0.3.1'
 LOG = logging.getLogger(__name__)
 
 
@@ -40,6 +40,22 @@ class HandlerError(PpError, RuntimeError):
     pass
 
 
+# =============================================================================
+class TempVmExistsError(HandlerError):
+    """Special error class for the case, if the temporary VM is already existing."""
+
+    # -------------------------------------------------------------------------
+    def __init__(self, vm_name):
+
+        self.vm_name = vm_name
+
+    # -------------------------------------------------------------------------
+    def __str__(self):
+
+        msg = "The temporary VM {!r} is already existing, cannot continue.".format(self.vm_name)
+        return msg
+
+
 # =============================================================================
 class CannotConnectError(HandlerError):
     """Special error class for the case, it cannot connect
@@ -120,7 +136,7 @@ class CrTplHandler(PpBaseObject):
 
         try:
             self.ensure_vm_folder()
-            #self.list_vms()
+            self.check_for_temp_tpl_vm()
         finally:
             LOG.debug("Disconnecting from vSphere host {h}:{p} ...".format(
                 h=self.config.vsphere_host, p=self.config.vsphere_port))
@@ -181,12 +197,6 @@ class CrTplHandler(PpBaseObject):
             self.tpl_vm_folder = tpl_vm_folder
             return True
 
-#        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()
@@ -194,34 +204,48 @@ class CrTplHandler(PpBaseObject):
         return True
 
     # -------------------------------------------------------------------------
-    def list_vms(self):
+    def check_for_temp_tpl_vm(self):
+
+        vm = self.get_temp_tpl_vm()
+        if vm:
+            if self.verbose > 1:
+                LOG.debug("Temporary VM {!r} exists, raising TempVmExistsError.".format(
+                    self.config.template_vm))
+            raise TempVmExistsError(self.config.template_vm)
+
+        LOG.debug("Temporary VM {!r} does not exists, will be created.".format(
+            self.config.template_vm))
+
+    # -------------------------------------------------------------------------
+    def get_temp_tpl_vm(self):
 
         content = self.server_instance.RetrieveContent()
+        dc = self.get_obj(content, [vim.Datacenter], self.config.dc)
 
-        for child in content.rootFolder.childEntity:
-            if hasattr(child, 'vmFolder'):
-                datacenter = child
-                vmfolder = datacenter.vmFolder
-                for vm in vmfolder.childEntity:
-                    self._printvminfo(vm)
+        for child in dc.vmFolder.childEntity:
+            vm = self._get_temp_tpl_vm(child)
+            if vm:
+                return vm
 
     # -------------------------------------------------------------------------
-    def _printvminfo(self, vm, depth=1):
+    def _get_temp_tpl_vm(self, child, depth=1):
 
-        if hasattr(vm, 'childEntity'):
+        if hasattr(child, '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
+                return None
+            for sub_child in child.childEntity:
+                vm = self._get_temp_tpl_vm(sub_child, depth + 1)
+                if vm:
+                    return vm
+            return None
+
+        if hasattr(child, 'summary'):
+            summary = child.summary
+            if summary.config.name == self.config.template_vm:
+                return child
+
+        return None
+
 
 # =============================================================================