-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathskydome
276 lines (258 loc) · 8.4 KB
/
skydome
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
#!/bin/sh
# =======================================================================
# This script contains common functions and is sourced by the
# the other Vaultron scripts- there's nothing useful in here to run directly.
#
# shellcheck disable=SC2059
# ^
# NB: we must roll with this sole shellcheck exception because the ANSI
# escape sequences as expanded by string formatting are ineffective
# leading the printing of literal characters instead, for example:
# \033[0;36m[=]%sForm Vaultron! ...%s\n\033[0mn
# so we take the ding in the name of ANSI color greatness
# (void where prohibited) ✨🎉
# =======================================================================
export VAULTRON_TMP="$PWD/tmp"
export VAULTRON_LIFECYCLE_LOG="$PWD/log/vaultron_lifecycle.log"
VAULTRON_USER="$(id -un)"
VAULTRON_GROUP="$(id -gn)"
VAULTRON_KATACODA=false
export VAULTRON_USER VAULTRON_GROUP VAULTRON_KATACODA
if [ "$HOME" = "/home/scrapbook" ]
then
export VAULTRON_KATACODA=true
fi
# Colors because the world is a colorful place 🌎
TXTBLU="$(tput setaf 4)"
TXTCYA="$(tput setaf 6)"
TXTGRN="$(tput setaf 2)"
TXTMGT="$(tput setaf 5)"
TXTRED="$(tput setaf 1)"
TXTYLW="$(tput setaf 3)"
TXTWHT="$(tput setaf 7)"
TXTRST="$(tput sgr0)"
msg() {
MSGSRC="[vaultron]"
MSGTYPE="$1"
MSGTXT="$2"
case "${MSGTYPE}" in
greeting)
printf "%s%s [=] %s %s\\n" "$TXTBLU" "$MSGSRC" "$MSGTXT" "$TXTRST"
;;
info)
printf "%s%s [i] %s %s\\n" "$TXTWHT" "$MSGSRC" "$MSGTXT" "$TXTRST"
;;
success)
printf "%s%s [+] %s %s\\n" "$TXTGRN" "$MSGSRC" "$MSGTXT" "$TXTRST"
;;
complete)
printf "%s%s [^] %s %s\\n" "$TXTGRN" "$MSGSRC" "$MSGTXT" "$TXTRST"
;;
boom)
printf "%s%s [*] %s %s\\n" "$TXTMGT" "$MSGSRC" "$MSGTXT" "$TXTRST"
;;
notice)
printf "%s%s [?] %s %s\\n" "$TXTYLW" "$MSGSRC" "$MSGTXT" "$TXTRST"
;;
alert)
>&2 printf "%s%s [!] %s %s\\n" "$TXTRED" "$MSGSRC" "$MSGTXT" "$TXTRST"
;;
*)
>&2 printf "%s%s [@] %s %s\\n" "$TXTCYA" "$MSGSRC" "$MSGTXT" "$TXTRST"
;;
esac
}
# -----------------------------------------------------------------------
# Basic CLI capabilities check (for renaming from <= v0.9.1 and v0.9.1+)
# -----------------------------------------------------------------------
check_cli_cap() {
VAULT_VERSION="$(vault version | awk '{print $2}' | cut -d 'v' -f2)"
VAULT_MIN_VERSION="$(vault version | awk '{print $2}' | cut -d 'v' -f2 | cut -d '.' -f1)"
if [ "$VAULT_MIN_VERSION" = "1" ]
then
export VAULT_CLI_CAP="1"
else
VAULT_MIN_VERSION="$(vault version | awk '{print $2}' | cut -d 'v' -f2 | cut -d '.' -f2)"
if [ "$VAULT_VERSION" = "0.9.1" ] || [ "$VAULT_MIN_VERSION" -lt "9" ]
then
export VAULT_CLI_CAP="0"
else
export VAULT_CLI_CAP="1"
fi
fi
}
# -----------------------------------------------------------------------
# Check for signs of execution in a Katakoda environment
# -----------------------------------------------------------------------
check_katacoda() {
if [ "$(hostname)" = "host01" ]
then
if [ "$(hostname)" = "host01" ]
then
# This is probably Katakoda host01
msg info "Detected Katakoda environment."
printf 1
fi
else
printf 0
fi
}
# -----------------------------------------------------------------------
# Check for desired Vaultron flavor and default to Consul if none set
# -----------------------------------------------------------------------
check_flavor() {
if [ -z "$TF_VAR_vault_flavor" ]
then
TF_VAR_vault_flavor=consul
else
if [ "$TF_VAR_vault_flavor" != "consul" ]
then
if [ "$TF_VAR_vault_flavor" != "raft" ]
then
msg alert "Cannot use flavor $TF_VAR_vault_flavor"
msg info "Currently supported flavors are: \"consul\", \"raft\""
exit 1
fi
fi
fi
}
# -----------------------------------------------------------------------
# Check installed Vaultron flavor based on presence of the tfstate
# -----------------------------------------------------------------------
check_installed_flavor() {
if ls flavors/consul/tfstate/terraform.tfstate > /dev/null 2>&1
then
export TF_VAR_vault_flavor=consul
else
if ls flavors/raft/tfstate/terraform.tfstate > /dev/null 2>&1
then
export TF_VAR_vault_flavor=raft
else
msg alert "Cannot determine the installed Vaultron flavor!"
msg info "Please use lion_torches to clean up instead."
exit 1
fi
fi
# msg info "Detected $TF_VAR_vault_flavor flavor installed."
}
# -----------------------------------------------------------------------
# Check for Docker proc and socket and bail out if either is absent
# -----------------------------------------------------------------------
check_docker() {
DOCK_PROC_COUNT="$(pgrep docker | wc -l)"
if [ "$DOCK_PROC_COUNT" -lt 1 ]
then
msg alert "Docker daemon process not detected- cannot form Vaultron."
exit 1
else
if ! docker ps >> "$VAULTRON_LIFECYCLE_LOG" 2>&1
then
msg alert "Cannot connect to Docker daemon- cannot form Vaultron."
exit 1
fi
fi
if ! [ -S /var/run/docker.sock ]
then
if [ -z $DOCKER_HOST ]
then
msg alert "Docker host or socket not detected- cannot form Vaultron."
exit 1
fi
fi
}
# -----------------------------------------------------------------------
# Basic connectivity check
# -----------------------------------------------------------------------
check_vault() {
if command nc -h >> "$VAULTRON_LIFECYCLE_LOG" 2>&1
then
if ! nc -z localhost 8200 >> "$VAULTRON_LIFECYCLE_LOG" 2>&1
then
msg alert "Cannot connect to Vault at $VAULT_ADDR."
msg info "Make sure to ./form Vaultron before trying blazing_sword."
exit 1
fi
elif command timeout >> "$VAULTRON_LIFECYCLE_LOG" 2>&1
then
if ! timeout 5 bash -c '</dev/tcp/localhost/8200' >> "$VAULTRON_LIFECYCLE_LOG" 2>&1
then
msg alert "Cannot connect to Vault at $VAULT_ADDR."
msg info "Make sure to ./form Vaultron before trying blazing_sword."
exit 1
fi
elif command gtimeout >> "$VAULTRON_LIFECYCLE_LOG" 2>&1
then
if ! gtimeout 5 bash -c '</dev/tcp/localhost/8200' >> "$VAULTRON_LIFECYCLE_LOG" 2>&1
then
msg alert "Cannot connect to Vault at $VAULT_ADDR."
msg info "Make sure to ./form Vaultron before trying blazing_sword."
exit 1
fi
fi
}
# -----------------------------------------------------------------------
# Terraform specific bits
# -----------------------------------------------------------------------
tfmsg() {
tfmsg_out="$(echo "$1" | awk '{
# strip control characters for printing and matching
gsub(/\033\[[0-9]+m/,"")
}
/^(Apply complete|Destroy complete|Plan:)/ {
print "info"
print "Terraform", tolower($0)
exit
}
/^Terraform.*initialized!/ {
print "info"
print
exit
}
/^([0-9]+ error\(s\) occurred:|Failed to load backend)/ {
print "alert"
sub(/:.*/,"")
print "Terraform", tolower($0)
exit
}')"
if [ -n "$tfmsg_out" ]
then
msg "$(echo "$tfmsg_out" | head -1)" \
"$(echo "$tfmsg_out" | tail -1)"
fi
}
tflogdir() {
if ! [ -d ./log ]
then
mkdir ./log
fi
}
init() {
tfout="$(terraform init 2>&1)"
ret=$?
init_out="./log/tf-$(date -u "+%Y-%m-%dT%H:%M:%SZ")-init.log"
echo "$tfout" > "$init_out"
tfmsg "$tfout"
return $ret
}
apply() {
tfout="$(terraform apply -auto-approve "$1" 2>&1)"
ret=$?
apply_out="./log/tf-$(date -u "+%Y-%m-%dT%H:%M:%SZ")-apply.log"
echo "$tfout" > "$apply_out"
tfmsg "$tfout"
return $ret
}
plan() {
tfout="$(terraform plan -out="$1" 2>&1)"
ret=$?
plan_out="./log/tf-$(date -u "+%Y-%m-%dT%H:%M:%SZ")-plan.log"
echo "$tfout" > "$plan_out"
tfmsg "$tfout"
return $ret
}
destroy() {
tfout="$(terraform destroy -auto-approve -state=./tfstate/terraform.tfstate 2>&1)"
echo "$tfout" > ./log/tf-"$(date -u "+%Y-%m-%dT%H:%M:%SZ")"-destroy.log
tfmsg "$tfout"
return $ret
}