Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add --retry-once-with-cleanup to terraform_validate #441

Merged
merged 43 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
a7e3d90
feat: Add --retry-once-with-cleanup to `terraform_validate`
baolsen Oct 21, 2022
c103542
Update 1
baolsen Oct 28, 2022
6cf6f32
Update 2
baolsen Nov 4, 2022
1541f3b
Fix README
MaxymVlasov Nov 4, 2022
de51a54
Clarify when jq needed. Without it - adding jq dep - is BREAKING CHANGE
MaxymVlasov Nov 4, 2022
0e556ed
Set constant warning-note style accross README
MaxymVlasov Nov 4, 2022
d99140c
Update 3
baolsen Nov 18, 2022
2e94853
Deduplicate code
MaxymVlasov Nov 24, 2022
4699596
Improve readability, remove magic numbers
MaxymVlasov Nov 24, 2022
a756cc6
Apply suggestions from code review
MaxymVlasov Nov 24, 2022
a65aaab
Fixup
MaxymVlasov Nov 24, 2022
3d758f5
fixup
MaxymVlasov Nov 24, 2022
e83def1
fixup
MaxymVlasov Nov 24, 2022
8a60156
fixup
MaxymVlasov Nov 24, 2022
b3e420b
Add tf init
MaxymVlasov Nov 24, 2022
c0d169d
fixup
MaxymVlasov Nov 24, 2022
d476447
Simplify codebase
MaxymVlasov Nov 24, 2022
7574616
f
MaxymVlasov Nov 24, 2022
c2d5c42
f
MaxymVlasov Nov 24, 2022
7a7a621
debug
MaxymVlasov Nov 24, 2022
c23cb25
t
MaxymVlasov Nov 24, 2022
bd66aff
t
MaxymVlasov Nov 24, 2022
0887cd8
drbug
MaxymVlasov Nov 24, 2022
b0800b2
debug
MaxymVlasov Nov 24, 2022
39604c4
remove debug and useless
MaxymVlasov Nov 24, 2022
3171311
Add tf re-init
MaxymVlasov Nov 24, 2022
a13ecf9
debug
MaxymVlasov Nov 24, 2022
2bdf052
debug
MaxymVlasov Nov 24, 2022
ac70245
debug
MaxymVlasov Nov 24, 2022
aba9811
Update tf init logic
MaxymVlasov Nov 24, 2022
89ecd5a
Show tf init
MaxymVlasov Nov 24, 2022
ae6d258
Remove debug and fixup
MaxymVlasov Nov 24, 2022
4231f76
debug
MaxymVlasov Nov 24, 2022
15cafcd
Relative path?
MaxymVlasov Nov 24, 2022
809e2ee
remove useless
MaxymVlasov Nov 24, 2022
632bccd
Update hooks/_common.sh
MaxymVlasov Nov 24, 2022
955cb58
Apply suggestions from code review
MaxymVlasov Nov 24, 2022
29086b8
Fixups
baolsen Nov 25, 2022
537c49f
Minor language fix
MaxymVlasov Nov 25, 2022
b52b475
Apply suggestions from code review
MaxymVlasov Nov 25, 2022
4464e13
Stop execution if more than one flag specified
MaxymVlasov Nov 25, 2022
3a12949
Revert "Stop execution if more than one flag specified"
MaxymVlasov Nov 25, 2022
b15168d
Stop execution if more than one flag specified
MaxymVlasov Nov 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand All @@ -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.

Expand Down
48 changes: 44 additions & 4 deletions hooks/terraform_validate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved

IFS=";" read -r -a configs <<< "${HOOK_CONFIG[*]}"
baolsen marked this conversation as resolved.
Show resolved Hide resolved

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
MaxymVlasov marked this conversation as resolved.
Show resolved Hide resolved
;;
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
baolsen marked this conversation as resolved.
Show resolved Hide resolved
}

# 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"
Copy link
Collaborator

@MaxymVlasov MaxymVlasov Oct 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to display that message in all cases. Please add to README example "verbose: true with the related code-comment about displaying that msg

rm -r .terraform
do_validate
baolsen marked this conversation as resolved.
Show resolved Hide resolved
exit_code=$?
fi
fi

if [ $exit_code -ne 0 ]; then
common::colorify "red" "Validation failed: $dir_path"
echo -e "$validate_output\n\n"
Expand Down