]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
Starting with requesting Consul.
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 24 May 2024 15:36:07 +0000 (17:36 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 24 May 2024 15:36:07 +0000 (17:36 +0200)
lib/create_terraform/__init__.py
lib/create_terraform/handler/__init__.py
lib/create_terraform/handler/first.py
lib/create_terraform/handler/vmware.py
test/test_01_config.py

index f5c7034c7efea4f08bfd87a0a6a9d6d2fd715d1b..85d6b74017baec5ae8dfdbe8ccb0406b3f8a9137 100644 (file)
@@ -12,7 +12,7 @@ CFGFILE_BASENAME = 'create-terraform.ini'
 
 DEFAULT_CONSUL_API_KV_ROOTPATH = '/v1/kv/terraform'
 DEFAULT_CONSUL_API_TIMEOUT = 20
-DEFAULT_CONSUL_PORT = 8500
+DEFAULT_CONSUL_PORT = 443
 DEFAULT_CONSUL_SERVER = 'terraform.pixelpark.com'
 
 # This library name will be used as a part of the user agent in HTTP(S) requests
index 8826e8382b0080db3925dc9221de0d28ec268042..0e23688297cb5983a38afd621859be6669029beb 100644 (file)
@@ -48,7 +48,7 @@ from ..errors import AbortExecution
 
 from ..xlate import XLATOR
 
-__version__ = '4.2.0'
+__version__ = '4.2.1'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -87,7 +87,7 @@ class CreateTerraformHandler(
     tz = pytz.timezone(tz_name)
 
     steps = (
-        'init', 'vmw-init', 'read-yaml', 'pdns-zones', 'vmw-test', 'collect-folders',
+        'init', 'vmw-init', 'read-yaml', 'pdns-zones', 'consul', 'vmw-test', 'collect-folders',
         'vmw-clusters', 'vmw-datastores', 'vmw-ds-clusters', 'vmw-vms', 'vmw-networks',
         'vmw-templates', 'validate-yaml', 'validate-storage', 'validate-iface', 'validate-dns',
         'perform-dns', 'project-dir', 'tf-files', 'ensure-vmw-folders',
@@ -97,6 +97,7 @@ class CreateTerraformHandler(
         'vmw-init': _('After initialisation of VSPhere handlers.'),
         'read-yaml': _('After reading the given YAML file.'),
         'pdns-zones': _('After retrieving all DNS zones from PowerDNS.'),
+        'consul': _('Check Consul database and cleanup, if necessary.'),
         'vmw-test': _('After testing VSPhere handlers.'),
         'collect-folders': _('After collecting all VMWare and local folders.'),
         'vmw-clusters': _('After collecting all VMWare clusters.'),
@@ -389,10 +390,14 @@ class CreateTerraformHandler(
 
             self.exec_pdns_zones()
 
+            print()
+            self.set_project(yaml_file)
+            self.check_consul()
+
             print()
             LOG.info(_("Collecting first information from vSPhere."))
             self.test_vsphere_handlers()
-            self.exec_collect_folders(yaml_file)
+            self.exec_collect_folders()
             self.assign_default_vmw_values()
 
             print()
@@ -451,6 +456,52 @@ class CreateTerraformHandler(
 
         print()
 
+    # -------------------------------------------------------------------------·
+    def check_consul(self):
+        """Checks for the existence of the project and cleaning it up, if it exists."""
+        if self.stop_at_step == 'consul':
+            self.incr_verbosity()
+
+        key = self.full_project_name
+        if not key:
+            key = self.project_name
+
+        print()
+        LOG.info(_(
+            'Checking the Consul database for key {!r} and cleanup, if necessary ...').format(
+            key)
+        )
+
+        project_data = self.consul.get_key(key)
+        if not project_data:
+            LOG.info(_('No data for project {!r} found on Consul database.').format(key))
+        else:
+            msg = _('Found data for project {!r} on Consul database.').format(key)
+            LOG.warn(msg)
+            if self.verbose > 2:
+                msg = _('Found data:') + '\n' + pp(project_data)
+                LOG.debug(msg)
+
+        LOG.info(_("Finished step {!r}.").format('consul'))
+        if self.stop_at_step == 'consul':
+            raise AbortExecution('consul')
+
+    # -------------------------------------------------------------------------·
+    def set_project(self, yaml_file):
+        """Set project name and directory."""
+        LOG.debug(_('Setting project name and directory based on file {!r} ...').format(
+            str(yaml_file)))
+
+        yfile = Path(yaml_file)
+
+        yfile_base = yfile.name
+        yfile_dir = yfile.parent.resolve()
+        (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)))
+        self.project_dir = yfile_dir / yfile_stem
+        LOG.info(_("Project directory is: {!r}.").format(str(self.project_dir)))
+
     # -------------------------------------------------------------------------·
     def exec_terraform(self):
 
index 03185b1509d404bb65161b9157bfb028760a97ff..4306d76a502f2d9d4bdb00bffad021ef8a79ab10 100644 (file)
@@ -57,6 +57,9 @@ class CrTfHandlerFirstMixin():
         for vsphere_name in self.vsphere:
             self.vsphere[vsphere_name].verbose = self.verbose
 
+        if self.consul:
+            self.consul.verbose = self.verbose
+
     # -------------------------------------------------------------------------
     def init_handlers(self):
 
index f24a73042e72118913700771e97bece8b107754a..e52f5b3293bcbc55cf4d8c92d5e801873a411a1d 100644 (file)
@@ -15,8 +15,6 @@ import os
 import re
 import sys
 
-from pathlib import Path
-
 from operator import attrgetter
 
 # Third party modules
@@ -33,7 +31,7 @@ from ..slim_vm import SlimVm
 
 from ..xlate import XLATOR
 
-__version__ = '1.0.0'
+__version__ = '1.0.1'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -45,7 +43,7 @@ class CrTfHandlerVmwMixin():
     """A mixin module for the handler module for interacting with VMware/VSphere.."""
 
     # -------------------------------------------------------------------------·
-    def exec_collect_folders(self, yaml_file):
+    def exec_collect_folders(self):
 
         if self.stop_at_step == 'collect-folders':
             self.incr_verbosity()
@@ -64,16 +62,6 @@ class CrTfHandlerVmwMixin():
         self.vsphere_folders.sort(key=str.lower)
         LOG.debug(_("Collected used vSphere folders:") + "\n" + pp(self.vsphere_folders))
 
-        # Set project name and directory
-        yfile = Path(yaml_file)
-        yfile_base = yfile.name
-        yfile_dir = yfile.parent.resolve()
-        (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)))
-        self.project_dir = yfile_dir / yfile_stem
-        LOG.info(_("Project directory is: {!r}.").format(str(self.project_dir)))
-
         # Evaluating root terraform directory
         if not self.is_venv:
             i = 4
index 8847f3b5925a4eca17778431f4cf5d5a2b8ad786..fe074e5116b779e4b5d7527ac9cc98d46513d67f 100755 (executable)
@@ -23,7 +23,7 @@ sys.path.insert(0, libdir)
 
 from general import CreateTerraformTestcase, get_arg_verbose, init_root_logger
 
-from fb_tools.common import to_bool
+from fb_tools.common import to_bool
 
 LOG = logging.getLogger('test_base_handler')