use enhanced terraform structure

This commit is contained in:
Michael Jerger 2023-05-25 16:48:05 +02:00
parent 4cf3ec8460
commit 85aa41b2a8
4 changed files with 85 additions and 52 deletions

View file

@ -32,10 +32,9 @@ class TerraformService:
) )
def __copy_build_resources_from_package__(self, devops: Devops): def __copy_build_resources_from_package__(self, devops: Devops):
self.__copy_build_resource_file_from_package__("versions.tf", devops) terraform_domain = devops.specialized_builds[BuildType.TERRAFORM]
self.__copy_build_resource_file_from_package__( for resource in terraform_domain.resources_from_package():
"terraform_build_vars.tf", devops self.__copy_build_resource_file_from_package__(resource, devops)
)
def __copy_build_resources_from_dir__(self, devops: Devops): def __copy_build_resources_from_dir__(self, devops: Devops):
terraform = devops.specialized_builds[BuildType.TERRAFORM] terraform = devops.specialized_builds[BuildType.TERRAFORM]

View file

@ -1,63 +1,84 @@
from .devops_terraform_build import DevopsTerraformBuild, create_devops_terraform_build_config from .devops_terraform_build import (
DevopsTerraformBuild,
create_devops_terraform_build_config,
)
def create_digitalocean_terraform_build_config(stage, def create_digitalocean_terraform_build_config(
project_root_path, stage,
module, project_root_path,
additional_vars, module,
do_api_key, additional_vars,
do_spaces_access_id, do_api_key,
do_spaces_secret_key, do_spaces_access_id,
build_dir_name='target', do_spaces_secret_key,
output_json_name=None, build_dir_name="target",
use_workspace=True, output_json_name=None,
use_package_common_files=True, use_workspace=True,
build_commons_path=None, use_package_common_files=True,
terraform_build_commons_dir_name='terraform', build_commons_path=None,
debug_print_terraform_command=False, terraform_build_commons_dir_name="terraform",
additional_tfvar_files=None, debug_print_terraform_command=False,
terraform_semantic_version="1.0.8", additional_tfvar_files=None,
): terraform_semantic_version="1.0.8",
):
if not additional_tfvar_files: if not additional_tfvar_files:
additional_tfvar_files = [] additional_tfvar_files = []
config = create_devops_terraform_build_config(stage, config = create_devops_terraform_build_config(
project_root_path, stage,
module, project_root_path,
additional_vars, module,
build_dir_name, additional_vars,
output_json_name, build_dir_name,
use_workspace, output_json_name,
use_package_common_files, use_workspace,
build_commons_path, use_package_common_files,
terraform_build_commons_dir_name, build_commons_path,
debug_print_terraform_command, terraform_build_commons_dir_name,
additional_tfvar_files, debug_print_terraform_command,
terraform_semantic_version) additional_tfvar_files,
config.update({'DigitaloceanTerraformBuild': terraform_semantic_version,
{'do_api_key': do_api_key, )
'do_spaces_access_id': do_spaces_access_id, config.update(
'do_spaces_secret_key': do_spaces_secret_key}}) {
"DigitaloceanTerraformBuild": {
"do_api_key": do_api_key,
"do_spaces_access_id": do_spaces_access_id,
"do_spaces_secret_key": do_spaces_secret_key,
}
}
)
return config return config
class DigitaloceanTerraformBuild(DevopsTerraformBuild): class DigitaloceanTerraformBuild(DevopsTerraformBuild):
def __init__(self, project, config): def __init__(self, project, config):
additional_resources = config.get("tf_additional_resources_from_package", [])
additional_resources += [
"provider_registry.tf",
"do_provider.tf",
"do_mixin_vars.tf",
]
config["tf_additional_resources_from_package"] = additional_resources
additional_vars = config.get("tf_additional_vars", {})
additional_vars.update(
{
"do_api_key": config.get("do_api_key"),
"do_spaces_access_id": config.get("do_spaces_access_id"),
"do_spaces_secret_key": config.get("do_spaces_secret_key"),
}
)
super().__init__(project, config) super().__init__(project, config)
do_mixin_config = config['DigitaloceanTerraformBuild']
self.do_api_key = do_mixin_config['do_api_key'] self.terraform_service = TerraformService.prod()
self.do_spaces_access_id = do_mixin_config['do_spaces_access_id']
self.do_spaces_secret_key = do_mixin_config['do_spaces_secret_key']
def project_vars(self): def project_vars(self):
ret = super().project_vars() ret = super().project_vars()
ret['do_api_key'] = self.do_api_key ret["do_api_key"] = self.do_api_key
ret['do_spaces_access_id'] = self.do_spaces_access_id ret["do_spaces_access_id"] = self.do_spaces_access_id
ret['do_spaces_secret_key'] = self.do_spaces_secret_key ret["do_spaces_secret_key"] = self.do_spaces_secret_key
return ret return ret
def copy_build_resources_from_package(self): def copy_build_resources_from_package(self):
super().copy_build_resources_from_package() super().copy_build_resources_from_package()
self.copy_build_resource_file_from_package('provider_registry.tf')
self.copy_build_resource_file_from_package('do_provider.tf')
self.copy_build_resource_file_from_package('do_mixin_vars.tf')

View file

@ -63,6 +63,19 @@ class InitService:
"name": "image_dockerhub_password", "name": "image_dockerhub_password",
}, },
] ]
if BuildType.DIGITALOCEAN_TERRAFORM in build_types:
default_mappings += [
{
"gopass_path": "server/devops/digitalocean/s3",
"gopass_field": "id",
"name": "do_spaces_access_id",
},
{
"gopass_path": "server/devops/digitalocean/s3",
"gopass_field": "secret",
"name": "do_spaces_secret_key",
},
]
if MixinType.RELEASE in mixin_types: if MixinType.RELEASE in mixin_types:
primary_build_file_id = inp.get( primary_build_file_id = inp.get(

View file

@ -24,14 +24,14 @@ def devops_config(overrides: dict) -> dict:
"k3s_letsencrypt_endpoint": "k3s_letsencrypt_endpoint", "k3s_letsencrypt_endpoint": "k3s_letsencrypt_endpoint",
"k3s_enable_echo": "false", "k3s_enable_echo": "false",
"k3s_app_filename_to_provision": "k3s_app.yaml", "k3s_app_filename_to_provision": "k3s_app.yaml",
"tf_additional_vars": None, "tf_additional_vars": [],
"tf_output_json_name": "the_out.json", "tf_output_json_name": "the_out.json",
"tf_use_workspace": None, "tf_use_workspace": None,
"tf_use_package_common_files": None, "tf_use_package_common_files": None,
"tf_build_commons_path": "build_commons_path", "tf_build_commons_path": "build_commons_path",
"tf_build_commons_dir_name": "terraform", "tf_build_commons_dir_name": "terraform",
"tf_debug_print_terraform_command": None, "tf_debug_print_terraform_command": None,
"tf_additional_tfvar_files": None, "tf_additional_tfvar_files": [],
"tf_terraform_semantic_version": None, "tf_terraform_semantic_version": None,
"do_api_key": "api_key", "do_api_key": "api_key",
"do_spaces_access_id": "spaces_id", "do_spaces_access_id": "spaces_id",