]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Powering VM off after installation
authorFrank Brehm <frank.brehm@pixelpark.com>
Thu, 12 Apr 2018 13:21:04 +0000 (15:21 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Thu, 12 Apr 2018 13:21:04 +0000 (15:21 +0200)
lib/cr_vmware_tpl/handler.py

index 424d9ee4b7e37cf310a581ae5de6180544055901..249d838ec3720818f127a54b59202b8e639fda1d 100644 (file)
@@ -38,7 +38,7 @@ from .obj import PpBaseObject
 
 from .config import CrTplConfiguration
 
-__version__ = '0.7.1'
+__version__ = '0.7.2'
 LOG = logging.getLogger(__name__)
 TZ = pytz.timezone('Europe/Berlin')
 
@@ -170,6 +170,7 @@ class CrTplHandler(PpBaseObject):
         self.ssh_port = 22
         self.ssh_user = 'root'
         self.ssh_timeout = 30
+        self.max_wait_for_shutdown = 600
 
         if initialized:
             self.initialized = True
@@ -218,6 +219,7 @@ class CrTplHandler(PpBaseObject):
             self.poweron_vm()
             self.wait_for_finish_install()
             self.post_install_tasks_ssh()
+            self.poweroff_vm()
         finally:
             LOG.debug("Disconnecting from vSphere host {h}:{p} ...".format(
                 h=self.config.vsphere_host, p=self.config.vsphere_port))
@@ -747,17 +749,44 @@ class CrTplHandler(PpBaseObject):
         printf "Current host FQDN: "
         hostname -f
 
+        if [ -f /root/original-ks.cfg ] ; then
+            echo
+            echo "Kickstart configuration:"
+            echo
+            echo "-----------------------------------------------------------"
+            cat /root/original-ks.cfg
+            echo "-----------------------------------------------------------"
+            echo
+            echo "Removing /root/original-ks.cfg ..."
+            rm -v /root/original-ks.cfg
+        fi
+
+        if [ -v /root/anaconda-ks.cfg ] ; then
+            echo
+            echo "Removing /root/anaconda-ks.cfg ..."
+            rm -v /root/anaconda-ks.cfg
+        fi
+
         if [ -f /var/log/post-install.log ] ; then
             echo
             echo "Post install log:"
             echo
+            echo "-----------------------------------------------------------"
             cat /var/log/post-install.log
+            echo "-----------------------------------------------------------"
             echo
+            echo "Removing /var/log/post-install.log ..."
             rm -v /var/log/post-install.log
         fi
 
-        # sleep 2
-        # poweroff && logout
+        echo
+        echo "Cleaning up /root/.ssh/authorized_keys ..."
+        cat /root/.ssh/authorized_keys | grep -v create-vmware-tpl@pixelpark.com > /tmp/authorized_keys
+        mv -v /tmp/authorized_keys /root/.ssh/authorized_keys
+
+        echo "Removing SSH host keys ..."
+        rm -v /etc/ssh/ssh_host_*
+
         """)
 
         try:
@@ -791,6 +820,47 @@ class CrTplHandler(PpBaseObject):
                     LOG.debug("Closing SSH connection.")
                 ssh.close()
 
+    # -------------------------------------------------------------------------
+    def poweroff_vm(self):
+
+        LOG.info("Powering off VM {!r} ...".format(self.config.template_vm))
+
+        vm = self.get_temp_tpl_vm()
+        guest_state = vm.guest.guestState
+        LOG.debug("Current state of template VM is {!r}".format(guest_state))
+        if guest_state.strip().lower() == "notrunning":
+            LOG.info("Template VM is already shut off.")
+            return
+
+        if guest_state.strip().lower() != "running":
+            raise ExpectedHandlerError(
+                "Cannot shut down VM {h!r}, is currently in state {s!r}.".format(
+                    h=self.config.template_vm, s=guest_state))
+
+        LOG.debug("Shutting down VM {!r} ...".format(self.config.template_vm))
+        start_shutdown = time.time()
+        cur_diff = 0
+        vm.ShutdownGuest()
+
+        LOG.debug("Waiting for successful shut down of VM ...")
+        while guest_state.strip().lower() != "notrunning":
+            time.sleep(0.2)
+            cur_time = time.time()
+            cur_diff = cur_time - start_shutdown
+            vm = self.get_temp_tpl_vm()
+            guest_state = vm.guest.guestState
+            if guest_state.strip().lower() == "notrunning":
+                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
+
+        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))
+
+
 # =============================================================================
 
 if __name__ == "__main__":