From d99851a9753c2c7011e30bb04aa84bd569566299 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 24 Oct 2023 17:17:04 +0200 Subject: [PATCH] Finishing generating terraform files. --- lib/cr_tf/handler/files.py | 75 +++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 25 deletions(-) diff --git a/lib/cr_tf/handler/files.py b/lib/cr_tf/handler/files.py index fb11b71..8d58328 100644 --- a/lib/cr_tf/handler/files.py +++ b/lib/cr_tf/handler/files.py @@ -27,7 +27,7 @@ from ..errors import AbortExecution from ..xlate import XLATOR -__version__ = '0.2.0' +__version__ = '0.3.0' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -734,7 +734,9 @@ class CrTfHandlerFilesMixin(): # ## Copying postinstall scripts to VM - files = ['conf-resolver', 'create-motd'] + files = ['functions.rc', 'conf-resolver', 'create-motd'] + if vm.is_rhel: + files.append('register-rhel') if vm.has_puppet: files.append('init-puppet') files.append('update-all-packages') @@ -759,6 +761,24 @@ class CrTfHandlerFilesMixin(): '''), ' ').format( d=self.script_dir_rel, f=sname, h=vm.fqdn) + if vm.is_rhel: + if self.verbose > 1: + LOG.debug(_("Generating file provisioner for {f!r} of {v!r}.").format( + f='rhsm-user-passwd', v=vm.name)) + + content += textwrap.indent(textwrap.dedent('''\ + provisioner "file" {{ + destination = "/tmp/rhsm-user-passwd" + content = "${{var.rhsm_user_password}}" + connection {{ + type = "ssh" + user = "root" + host = "{h}" + }} + }} + + '''), ' ').format(h=vm.fqdn) + # ## Postinstall commands on host commands = [] @@ -775,7 +795,7 @@ class CrTfHandlerFilesMixin(): else: cmd += ' --options {!r}'.format('') commands.append(cmd) - commands.append("rm -f /tmp/conf-resolver") + commands.append("rm -fv /tmp/conf-resolver") purpose = self.re_doublequote.sub('\\\"', vm.purpose) @@ -784,7 +804,7 @@ class CrTfHandlerFilesMixin(): commands.append("chmod +x /tmp/create-motd") cmd = ( "/tmp/create-motd --purpose '{p}' --hardware 'vmware (x86_64)' --owner '{o}' " - "--location 'VMWare' --zone '{z}' --customer '{c}' --email '{m}' --tier '{t}' " + "--location 'VMWare' --zone '{z}' --customer '{c}' --email '{m}' --tier '{t}' " "--environment '{e}' --role '{r}'").format( p=purpose, t=vm.puppet_tier, o=vm.customer, z=zone, c=vm.puppet_customer, m=vm.puppet_contact, e=vm.puppet_env, r=vm.puppet_role) @@ -792,24 +812,13 @@ class CrTfHandlerFilesMixin(): cmd += " --project '{pr}'".format(pr=vm.puppet_project) cmd += " | tee /etc/motd" commands.append(cmd) - commands.append("rm -f /tmp/create-motd") - - # ## Backup - Legato networker - commands.append("systemctl stop networker.service") - commands.append("rm -rfv /nsr/tmp /nsr/res") - if vm.has_backup: - commands.append("mkdir -pv /nsr/res") - commands.append( - "if [ ! -f /nsr/res/servers ] ; then " - "echo 'legato01.pixelpark.com' > /nsr/res/servers; fi") - commands.append("systemctl start networker.service; sleep 2") - commands.append("nsrports -S 7937-7999; sleep 2") - commands.append("systemctl stop networker.service; sleep 2") - commands.append( - "systemctl enable networker.service; systemctl start networker.service; sleep 2") - commands.append("nsrports; sleep 2") - else: - commands.append("systemctl disable networker.service") + commands.append("rm -fv /tmp/create-motd") + + # ## Registring RHEL on RedHat Subscription Management + if vm.is_rhel: + commands.append("chmod +x /tmp/register-rhel") + commands.append("/tmp/register-rhel -v -U '${var.rhsm_user_name}'") + commands.append("rm -fv /tmp/rhsm-user-passwd /tmp/register-rhel") # ## Configuring and starting puppet if vm.has_puppet: @@ -825,7 +834,7 @@ class CrTfHandlerFilesMixin(): p=purpose, t=vm.puppet_tier, o=vm.customer, c=vm.puppet_customer, z=zone, pr=vm.puppet_project, m=vm.puppet_contact, e=vm.puppet_env, r=vm.puppet_role) commands.append(cmd) - commands.append("rm -f /tmp/init-puppet") + commands.append("rm -fv /tmp/init-puppet") content += ' provisioner "remote-exec" {\n' content += ' inline = [\n' @@ -843,6 +852,22 @@ class CrTfHandlerFilesMixin(): if vm.has_puppet: content += self._create_instfile_puppet(vm) + # ## Unregistring from RedHat Subscription Management + cmd = ('if [ -x /sbin/subscription-manager ] ; then ' + '/sbin/subscription-manager unregister --no-progress-messages; fi || true') + if vm.is_rhel: + content += ' provisioner "remote-exec" {\n' + content += ' inline = [\n' + content += ' "{}"\n'.format(cmd) + content += ' ]\n' + content += ' when = destroy\n' + content += ' connection {\n' + content += ' type = "ssh"\n' + content += ' user = "root"\n' + content += ' host = "{}"\n'.format(vm.fqdn) + content += ' }\n' + content += ' }\n\n' + # ## local SSH cleanup on destroy content += textwrap.indent(textwrap.dedent('''\ provisioner "local-exec" {{ @@ -915,7 +940,7 @@ class CrTfHandlerFilesMixin(): "/usr/bin/systemctl enable puppet.service", "chmod +x /tmp/update-all-packages", "/tmp/update-all-packages", - "rm -f /tmp/update-all-packages", + "rm -fv /tmp/update-all-packages /tmp/functions.rc", ] connection {{ type = "ssh" @@ -940,7 +965,7 @@ class CrTfHandlerFilesMixin(): inline = [ "/usr/bin/systemctl stop puppet.service || true", ] - when = destroy + when = destroy connection {{ type = "ssh" user = "root" -- 2.39.5