import textwrap
import time
+from pathlib import Path
+
# Third party modules
import paramiko
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'])