fix terraform-local-state handling

This commit is contained in:
jem 2020-05-08 19:53:06 +02:00
parent 8715d2bab3
commit ebfa2e5ecd
2 changed files with 22 additions and 2 deletions

View file

@ -30,6 +30,12 @@ class AwsBackendPropertiesMixin(DevopsTerraformBuild):
self.copy_build_resource_file_from_package( self.copy_build_resource_file_from_package(
'aws_backend_with_properties.tf') 'aws_backend_with_properties.tf')
def copy_local_state(self):
pass
def rescue_local_state(self):
pass
def init_client(self): def init_client(self):
tf = WorkaroundTerraform(working_dir=self.build_path()) tf = WorkaroundTerraform(working_dir=self.build_path())
tf.init(backend_config=self.backend_config) tf.init(backend_config=self.backend_config)

View file

@ -94,16 +94,26 @@ class DevopsTerraformBuild(DevopsBuild):
run('cp -f ' + self.terraform_build_commons_path() + run('cp -f ' + self.terraform_build_commons_path() +
'* ' + self.build_path(), shell=True) '* ' + self.build_path(), shell=True)
def copy_local_state(self):
run('cp terraform.tfstate ' + self.build_path(), shell=True)
def rescue_local_state(self):
run('cp ' + self.build_path() + '/terraform.tfstate .', shell=True)
def initialize_build_dir(self): def initialize_build_dir(self):
super().initialize_build_dir() super().initialize_build_dir()
if self.use_package_common_files: if self.use_package_common_files:
self.copy_build_resources_from_package() self.copy_build_resources_from_package()
else: else:
self.copy_build_resources_from_dir() self.copy_build_resources_from_dir()
self.copy_local_state()
run('cp *.tf ' + self.build_path(), shell=True) run('cp *.tf ' + self.build_path(), shell=True)
run('cp *.properties ' + self.build_path(), shell=True) run('cp *.properties ' + self.build_path(), shell=True)
run('cp *.tfvars ' + self.build_path(), shell=True) run('cp *.tfvars ' + self.build_path(), shell=True)
def post_build(self):
self.rescue_local_state()
def init_client(self): def init_client(self):
tf = WorkaroundTerraform(working_dir=self.build_path()) tf = WorkaroundTerraform(working_dir=self.build_path())
tf.init() tf.init()
@ -132,6 +142,7 @@ class DevopsTerraformBuild(DevopsBuild):
tf.plan(capture_output=False, raise_on_error=False, tf.plan(capture_output=False, raise_on_error=False,
var=self.project_vars(), var=self.project_vars(),
var_file=self.additional_tfvar_files) var_file=self.additional_tfvar_files)
self.post_build()
self.print_terraform_command(tf) self.print_terraform_command(tf)
def apply(self, auto_approve=False): def apply(self, auto_approve=False):
@ -140,8 +151,9 @@ class DevopsTerraformBuild(DevopsBuild):
skip_plan=auto_approve, skip_plan=auto_approve,
var=self.project_vars(), var=self.project_vars(),
var_file=self.additional_tfvar_files) var_file=self.additional_tfvar_files)
self.print_terraform_command(tf)
self.write_output(tf) self.write_output(tf)
self.post_build()
self.print_terraform_command(tf)
def destroy(self, auto_approve=False): def destroy(self, auto_approve=False):
tf = self.init_client() tf = self.init_client()
@ -153,6 +165,7 @@ class DevopsTerraformBuild(DevopsBuild):
force=force, force=force,
var=self.project_vars(), var=self.project_vars(),
var_file=self.additional_tfvar_files) var_file=self.additional_tfvar_files)
self.post_build()
self.print_terraform_command(tf) self.print_terraform_command(tf)
def tf_import(self, tf_import_name, tf_import_resource,): def tf_import(self, tf_import_name, tf_import_resource,):
@ -161,6 +174,7 @@ class DevopsTerraformBuild(DevopsBuild):
capture_output=False, raise_on_error=True, capture_output=False, raise_on_error=True,
var=self.project_vars(), var=self.project_vars(),
var_file=self.additional_tfvar_files) var_file=self.additional_tfvar_files)
self.post_build()
self.print_terraform_command(tf) self.print_terraform_command(tf)
def print_terraform_command(self, tf): def print_terraform_command(self, tf):