-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMakefile
131 lines (97 loc) · 4.43 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
UNAME:= $(shell uname)
ifeq ($(UNAME),Darwin)
OS_X := true
SHELL := /bin/bash
else
OS_DEB := true
SHELL := /bin/bash
endif
TERRAFORM:= $(shell command -v terraform 2> /dev/null)
TERRAFORM_VERSION:= "0.15.4"
ifeq ($(OS_X),true)
TERRAFORM_MD5:= $(shell md5 -q `which terraform`)
TERRAFORM_REQUIRED_MD5:= 87259bd9e1b1716c6fc424af0c32454b
else
TERRAFORM_MD5:= $(shell md5sum - < `which terraform` | tr -d ' -')
TERRAFORM_REQUIRED_MD5:= 8e463d4a8abd62b8bc1a6ad9cfd7cc54
endif
default:
@echo "Creates a Terraform system from a template."
@echo "The following commands are available:"
@echo " - plan : runs terraform plan for an environment"
@echo " - apply : runs terraform apply for an environment"
@echo " - destroy : will delete the entire project's infrastructure"
check:
@echo "Checking Terraform version... success expecting md5 of [${TERRAFORM_REQUIRED_MD5}], found [${TERRAFORM_MD5}]"
@if [ "${TERRAFORM_MD5}" != "${TERRAFORM_REQUIRED_MD5}" ]; then echo "Please ensure you are running terraform ${TERRAFORM_VERSION}."; exit 1; fi
plan: check
$(call check_defined, ENV, Please set the ENV to plan for. Values should be dev, test, uat or prod)
@terraform fmt
@echo "Pulling the required modules..."
@terraform get
@echo 'Switching to the [$(value ENV)] environment ...'
@terraform workspace select $(value ENV)
@terraform plan \
-var-file="env/$(value ENV).tfvars" \
-out $(value ENV).plan
targetplan: check
$(call check_defined, ENV, Please set the ENV to plan for. Values should be dev, test, uat or prod)
@terraform fmt
@echo "Pulling the required modules..."
@terraform get
@echo 'Switching to the [$(value ENV)] environment ...'
@terraform workspace select $(value ENV)
@terraform plan \
-var-file="env/$(value ENV).tfvars" \
-target "$(value restype).$(value resname)" \
-out $(value ENV).plan
apply: check
$(call check_defined, ENV, Please set the ENV to apply. Values should be dev, test, uat or prod)
@echo 'Switching to the [$(value ENV)] environment ...'
@terraform workspace select $(value ENV)
@echo "Will be applying the following to [$(value ENV)]environment:"
@terraform show -no-color $(value ENV).plan
@terraform apply $(value ENV).plan
@rm $(value ENV).plan
taint: check
$(call check_defined, ENV, Please set the ENV to plan for. Values should be dev, test, uat or prod)
@terraform fmt
@echo "Pulling the required modules..."
@terraform get
@echo 'Switching to the [$(value ENV)] environment ...'
@terraform workspace select $(value ENV)
@terraform taint \
"$(value restype).$(value resname)"
@ENV=$(value ENV) restype=$(value restype) resname=$(value resname) make targetplan
@ENV=$(value ENV) make apply
destroy: check
@echo "Switching to the [$(value ENV)] environment ..."
@terraform workspace select $(value ENV)
@echo "## 💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥 ##"
@echo "Are you really sure you want to completely destroy [$(value ENV)] environment ?"
@echo "## 💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥 ##"
@read -p "Press enter to continue"
@terraform destroy \
-var-file="env/$(value ENV).tfvars"
destroytarget: check
@echo "Switching to the [$(value ENV)] environment ..."
@terraform workspace select $(value ENV)
@echo "## 💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥 ##"
@echo "Are you really sure you want to completely destroy [$(value ENV)] Resource: [$(value restype).$(value resname)] ?"
@echo "## 💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥💥 ##"
@read -p "Press enter to continue"
@terraform destroy \
-var-file="env/$(value ENV).tfvars" \
-target "$(value restype).$(value resname)" \
# Check that given variables are set and all have non-empty values,
# die with an error otherwise.
#
# Params:
# 1. Variable name(s) to test.
# 2. (optional) Error message to print.
check_defined = \
$(strip $(foreach 1,$1, \
$(call __check_defined,$1,$(strip $(value 2)))))
__check_defined = \
$(if $(value $1),, \
$(error Undefined $1$(if $2, ($2))))