]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Start refactoring lib/cr_vmware_tpl/handler.py for using datastore clusters.
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 25 Aug 2020 16:03:33 +0000 (18:03 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 25 Aug 2020 16:03:33 +0000 (18:03 +0200)
lib/cr_vmware_tpl/handler.py

index e99e39150ae934af1739f4dad520e5f14f1e19da..5997cdb2ad8d702a8705ec517ca72341f2204829 100644 (file)
@@ -42,7 +42,7 @@ from .cobbler import Cobbler
 
 from .xlate import XLATOR
 
-__version__ = '1.7.6'
+__version__ = '1.8.0'
 
 LOG = logging.getLogger(__name__)
 TZ = pytz.timezone('Europe/Berlin')
@@ -67,6 +67,31 @@ class TempVmExistsError(ExpectedHandlerError):
         return msg
 
 
+# =============================================================================
+class NoDatastoreFoundError(ExpectedHandlerError):
+    """Special error class for the case, no appropriate datastore coud be found."""
+
+    # -------------------------------------------------------------------------
+    def __init__(self, data_size_gb, ds_cluster_name=None):
+
+        self.data_size_gb = data_size_gb
+        self.ds_cluster_name = ds_cluster_name
+
+    # -------------------------------------------------------------------------
+    def __str__(self):
+
+        if self.ds_cluster_name:
+            msg = _(
+                "Could not find a datastore of {size:0.1f} GiB size in "
+                "datastore cluster {c_name!r}.").format(
+                size=self.data_size_gb, c_name=self.ds_cluster_name)
+        else:
+            msg = _("Could not find a datastore of {:0.1f} GiB size.").format(
+                self.data_size_gb)
+
+        return msg
+
+
 # =============================================================================
 class CrTplHandler(BaseHandler):
     """
@@ -363,8 +388,29 @@ class CrTplHandler(BaseHandler):
             "Selecting a SAN based datastore with at least {:0.1f} GiB available "
             "space.").format(self.config.data_size_gb))
 
+        self.vsphere.get_ds_clusters()
         self.vsphere.get_datastores()
 
+        ds_to_use = None
+        if self.config.storage_cluster:
+            ds_to_use = self.select_data_store_from_cluster()
+        if not ds_to_use:
+            ds_to_use = self.select_simple_data_store()
+
+        if not ds_to_use:
+            c_name = None
+            if self.config.storage_cluster:
+                c_name = self.config.storage_cluster
+            raise NoDatastoreFoundError(self.config.data_size_gb, c_name)
+
+        self.tpl_data_store = ds_to_use
+        LOG.info(_("Using datastore {!r} for volume of temporary VM to create.").format(
+            ds_to_use.name))
+        return
+
+    # -------------------------------------------------------------------------
+    def select_simple_data_store(self):
+
         usable_ds = []
         for ds in self.vsphere.datastores.values():
             if not ds.accessible:
@@ -407,10 +453,9 @@ class CrTplHandler(BaseHandler):
             if not len(ds_list):
                 continue
 
-            self.tpl_data_store = random.choice(ds_list)
-            LOG.info(_("Using datastore {!r} for volume of temporary VM to create.").format(
-                self.tpl_data_store.name))
-            break
+            return random.choice(ds_list)
+
+        return None
 
     # -------------------------------------------------------------------------
     def create_vm(self):