]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Implementing renaming VM and changing it into a template.
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 12 Apr 2018 13:53:55 +0000 (15:53 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 12 Apr 2018 13:53:55 +0000 (15:53 +0200)
lib/cr_vmware_tpl/handler.py

index 249d838ec3720818f127a54b59202b8e639fda1d..650f1ba03e7cf425a604e4225f680542cf3c3065 100644 (file)
@@ -38,7 +38,7 @@ from .obj import PpBaseObject
 
 from .config import CrTplConfiguration
 
-__version__ = '0.7.2'
+__version__ = '0.7.3'
 LOG = logging.getLogger(__name__)
 TZ = pytz.timezone('Europe/Berlin')
 
@@ -220,6 +220,7 @@ class CrTplHandler(PpBaseObject):
             self.wait_for_finish_install()
             self.post_install_tasks_ssh()
             self.poweroff_vm()
+            self.rename_and_change_vm()
         finally:
             LOG.debug("Disconnecting from vSphere host {h}:{p} ...".format(
                 h=self.config.vsphere_host, p=self.config.vsphere_port))
@@ -841,25 +842,61 @@ class CrTplHandler(PpBaseObject):
         start_shutdown = time.time()
         cur_diff = 0
         vm.ShutdownGuest()
+        cur_time = start_shutdown
+        last_dot = cur_time
+        i = 0
 
         LOG.debug("Waiting for successful shut down of VM ...")
+        if self.verbose <= 2:
+            print('   ==> ', end='', flush=True)
+
         while guest_state.strip().lower() != "notrunning":
+
             time.sleep(0.2)
+
             cur_time = time.time()
             cur_diff = cur_time - start_shutdown
+            if (self.verbose <= 2) and ((cur_time - last_dot) >= self.interval_dot):
+                print('.', end='', flush=True)
+                i += 1
+                if i >= 60:
+                    print('', flush=True)
+                    print('       ', end='', flush=True)
+                    i = 0
+                last_dot = cur_time
+
             vm = self.get_temp_tpl_vm()
             guest_state = vm.guest.guestState
+            if self.verbose > 2:
+                LOG.debug("Still waiting for completing shutdown, current state is {!r}.".format(
+                    guest_state))
             if guest_state.strip().lower() == "notrunning":
+                print('', flush=True)
                 LOG.info("Template VM {h!r} was shutting down in {t:0.1f} seconds.".format(
                     h=self.config.template_vm, t=cur_diff))
                 return
             if cur_diff >= self.max_wait_for_shutdown:
                 break
 
+        print('', flush=True)
         raise ExpectedHandlerError(
             "VM {h!r} was not shut down after {t:0.1f} seconds, current state is {s!r}.".format(
                 h=self.config.template_vm, t=cur_diff, s=guest_state))
 
+    # -------------------------------------------------------------------------
+    def rename_and_change_vm(self):
+
+        LOG.info("Renaming VM {o!r} => {n!r} ...".format(
+            o=self.config.template_vm, n=self.config.template_name)
+
+        vm = self.get_temp_tpl_vm()
+        task = vm.Rename_Task(self.config.template_name)
+        self.wait_for_tasks([task])
+        LOG.debug("Successful renamed VM into {!r}.".format(self.config.template_name))
+
+        LOG.info("Changing VM {!r} into a VMWare template ...".format(self.config.template_name))
+        vm.MarkAsTemplate()
+        LOG.debug("Object {!r} is now a VMWare template.".format(self.config.template_name))
 
 # =============================================================================