Skip to content

Commit

Permalink
FIX: edb-terraform cli - Avoid destroying projects with remote backen…
Browse files Browse the repository at this point in the history
…ds. We should always run 'terraform state list' to see if a remote backend is set.
  • Loading branch information
bryan-bar committed Feb 15, 2024
1 parent f81483a commit d78714f
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions edbterraform/CLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d78714f

Please sign in to comment.