Implement backend git api and respective checks

This commit is contained in:
patdyn 2024-06-26 15:23:20 +02:00
parent 14db8bed54
commit 6e5275b56b
5 changed files with 31 additions and 3 deletions

View file

@ -3,17 +3,18 @@ from dda_python_terraform import Terraform, IsFlagged
from packaging import version from packaging import version
from ..domain import Devops, BuildType 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 ? # TODO: mv more fkt to Terraform_api ?
class TerraformService: class TerraformService:
def __init__( 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.file_api = file_api
self.resource_api = resource_api self.resource_api = resource_api
self.terraform_api = terraform_api self.terraform_api = terraform_api
self.tf_backend_git_api = tf_backend_git_api
@classmethod @classmethod
def prod(cls): def prod(cls):
@ -21,6 +22,7 @@ class TerraformService:
FileApi(), FileApi(),
ResourceApi(), ResourceApi(),
TerraformApi(), TerraformApi(),
TerraformBackendGitApi(),
) )
def initialize_build_dir(self, devops: Devops): 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("*.tfvars", devops.build_path(), check=False)
self.file_api.cp_recursive("scripts", 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: def read_output(self, devops: Devops) -> map:
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
return self.file_api.read_json_fro_file( return self.file_api.read_json_fro_file(
@ -154,6 +163,8 @@ class TerraformService:
def post_build(self, devops: Devops): def post_build(self, devops: Devops):
self.__rescue_local_state__(devops) self.__rescue_local_state__(devops)
self.tf_backend_git_api.stop()
def __copy_build_resource_file_from_package__(self, resource_name, devops: Devops): def __copy_build_resource_file_from_package__(self, resource_name, devops: Devops):
data = self.resource_api.read_resource( data = self.resource_api.read_resource(

View file

@ -14,12 +14,18 @@ class DevopsTerraformBuild(DevopsBuild):
super().__init__(project, inp) super().__init__(project, inp)
project.build_depends_on("dda-python-terraform") project.build_depends_on("dda-python-terraform")
self.teraform_service = TerraformService.prod() 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): def initialize_build_dir(self):
super().initialize_build_dir() super().initialize_build_dir()
devops = self.devops_repo.get_devops(self.project) devops = self.devops_repo.get_devops(self.project)
self.teraform_service.initialize_build_dir(devops) 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): def post_build(self):
devops = self.devops_repo.get_devops(self.project) devops = self.devops_repo.get_devops(self.project)
self.teraform_service.post_build(devops) self.teraform_service.post_build(devops)

View file

@ -85,6 +85,9 @@ class TerraformDomain(Validateable):
for provider in self.providers.values(): for provider in self.providers.values():
result = result and provider.is_local_state() result = result and provider.is_local_state()
return result return result
def uses_backend_git(self) -> bool:
return ProviderType.TERRAFORM_GIT_BACKEND in self.providers.keys()
def backend_config(self) -> Dict[str, Any]: def backend_config(self) -> Dict[str, Any]:
result = {} result = {}

View file

@ -7,6 +7,7 @@ from .infrastructure import (
CredentialsApi, CredentialsApi,
GitApi, GitApi,
TerraformApi, TerraformApi,
TerraformBackendGitApi,
ArtifactDeploymentApi, ArtifactDeploymentApi,
) )
from .repository import DevopsRepository, BuildFileRepository from .repository import DevopsRepository, BuildFileRepository

View file

@ -215,6 +215,13 @@ class GitApi:
class TerraformApi: class TerraformApi:
pass 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: class ArtifactDeploymentApi:
def __init__(self): def __init__(self):