make local methods more private

This commit is contained in:
Michael Jerger 2023-05-31 08:52:19 +02:00
parent 74027d5d6f
commit e2fd3b1eb9
3 changed files with 77 additions and 84 deletions

View file

@ -18,12 +18,8 @@ classDiagram
copy_build_resource_file_from_package(name) copy_build_resource_file_from_package(name)
copy_build_resources_from_package() copy_build_resources_from_package()
copy_build_resources_from_dir() copy_build_resources_from_dir()
copy_local_state()
rescue_local_state()
initialize_build_dir() initialize_build_dir()
post_build() post_build()
init_client()
write_output(terraform)
read_output_json() read_output_json()
plan() plan()
plan_fail_on_diff() plan_fail_on_diff()
@ -49,13 +45,11 @@ classDiagram
class AwsBackendPropertiesMixin { class AwsBackendPropertiesMixin {
def project_vars() def project_vars()
copy_build_resources_from_package() copy_build_resources_from_package()
init_client()
} }
class DigitaloceanBackendPropertiesMixin { class DigitaloceanBackendPropertiesMixin {
project_vars(self) project_vars(self)
copy_build_resources_from_package(self) copy_build_resources_from_package(self)
init_client(self)
} }
class DevopsImageBuild { class DevopsImageBuild {

View file

@ -23,87 +23,18 @@ class TerraformService:
TerraformApi(), TerraformApi(),
) )
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):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
for resource in terraform_domain.resources_from_package():
self.__copy_build_resource_file_from_package__(resource, 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 __print_terraform_command__(self, terraform: Terraform, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
if terraform_domain.tf_debug_print_terraform_command:
output = f"cd {devops.build_path()} && {terraform.latest_cmd()}"
print(output)
# TODO: internal?
def copy_local_state(self, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
if terraform_domain.is_local_state():
self.file_api.cp("terraform.tfstate", devops.build_path(), check=False)
# TODO: internal?
def rescue_local_state(self, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
if terraform_domain.is_local_state():
self.file_api.cp(
f"{devops.build_path()}/terraform.tfstate", ".", check=False
)
def initialize_build_dir(self, devops: Devops): def initialize_build_dir(self, devops: Devops):
terraform = devops.specialized_builds[BuildType.TERRAFORM] terraform = devops.specialized_builds[BuildType.TERRAFORM]
if terraform.tf_use_package_common_files: if terraform.tf_use_package_common_files:
self.__copy_build_resources_from_package__(devops) self.__copy_build_resources_from_package__(devops)
else: else:
self.__copy_build_resources_from_dir__(devops) self.__copy_build_resources_from_dir__(devops)
self.copy_local_state(devops) self.__copy_local_state__(devops)
self.file_api.cp("*.tf", devops.build_path(), check=False) self.file_api.cp("*.tf", devops.build_path(), check=False)
self.file_api.cp("*.properties", devops.build_path(), check=False) self.file_api.cp("*.properties", devops.build_path(), check=False)
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)
# TODO: internal?
def init_client(self, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
terraform = Terraform(
working_dir=devops.build_path(),
terraform_semantic_version=terraform_domain.tf_terraform_semantic_version,
)
if terraform_domain.is_local_state():
terraform.init()
else:
terraform.init(backend_config=terraform_domain.backend_config())
self.__print_terraform_command__(terraform, devops)
if terraform_domain.tf_use_workspace:
try:
terraform.workspace("select", devops.stage)
self.__print_terraform_command__(terraform, devops)
except:
terraform.workspace("new", devops.stage)
self.__print_terraform_command__(terraform, devops)
return terraform
# TODO: internal?
def write_output(self, terraform, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
result = terraform.output(json=IsFlagged)
self.__print_terraform_command__(terraform, devops)
self.file_api.write_json_to_file(
Path(f"{devops.build_path()}{terraform_domain.tf_output_json_name}"), result
)
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(
@ -116,7 +47,7 @@ class TerraformService:
detailed_exitcode = IsFlagged detailed_exitcode = IsFlagged
else: else:
detailed_exitcode = None detailed_exitcode = None
terraform = self.init_client(devops) terraform = self.__init_client__(devops)
return_code, _, stderr = terraform.plan( return_code, _, stderr = terraform.plan(
detailed_exitcode=detailed_exitcode, detailed_exitcode=detailed_exitcode,
capture_output=False, capture_output=False,
@ -136,7 +67,7 @@ class TerraformService:
auto_approve_flag = IsFlagged auto_approve_flag = IsFlagged
else: else:
auto_approve_flag = None auto_approve_flag = None
terraform = self.init_client(devops) terraform = self.__init_client__(devops)
if version.parse( if version.parse(
terraform_domain.tf_terraform_semantic_version terraform_domain.tf_terraform_semantic_version
) >= version.parse("1.0.0"): ) >= version.parse("1.0.0"):
@ -158,11 +89,11 @@ class TerraformService:
self.__print_terraform_command__(terraform, devops) self.__print_terraform_command__(terraform, devops)
if return_code > 0: if return_code > 0:
raise RuntimeError(return_code, "terraform error:", stderr) raise RuntimeError(return_code, "terraform error:", stderr)
self.write_output(terraform, devops) self.__write_output__(terraform, devops)
def refresh(self, devops: Devops): def refresh(self, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
terraform = self.init_client(devops) terraform = self.__init_client__(devops)
return_code, _, stderr = terraform.refresh( return_code, _, stderr = terraform.refresh(
var=terraform_domain.project_vars(), var=terraform_domain.project_vars(),
var_file=terraform_domain.tf_additional_tfvar_files, var_file=terraform_domain.tf_additional_tfvar_files,
@ -170,7 +101,7 @@ class TerraformService:
self.__print_terraform_command__(terraform, devops) self.__print_terraform_command__(terraform, devops)
if return_code > 0: if return_code > 0:
raise RuntimeError(return_code, "terraform error:", stderr) raise RuntimeError(return_code, "terraform error:", stderr)
self.write_output(terraform, devops) self.__write_output__(terraform, devops)
def destroy(self, devops: Devops, auto_approve=False): def destroy(self, devops: Devops, auto_approve=False):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
@ -178,7 +109,7 @@ class TerraformService:
auto_approve_flag = IsFlagged auto_approve_flag = IsFlagged
else: else:
auto_approve_flag = None auto_approve_flag = None
terraform = self.init_client(devops) terraform = self.__init_client__(devops)
if version.parse( if version.parse(
terraform_domain.tf_terraform_semantic_version terraform_domain.tf_terraform_semantic_version
) >= version.parse("1.0.0"): ) >= version.parse("1.0.0"):
@ -208,7 +139,7 @@ class TerraformService:
tf_import_resource, tf_import_resource,
): ):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM] terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
terraform = self.init_client(devops) terraform = self.__init_client__(devops)
return_code, _, stderr = terraform.import_cmd( return_code, _, stderr = terraform.import_cmd(
tf_import_name, tf_import_name,
tf_import_resource, tf_import_resource,
@ -220,3 +151,71 @@ class TerraformService:
self.__print_terraform_command__(terraform, devops) self.__print_terraform_command__(terraform, devops)
if return_code > 0: if return_code > 0:
raise RuntimeError(return_code, "terraform error:", stderr) raise RuntimeError(return_code, "terraform error:", stderr)
def post_build(self, devops: Devops):
self.__rescue_local_state__(devops)
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):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
for resource in terraform_domain.resources_from_package():
self.__copy_build_resource_file_from_package__(resource, 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 __print_terraform_command__(self, terraform: Terraform, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
if terraform_domain.tf_debug_print_terraform_command:
output = f"cd {devops.build_path()} && {terraform.latest_cmd()}"
print(output)
def __copy_local_state__(self, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
if terraform_domain.is_local_state():
self.file_api.cp("terraform.tfstate", devops.build_path(), check=False)
def __rescue_local_state__(self, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
if terraform_domain.is_local_state():
self.file_api.cp(
f"{devops.build_path()}/terraform.tfstate", ".", check=False
)
def __init_client__(self, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
terraform = Terraform(
working_dir=devops.build_path(),
terraform_semantic_version=terraform_domain.tf_terraform_semantic_version,
)
if terraform_domain.is_local_state():
terraform.init()
else:
terraform.init(backend_config=terraform_domain.backend_config())
self.__print_terraform_command__(terraform, devops)
if terraform_domain.tf_use_workspace:
try:
terraform.workspace("select", devops.stage)
self.__print_terraform_command__(terraform, devops)
except:
terraform.workspace("new", devops.stage)
self.__print_terraform_command__(terraform, devops)
return terraform
def __write_output__(self, terraform, devops: Devops):
terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
result = terraform.output(json=IsFlagged)
self.__print_terraform_command__(terraform, devops)
self.file_api.write_json_to_file(
Path(f"{devops.build_path()}{terraform_domain.tf_output_json_name}"), result
)

View file

@ -22,7 +22,7 @@ class DevopsTerraformBuild(DevopsBuild):
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.rescue_local_state(devops) self.teraform_service.post_build(devops)
def read_output_json(self) -> map: def read_output_json(self) -> map:
devops = self.devops_repo.get_devops(self.project) devops = self.devops_repo.get_devops(self.project)