From a6c0c56329b447e0d0d730a8affa210af2e16d28 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Tue, 5 Nov 2019 17:32:57 +0100 Subject: [PATCH] New poperties terraform_root_dir and full_project_name in class CreateTerraformHandler --- lib/cr_tf/handler.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/cr_tf/handler.py b/lib/cr_tf/handler.py index 25bc294..ef8198b 100644 --- a/lib/cr_tf/handler.py +++ b/lib/cr_tf/handler.py @@ -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') -- 2.39.5