-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathterraform.sh
executable file
·163 lines (139 loc) · 4.48 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
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
#!/bin/sh
set -eo pipefail
print() {
local GREEN='\033[1;32m'
local YELLOW='\033[1;33m'
local RED='\033[0;31m'
local NC='\033[0m'
local BOLD='\e[1m'
local REGULAR='\e[0m'
case "$1" in
'failure' ) echo -e "${RED}✗ $2${NC}" ;;
'success' ) echo -e "${GREEN}√ $2${NC}" ;;
'warning' ) echo -e "${YELLOW}⚠ $2${NC}" ;;
'header' ) echo -e "${BOLD}$2${REGULAR}" ;;
esac
}
setup() {
export DIR="$PWD"
export TMPDIR=${TMPDIR:-/tmp}
export AWS_ACCESS_KEY_ID="${access_key}"
export AWS_SECRET_ACCESS_KEY="${secret_key}"
export AWS_SESSION_TOKEN="${session_token}"
if [ ! -z "${github_access_token}" ]; then
cat > "${HOME}"/.netrc <<EOF
machine github.com
login x-oauth-basic
password ${github_access_token}
machine api.github.com
login x-oauth-basic
password ${github_access_token}
EOF
print success "configured github_access_token"
fi
if [ ! -z "${github_private_key}" ]; then
setup_ssh
print success "configured github_private_key"
fi
}
setup_ssh() {
# Source/credit: https://github.com/jtarchie/github-pullrequest-resource/blob/master/assets/common.sh
local private_key_path=$TMPDIR/git-private-key
echo "${github_private_key}" > $private_key_path
if [ -s $private_key_path ]; then
chmod 0600 $private_key_path
eval $(ssh-agent) >/dev/null 2>&1
trap "kill $SSH_AGENT_PID" 0
SSH_ASKPASS=$DIR/common-tasks/terraform/askpass.sh DISPLAY= ssh-add $private_key_path >/dev/null
fi
mkdir -p ~/.ssh
cat > ~/.ssh/config <<EOF
StrictHostKeyChecking no
LogLevel quiet
EOF
chmod 0600 ~/.ssh/config
}
setup_cache() {
export TF_DATA_DIR="${DIR}/terraform-cache/${1}"
mkdir -p "${TF_DATA_DIR}"
if [ -z "$(ls -A ${DIR}/terraform-cache/${1})" ]; then
print warning "cache enabled but empty (fresh worker)"
else
print success "cache enabled and found existing cache"
fi
}
terraform_fmt() {
if ! terraform fmt -check=true >> /dev/null; then
print failure "terraform fmt (Some files need to be formatted, run 'terraform fmt' to fix.)"
exit 1
fi
print success "terraform fmt"
}
terraform_get() {
# NOTE: We are using init here to download providers in addition to modules.
terraform init -backend=false -input=false >> /dev/null
print success "terraform get (init without backend)"
}
terraform_init() {
terraform init -input=false -lock-timeout=$lock_timeout >> /dev/null
print success "terraform init"
}
terraform_plan() {
terraform_init
terraform plan -lock=false -no-color $command_params | tee "${DIR}/terraform/full-plan"
# Create a sanitized plan for Github comments
echo "### ${1}" >> "${DIR}/terraform/plan"
echo "\`\`\`diff" >> "${DIR}/terraform/plan"
sed -n -e '/------------------------------------------------------------------------/,$p' "${DIR}/terraform/full-plan" >> "${DIR}/terraform/plan"
echo "\`\`\`" >> "${DIR}/terraform/plan"
}
terraform_apply() {
terraform_init
terraform apply -refresh=true -auto-approve=true -lock-timeout=$lock_timeout $command_params
# Fails if there is no output (which is not really a failure)
set +e
terraform output -json > ${DIR}/terraform/output.json
set -e
}
terraform_test_module() {
terraform_fmt
terraform_get
terraform validate -check-variables=false
print success "terraform validate (not including variables)"
}
terraform_test() {
terraform_fmt
terraform_get
terraform validate
print success "terraform validate"
}
main() {
if [ -z "$command" ]; then
echo "Command is a required parameter and must be set."
exit 1
fi
if [ -z "$directories" ]; then
echo "No directories provided. Please set the parameter."
exit 1
fi
setup
for directory in $directories; do
if [ ! -d "$DIR/source/$directory" ]; then
print failure "Directory not found: $directory"
exit 1
fi
cd $DIR/source/$directory
print header "Current directory: $directory"
if [ "$cache" = "true" ];then
setup_cache "$directory"
fi
case "$command" in
'test' ) terraform_test ;;
'test-module' ) terraform_test_module ;;
'plan' ) terraform_plan "$directory" ;;
'apply' ) terraform_apply ;;
* ) echo "Command not supported: $command" && exit 1;;
esac
done
}
main