forked from EngineerBetter/iac-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
253 lines (214 loc) · 6.55 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
# ===== Deployment ============================================================
terraform-bootstrap: \
guard-BOOTSTRAP_AWS_REGION \
guard-BOOTSTRAP_BUCKET_NAME \
guard-BOOTSTRAP_DYNAMO_TABLE_NAME
@./bootstrap/bootstrap.bash
@$(call print_success,OK)
terraform-init: \
load-env \
guard-BOOTSTRAP_AWS_REGION \
guard-BOOTSTRAP_BUCKET_NAME \
guard-BOOTSTRAP_DYNAMO_TABLE_NAME
terraform \
-chdir=$(CLUSTER) \
init \
-backend=true \
-reconfigure \
-input=false \
-backend-config=bucket=$(BOOTSTRAP_BUCKET_NAME) \
-backend-config=region=$(BOOTSTRAP_AWS_REGION) \
-backend-config=dynamodb_table=$(BOOTSTRAP_DYNAMO_TABLE_NAME) \
-backend-config=key=$(ENV_NAME)
terraform-plan: load-env terraform-init
terraform -chdir=$(CLUSTER) plan -var="env_name=$(ENV_NAME)" -out "$$( pwd )/build/tfplan"
terraform -chdir=$(CLUSTER) show -json "$$( pwd )/build/tfplan" > build/tfplan.json
deploy-cluster: load-env terraform-plan snyk-test-plan
rm build/tfplan.json
terraform \
-chdir=$(CLUSTER) \
apply \
"$$( pwd )/build/tfplan"
rm build/tfplan
deploy-sock-shop: load-env
kubectl \
--kubeconfig=secrets/config-$(ENV_NAME).yml \
apply \
--filename deployments/sock-shop/manifest.yml
kubectl \
--kubeconfig=secrets/config-$(ENV_NAME).yml \
wait \
--namespace sock-shop \
--all=true \
--for condition=ready \
--timeout=600s \
pod
# ===== Destroy ===============================================================
delete-sock-shop: load-env
kubectl \
--kubeconfig=secrets/config-$(ENV_NAME).yml \
delete \
--filename deployments/sock-shop/manifest.yml \
--ignore-not-found=true \
--wait=true
destroy-cluster: load-env terraform-init
terraform -chdir=$(CLUSTER) destroy -var="env_name=$(ENV_NAME)"
# ===== Tests & Checks ========================================================
test: \
terraform-validate \
terraform-lint \
terraform-fmt-check \
snyk-test-terraform \
snyk-test-deployments
terraform-validate:
terraform -chdir=$(CLUSTER) validate
terraform-lint:
tflint $(CLUSTER)
terraform-fmt-check:
@if ! terraform -chdir=$(CLUSTER) fmt -check -diff; then \
$(call print_fail,Run `terraform fmt $(CLUSTER)` to fix format errors); \
fi
snyk-test-terraform: guard-SNYK_TOKEN
snyk iac test terraform/
snyk-test-deployments: guard-SNYK_TOKEN
ifdef IGNORE_SNYK_TEST_DEPLOYMENTS_FAILURE
snyk iac test deployments/ || true
else
snyk iac test deployments/
endif
snyk-test-plan: guard-SNYK_TOKEN
ifdef IGNORE_SNYK_TEST_PLAN_FAILURE
snyk iac test --scan=planned-values build/tfplan.json || true
else
snyk iac test --scan=planned-values build/tfplan.json
endif
integration-test: load-env
cd tests/integration && \
SOCK_SHOP_URL="$$( kubectl \
--kubeconfig=../../secrets/config-$(ENV_NAME).yml \
-n sock-shop \
get service/front-end \
-o jsonpath="{.status.loadBalancer.ingress[0].hostname}" \
)" $(GINKGO) --race --randomizeAllSpecs -r .
policy-test:
conftest test -p tests/policies deployments/sock-shop/manifest.yml
# ===== Jenkins ===============================================================
jenkins-update-pipelines: \
jenkins-update-deploy-pipeline \
jenkins-update-destroy-pipeline \
jenkins-update-promote-pipeline
jenkins-create-pipelines: \
jenkins-create-deploy-pipeline \
jenkins-create-destroy-pipeline \
jenkins-create-promote-pipeline
jenkins-%-deploy-pipeline: \
load-env \
guard-JENKINS_CLI \
guard-JENKINS_URL \
guard-JENKINS_PASSWORD \
guard-JENKINS_USERNAME \
guard-SLACK_CHANNEL
@echo -n Setting deploy pipeline...
@sed \
-e 's#REPLACE_ME_REPOSITORY_URL#$(REPOSITORY_URL)#' \
-e 's/REPLACE_ME_SLACK_CHANNEL/$(SLACK_CHANNEL)/' \
-e 's/REPLACE_ME_PROMOTES_FROM/$(PROMOTES_FROM)/' \
-e 's/REPLACE_ME_ENV_NAME/$(ENV_NAME)/' \
-e 's/REPLACE_ME_PROMOTES_TO/$(PROMOTES_TO)/' \
pipelines/deploy.xml \
| java \
-jar $(JENKINS_CLI) \
-s $(JENKINS_URL) \
-auth "$(JENKINS_USERNAME):$${JENKINS_PASSWORD}" \
$*-job 'Deploy ($(ENV_NAME))'
@$(call print_success, OK!)
jenkins-%-destroy-pipeline: \
load-env \
guard-JENKINS_CLI \
guard-JENKINS_URL \
guard-JENKINS_PASSWORD \
guard-JENKINS_USERNAME \
guard-SLACK_CHANNEL
@echo -n Setting destroy pipeline...
@sed \
-e 's/REPLACE_ME_SLACK_CHANNEL/$(SLACK_CHANNEL)/' \
-e 's/REPLACE_ME_ENV_NAME/$(ENV_NAME)/' \
-e 's#REPLACE_ME_REPOSITORY_URL#$(REPOSITORY_URL)#' \
-e 's/REPLACE_ME_PROMOTES_TO/$(PROMOTES_TO)/' \
pipelines/destroy.xml \
| java \
-jar $(JENKINS_CLI) \
-s $(JENKINS_URL) \
-auth "$(JENKINS_USERNAME):$${JENKINS_PASSWORD}" \
$*-job 'Destroy ($(ENV_NAME))'
@$(call print_success, OK!)
jenkins-%-promote-pipeline: \
guard-JENKINS_CLI \
guard-JENKINS_URL \
guard-JENKINS_PASSWORD \
guard-JENKINS_USERNAME
@echo -n Setting promote pipeline...
@sed \
-e 's#REPLACE_ME_REPOSITORY_URL#$(REPOSITORY_URL)#' \
pipelines/push-git-branch.xml \
| java \
-jar $(JENKINS_CLI) \
-s $(JENKINS_URL) \
-auth "$(JENKINS_USERNAME):$${JENKINS_PASSWORD}" \
$*-job 'Push Git Branch'
@$(call print_success, OK!)
# ===== Miscellaneous =========================================================
GINKGO := go run github.com/onsi/ginkgo/ginkgo
COLOUR_GREEN=\033[0;32m
COLOUR_RED=\033[;31m
COLOUR_NONE=\033[0m
CLUSTER=terraform/cluster
GITHUB_ORG ?= EngineerBetter
GITHUB_REPOSITORY ?= iac-example
REPOSITORY_URL=git@github.com:$(GITHUB_ORG)/$(GITHUB_REPOSITORY).git
load-env:
@if [ -z "$$(yq eval '.[] | select(.name == "$(ENV_NAME)") | .name' environments.yml)" ]; then \
$(call print_fail,No such environment) \
&& exit 1; \
fi
$(eval PROMOTES_FROM := $(shell \
yq \
eval \
'.[] | select(.name == "$(ENV_NAME)") | .promotes_from // "main"' \
environments.yml \
))
$(eval PROMOTES_TO := $(shell \
yq \
eval \
'.[] | select(.name == "$(ENV_NAME)") | .promotes_to' \
environments.yml \
))
@if [ "$(PROMOTES_TO)" = "null" ]; then \
$(call print_fail,Required field "promotes_to" not defined for $(ENV_NAME)) \
&& exit 1; \
fi
configure-pre-commit-hook:
@echo \
"make terraform-validate; make terraform-lint; make terraform-fmt-check" \
> .git/hooks/pre-commit
@chmod +rx .git/hooks/pre-commit
@$(call print_success,Configured pre-commit hook)
fetch-cluster-config: load-env terraform-init
@terraform \
-chdir=$(CLUSTER) \
output \
-raw \
kubeconfig \
> ./secrets/config-$(ENV_NAME).yml
@$(call print_success,Config written to secrets/config-$(ENV_NAME).yml)
guard-%:
@if [ "${${*}}" = "" ]; then \
$(call print_fail,Environment variable $* must be set) \
&& exit 1; \
fi
define print_success
printf '$(COLOUR_GREEN)$1$(COLOUR_NONE)\n'
endef
define print_fail
printf '$(COLOUR_RED)$1$(COLOUR_NONE)\n'
endef