-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMakefile.terraform
172 lines (131 loc) · 6.26 KB
/
Makefile.terraform
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Usage - Define TERRAFORM_VERSION, and include this file as below.
#
# TERRAFORM_VERSION := latest
# include Makefile.terraform
# If this variable is not set in your makefile, the program /bin/sh is used as the shell.
# https://www.gnu.org/software/make/manual/html_node/Choosing-the-Shell.html
SHELL := /bin/bash
# This option causes make to display a warning whenever an undefined variable is expanded.
MAKEFLAGS += --warn-undefined-variables
# Disable any builtin pattern rules, then speedup a bit.
MAKEFLAGS += --no-builtin-rules
# Disable any builtin suffix rules, then speedup a bit.
.SUFFIXES:
# The arguments passed to the shell are taken from the variable .SHELLFLAGS.
#
# The -e flag causes bash with qualifications to exit immediately if a command it executes fails.
# The -u flag causes bash to exit with an error message if a variable is accessed without being defined.
# The -o pipefail option causes bash to exit if any of the commands in a pipeline fail.
# The -c flag is in the default value of .SHELLFLAGS and we must preserve it.
# Because it is how make passes the script to be executed to bash.
.SHELLFLAGS := -eu -o pipefail -c
# Sets the default goal to be used if no targets were specified on the command line.
.DEFAULT_GOAL := help
# https://gist.github.com/tadashi-aikawa/da73d277a3c1ec6767ed48d1335900f3
.PHONY: $(shell grep --no-filename -E '^[a-zA-Z0-9_-]+:' $(MAKEFILE_LIST) | sed 's/://')
# Constant definitions
TERRAFORM_IMAGE := hashicorp/terraform:${TERRAFORM_VERSION}
ENVIRONMENT_VARIABLES := AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_DEFAULT_REGION
LINTER_IMAGES := koalaman/shellcheck tmknom/markdownlint tmknom/yamllint
FORMATTER_IMAGES := mvdan/shfmt tmknom/prettier
TERRAFORM_IMAGES := ${TERRAFORM_IMAGE} wata727/tflint tmknom/terraform-docs
DOCKER_IMAGES := ${LINTER_IMAGES} ${FORMATTER_IMAGES} ${TERRAFORM_IMAGES}
EXAMPLE_DIRS := $(shell find . -type f -name '*.tf' -path "./examples/*" -not -path "**/.terraform/*" -exec dirname {} \; | sort -u)
# Macro definitions
define list_shellscript
grep '^#!' -rn . | grep ':1:#!' | cut -d: -f1 | grep -v .git
endef
define terraform
run_dir="${1}" && \
sub_command="${2}" && \
option="${3}" && \
docker run --rm -i -v "$$PWD:/work" -w /work \
-v $${TF_PLUGIN_CACHE_DIR:-/tmp/.terraform.d/plugin-cache}:/.terraform.d/plugin-cache \
-e TF_PLUGIN_CACHE_DIR=/.terraform.d/plugin-cache \
-e AWS_ACCESS_KEY_ID=$$AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY=$$AWS_SECRET_ACCESS_KEY \
-e AWS_DEFAULT_REGION=$$AWS_DEFAULT_REGION \
${TERRAFORM_IMAGE} $${sub_command} $${option} $${run_dir}
endef
define check_requirement
if ! type ${1} >/dev/null 2>&1; then \
printf "\nNot found %s, run command\n\n" ${1}; \
printf " \033[36mbrew install %s\033[0m\n" ${1}; \
fi
endef
define check_environment_variable
key="\$$${1}" && \
value=$$(eval "echo $${key}") && \
if [ -z "$${value}" ]; then \
printf "\n%s is unset, run command\n\n" $${key}; \
printf " \033[36mexport %s=<value>\033[0m\n" ${1}; \
fi
endef
# Phony Targets
install: check-requirements install-images check-env ## Install requirements
install-images:
@for image in ${DOCKER_IMAGES}; do \
echo "docker pull $${image}" && docker pull $${image}; \
done
check-requirements:
@$(call check_requirement,docker)
check-env:
@for val in ${ENVIRONMENT_VARIABLES}; do \
$(call check_environment_variable,$${val}); \
done
lint: lint-shellscript lint-markdown lint-yaml lint-terraform validate-terraform ## Lint code
lint-terraform:
docker run --rm -v "$(CURDIR):/data" wata727/tflint
validate-terraform: validate-terraform-module validate-terraform-examples
validate-terraform-module:
$(call terraform,.,init,) && $(call terraform,.,validate,)
validate-terraform-examples:
@for dir in ${EXAMPLE_DIRS}; do \
$(call terraform,$${dir},init,) && $(call terraform,$${dir},validate,); \
done
lint-shellscript:
$(call list_shellscript) | xargs -I {} docker run --rm -v "$(CURDIR):/mnt" koalaman/shellcheck {}
lint-markdown:
docker run --rm -i -v "$(CURDIR):/work" tmknom/markdownlint
lint-yaml:
docker run --rm -v "$(CURDIR):/work" tmknom/yamllint --strict .
format: format-terraform format-shellscript format-markdown ## Format code
format-terraform:
$(call terraform,.,fmt,-recursive)
format-shellscript:
$(call list_shellscript) | xargs -I {} docker run --rm -v "$(CURDIR):/work" -w /work mvdan/shfmt -i 2 -ci -kp -w {}
format-markdown:
docker run --rm -v "$(CURDIR):/work" tmknom/prettier --parser=markdown --write '**/*.md'
check-format: check-format-terraform check-format-shellscript check-format-markdown ## Check format code
check-format-terraform:
$(call terraform,.,fmt,-recursive -check=true)
check-format-shellscript:
$(call list_shellscript) | xargs -I {} docker run --rm -v "$(CURDIR):/work" -w /work tmknom/shfmt -d -i 2 -ci -kp -w {}
check-format-markdown:
docker run --rm -v "$(CURDIR):/work" tmknom/prettier --list-different --parser=markdown '**/*.md'
docs: ## Generate docs
first_part=$$(grep -B 10000 -F "BEGINNING OF GENERATED BY TERRAFORM-DOCS" README.md) && \
generated_part=$$(docker run --rm -v "$$PWD:/work" tmknom/terraform-docs --sort-by-required markdown .) && \
last_part=$$(grep -A 10000 -F "END OF GENERATED BY TERRAFORM-DOCS" README.md) && \
combined="$${first_part}\n\n$${generated_part}\n\n$${last_part}" && \
echo -e "$${combined}" | docker run --rm -i -v "$(CURDIR):/work" tmknom/prettier --parser=markdown > README.md
release: ## Release GitHub and Terraform Module Registry
version=$$(cat VERSION) && git tag "$${version}" && git push origin "$${version}"
diff: ## Word diff
git diff head --word-diff-regex=$$'[^\x80-\xbf][\x80-\xbf]*' --word-diff=color
clean: ## Clean .terraform
find . -type d -name .terraform -prune -exec rm -rf {} \;
upgrade: ## Upgrade makefile
rm -rf .Makefile.terraform
$(MAKE)
bump-version: ## Bump version (Required argument 'VERSION')
@if [ -z "$${VERSION}" ]; then \
printf "\nVERSION is unset, run command\n\n"; \
printf "\033[36m make bump-version VERSION=X.Y.Z\033[0m\n\n"; \
exit 1; \
fi
sed -i '' "s/tags\/[0-9]\.[0-9]\.[0-9]/tags\/$${VERSION}/" README.md
echo $${VERSION} > VERSION
# https://postd.cc/auto-documented-makefile/
help: ## Show help
@grep --no-filename -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'