]> Frank Brehm's Git Trees - pixelpark/create-terraform.git/commitdiff
New poperties terraform_root_dir and full_project_name in class CreateTerraformHandler
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 5 Nov 2019 16:32:57 +0000 (17:32 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 5 Nov 2019 16:32:57 +0000 (17:32 +0100)
lib/cr_tf/handler.py

index 25bc2940a6f40e629abe9afd49f43906e8c63b59..ef8198b3f02bca5757d0e2fa817c0aa7c1cb870a 100644 (file)
@@ -61,7 +61,7 @@ from .terraform.disk import TerraformDisk
 
 from .xlate import XLATOR
 
-__version__ = '3.0.4'
+__version__ = '3.1.0'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -174,6 +174,8 @@ class CreateTerraformHandler(BaseHandler):
         self.project_dir = None
         self.project_name = None
 
+        self._terraform_root_dir = None
+
         self.all_vms = {}
         self.existing_vms = []
 
@@ -268,6 +270,26 @@ class CreateTerraformHandler(BaseHandler):
             raise ValueError(msg)
         self._stop_at_step = v
 
+    # -----------------------------------------------------------
+    @property
+    def terraform_root_dir(self):
+        """Root directory of all terraform directories."""
+        if self.is_venv:
+            return self.base_dir.parent
+        return self._terraform_root_dir
+
+    # -----------------------------------------------------------
+    @property
+    def full_project_name(self):
+        """Complete project name with parent paths."""
+        if not self.project_name:
+            return None
+        if not self.project_dir:
+            return None
+        if not self.terraform_root_dir:
+            return self.project_name
+        return os.path.relpath(str(self.project_dir), self.terraform_root_dir)
+
     # -------------------------------------------------------------------------
     def as_dict(self, short=True):
         """
@@ -287,6 +309,8 @@ class CreateTerraformHandler(BaseHandler):
         res['stop_at_step'] = self.stop_at_step
         res['steps'] = copy.copy(self.steps)
         res['tz_name'] = self.tz_name
+        res['terraform_root_dir'] = self.terraform_root_dir
+        res['full_project_name'] = self.full_project_name
         res['vsphere'] = {}
         for vsphere_name in self.vsphere.keys():
             res['vsphere'][vsphere_name] = self.vsphere[vsphere_name].as_dict(short=short)
@@ -581,6 +605,24 @@ class CreateTerraformHandler(BaseHandler):
         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
+            cdir = copy.copy(self.project_dir).parent
+            while i > 0:
+                git_dir = cdir / '.git'
+                if git_dir.is_dir():
+                    self._terraform_root_dir = cdir
+                    break
+                i -= 1
+                if cdir == cdir.parent:
+                    break
+                cdir = cdir.parent
+            if not self._terraform_root_dir:
+                msg = _("Did not found root terraform directory above {!r}.").format(
+                    str(self.project_dir))
+                LOG.warn(msg)
+
         LOG.info(_("Finished step {!r}.").format('collect-folders'))
         if self.stop_at_step == 'collect-folders':
             raise AbortExecution('collect-folders')