-
Notifications
You must be signed in to change notification settings - Fork 25
/
terraform.sh
executable file
·137 lines (115 loc) · 3.1 KB
/
terraform.sh
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
#/
#/ Usage:
#/ ./terraform.sh [action]
#/ Description:
#/ Terraform Wrapper Script
#/ Examples:
#/ ./terraform.sh plan
#/ ./terraform.sh apply
#/ Actions:
#/ init - Init configuration
#/ validate - Validate terraform file
#/ plan - Test terraform configuration
#/ apply - Apply terraform configuration
#/ destroy - Destroy all resources created in terraform
#/ Options:
#/ --help: Display this help message
#/
#/
usage() { grep '^#/' "${0}" | cut -c4- ; exit 0 ; }
expr "$*" : ".*--help" > /dev/null && usage
readonly LOG_FILE="/tmp/$(basename "${0}").log"
info() { echo "[INFO] $*" | tee -a "${LOG_FILE}" >&2 ; }
warning() { echo "[WARNING] $*" | tee -a "${LOG_FILE}" >&2 ; }
error() { echo "[ERROR] $*" | tee -a "${LOG_FILE}" >&2 ; }
fatal() { echo "[FATAL] $*" | tee -a "${LOG_FILE}" >&2 ; exit 1 ; }
terraform_fmt() {
info "Running terraform fmt"
terraform fmt || fatal "Could not fmt terraform"
}
terraform_lint() {
info "Running terraform lint"
terraform fmt -diff=true -check || fatal "Could not lint terraform"
}
terraform_validate() {
info "Running terraform validate"
for i in $(find . -type f -name "*.tf" -exec dirname {} \; | grep -v "/test"); do
terraform validate "$i" || fatal "Could not validate terraform"
if [ $? -ne 0 ]; then
error "Failed Terraform .tf file validation"
fi;
done
}
terraform_init() {
info "Running terraform init"
terraform init -input=false || fatal "Could not initialize terraform"
}
terraform_plan() {
info "Running terraform plan"
terraform plan -out=plan.out || error "Terraform plan failed"
}
terraform_apply() {
terraform_plan
info "Running terraform apply"
terraform apply \
-lock=true \
-input=false \
-refresh=true \
-auto-approve=true \
./plan.out || error "Terraform apply failed"
}
terraform_destroy() {
info "Running terraform destroy"
terraform destroy \
-lock=true \
-input=false \
-refresh=true \
-force || error "Terraform destroy failed"
rm -rf tfplan
rm -rf terraform.tfstate.backup
rm -rf .terraform
}
setup() {
terraform_fmt
if [[ ! -d ".terraform" ]] ; then
terraform_init
fi
}
cleanup() {
info "Cleaning up temporary files."
rm -rf plan.out
}
if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
trap cleanup EXIT
setup
if [[ ${#} -gt 0 ]] ; then
case "${1}" in
"init")
terraform_init
;;
"validate")
terraform_validate
;;
"plan")
terraform_plan
;;
"apply")
terraform_apply
;;
"destroy")
terraform_destroy
;;
"help")
usage
;;
*)
fatal "Unknown command: ${1} $(usage)"
;;
esac
else
fatal "No command supplied $(usage)"
fi
fi