-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.sh
executable file
·413 lines (372 loc) · 15.5 KB
/
action.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env bash
# Need tools:
needTools=()
needTools+=( terraform )
needTools+=( ansible )
needTools+=( ansible-playbook )
needTools+=( ansible-inventory )
needTools+=( kubectl )
needTools+=( jq )
needTools+=( awk )
needTools+=( grep )
needTools+=( tr )
needTools+=( wc )
for command in ${needTools[@]}; do
if [ ! "$(command -v ${command})" ]; then
echo "command \"${command}\" does not exist on the system"
exit 1
fi
done
working_dir="$(dirname $0)"
thisScriptPath="${working_dir}/$(basename "$0")"
sshKeyPath="${working_dir}/tf_k8s.pem"
# Apply lock file to avoid multiple run in the same time
# Due to the issue with terraform and ansible, ansible is depended on configuration file that is rendered by terraform
# We just allow to run only one process at once
# Refer: https://www.putorius.net/lock-files-bash-scripts.html
internalCallMark="internalCallMark"
# Check the first argument to know is it called form internal?
if [[ "${internalCallMark}" != "${1}" ]]; then
# Don't apply lock strategy for internal call
# Check is Lock File exists, if not create it and set trap on exit
if { set -C; 2>/dev/null > "${thisScriptPath}.lock"; }; then
trap "rm -f ${thisScriptPath}.lock" EXIT
else
echo "This script is running in another process, try again later"
exit 1
fi
else
# Remove the first argument
shift 1
fi
function sigint_func()
{
exit 0
}
trap sigint_func SIGINT
kubectl_func(){
kubectl --kubeconfig="${working_dir}/kubeconfig" "$@"
return $?
}
terraform_func(){
terraform -chdir="${working_dir}" "$@"
return $?
}
ansible_playbook_func(){
ANSIBLE_CONFIG="${working_dir}/ansible.cfg" ansible-playbook --flush-cache "$@"
return $?
}
ansible_inventory_func(){
ANSIBLE_CONFIG="${working_dir}/ansible.cfg" ansible-inventory "$@"
return $?
}
ansible_galaxy_func(){
ANSIBLE_CONFIG="${working_dir}/ansible.cfg" ansible-galaxy "$@"
return $?
}
main(){
case "${1}" in
init)
shift 1
# Check current infrastructure
if [[ -n "$(terraform_func state list)" ]]; then
echo "Cluster is existed"
return
fi
# Terraform init
if ! terraform_func init; then
echo "Check your terraform configuration and try again"
return 1
fi
# Create infrastructure
local count
count=1
while true; do
if terraform_func apply -auto-approve; then
break 1
fi
# Give it 3 times to try
if (( $(echo "${count} >= 3" | bc -l) )); then
return 1
fi
((count++))
sleep 1
done
# Install ansible requirements
ansible_galaxy_func install -r "${working_dir}/requirements.ansible-galaxy.yml"
# Install cluster
local count
count=1
while true; do
if ansible_playbook_func "${working_dir}/k8s.playbook.yml" --limit 'masters,master_lbs,workers,worker_lbs'; then
break 1
fi
# Give it 3 times to try
if (( $(echo "${count} >= 3" | bc -l) )); then
return 1
fi
((count++))
sleep 1
done
kubectl_func get nodes -o wide
return $?
;;
scale)
shift 1
# Get current instances
local master_count
master_count=$(terraform_func state list | grep -oP "(?<=aws_lightsail_instance.master\[)\d+" | wc -l)
local master_lb_count
master_lb_count=$(terraform_func state list | grep -oP "(?<=aws_lightsail_instance.master_lb\[)\d+" | wc -l)
local worker_count
worker_count=$(terraform_func state list | grep -oP "(?<=aws_lightsail_instance.worker\[)\d+" | wc -l)
local worker_lb_count
worker_lb_count=$(terraform_func state list | grep -oP "(?<=aws_lightsail_instance.worker_lb\[)\d+" | wc -l)
local currentTerraformVarOptions
currentTerraformVarOptions="-var="master_count=${master_count}" -var="master_lb_count=${master_lb_count}" -var="worker_count=${worker_count}" -var="worker_lb_count=${worker_lb_count}""
local nodeRole
# By default: nodeRole is worker
nodeRole="worker"
if [[ "${2}" == "master" ]]; then
nodeRole="master"
elif [[ "${2}" == "worker_lb" ]]; then
nodeRole="worker_lb"
fi
case "${1}" in
up)
shift 1
local ansibleLimit
ansibleLimit="master_1,worker_lbs,worker_$((worker_count +1))"
local terraformVarOptions
terraformVarOptions="${currentTerraformVarOptions} -var="worker_count=$((worker_count+1))""
if [[ "${nodeRole}" == "master" ]]; then
# Must add master_lb to update loadbalancing configuration
ansibleLimit="master_1,master_lb_1,master_$((master_count +1))"
terraformVarOptions="${currentTerraformVarOptions} -var="master_count=$((master_count+1))""
elif [[ "${nodeRole}" == "worker_lb" ]]; then
ansibleLimit="worker_lb_$((worker_lb_count +1))"
terraformVarOptions="${currentTerraformVarOptions} -var="worker_lb_count=$((worker_lb_count+1))""
fi
# Scale up infrastructure
local count
count=1
while true; do
if terraform_func apply -auto-approve ${terraformVarOptions}; then
sleep 15
break 1
fi
# Give it 3 times to try
if (( $(echo "${count} >= 3" | bc -l) )); then
return 1
fi
((count++))
sleep 1
done
# Run ansible playbook but limit to:
# master_1: get join command
# The new node: install components and join
# Just run tasks with tag addNode
local count
count=1
while true; do
if ! ansible_playbook_func "${working_dir}/k8s.playbook.yml" --limit "${ansibleLimit}" --tags addNode; then
local count1
count1=1
while true; do
if terraform_func apply -auto-approve ${currentTerraformVarOptions}; then
break 1
fi
# Give it 3 times to try
if (( $(echo "${count1} >= 3" | bc -l) )); then
return 1
fi
((count1++))
sleep 1
done
fi
# Give it 3 times to try
if (( $(echo "${count} >= 3" | bc -l) )); then
return 1
fi
((count++))
sleep 1
done
sleep 15
kubectl_func get nodes -o wide
return $?
;;
down)
shift 1
# TODO: Issue when scale down master, due to the issue with the ETCD node in master
if [[ "${nodeRole}" == "master" ]]; then
echo "Scale ${nodeRole} down is not allowed this time"
return 1
fi
# Get current instances
local instance_count
instance_count=$(terraform_func state list | grep -oP "(?<=aws_lightsail_instance.${nodeRole}\[)\d+" | wc -l)
if (( $instance_count <= 1 )); then
echo "The ${nodeRole} must have at least one node"
return 1
fi
local private_ip
private_ip=$(ansible_inventory_func --host "${nodeRole}_${instance_count}" | jq .private_ip | tr -d '"')
if [[ "${nodeRole}" == "worker" ]]; then
local node_name
while [[ -z "${node_name}" ]]; do
node_name=$(kubectl_func get node -o wide | grep "${private_ip}" | awk '{ print $1 }')
if [[ -n "${node_name}" ]]; then
break 1
fi
# when the node is in inventory but not in kubernetes nodes
# Don't forget to put internalCallMark to notify this call is from internal
eval "${working_dir}/$(basename "$0") ${internalCallMark} refresh"
done
# Kubernetes cordon
kubectl_func cordon "${node_name}"
# Kubernetes drain
if kubectl_func drain "${node_name}" --ignore-daemonsets; then
# Kubernetes delete node
kubectl_func delete node "${node_name}"
# Scale down infrastructure
local terraformVarOptions
terraformVarOptions="${currentTerraformVarOptions} -var="worker_count=$((worker_count-1))""
if [[ "${nodeRole}" == "master" ]]; then
terraformVarOptions="${currentTerraformVarOptions} -var="master_count=$((master_count-1))""
fi
local count
count=1
while true; do
if terraform_func apply -auto-approve ${terraformVarOptions}; then
if ansible_playbook_func "${working_dir}/k8s.playbook.yml" --limit "worker_lbs" --tags deleteNode; then
break 1
fi
fi
# Give it 3 times to try
if (( $(echo "${count} >= 3" | bc -l) )); then
return 1
fi
((count++))
sleep 1
done
fi
kubectl_func get nodes -o wide
return $?
elif [[ "${nodeRole}" == "worker_lb" ]]; then
# Scale down infrastructure
local terraformVarOptions
terraformVarOptions="${currentTerraformVarOptions} -var="worker_lb_count=$((worker_lb_count-1))""
local count
count=1
while true; do
if terraform_func apply -auto-approve ${terraformVarOptions}; then
break 1
fi
# Give it 3 times to try
if (( $(echo "${count} >= 3" | bc -l) )); then
return 1
fi
((count++))
sleep 1
done
else
return 1
fi
;;
*)
echo "Please choose:"
echo "up"
echo "down"
exit 1
;;
esac
;;
destroy)
shift 1
local count
count=1
while true; do
if terraform_func apply -auto-approve -destroy; then
# Remove kubeconfig, created by ansible
rm -f "${working_dir}/kubeconfig"
# Remove key
rm -f ${sshKeyPath}*
break 1
fi
# Give it 3 times to try
if (( $(echo "${count} >= 3" | bc -l) )); then
return 1
fi
((count++))
sleep 1
done
return 0
;;
refresh)
shift 1
local count
count=1
while true; do
if ansible_playbook_func "${working_dir}/k8s.playbook.yml"; then
break 1
fi
# Give it 3 times to try
if (( $(echo "${count} >= 3" | bc -l) )); then
return 1
fi
((count++))
sleep 1
done
return 0
;;
*)
local thisScriptPath
thisScriptPath="${working_dir}/$(basename "$0")"
local actions
actions=()
#####################################
#####################################
#### Declare the list of actions ####
actions+=("init")
actions+=("scale up")
actions+=("scale down")
actions+=("scale up master")
actions+=("scale up worker_lb")
actions+=("scale down worker_lb")
actions+=("refresh")
actions+=("destroy")
#####################################
#####################################
#####################################
declare -A actionMap
for action in "${actions[@]}"; do
# Replace all space to dash (-)
actionMap["${action// /-}"]="${action}"
done
local actionKeys
actionKeys=()
for actionKey in "${!actionMap[@]}"; do
actionKeys+=(${actionKey})
done
IFS=$'\n' sortedActionKeys=($(sort <<<"${actionKeys[*]}")); unset IFS
# Load lib for selection feature
source "$working_dir/shell_libs/bash_selection_lib.sh"
selected_item=0
run_menu "$selected_item" "${sortedActionKeys[@]}"
menu_result="$?"
echo
local actionKeyChose
actionKeyChose="${sortedActionKeys[${menu_result}]}"
if [[ -z "${actionKeyChose}" ]]; then
echo "Error"
return 1
fi
echo "You chose: ${actionMap[${actionKeyChose}]}"
# Don't forget to put internalCallMark to notify this call is from internal
eval "${thisScriptPath} ${internalCallMark} ${actionMap[${actionKeyChose}]}"
return $?
;;
esac
return 0
}
main "${@}"