]> Frank Brehm's Git Trees - pixelpark/create-vmware-tpl.git/commitdiff
Fixing and using files/postinstall
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 17 Nov 2023 11:26:35 +0000 (12:26 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 17 Nov 2023 11:26:35 +0000 (12:26 +0100)
files/postinstall
lib/cr_vmware_tpl/handler/vm.py

index ef3ec2ecd8a973f903e897bfacd39d7614b4cb02..37574678b2c32ae1afc7abf22515dcadffc5ce65 100644 (file)
@@ -1,5 +1,17 @@
 #!/bin/bash
 
+##########################################################
+# Postinstallation script
+#
+# This script i called after the successful deployment of the template VM
+# via kickstart.
+#
+# The target of this scrit is to prepare the VM to migrate it to a real
+# VMWare template after shuting it dow.
+# This script should truncate all logs, and journal entries, remove
+# all runtime data and to prepare network configuration.
+#
+
 printf "Current host FQDN: "
 hostname -f
 
@@ -37,21 +49,21 @@ if [ -x /sbin/subscription-manager ] ; then
 fi
 
 echo
-for f in '/var/log/audit/audit.log'
-         '/var/log/boot.log'
-         '/var/log/cloud-init.log'
-         '/var/log/cloud-init-output.log'
-         '/var/log/cron'
-         '/var/log/dnf*.log'
-         '/var/log/grubby*'
-         '/var/log/hawkey.log'
-         '/var/log/messages'
-         '/var/log/secure'
-         '/var/log/tuned/tuned.log'
-         '/var/log/wtmp'
-         '/var/log/vmware-*.log*'
-         '/var/log/yum*.log'
-         '/var/log/rhsm/*.log'
+for f in /var/log/audit/audit.log \
+         /var/log/boot.log \
+         /var/log/cloud-init.log \
+         /var/log/cloud-init-output.log \
+         /var/log/cron \
+         /var/log/dnf*.log \
+         /var/log/grubby* \
+         /var/log/hawkey.log \
+         /var/log/messages \
+         /var/log/secure \
+         /var/log/tuned/tuned.log \
+         /var/log/wtmp \
+         /var/log/vmware-*.log* \
+         /var/log/yum*.log \
+         /var/log/rhsm/*.log \
          ; do
     if [ -f "${f}" ] ; then
         echo "Truncating ${f} ..."
index 48570e462bb6d28843373b24e4e1a8ad88be9f39..465509b74567472c330f561a63f3f869103e8f31 100644 (file)
@@ -15,6 +15,8 @@ import socket
 import textwrap
 import time
 
+from pathlib import Path
+
 # Third party modules
 import paramiko
 
@@ -285,143 +287,76 @@ class HandlerVmMixin():
         LOG.info(_("Executing tasks per SSH after installation ..."))
         print_section_start('post_install_tasks', 'Exec post install tasks ...', collapsed=True)
 
-        logfiles = (
-            '/var/log/audit/audit.log',
-            '/var/log/boot.log',
-            '/var/log/cloud-init.log',
-            '/var/log/cloud-init-output.log',
-            '/var/log/cron',
-            '/var/log/dnf*.log',
-            '/var/log/grubby*',
-            '/var/log/hawkey.log',
-            '/var/log/messages',
-            '/var/log/secure',
-            '/var/log/tuned/tuned.log'
-            '/var/log/wtmp',
-            '/var/log/vmware-*.log*',
-            '/var/log/yum*.log',
-            '/var/log/rhsm/*.log'
-        )
+        ssh = None
 
-        cmd = textwrap.dedent("""\
-        printf "Current host FQDN: "
-        hostname -f
+        local_file = self.base_dir / 'files' / 'postinstall'
+        remote_file = Path('/tmp') / 'postinstall'
 
-        echo
-        echo "All installed packages:"
-        echo "-----------------------"
-        rpm -qa | sort
+        try:
 
-        echo
-        echo "All filesystems:"
-        echo "----------------"
-        df -m -a -T
+            if self.verbose > 2:
+                LOG.debug(_("Initializing {} ...").format('paramiko SSHClient'))
+            ssh = paramiko.SSHClient()
+            if self.verbose > 2:
+                LOG.debug(_("Loading SSH system host keys."))
+            ssh.load_system_host_keys()
+            if self.verbose > 2:
+                LOG.debug(_("Setting SSH missing host key policy to {}.").format('AutoAddPolicy'))
+            ssh.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
 
-        for ks_cfg in "/root/original-ks.cfg" "/root/anaconda-ks.cfg" ; do
-            echo
-            echo "-----------------------------------------------------------"
-            if [ -f "${ks_cfg}" ] ; then
-                echo "Moving ${ks_cfg} => /var/log/anaconda/ ..."
-                mv -v "${ks_cfg}" /var/log/anaconda/
-            else
-                echo "File ${ks_cfg} not found." >&2
-            fi
-        done
+            if self.verbose > 1:
+                LOG.debug(_("Connecting to {h!r}, port {p} as {u!r} per SSH ...").format(
+                    h=self.tpl_ip, p=self.ssh_port, u=self.ssh_user))
 
-        echo
-        echo "Removing /var/log/anaconda ..."
-        echo "------------------------------"
-        rm -rfv /var/log/anaconda
+            if self.simulate:
+                LOG.debug(_(
+                    "Simulating SCP of {local!r} to {user}@{host}:{remote} ...").format(
+                    local=str(local_file), user=self.ssh_user,
+                    host=self.tpl_ip, remote=str(remote_file)))
 
-        if [ -x /sbin/subscription-manager ] ; then
-            echo
-            echo "Unregistring current host from Red Hat Subscription Management ..."
-            /sbin/subscription-manager unregister --no-progress-messages
-            sleep 2
-        fi
+            else:
+                ssh.connect(
+                    self.tpl_ip, port=self.ssh_port, timeout=self.ssh_timeout,
+                    username=self.ssh_user, key_filename=self.private_ssh_key)
 
-        echo
-        for f in @@@LOGFILES@@@ ; do
-            if [ -f "${f}" ] ; then
-                echo "Truncating ${f} ..."
-                cp /dev/null "${f}"
-            fi
-        done
+                sftp = ssh.open_sftp()
 
-        echo
-        echo "-----------------------------------------------------------"
-        echo "Clearing journal log ..."
-        journalctl --flush
-        journalctl -m --vacuum-time=1s
+                LOG.debug(_("SCP of {local!r} to {user}@{host}:{remote} ...").format(
+                    local=str(local_file), user=self.ssh_user,
+                    host=self.tpl_ip, remote=str(remote_file)))
 
-        echo
-        echo "-----------------------------------------------------------"
-        echo "Truncating /var/log/*tmp ..."
-        cp -v /dev/null /var/log/btmp
-        cp -v /dev/null /var/log/wtmp
+                sftp.put(str(local_file), str(remote_file))
 
-        echo
-        echo "-----------------------------------------------------------"
-        echo "Remaining files in /var/log:"
-        find /var/log -type f | xargs ls -l -S
+        except SSHException as e:
+            msg = _("Could not connect via {w} to {user}@{host}: {e}").format(
+                w='SCP', user=self.ssh_user, host=self.tpl_ip, e=e)
+            raise ExpectedCobblerError(msg)
 
-        echo
-        echo "-----------------------------------------------------------"
-        echo "Current network configuration:"
-        echo
-        /usr/sbin/ip address show
-        echo
-        echo "Current routing configuration:"
-        echo
-        /usr/sbin/ip route show
-        echo
-        echo "NetworkManager connections:
-        echo
-        /bin/nmcli connection show
+        finally:
+            sftp = None
+            if ssh:
+                if self.verbose > 2:
+                    LOG.debug(_("Closing SSH connection."))
+                ssh.close()
+
+        cmd = textwrap.dedent("""\
+
+        PI_SCRIPT='/tmp/postinstall'
+
+        echo "Checking for '${PI_SCRIPT}' ..."
+        if [[ ! -f "${PI_SCRIPT}" ]] ; then
+            echo "File '${PI_SCRIPT}' not found!!!"
+        else
+
+            echo "Executing '${PI_SCRIPT}' ..."
+            bash "${PI_SCRIPT}"
 
-        echo
-        echo "-----------------------------------------------------------"
-        echo "Networking config files:"
-        echo
-        echo "Searching for file like /etc/sysconfig/network-scripts/ifcfg-* ..."
-        for f in /etc/sysconfig/network-scripts/ifcfg-* ; do
-            if [ ! -f "${f}" ] ; then
-                continue
-            fi
-            base_name=$( basename "${f}" )
-            if [ "${base_name}" = 'ifcfg-lo' ] ; then
-                continue
-            fi
-            echo "${f}:"
-            ls -l "${f}"
-            echo
-            echo "Content:"
-            echo "---------- snip ----------"
-            cat "${f}"
-            echo "---------- snip ----------"
-            echo "Removing ${f} ..."
-            rm -vf "${f}"
-        done
-        echo
-        echo "DHCP leases:"
-        echo
-        echo "Searching for file like /var/lib/dhclient/*.leases ..."
-        for f in /var/lib/dhclient/*.leases ; do
-            if [ ! -f "${f}" ] ; then
-                continue
-            fi
-            echo "${f}:"
-            ls -l "${f}"
             echo
-            echo "Content:"
-            echo "---------- snip ----------"
-            cat "${f}"
-            echo "---------- snip ----------"
-            echo "Removing ${f} ..."
-            rm -vf "${f}"
-        done
-
-        """).replace('@@@LOGFILES@@@', ' '.join(logfiles))
+            echo "Removing '${PI_SCRIPT}' ..."
+            rm --verbose "${PI_SCRIPT}"
+
+        fi
+        """)
 
         result = self.exec_remote(cmd)
         LOG.debug(_("Output on {}:").format('STDOUT') + '\n' + result['out'])