From 14a85802eabd198c046dfe1817cf20cf84bb0c87 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 13 Oct 2023 14:02:49 +0200 Subject: [PATCH] Start splitting lib/cr_tf/handler.py --- lib/cr_tf/errors.py | 20 ++++- lib/cr_tf/{handler.py => handler/__init__.py} | 85 +++++++++---------- 2 files changed, 60 insertions(+), 45 deletions(-) rename lib/cr_tf/{handler.py => handler/__init__.py} (98%) diff --git a/lib/cr_tf/errors.py b/lib/cr_tf/errors.py index 1dda2c2..b4f6fa3 100644 --- a/lib/cr_tf/errors.py +++ b/lib/cr_tf/errors.py @@ -16,7 +16,7 @@ from fb_tools.config import ConfigError from .xlate import XLATOR -__version__ = '1.2.0' +__version__ = '1.2.1' _ = XLATOR.gettext ngettext = XLATOR.ngettext @@ -81,6 +81,24 @@ class CrTfConfigError(ConfigError): pass +# ============================================================================= +class AbortExecution(ExpectedHandlerError): + """Indicating an abort of the execution.""" + + # ------------------------------------------------------------------------- + def __init__(self, step=None): + + if step: + self.step = step + else: + self.step = _('') + + # ------------------------------------------------------------------------- + def __str__(self): + + return _("Aborting after {!r}.").format(self.step) + + # ============================================================================= if __name__ == "__main__": diff --git a/lib/cr_tf/handler.py b/lib/cr_tf/handler/__init__.py similarity index 98% rename from lib/cr_tf/handler.py rename to lib/cr_tf/handler/__init__.py index 6a36f72..e19012e 100644 --- a/lib/cr_tf/handler.py +++ b/lib/cr_tf/handler/__init__.py @@ -40,7 +40,6 @@ import pytz import yaml import six -# Own modules from fb_tools.common import pp, to_bool, to_str, RE_DOT_AT_END from fb_tools.errors import HandlerError, ExpectedHandlerError, CommandNotFoundError from fb_tools.handling_obj import HandlingObject, CalledProcessError @@ -53,18 +52,21 @@ from fb_vmware.connect import VsphereConnection from fb_pdnstools.server import PowerDNSServer from fb_pdnstools.errors import PowerDNSHandlerError -from . import MIN_VERSION_TERRAFORM, MAX_VERSION_TERRAFORM -from . import MIN_VERSION_VSPHERE_PROVIDER +# Own modules +from .. import MIN_VERSION_TERRAFORM, MAX_VERSION_TERRAFORM +from .. import MIN_VERSION_VSPHERE_PROVIDER + +from ..errors import AbortExecution -from .config import CrTfConfiguration +from ..config import CrTfConfiguration -from .terraform.vm import TerraformVm +from ..terraform.vm import TerraformVm -from .terraform.disk import TerraformDisk +from ..terraform.disk import TerraformDisk -from .xlate import XLATOR +from ..xlate import XLATOR -__version__ = '3.8.1' +__version__ = '3.9.0' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -76,9 +78,9 @@ def password_input_getch(prompt='', fill_char='*', max_len=64): p_s = '' proxy_string = ' ' * 64 - fch = ' ' - if len(fill_char) >= 1: - fch = fill_char[0] + # fch = ' ' + # if len(fill_char) >= 1: + # fch = fill_char[0] while True: @@ -112,24 +114,6 @@ def password_input(prompt='', fill_char='*', max_len=64): return getpass.getpass(prompt=prompt) -# ============================================================================= -class AbortExecution(ExpectedHandlerError): - """Indicating an abort of the execution.""" - - # ------------------------------------------------------------------------- - def __init__(self, step=None): - - if step: - self.step = step - else: - self.step = _('') - - # ------------------------------------------------------------------------- - def __str__(self): - - return _("Aborting after {!r}.").format(self.step) - - # ============================================================================= class CreateTerraformHandler(BaseHandler): """ @@ -904,7 +888,10 @@ class CreateTerraformHandler(BaseHandler): nr_total += len(self.vsphere[vname].ds_clusters.keys()) if nr_total: - msg = ngettext("Found one datastore cluster.", "Found {n} datastore clusters.", nr_total) + msg = ngettext( + "Found one datastore cluster.", + "Found {n} datastore clusters.", + nr_total) LOG.debug(msg.format(n=nr_total)) else: LOG.warn(_("No VSPhere datastore clusters found.")) @@ -2750,11 +2737,12 @@ class CreateTerraformHandler(BaseHandler): zone = "{z}/{c}".format(z=vm.vsphere, c=vm.cluster) 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}' " - "--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) + cmd = ( + "/tmp/create-motd --purpose '{p}' --hardware 'vmware (x86_64)' --owner '{o}' " + "--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) if vm.puppet_project: cmd += " --project '{pr}'".format(pr=vm.puppet_project) cmd += " | tee /etc/motd" @@ -2865,9 +2853,14 @@ class CreateTerraformHandler(BaseHandler): # -------------------------------------------------------------------------· def _create_instfile_puppet(self, vm): + cmd = ( + "ssh -o StrictHostKeyChecking=no {ca} " + "'sudo /opt/puppetlabs/bin/puppetserver ca sign --certname {h} || true'").format( + ca=self.config.puppetca, h=vm.fqdn) + content = textwrap.indent(textwrap.dedent('''\ provisioner "local-exec" {{ - command = "ssh -o StrictHostKeyChecking=no {ca} 'sudo /opt/puppetlabs/bin/puppetserver ca sign --certname {h} || true'" + command = "{cmd}" }} provisioner "remote-exec" {{ @@ -2886,11 +2879,17 @@ class CreateTerraformHandler(BaseHandler): }} }} - '''), ' ').format( - ca=self.config.puppetca, h=vm.fqdn, - ) + '''), ' ').format(cmd=cmd, h=vm.fqdn,) # Destroy actions with puppet + cmd1 = "ssh -o StrictHostKeyChecking=no {ma} " + cmd1 += "'sudo /opt/puppetlabs/bin/puppet node deactivate {h} || true'" + cmd1 = cmd1.format(ma=self.config.puppetmaster, h=vm.fqdn) + + cmd2 = "ssh -o StrictHostKeyChecking=no {ca} " + cmd2 += "'sudo /opt/puppetlabs/bin/puppetserver ca clean --certname {h} || true'" + cmd2 = cmd2.format(ca=self.config.puppetca, h=vm.fqdn) + content += textwrap.indent(textwrap.dedent('''\ provisioner "remote-exec" {{ inline = [ @@ -2905,18 +2904,16 @@ class CreateTerraformHandler(BaseHandler): }} provisioner "local-exec" {{ - command = "ssh -o StrictHostKeyChecking=no {ma} 'sudo /opt/puppetlabs/bin/puppet node deactivate {h} || true'" + command = "{cmd1}" when = destroy }} provisioner "local-exec" {{ - command = "ssh -o StrictHostKeyChecking=no {ca} 'sudo /opt/puppetlabs/bin/puppetserver ca clean --certname {h} || true'" + command = "{cmd2}" when = destroy }} - '''), ' ').format( - ca=self.config.puppetca, h=vm.fqdn, ma=self.config.puppetmaster, - ) + '''), ' ').format(cmd1=cmd1, cmd2=cmd2, h=vm.fqdn) return content -- 2.39.5