Skip to content

Commit d69e86d

Browse files
feat: Add new hook for terraform providers lock operation (antonbabenko#173)
1 parent f7e28e1 commit d69e86d

File tree

4 files changed

+138
-1
lines changed

4 files changed

+138
-1
lines changed

.pre-commit-hooks.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
files: (\.tf|\.tfvars)$
4343
exclude: \.terraform\/.*$
4444

45+
- id: terraform_providers_lock
46+
name: Lock terraform provider versions
47+
description: Updates provider signatures in dependency lock files.
48+
require_serial: true
49+
entry: terraform_providers_lock.sh
50+
language: script
51+
files: (\.terraform\.lock\.hcl)$
52+
exclude: \.terraform\/.*$
53+
4554
- id: terraform_tflint
4655
name: Terraform validate with tflint
4756
description: Validates all Terraform configuration files with TFLint.

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ All notable changes to this project will be documented in this file.
164164

165165
- fix: Change terraform_validate hook functionality for subdirectories with terraform files ([#100](https://github.com/antonbabenko/pre-commit-terraform/issues/100))
166166

167-
###
167+
###
168168

169169
configuration for the appropriate working directory.
170170

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Want to Contribute? Check [open issues](https://github.com/antonbabenko/pre-comm
1414
* [checkov](#checkov)
1515
* [terraform_docs](#terraform_docs)
1616
* [terraform_docs_replace](#terraform_docs_replace)
17+
* [terraform_providers_lock](#terraform_providers_lock)
1718
* [terraform_tflint](#terraform_tflint)
1819
* [terraform_tfsec](#terraform_tfsec)
1920
* [terraform_validate](#terraform_validate)
@@ -183,6 +184,7 @@ There are several [pre-commit](https://pre-commit.com/) hooks to keep Terraform
183184
| `terraform_docs_without_aggregate_type_defaults` | Inserts input and output documentation into `README.md` without aggregate type defaults. Hook notes same as for [terraform_docs](#terraform_docs) |
184185
| `terraform_docs` | Inserts input and output documentation into `README.md`. Recommended. [Hook notes](#terraform_docs) |
185186
| `terraform_fmt` | Rewrites all Terraform configuration files to a canonical format. [Hook notes](#terraform_docs) |
187+
| `terraform_providers_lock` | Updates provider signatures in [dependency lock files](https://www.terraform.io/docs/cli/commands/providers/lock.html). [Hook notes](#terraform_providers_lock)
186188
| `terraform_tflint` | Validates all Terraform configuration files with [TFLint](https://github.com/terraform-linters/tflint). [Available TFLint rules](https://github.com/terraform-linters/tflint/tree/master/docs/rules#rules). [Hook notes](#terraform_tflint). |
187189
| `terraform_tfsec` | [TFSec](https://github.com/liamg/tfsec) static analysis of terraform templates to spot potential security issues. [Hook notes](#terraform_tfsec) |
188190
| `terraform_validate` | Validates all Terraform configuration files. [Hook notes](#terraform_validate) |
@@ -342,6 +344,44 @@ Example:
342344
343345
**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
344346
347+
### terraform_providers_lock
348+
349+
1. The hook requires Terraform 0.14 or later.
350+
351+
1. The hook invokes two operations that can be really slow:
352+
`terraform init` (in case `.terraform` directory is not initialised)
353+
and `terraform providers lock`. Both operations require downloading
354+
data from remote Terraform registries, and not all of that
355+
downloaded data or meta-data is currently being cached by Terraform.
356+
357+
1. `terraform_providers_lock` supports custom arguments.
358+
359+
Example:
360+
361+
```yaml
362+
hooks:
363+
- id: terraform_providers_lock
364+
args: ['--args=-platform=windows_amd64']
365+
```
366+
367+
In order to pass multiple args, try the following:
368+
369+
```yaml
370+
- id: terraform_providers_lock
371+
args:
372+
- '--args=-platform=windows_amd64'
373+
- '--args=-platform=darwin_amd64'
374+
```
375+
376+
1. It may happen that Terraform working directory (`.terraform`) already exists but is outdated
377+
(e.g. not initialized modules, wrong version of Terraform, etc).
378+
To solve this problem you can find and delete all `.terraform` directories in your repository using this command:
379+
380+
```shell
381+
find . -type d -name .terraform -prune -print -exec rm -rf {} \;
382+
```
383+
384+
`terraform_providers_lock` hook will try to reinitialize them before running `terraform providers lock` command.
345385
346386
## Authors
347387

terraform_providers_lock.sh

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#!/usr/bin/env bash
2+
3+
set -eo pipefail
4+
5+
main() {
6+
initialize_
7+
parse_cmdline_ "$@"
8+
terraform_providers_lock_
9+
}
10+
11+
initialize_() {
12+
# get directory containing this script
13+
local dir
14+
local source
15+
source="${BASH_SOURCE[0]}"
16+
while [[ -L $source ]]; do # resolve $source until the file is no longer a symlink
17+
dir="$(cd -P "$(dirname "$source")" > /dev/null && pwd)"
18+
source="$(readlink "$source")"
19+
# if $source was a relative symlink, we need to resolve it relative to the path where the symlink file was located
20+
[[ $source != /* ]] && source="$dir/$source"
21+
done
22+
_SCRIPT_DIR="$(dirname "$source")"
23+
24+
# source getopt function
25+
# shellcheck source=lib_getopt
26+
. "$_SCRIPT_DIR/lib_getopt"
27+
}
28+
29+
parse_cmdline_() {
30+
declare argv
31+
argv=$(getopt -o a: --long args: -- "$@") || return
32+
eval "set -- $argv"
33+
34+
for argv; do
35+
case $argv in
36+
-a | --args)
37+
shift
38+
ARGS+=("$1")
39+
shift
40+
;;
41+
--)
42+
shift
43+
FILES=("$@")
44+
break
45+
;;
46+
esac
47+
done
48+
}
49+
50+
terraform_providers_lock_() {
51+
local -a paths
52+
local index=0
53+
local file_with_path
54+
55+
for file_with_path in "${FILES[@]}"; do
56+
file_with_path="${file_with_path// /__REPLACED__SPACE__}"
57+
58+
paths[index]=$(dirname "$file_with_path")
59+
60+
((index += 1))
61+
done
62+
63+
local path_uniq
64+
for path_uniq in $(echo "${paths[*]}" | tr ' ' '\n' | sort -u); do
65+
path_uniq="${path_uniq//__REPLACED__SPACE__/ }"
66+
67+
if [[ ! -d "${path_uniq}/.terraform" ]]; then
68+
set +e
69+
init_output=$(terraform -chdir="${path_uniq}" init -backend=false 2>&1)
70+
init_code=$?
71+
set -e
72+
73+
if [[ $init_code != 0 ]]; then
74+
echo "Init before validation failed: $path_uniq"
75+
echo "$init_output"
76+
exit 1
77+
fi
78+
fi
79+
80+
terraform -chdir="${path_uniq}" providers lock "${ARGS[@]}"
81+
done
82+
}
83+
84+
# global arrays
85+
declare -a ARGS
86+
declare -a FILES
87+
88+
[[ ${BASH_SOURCE[0]} != "$0" ]] || main "$@"

0 commit comments

Comments
 (0)