]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Selecting appropriate storage
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 24 Oct 2018 12:54:39 +0000 (14:54 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 24 Oct 2018 12:54:39 +0000 (14:54 +0200)
lib/cr_vmware_tpl/app.py
lib/cr_vmware_tpl/handler.py
python_fb_tools

index 9171d18578e81a00b4fed3c6509bb0fe39852c01..8d6ba5d9e1fdf6d94cf3e35e566d88e8b14c8377 100644 (file)
@@ -123,12 +123,8 @@ class CrTplApplication(BaseApplication):
         self.config = None
 
         super(CrTplApplication, self).__init__(
-            appname=appname,
-            verbose=verbose,
-            version=version,
-            base_dir=base_dir,
-            description=desc,
-            initialized=False,
+            appname=appname, verbose=verbose, version=version, base_dir=base_dir,
+            description=desc, initialized=False,
         )
 
         self.initialized = True
@@ -248,7 +244,9 @@ class CrTplApplication(BaseApplication):
             self.config.password = getpass.getpass(prompt=prompt)
 
         self.handler = CrTplHandler(
-            appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, config=self.config)
+            appname=self.appname, verbose=self.verbose, base_dir=self.base_dir,
+            simulate=self.simulate, force=self.force, config=self.config,
+            terminal_has_colors=self.terminal_has_colors)
 
         if self.args.rotate:
             self.handler.rotate_only = True
index 9996cc84f4aa3543824bed318848a2e6ca5dad40..a3c03380abf379a1b8c0a273d6202ed01cfef2da 100644 (file)
@@ -42,7 +42,7 @@ from fb_tools.vsphere.server import VsphereServer
 
 from .config import CrTplConfiguration
 
-__version__ = '0.10.3'
+__version__ = '0.10.4'
 
 LOG = logging.getLogger(__name__)
 TZ = pytz.timezone('Europe/Berlin')
@@ -76,14 +76,12 @@ class CrTplHandler(BaseHandler):
     # -------------------------------------------------------------------------
     def __init__(
         self, appname=None, verbose=0, version=__version__, base_dir=None,
-            config=None, initialized=False):
+            config=None, terminal_has_colors=False, simulate=None, force=None, initialized=False):
 
         super(CrTplHandler, self).__init__(
-            appname=appname,
-            verbose=verbose,
-            version=version,
-            base_dir=base_dir,
-            initialized=False,
+            appname=appname, verbose=verbose, version=version, base_dir=base_dir,
+            terminal_has_colors=terminal_has_colors, simulate=simulate,
+            force=force, initialized=False,
         )
 
         self.config = config
@@ -159,6 +157,8 @@ class CrTplHandler(BaseHandler):
             return 6
 
         self.vsphere.ensure_vm_folder(self.config.folder)
+        self.check_for_temp_tpl_vm(no_error=True)
+        self.select_data_store()
 
         return 0
 
@@ -184,8 +184,6 @@ class CrTplHandler(BaseHandler):
         try:
 #            self.get_cluster()
 #            self.ensure_vm_folder()
-            self.check_for_temp_tpl_vm()
-            self.select_data_store()
             if self.rotate_only:
                 LOG.warn("Only executing of template rotating.")
             else:
@@ -291,18 +289,6 @@ class CrTplHandler(BaseHandler):
             if pcfilter:
                 pcfilter.Destroy()
 
-    # -------------------------------------------------------------------------
-    def _create_folder(self, host_folder, folder_name, ftype=None):
-
-        if not ftype:
-            ftype = host_folder.__class__.__name__
-
-        LOG.debug("Creating {t}-folder {f!r} in data center {d!r} ...".format(
-            t=ftype, f=folder_name, d=self.config.dc))
-        host_folder.CreateFolder(folder_name)
-        LOG.info("Successfully created the {t}-folder {f!r} in data center {d!r}.".format(
-            t=ftype, f=folder_name, d=self.config.dc))
-
     # -------------------------------------------------------------------------
     def get_tpl_folder(self, vm_folder=None):
 
@@ -323,9 +309,11 @@ class CrTplHandler(BaseHandler):
         return None
 
     # -------------------------------------------------------------------------
-    def check_for_temp_tpl_vm(self):
+    def check_for_temp_tpl_vm(self, no_error=False):
+
+        LOG.debug("First checking, whether {!r} exists ...".format(self.config.template_vm))
+        vm = self.vsphere.get_vm(self.config.template_vm, no_error=no_error)
 
-        vm = self.get_temp_tpl_vm()
         if vm:
             if self.verbose > 1:
                 LOG.debug("Temporary VM {!r} exists, raising TempVmExistsError.".format(
@@ -452,57 +440,44 @@ class CrTplHandler(BaseHandler):
             "Selecting a SAN based datastore with at least {:0.1f} GiB available "
             "space.").format(self.config.data_size_gb))
 
-        content = self.service_instance.RetrieveContent()
-        dc = self.get_obj(content, [vim.Datacenter], self.config.dc)
+        self.vsphere.get_datastores()
 
-        ds_list = []
+        usable_ds = []
+        for ds in self.vsphere.datastores.values():
+            if not ds.accessible:
+                if self.verbose > 1:
+                    LOG.debug("Cannot use datastore {n!r} - not accessible.".format(n=ds.name))
+                continue
+            if ds.name not in self.cluster.datastores:
+                if self.verbose > 1:
+                    LOG.debug("Cannot use datastore {n!r}, not in cluster {c!r}.".format(
+                        n=ds.name, c=self.cluster.name))
+                continue
+            if self.verbose > 3:
+                LOG.debug("Checking datastore:\n{}".format(pp(ds.as_dict())))
+            if ds.storage_type not in ('SAS', 'SSD', 'SATA'):
+                if self.verbose > 1:
+                    LOG.debug("Cannot use datastore {n!r}, is of type {t!r}.".format(
+                        n=ds.name, t=ds.storage_type))
+                continue
+            if ds.free_space_gb <= self.config.data_size_gb:
+                if self.verbose > 1:
+                    LOG.debug((
+                        "Cannot use datastore {n!r}, free space "
+                        "{free:0.1f} GiB is less than {min:0.1f} GiB.").format(
+                        n=ds.name, free=ds.free_space_gb, min=self.config.data_size_gb))
+                continue
 
-        for child in dc.datastoreFolder.childEntity:
-            self._get_data_stores(child, ds_list)
+            usable_ds.append(ds)
 
-        if not len(ds_list):
-            raise NoDatastoreFoundError(self.config.data_size)
+        LOG.debug("Found {} usable datastores.".format(len(usable_ds)))
+        if len(usable_ds) < 1:
+            msg = "Did not found an usable datastore."
+            raise ExpectedHandlerError(msg)
 
-        LOG.debug("Found {} usable datastores.".format(len(ds_list)))
-        self.tpl_data_store = random.choice(ds_list)
+        self.tpl_data_store = random.choice(usable_ds)
         LOG.info("Using datastore {!r} for volume of temporary VM to create.".format(
-            self.tpl_data_store.summary.name))
-
-    # -------------------------------------------------------------------------
-    def _get_data_stores(self, child, ds_list, depth=1):
-
-        if hasattr(child, 'childEntity'):
-            if depth > self.max_depth:
-                return
-            for sub_child in child.childEntity:
-                self._get_data_stores(sub_child, ds_list, depth + 1)
-            return
-
-        if isinstance(child, vim.Datastore):
-            if self.re_local_ds.match(child.summary.name):
-                if self.verbose > 2:
-                    LOG.debug("Datastore {!r} seems to be local.".format(child.summary.name))
-                return
-            if self.re_share_nfs_ds.search(child.summary.name):
-                if self.verbose > 2:
-                    LOG.debug("Datastore {!r} seems to be a NFS share.".format(child.summary.name))
-                return
-            if child.summary.name in self.config.excluded_datastores:
-                LOG.debug("Datastore {!r} is excluded.".format(child.summary.name))
-                return
-            free_bytes = child.summary.freeSpace
-            free_gbytes = float(free_bytes) / 1024.0 / 1024.0 / 1024.0
-            if free_bytes >= self.config.data_size:
-                if self.verbose > 2:
-                    LOG.debug("Found datastore {n!r} with {f:0.1f} GiB free space.".format(
-                        n=child.summary.name, f=free_gbytes))
-                ds_list.append(child)
-                return
-
-            LOG.debug("Datastore {n!r} has too less space ({f:0.1f} GiB available).".format(
-                n=child.summary.name, f=free_gbytes))
-
-        return
+            self.tpl_data_store.name))
 
     # -------------------------------------------------------------------------
     def create_vm(self):
index 001e1212e80247f144ad8291e8aec6588cfdf6fe..b192448f42e85829d04a55044dfd9c244d9106be 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 001e1212e80247f144ad8291e8aec6588cfdf6fe
+Subproject commit b192448f42e85829d04a55044dfd9c244d9106be