From 161d885df00d697ca4c52ab5eb516ca5498968aa Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 1 Oct 2019 12:19:35 +0200 Subject: [PATCH] Initialising all VSPhere server objects --- lib/cr_tf/handler.py | 96 ++++++++++++++++++++++++++++++++------------ 1 file changed, 71 insertions(+), 25 deletions(-) diff --git a/lib/cr_tf/handler.py b/lib/cr_tf/handler.py index 4739628..4ccddaa 100644 --- a/lib/cr_tf/handler.py +++ b/lib/cr_tf/handler.py @@ -59,7 +59,7 @@ from .terraform.disk import TerraformDisk from .xlate import XLATOR -__version__ = '2.9.1' +__version__ = '2.9.2' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -113,7 +113,7 @@ class CreateTerraformHandler(BaseHandler): tz = pytz.timezone(tz_name) steps = ( - 'init', 'read-yaml', 'collect-folders', 'pdns-zones', 'vmw-clusters', + 'init', 'read-yaml', 'pdns-zones', 'vmw-init', 'collect-folders', 'vmw-clusters', 'vmw-datastores', 'vmw-ds-clusters', 'vmw-networks', 'vmw-templates', 'validate-yaml', 'validate-storage', 'validate-iface', 'validate-dns', 'perform-dns', 'project-dir', 'tf-files', 'ensure-vmw-folders', @@ -121,8 +121,9 @@ class CreateTerraformHandler(BaseHandler): step_desc = { 'init': _('After initialization of all objects and handlers.'), 'read-yaml': _('After reading the given YAML file.'), - 'collect-folders': _('After collecting all VMWare and local folders.'), 'pdns-zones': _('After retrieving all DNS zones from PowerDNS.'), + 'vmw-init': _('After initialisation of VSPhere handlers.'), + 'collect-folders': _('After collecting all VMWare and local folders.'), 'vmw-clusters': _('After collecting all VMWare clusters.'), 'vmw-datastores': _('After collecting all VMWare datastores.'), 'vmw-ds-clusters': _('After collecting all VMWare datastore clusters.'), @@ -311,8 +312,8 @@ class CreateTerraformHandler(BaseHandler): if self.pdns: self.pdns.verbose = self.verbose - if self.vsphere: - self.vsphere.verbose = self.verbose + for vname in self.vsphere: + self.vsphere[vname].verbose = self.verbose # ------------------------------------------------------------------------- def init_handlers(self): @@ -353,19 +354,6 @@ class CreateTerraformHandler(BaseHandler): except PowerDNSHandlerError as e: raise ExpectedHandlerError(str(e)) -# try: -# self.vsphere = VsphereServer( -# appname=self.appname, verbose=self.verbose, base_dir=self.base_dir, -# host=self.config.vsphere_host, port=self.config.vsphere_port, -# user=self.config.vsphere_user, password=self.config.vsphere_password, -# dc=self.config.vsphere_dc, simulate=self.simulate, force=self.force, -# terminal_has_colors=self.terminal_has_colors, initialized=True, -# ) -# -# self.vsphere.get_about() -# except VSphereExpectedError as e: -# raise ExpectedHandlerError(str(e)) - # ------------------------------------------------------------------------- def check_terraform_version(self): """ Checking, that the called terraform has a minimum version.""" @@ -465,9 +453,13 @@ class CreateTerraformHandler(BaseHandler): print(self.colored('#' * (ll + 4), 'AQUA')) print() - self.exec_collect_folders(yaml_file) self.exec_pdns_zones() + print() + LOG.info(_("Initialising VSPhere handlers.")) + self.init_vspheres() + self.exec_collect_folders(yaml_file) + print() LOG.info(_("Retrieving information from vSphere.")) @@ -563,7 +555,8 @@ class CreateTerraformHandler(BaseHandler): LOG.info(_("Collecting all VMWare and local folders ...")) LOG.info(_("Get vSphere datacenter ...")) - self.vsphere.get_datacenter() + for vname in self.vsphere: + self.vsphere[vname].get_datacenter() LOG.debug(_("Collecting vSphere folders.")) self.vsphere_folders = [] @@ -581,11 +574,7 @@ class CreateTerraformHandler(BaseHandler): (yfile_stem, yfile_ext) = os.path.splitext(yfile_base) self.project_name = yfile_stem LOG.info(_("Project name is {!r}.").format(str(self.project_name))) - if self.config.relative_tf_dir: - LOG.debug(_("Using a relative project directory path ...")) - self.project_dir = yfile_dir / yfile_stem - else: - self.project_dir = self.config.terraform_dir / yfile_stem + self.project_dir = yfile_dir / yfile_stem LOG.info(_("Project directory is: {!r}.").format(str(self.project_dir))) LOG.info(_("Finished step {!r}.").format('collect-folders')) @@ -613,6 +602,63 @@ class CreateTerraformHandler(BaseHandler): if self.stop_at_step == 'pdns-zones': raise AbortExecution('pdns-zones') + # -------------------------------------------------------------------------· + def init_vspheres(self): + + if self.stop_at_step == 'vmw-init': + self.incr_verbosity() + + self._init_vspheres() + + LOG.info(_("Finished step {!r}.").format('vmw-init')) + if self.stop_at_step == 'vmw-init': + raise AbortExecution('vmw-init') + + # -------------------------------------------------------------------------· + def _init_vspheres(self): + + for vm in self.vms: + if vm.vsphere in self.vsphere: + continue + vname = vm.vsphere + if vname not in self.config.vsphere: + msg = _("VSPhere {!r} not defined in configuration.").format(vname) + raise ExpectedHandlerError(msg) + + try: + params = { + 'appname': self.appname, + 'verbose': self.verbose, + 'base_dir': self.base_dir, + 'host': self.config.vsphere[vname].host, + 'port': self.config.vsphere[vname].port, + 'user': self.config.vsphere[vname].user, + 'password': self.config.vsphere[vname].password, + 'dc': self.config.vsphere[vname].dc, + 'simulate': self.simulate, + 'force': self.force, + 'terminal_has_colors': self.terminal_has_colors, + 'initialized': True, + } + if self.verbose > 1: + params_out = copy.copy(params) + if self.verbose < 5: + params_out['password'] = '******' + msg = _("Initialising a {}-object with params:").format('VsphereServer') + msg += '\n' + pp(params_out) + LOG.debug(msg) + + vsphere = VsphereServer(**params) + vsphere.get_about() + if self.verbose > 2: + msg = _("Created {}-object:").format('VsphereServer') + msg += '\n' + pp(vsphere.as_dict()) + LOG.debug(msg) + self.vsphere[vname] = vsphere + + except VSphereExpectedError as e: + raise ExpectedHandlerError(str(e)) + # -------------------------------------------------------------------------· def exec_vmw_clusters(self): -- 2.39.5