This commit is contained in:
patdyn 2024-06-27 12:43:24 +02:00
parent ff61ff383e
commit d6b6cb7a72
6 changed files with 80 additions and 72 deletions

View file

@ -9,12 +9,12 @@ from ..infrastructure import FileApi, ResourceApi, TerraformApi, TerraformBacken
# TODO: mv more fkt to Terraform_api ?
class TerraformService:
def __init__(
self, file_api: FileApi, resource_api: ResourceApi, terraform_api: TerraformApi, tf_backend_git_api: TerraformBackendGitApi
self, file_api: FileApi, resource_api: ResourceApi, terraform_api: TerraformApi, tf_backend_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
self.tf_backend_git_api = tf_backend_api
@classmethod
def prod(cls):
@ -167,7 +167,6 @@ class TerraformService:
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(
f"src/main/resources/terraform/{resource_name}"

View file

@ -14,6 +14,7 @@ class DevopsTerraformBuild(DevopsBuild):
super().__init__(project, inp)
project.build_depends_on("dda-python-terraform")
self.terraform_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()
@ -24,7 +25,7 @@ class DevopsTerraformBuild(DevopsBuild):
self.initialize_build_dir()
devops = self.devops_repo.get_devops(self.project)
if self.terraform_service.uses_backend_git(devops):
self.terraform_service.start_tf_backend_git_daemon()
self.terraform_service.start_tf_backend_git_daemon(devops)
def post_build(self):
devops = self.devops_repo.get_devops(self.project)

View file

@ -1,4 +1,4 @@
from typing import List, Dict, Any
from typing import List, Dict, Set, Any
from .common import Validateable, CredentialMappingDefault
class TerraformBackendGit(Validateable, CredentialMappingDefault):
@ -35,13 +35,13 @@ class TerraformBackendGit(Validateable, CredentialMappingDefault):
"unlock_address": self.__make_http_backend_address__(),
}
def resources_from_package(self) -> List[str]:
def resources_from_package(self) -> Set[str]:
return {"tf_backend_git_backend.tf", "tf_backend_git_backend_vars.tf"}
# TODO: This can not be used for backend config, as the backend block can not reference vars.
def project_vars(self) -> Dict[str, Any]:
return {
"http_backend_address": self.__make_http_backend_address__(self.git_backend_ref, self.git_backend_repo, self.git_backend_state)
"http_backend_address": self.__make_http_backend_address__()
}
def is_local_state(self):
@ -50,7 +50,8 @@ class TerraformBackendGit(Validateable, CredentialMappingDefault):
def __make_http_backend_address__(self) -> str:
# TODO Should we make this configurable?
base_string = "http://localhost:6061/?type=git"
return f"{base_string}&repository={self.git_backend_repo}&ref={self.git_backend_ref}&state={self.stage}/{self.module}/{self.git_backend_state}.json"
state = f"{self.stage}/{self.module}/{self.git_backend_state}"
return f"{base_string}&repository={self.git_backend_repo}&ref={self.git_backend_ref}&state={state}"
# TODO: Implement ssh auth too
@classmethod

View file

@ -12,6 +12,7 @@ class BuildType(Enum):
K3S = 2
TERRAFORM = 3
# TODO: We could follow domain implications and make a 'BackendType' enum
class ProviderType(Enum):
DIGITALOCEAN = 0

View file

@ -90,7 +90,9 @@ class TerraformDomain(Validateable):
return result
def uses_backend_git(self) -> bool:
return ProviderType.TERRAFORM_BACKEND_GIT in self.providers.keys()
if ProviderType.TERRAFORM_BACKEND_GIT in self.providers.keys():
return True
return False
# TODO: Add ssh method case and make this default
# TODO: How do we get to the credentials?
@ -98,10 +100,10 @@ class TerraformDomain(Validateable):
tf_backend_git = self.providers[ProviderType.TERRAFORM_BACKEND_GIT]
if tf_backend_git.git_backend_token != "":
return {
"GITHUB_TOKEN" : tf_backend_git.git_backend_token,
"GIT_USERNAME" : tf_backend_git.git_backend_username,
"GITHUB_TOKEN": tf_backend_git.git_backend_token,
"GIT_USERNAME": tf_backend_git.git_backend_username,
}
return {"" : ""}
return {"": ""}
def backend_config(self) -> Dict[str, Any]:
result = {}

View file

@ -216,17 +216,21 @@ class GitApi:
class TerraformApi:
pass
class TerraformBackendGitApi:
def __init__(self):
self.execution_api = ExecutionApi()
def start(self, credentials: Dict[str, str]):
env = ""
for key in credentials:
env = env + f'{key}' + "=" + f'{credentials[key]}' + " "
self.execution_api.execute(f'{env}' + " " + "terraform-backend-git &")
def stop(self):
self.execution_api.execute("terraform-backend-git stop")
class ArtifactDeploymentApi:
def __init__(self):
self.execution_api = ExecutionApi()