From 6e5275b56b78c0dea7844926ac2eecd773b72b66 Mon Sep 17 00:00:00 2001 From: patdyn Date: Wed, 26 Jun 2024 15:23:20 +0200 Subject: [PATCH] Implement backend git api and respective checks --- .../ddadevops/application/terraform_service.py | 15 +++++++++++++-- .../python/ddadevops/devops_terraform_build.py | 8 +++++++- src/main/python/ddadevops/domain/terraform.py | 3 +++ .../python/ddadevops/infrastructure/__init__.py | 1 + .../ddadevops/infrastructure/infrastructure.py | 7 +++++++ 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/python/ddadevops/application/terraform_service.py b/src/main/python/ddadevops/application/terraform_service.py index 94d2a2b..37687f1 100644 --- a/src/main/python/ddadevops/application/terraform_service.py +++ b/src/main/python/ddadevops/application/terraform_service.py @@ -3,17 +3,18 @@ from dda_python_terraform import Terraform, IsFlagged from packaging import version from ..domain import Devops, BuildType -from ..infrastructure import FileApi, ResourceApi, TerraformApi +from ..infrastructure import FileApi, ResourceApi, TerraformApi, TerraformBackendGitApi # TODO: mv more fkt to Terraform_api ? class TerraformService: def __init__( - self, file_api: FileApi, resource_api: ResourceApi, terraform_api: TerraformApi + self, file_api: FileApi, resource_api: ResourceApi, terraform_api: TerraformApi, tf_backend_git_api: TerraformBackendGitApi ): self.file_api = file_api self.resource_api = resource_api self.terraform_api = terraform_api + self.tf_backend_git_api = tf_backend_git_api @classmethod def prod(cls): @@ -21,6 +22,7 @@ class TerraformService: FileApi(), ResourceApi(), TerraformApi(), + TerraformBackendGitApi(), ) def initialize_build_dir(self, devops: Devops): @@ -35,6 +37,13 @@ class TerraformService: self.file_api.cp("*.tfvars", devops.build_path(), check=False) self.file_api.cp_recursive("scripts", devops.build_path(), check=False) + def start_tf_backend_git_daemon(self): + self.tf_backend_git_api.start() + + def uses_backend_git(self, devops: Devops) -> bool: + terraform = devops.specialized_builds[BuildType.TERRAFORM] + return terraform.uses_backend_git() + def read_output(self, devops: Devops) -> map: terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] return self.file_api.read_json_fro_file( @@ -154,6 +163,8 @@ class TerraformService: def post_build(self, devops: Devops): self.__rescue_local_state__(devops) + self.tf_backend_git_api.stop() + def __copy_build_resource_file_from_package__(self, resource_name, devops: Devops): data = self.resource_api.read_resource( diff --git a/src/main/python/ddadevops/devops_terraform_build.py b/src/main/python/ddadevops/devops_terraform_build.py index 89901a0..25ab12f 100644 --- a/src/main/python/ddadevops/devops_terraform_build.py +++ b/src/main/python/ddadevops/devops_terraform_build.py @@ -14,12 +14,18 @@ class DevopsTerraformBuild(DevopsBuild): super().__init__(project, inp) project.build_depends_on("dda-python-terraform") self.teraform_service = TerraformService.prod() - + # ToDo: we might want to make this private in the future, keeping this for compatibility def initialize_build_dir(self): super().initialize_build_dir() devops = self.devops_repo.get_devops(self.project) self.teraform_service.initialize_build_dir(devops) + def pre_build(self): + self.initialize_build_dir() + devops = self.devops_repo.get_devops(self.project) + if self.teraform_service.uses_backend_git(devops): + self.teraform_service.start_tf_backend_git_daemon() + def post_build(self): devops = self.devops_repo.get_devops(self.project) self.teraform_service.post_build(devops) diff --git a/src/main/python/ddadevops/domain/terraform.py b/src/main/python/ddadevops/domain/terraform.py index dc38530..f45e091 100644 --- a/src/main/python/ddadevops/domain/terraform.py +++ b/src/main/python/ddadevops/domain/terraform.py @@ -85,6 +85,9 @@ class TerraformDomain(Validateable): for provider in self.providers.values(): result = result and provider.is_local_state() return result + + def uses_backend_git(self) -> bool: + return ProviderType.TERRAFORM_GIT_BACKEND in self.providers.keys() def backend_config(self) -> Dict[str, Any]: result = {} diff --git a/src/main/python/ddadevops/infrastructure/__init__.py b/src/main/python/ddadevops/infrastructure/__init__.py index 1a520ed..c168427 100644 --- a/src/main/python/ddadevops/infrastructure/__init__.py +++ b/src/main/python/ddadevops/infrastructure/__init__.py @@ -7,6 +7,7 @@ from .infrastructure import ( CredentialsApi, GitApi, TerraformApi, + TerraformBackendGitApi, ArtifactDeploymentApi, ) from .repository import DevopsRepository, BuildFileRepository diff --git a/src/main/python/ddadevops/infrastructure/infrastructure.py b/src/main/python/ddadevops/infrastructure/infrastructure.py index a57977d..2dbf62e 100644 --- a/src/main/python/ddadevops/infrastructure/infrastructure.py +++ b/src/main/python/ddadevops/infrastructure/infrastructure.py @@ -215,6 +215,13 @@ class GitApi: class TerraformApi: pass +class TerraformBackendGitApi: + def __init__(self): + self.execution_api = ExecutionApi() + def start(self): + self.execution_api.execute("terraform-backend-git &") + def stop(self): + self.execution_api.execute("terraform-backend-git stop") class ArtifactDeploymentApi: def __init__(self):