From d78714f84f70e5aeee2cf978ee65c5f4f48c1d11 Mon Sep 17 00:00:00 2001 From: Bryan Barajas Date: Wed, 14 Feb 2024 18:53:25 +0000 Subject: [PATCH] FIX: edb-terraform cli - Avoid destroying projects with remote backends. We should always run 'terraform state list' to see if a remote backend is set. --- edbterraform/CLI.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/edbterraform/CLI.py b/edbterraform/CLI.py index 009cd839..1bdcda58 100644 --- a/edbterraform/CLI.py +++ b/edbterraform/CLI.py @@ -149,19 +149,26 @@ def destroy_command(self, cwd): if not (cwd / 'terraform.tfstate').exists(): raise IOError('terraform.tfstate not found.') - if (cwd / 'terraform.tfstate').stat().st_size == 0: - logger.info('terraform.tfstate is empty, no destruction needed') - return True - terraform_path = self.get_compatible_terraform() command = [terraform_path, 'state', 'list',] - output = execute_shell( - args=command, - environment=os.environ.copy(), - cwd=cwd, + fmt_args = ' '.join([str(x) for x in command]) + logger.info("Executing command: %s", fmt_args) + process = subprocess.run( + fmt_args, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + shell=True, + cwd=cwd, + env=os.environ.copy(), ) - if len(output.decode("utf-8").split('\n'))-1 == 0: + if "No state file was found!" in process.stderr.decode("utf-8") \ + or "Backend initialization required" in process.stderr.decode("utf-8") \ + and (cwd / 'terraform.tfstate').stat().st_size == 0: + logger.info('Backend not initialized, no resource destruction needed') + return True + + if len(process.stdout.decode("utf-8").split('\n'))-1 == 0 and process.returncode == 0: logger.info('state list return 0 results, no destruction needed') return True