From a7e3d90daeac7b74b7231a68078cec614c8aae3d Mon Sep 17 00:00:00 2001 From: Bjorn Olsen Date: Fri, 21 Oct 2022 09:39:42 +0200 Subject: [PATCH] feat: Add --retry-once-with-cleanup to `terraform_validate` --- README.md | 14 +++++++++-- hooks/terraform_validate.sh | 48 +++++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 82c04dad6..da51720ea 100644 --- a/README.md +++ b/README.md @@ -652,7 +652,17 @@ Example: - --tf-init-args=-lockfile=readonly ``` -3. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc.). To solve this problem, you can find and delete all `.terraform` directories in your repository: +3. It may happen that Terraform working directory (`.terraform`) already exists but not in the best condition (eg, not initialized modules, wrong version of Terraform, etc.). To solve this problem, you can delete broken `.terraform` directories in your repository: + + ```yaml + - id: terraform_validate + args: + - --hook-config=--retry-once-with-cleanup=true # Boolean. true or false + ``` + + If `--retry-once-with-cleanup=true`, then in each failed directory the `.terraform` directory will first be deleted before retrying once more. + + An alternative solution is to find and delete all `.terraform` directories in your repository: ```bash echo " @@ -666,7 +676,7 @@ Example: `terraform_validate` hook will try to reinitialize them before running the `terraform validate` command. - **Warning:** If you use Terraform workspaces, DO NOT use this workaround ([details](https://github.com/antonbabenko/pre-commit-terraform/issues/203#issuecomment-918791847)). Wait to [`force-init`](https://github.com/antonbabenko/pre-commit-terraform/issues/224) option implementation. + **Warning:** If you use Terraform workspaces, DO NOT use these workarounds ([details](https://github.com/antonbabenko/pre-commit-terraform/issues/203#issuecomment-918791847)). Wait to [`force-init`](https://github.com/antonbabenko/pre-commit-terraform/issues/224) option implementation. 4. `terraform_validate` in a repo with Terraform module, written using Terraform 0.15+ and which uses provider `configuration_aliases` ([Provider Aliases Within Modules](https://www.terraform.io/language/modules/develop/providers#provider-aliases-within-modules)), errors out. diff --git a/hooks/terraform_validate.sh b/hooks/terraform_validate.sh index cd23a52a8..3ba568ef8 100755 --- a/hooks/terraform_validate.sh +++ b/hooks/terraform_validate.sh @@ -43,17 +43,57 @@ function per_dir_hook_unique_part { local -a -r args=("$@") local exit_code - local validate_output - common::terraform_init 'terraform validate' "$dir_path" || { + # + # Get hook settings + # + local retry_once_with_cleanup=false + + IFS=";" read -r -a configs <<< "${HOOK_CONFIG[*]}" + + for c in "${configs[@]}"; do + + IFS="=" read -r -a config <<< "$c" + key=${config[0]} + value=${config[1]} + + case $key in + --retry-once-with-cleanup) + retry_once_with_cleanup=$value + ;; + esac + done + + function do_validate { + + local exit_code + local validate_output + + common::terraform_init 'terraform validate' "$dir_path" || { + exit_code=$? + return $exit_code + } + + # pass the arguments to hook + validate_output=$(terraform validate "${args[@]}" 2>&1) exit_code=$? + return $exit_code } - # pass the arguments to hook - validate_output=$(terraform validate "${args[@]}" 2>&1) + do_validate exit_code=$? + if [ $exit_code -ne 0 ] && [ "$retry_once_with_cleanup" = true ]; then + if [ -d .terraform ]; then + # Will only be displayed if validation fails again. + common::colorify "yellow" "Validation failed. Re-initialising: $dir_path" + rm -r .terraform + do_validate + exit_code=$? + fi + fi + if [ $exit_code -ne 0 ]; then common::colorify "red" "Validation failed: $dir_path" echo -e "$validate_output\n\n"