From 33e318368a1a3c9eb360baff5c8ffbd00856094b Mon Sep 17 00:00:00 2001 From: jconstance Date: Sun, 14 Jan 2024 12:08:34 -0500 Subject: [PATCH] Fix: Add Support For Pushing State if Error While Pushing State --- bin/tf | 21 +++++++++++++++++++++ terrawrap/version.py | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/bin/tf b/bin/tf index 665e42b..9bc2715 100755 --- a/bin/tf +++ b/bin/tf @@ -138,6 +138,9 @@ def exec_tf_command( elif "state data in S3 does not have the expected content" in error: if update_digest(error, path, variables): continue + elif "Error: Failed to persist state to backend" in error: + if repush_state(path): + continue sys.exit(exit_code) @@ -218,6 +221,24 @@ def update_digest(error: str, path: str, variables: Dict[str, str]) -> bool: return False +def repush_state(path: str) -> bool: + """ + Repush terraform state if there was an error pushing it originally + :param path: Terraform path with errored state + :return: True if state is pushed successfully, false otherwise + """ + exit_code, output = execute_command( + ["terraform", "state", "push", "errored.tfstate"], + print_command=True, + cwd=path, + retry=True, + ) + if exit_code == 0: + return True + + return False + + def process_arguments(args: List[str]) -> Tuple[str, str, List[str], bool]: try: resolve_envvars = True diff --git a/terrawrap/version.py b/terrawrap/version.py index ea872ba..7f40072 100644 --- a/terrawrap/version.py +++ b/terrawrap/version.py @@ -1,4 +1,4 @@ """Place of record for the package version""" -__version__ = "0.9.31" +__version__ = "0.9.32" __git_hash__ = "GIT_HASH"