migrate fkt to service
This commit is contained in:
parent
d909251c08
commit
7ede8e345a
2 changed files with 67 additions and 44 deletions
62
src/main/python/ddadevops/application/terraform_service.py
Normal file
62
src/main/python/ddadevops/application/terraform_service.py
Normal file
|
@ -0,0 +1,62 @@
|
|||
from pathlib import Path
|
||||
from ..domain import Devops, BuildType
|
||||
from ..infrastructure import FileApi, ResourceApi, ImageApi
|
||||
|
||||
|
||||
class TerraformService:
|
||||
def __init__(
|
||||
self, file_api: FileApi, resource_api: ResourceApi, image_api: ImageApi
|
||||
):
|
||||
self.file_api = file_api
|
||||
self.resource_api = resource_api
|
||||
self.image_api = image_api
|
||||
|
||||
@classmethod
|
||||
def prod(cls):
|
||||
return cls(
|
||||
FileApi(),
|
||||
ResourceApi(),
|
||||
ImageApi(),
|
||||
)
|
||||
|
||||
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}"
|
||||
)
|
||||
self.file_api.write_data_to_file(
|
||||
Path(f"{devops.build_path()}/{resource_name}"), data
|
||||
)
|
||||
|
||||
def __copy_build_resources_from_package__(self, devops: Devops):
|
||||
self.__copy_build_resource_file_from_package__("versions.tf", devops)
|
||||
self.__copy_build_resource_file_from_package__(
|
||||
"terraform_build_vars.tf", devops
|
||||
)
|
||||
|
||||
def __copy_build_resources_from_dir__(self, devops: Devops):
|
||||
terraform = devops.specialized_builds[BuildType.TERRAFORM]
|
||||
self.file_api.cp_force(
|
||||
f"{terraform.build_commons_path()}/*", devops.build_path()
|
||||
)
|
||||
|
||||
def copy_local_state(self, devops: Devops):
|
||||
# TODO: orignal was unchecked ...
|
||||
self.file_api.cp("terraform.tfstate", devops.build_path())
|
||||
|
||||
def rescue_local_state(self, devops: Devops):
|
||||
# TODO: orignal was unchecked ...
|
||||
self.file_api.cp(f"{devops.build_path()}/terraform.tfstate", ".")
|
||||
|
||||
def initialize_build_dir(self, devops: Devops):
|
||||
terraform = devops.specialized_builds[BuildType.TERRAFORM]
|
||||
if terraform.tf_use_package_common_files:
|
||||
self.__copy_build_resources_from_package__(devops)
|
||||
else:
|
||||
self.__copy_build_resources_from_dir__(devops)
|
||||
# TODO: orignal was unchecked ...
|
||||
self.copy_local_state(devops)
|
||||
self.file_api.cp("*.tf", devops.build_path())
|
||||
self.file_api.cp("*.properties", devops.build_path())
|
||||
self.file_api.cp("*.tfvars", devops.build_path())
|
||||
self.file_api.cp_recursive("scripts", devops.build_path())
|
||||
|
|
@ -56,55 +56,16 @@ class DevopsTerraformBuild(DevopsBuild):
|
|||
inp["mixin_types"] = config.get("mixin_types", [])
|
||||
super().__init__(project, inp)
|
||||
project.build_depends_on("dda-python-terraform")
|
||||
|
||||
def terraform_build_commons_path(self):
|
||||
mylist = [self.build_commons_path, self.terraform_build_commons_dir_name]
|
||||
return "/".join(filter_none(mylist)) + "/"
|
||||
|
||||
def project_vars(self):
|
||||
ret = {"stage": self.stage}
|
||||
if self.module:
|
||||
ret["module"] = self.module
|
||||
if self.additional_vars:
|
||||
ret.update(self.additional_vars)
|
||||
return ret
|
||||
|
||||
def copy_build_resource_file_from_package(self, name):
|
||||
my_data = resource_string(__name__, "src/main/resources/terraform/" + name)
|
||||
with open(self.build_path() + "/" + name, "w", encoding="utf-8") as output_file:
|
||||
output_file.write(my_data.decode(sys.stdout.encoding))
|
||||
|
||||
def copy_build_resources_from_package(self):
|
||||
self.copy_build_resource_file_from_package("versions.tf")
|
||||
self.copy_build_resource_file_from_package("terraform_build_vars.tf")
|
||||
|
||||
def copy_build_resources_from_dir(self):
|
||||
run(
|
||||
"cp -f " + self.terraform_build_commons_path() + "* " + self.build_path(),
|
||||
shell=True,
|
||||
check=False,
|
||||
)
|
||||
|
||||
def copy_local_state(self):
|
||||
run("cp terraform.tfstate " + self.build_path(), shell=True, check=False)
|
||||
|
||||
def rescue_local_state(self):
|
||||
run("cp " + self.build_path() + "/terraform.tfstate .", shell=True, check=False)
|
||||
self.teraform_service = Terraform.prod()
|
||||
|
||||
def initialize_build_dir(self):
|
||||
super().initialize_build_dir()
|
||||
if self.use_package_common_files:
|
||||
self.copy_build_resources_from_package()
|
||||
else:
|
||||
self.copy_build_resources_from_dir()
|
||||
self.copy_local_state()
|
||||
run("cp *.tf " + self.build_path(), shell=True, check=False)
|
||||
run("cp *.properties " + self.build_path(), shell=True, check=False)
|
||||
run("cp *.tfvars " + self.build_path(), shell=True, check=False)
|
||||
run("cp -r scripts " + self.build_path(), shell=True, check=False)
|
||||
devops = self.devops_repo.get_devops(self.project)
|
||||
self.teraform_service.initialize_build_dir(devops)
|
||||
|
||||
def post_build(self):
|
||||
self.rescue_local_state()
|
||||
devops = self.devops_repo.get_devops(self.project)
|
||||
self.teraform_service.rescue_local_state(devops)
|
||||
|
||||
def init_client(self):
|
||||
terraform = Terraform(
|
||||
|
|
Loading…
Reference in a new issue