-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
279 lines (256 loc) · 11.4 KB
/
Makefile
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
ifneq (,)
.error This Makefile requires GNU Make.
endif
.PHONY: help gen lint test _gen-main _gen-examples _gen-modules _lint-files _lint-fmt _lint-json _pull-tf _pull-tfdocs _pull-fl _pull-jl
CURRENT_DIR = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
TF_EXAMPLES = $(sort $(dir $(wildcard $(CURRENT_DIR)examples/*/)))
TF_MODULES = $(sort $(dir $(wildcard $(CURRENT_DIR)modules/*/)))
# -------------------------------------------------------------------------------------------------
# Container versions
# -------------------------------------------------------------------------------------------------
TF_VERSION = 1.5.7
TFDOCS_VERSION = 0.16.0-0.34
FL_VERSION = latest-0.8
JL_VERSION = 1.6.0-0.14
# -------------------------------------------------------------------------------------------------
# Enable linter (file-lint, terraform fmt, jsonlint)
# -------------------------------------------------------------------------------------------------
LINT_FL_ENABLE = 1
LINT_TF_ENABLE = 1
LINT_JL_ENABLE = 1
# -------------------------------------------------------------------------------------------------
# terraform-docs defines
# -------------------------------------------------------------------------------------------------
# Adjust your delimiter here or overwrite via make arguments
DELIM_START = <!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
DELIM_CLOSE = <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
# What arguments to append to terraform-docs command
TFDOCS_ARGS = --sort-by required
# -------------------------------------------------------------------------------------------------
# Default target
# -------------------------------------------------------------------------------------------------
help:
@echo "gen Generate terraform-docs output and replace in README.md's"
@echo "lint Static source code analysis"
@echo "test Integration tests"
# -------------------------------------------------------------------------------------------------
# Standard targets
# -------------------------------------------------------------------------------------------------
gen: _pull-tfdocs
@echo "################################################################################"
@echo "# Terraform-docs generate"
@echo "################################################################################"
@$(MAKE) --no-print-directory _gen-main
@$(MAKE) --no-print-directory _gen-examples
@$(MAKE) --no-print-directory _gen-modules
lint:
@if [ "$(LINT_FL_ENABLE)" = "1" ]; then \
$(MAKE) --no-print-directory _lint-files; \
fi
@if [ "$(LINT_TF_ENABLE)" = "1" ]; then \
$(MAKE) --no-print-directory _lint-fmt; \
fi
@if [ "$(LINT_JL_ENABLE)" = "1" ]; then \
$(MAKE) --no-print-directory _lint-json; \
fi
test: _pull-tf
@$(foreach example,\
$(TF_EXAMPLES),\
DOCKER_PATH="/t/examples/$(notdir $(patsubst %/,%,$(example)))"; \
echo "################################################################################"; \
echo "# examples/$$( basename $${DOCKER_PATH} )"; \
echo "################################################################################"; \
echo; \
echo "------------------------------------------------------------"; \
echo "# Terraform init"; \
echo "------------------------------------------------------------"; \
if docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" hashicorp/terraform:$(TF_VERSION) \
init \
-upgrade=true \
-reconfigure \
-input=false \
-get=true; then \
echo "OK"; \
else \
echo "Failed"; \
docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" --entrypoint=rm hashicorp/terraform:$(TF_VERSION) -rf .terraform/ || true; \
exit 1; \
fi; \
echo; \
echo "------------------------------------------------------------"; \
echo "# Terraform validate"; \
echo "------------------------------------------------------------"; \
if docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" hashicorp/terraform:$(TF_VERSION) \
validate \
.; then \
echo "OK"; \
else \
echo "Failed"; \
docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" --entrypoint=rm hashicorp/terraform:$(TF_VERSION) -rf .terraform/ || true; \
exit 1; \
fi; \
echo; \
echo "------------------------------------------------------------"; \
echo "# Terraform plan"; \
echo "------------------------------------------------------------"; \
if docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY --workdir "$${DOCKER_PATH}" hashicorp/terraform:$(TF_VERSION) \
plan \
$(ARGS) \
-out=tfplan \
; then \
echo "OK"; \
else \
echo "Failed"; \
docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" --entrypoint=rm hashicorp/terraform:$(TF_VERSION) -rf .terraform/ || true; \
exit 1; \
fi; \
echo; \
echo "------------------------------------------------------------"; \
echo "# Terraform apply & destroy"; \
echo "------------------------------------------------------------"; \
if docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY --workdir "$${DOCKER_PATH}" hashicorp/terraform:$(TF_VERSION) \
apply \
-auto-approve \
tfplan \
; then \
echo "Apply OK"; \
if docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY --workdir "$${DOCKER_PATH}" hashicorp/terraform:$(TF_VERSION) \
destroy \
-auto-approve \
$(ARGS) \
; then \
echo "Destroy OK"; \
docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" --entrypoint=rm hashicorp/terraform:$(TF_VERSION) -rf .terraform/ terraform.tfstate terraform.tfstate.backup || true; \
else \
echo "Destroy failed. You should check for dangling resources."; \
exit 1; \
fi; \
else \
echo "Apply failed"; \
if docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY --workdir "$${DOCKER_PATH}" hashicorp/terraform:$(TF_VERSION) \
destroy \
-auto-approve \
$(ARGS) \
; then \
echo "Destroy OK"; \
docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t" --workdir "$${DOCKER_PATH}" --entrypoint=rm hashicorp/terraform:$(TF_VERSION) -rf .terraform/ terraform.tfstate terraform.tfstate.backup || true; \
else \
echo "Destroy failed. You should check for dangling resources."; \
exit 1; \
fi; \
fi; \
)
# -------------------------------------------------------------------------------------------------
# Helper Targets
# -------------------------------------------------------------------------------------------------
_gen-main:
@echo "------------------------------------------------------------"
@echo "# Main module"
@echo "------------------------------------------------------------"
@if docker run --rm \
-v $(CURRENT_DIR):/data \
-e DELIM_START='$(DELIM_START)' \
-e DELIM_CLOSE='$(DELIM_CLOSE)' \
cytopia/terraform-docs:$(TFDOCS_VERSION) \
terraform-docs-replace $(TFDOCS_ARGS) markdown README.md; then \
echo "OK"; \
else \
echo "Failed"; \
exit 1; \
fi
_gen-examples:
@$(foreach example,\
$(TF_EXAMPLES),\
DOCKER_PATH="examples/$(notdir $(patsubst %/,%,$(example)))"; \
echo "------------------------------------------------------------"; \
echo "# $${DOCKER_PATH}"; \
echo "------------------------------------------------------------"; \
if docker run --rm \
-v $(CURRENT_DIR):/data \
-e DELIM_START='$(DELIM_START)' \
-e DELIM_CLOSE='$(DELIM_CLOSE)' \
cytopia/terraform-docs:$(TFDOCS_VERSION) \
terraform-docs-replace $(TFDOCS_ARGS) markdown $${DOCKER_PATH}/README.md; then \
echo "OK"; \
else \
echo "Failed"; \
exit 1; \
fi; \
)
_gen-modules:
@$(foreach module,\
$(TF_MODULES),\
DOCKER_PATH="modules/$(notdir $(patsubst %/,%,$(module)))"; \
echo "------------------------------------------------------------"; \
echo "# $${DOCKER_PATH}"; \
echo "------------------------------------------------------------"; \
if docker run --rm \
-v $(CURRENT_DIR):/data \
-e DELIM_START='$(DELIM_START)' \
-e DELIM_CLOSE='$(DELIM_CLOSE)' \
cytopia/terraform-docs:$(TFDOCS_VERSION) \
terraform-docs-replace $(TFDOCS_ARGS) markdown $${DOCKER_PATH}/README.md; then \
echo "OK"; \
else \
echo "Failed"; \
exit 1; \
fi; \
)
_lint-files: _pull-fl
@# Basic file linting
@echo "################################################################################"
@echo "# File-lint"
@echo "################################################################################"
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-cr --text --ignore '.git/,.github/,.terraform/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-crlf --text --ignore '.git/,.github/,.terraform/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-trailing-single-newline --text --ignore '.git/,.github/,.terraform/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-trailing-space --text --ignore '.git/,.github/,.terraform/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-utf8 --text --ignore '.git/,.github/,.terraform/' --path .
@docker run --rm -v $(CURRENT_DIR):/data cytopia/file-lint:$(FL_VERSION) file-utf8-bom --text --ignore '.git/,.github/,.terraform/' --path .
_lint-fmt: _pull-tf
@# Lint all Terraform files
@echo "################################################################################"
@echo "# Terraform fmt"
@echo "################################################################################"
@echo
@echo "------------------------------------------------------------"
@echo "# *.tf files"
@echo "------------------------------------------------------------"
@if docker run $$(tty -s && echo "-it" || echo) --rm -v "$(CURRENT_DIR):/t:ro" --workdir "/t" hashicorp/terraform:$(TF_VERSION) \
fmt -check=true -diff=true -write=false -list=true .; then \
echo "OK"; \
else \
echo "Failed"; \
exit 1; \
fi;
@echo
@echo "------------------------------------------------------------"
@echo "# *.tfvars files"
@echo "------------------------------------------------------------"
@if docker run --rm --entrypoint=/bin/sh -v "$(CURRENT_DIR):/t:ro" --workdir "/t" hashicorp/terraform:$(TF_VERSION) \
-c "find . -name '*.tfvars' -type f -print0 | xargs -0 -n1 terraform fmt -check=true -write=false -diff=true -list=true"; then \
echo "OK"; \
else \
echo "Failed"; \
exit 1; \
fi;
@echo
_lint-json: _pull-jl
@# Lint all JSON files
@echo "################################################################################"
@echo "# Jsonlint"
@echo "################################################################################"
@if docker run --rm -v "$(CURRENT_DIR):/data:ro" cytopia/jsonlint:$(JL_VERSION) \
-t ' ' -i '*.terraform/*' '*.json'; then \
echo "OK"; \
else \
echo "Failed"; \
exit 1; \
fi;
@echo
_pull-tf:
docker pull hashicorp/terraform:$(TF_VERSION)
_pull-tfdocs:
docker pull cytopia/terraform-docs:$(TFDOCS_VERSION)
_pull-fl:
docker pull cytopia/file-lint:$(FL_VERSION)