-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap-ha.sh
executable file
·477 lines (454 loc) · 14.7 KB
/
bootstrap-ha.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/bin/bash
usage () {
echo "bootstrap-ha.sh [create | delete] <args>
args:
-n name to use for all resources and cluster default: rancher-lab
-d domain for rancher/server (uses letsencrypt certificate) default: none
-e email for letsencrypt certificate default: postmaster@<domain from -d>
-i pathname to your public SSH key default: ~/.ssh/id_rsa.pub
-p optional | provider (aws only so far) default: aws
-r optional | region to run the resources default: us-east-1
-c optional | number of nodes to launch default: 3
-a optional | i'm feeling lucky (yes to everything) default: prompt me
-z optional | instance type default: t3a.medium
-k optional | RKE cluster only, don't install rancher/server default: run everything
-o optional | terraform only default: run everything
-s optional | rancher server only default: run everything
-t optional | rancher/server tag, eg: v2.3.0 default: stable
-l optional | don't create a load balancer default: create an NLB
example:
Create everything, but prompt during the functions:
bootstrap-ha.sh create -n rancher-lab -r us-east-1 -i ~/.ssh/id_rsa.pub -d r.domain.com
Just create/update terraform, useful for updating admin security group
bootstrap-ha.sh create -n rancher-lab -d r.domain.com -o"
}
testcredentials () {
case ${_provider} in
aws)
if aws --version > /dev/null 2>&1
then
_iam=`aws sts get-caller-identity | jq -r '.Arn'`
if [ $? -eq 0 ]
then
echo "[Info] ${_scope} | Using the following IAM entity: ${_iam}"
else
echo "[Error] ${_scope} | We'll need the AWS CLI configured with credentials, please configure and run me again"
exit 1
fi
fi
;;
*)
echo "Sorry, on the to do list"
exit 1
;;
esac
}
testprereqs () {
for _command in aws terraform rke curl jq
do
if ! ${_command} --version > /dev/null 2>&1
then
echo "[Error] ${_scope} | Can't find the ${_command}, please check or install it"
exit 1
fi
done
}
adminip () {
_pubip="$(curl -s ifconfig.io)"
if echo $_pubip | grep "." > /dev/null 2>&1
then
_adminip="$_pubip/32"
elif echo $_pubip | grep ":" > /dev/null 2>&1
then
_adminip="$_pubip/64"
fi
}
importkey () {
if [ ! -f ${_pubsshkey} ]
then
echo "[Info] ${_scope} | Ooops, it looks like that public SSH key doesn't exist"
echo "Shall I create a key for you?"
echo -n " y/n: "
read _reply
if [[ ! ${_reply} =~ ^[Yy]$ ]]
then
echo -e "\n Ok, lets revist this later..."
exit 1
else
ssh-keygen -t rsa -b 2048 -f ~/.ssh/${_name}
_pubsshkey="~/.ssh/${_name}"
ssh-add ~/.ssh/${_name}
fi
fi
case ${_provider} in
aws)
aws ec2 describe-key-pairs --key-name ${_name}-keypair --region ${_region} > /dev/null 2>&1
if [ $? -ne 0 ]
then
aws ec2 import-key-pair --region ${_region} --key-name ${_name}-keypair --public-key-material file://${_pubsshkey}
else
echo "[Info] ${_scope} | Skipping key pair, it already exists"
fi
;;
*)
echo "Sorry, on the to do list"
exit 1
;;
esac
if ! ps -a | grep ssh-agent > /dev/null 2>&1
then
echo "[Error] ${_scope} | Looks like the ssh-agent isn't running, please make sure it's running and ssh-add your private SSH key"
exit 1
fi
}
terraformvars () {
## Collect VPC details from VPC terraform state
_publicsubnet="$(terraform output --state=./terraform.vpc.${_region}.tfstate public-subnet)"
_vpc="$(terraform output --state=./terraform.vpc.${_region}.tfstate vpc)"
if [[ -n ${_name} && -n ${_region} ]]
then
cat <<- EOF > .${_region}.${_name}.ha.terraform.tfvars
name = "${_name}"
region = "${_region}"
admin-ip = "${_adminip}"
key-name = "${_name}-keypair"
nodes = ${_nodes}
load-balancer = ${_nlb}
vpc = "${_vpc}"
public-subnet = ${_publicsubnet}
instance-type = "${_instancetype}"
EOF
fi
}
terraformapply () {
if [ ! -d .terraform ]
then
if ! terraform init terraform/${_provider}/rancher-ha > /dev/null 2>&1
then
echo "[Error] ${_scope} | Something went wrong with initialising terraform"
exit 1
fi
fi
terraform apply -var-file=./.${_region}.${_name}.ha.terraform.tfvars -state=./terraform.ha.${_region}.${_name}.tfstate ${_imfeelinglucky} terraform/${_provider}/rancher-ha
if [ $? -ne 0 ]
then
echo "[Error] ${_scope} | Something went wrong with applying terraform"
exit 1
fi
}
buildclusteryml () {
if [ ! -f ${_configdir}/.example.cluster.yml ]
then
echo "[Error] ${_scope} | Appears my .example.rancher-cluster.yml file is missing, i'll need that, a little help please..."
exit 1
fi
_clusteryaml="${_configdir}/${_name}-ha.cluster.yml"
if [ ! -f ${_clusteryaml} ]
then
cp ${_configdir}/.example.cluster.yml ${_clusteryaml}
else
echo "[Info] ${_scope} | Looks like a cluster.yml exists, taking a backup"
mv ${_clusteryaml} ${_configdir}/.bkp.`date "+%F-%T"`.${_name}.cluster.yml
cp ${_configdir}/.example.cluster.yml ${_clusteryaml}
fi
if [ ${_provider} = "aws" ]
then
aws ec2 describe-instances --filters "Name=tag:Name,Values=${_name}-ha-asg" "Name=instance-state-name,Values=running" --region ${_region} | jq -r '.Reservations[].Instances[] | .PublicIpAddress + " " + .PrivateIpAddress' > ${_configdir}/.tmp.nodes.txt
_running=`cat ${_configdir}/.tmp.nodes.txt | wc -l`
if [ ${_running} -eq ${_nodes} ]
then
for _num in `seq 1 ${_nodes}`
do
pub_ip=`awk '{ if (NR=='$_num') print $1 }' ${_configdir}/.tmp.nodes.txt`
priv_ip=`awk '{ if (NR=='$_num') print $2 }' ${_configdir}/.tmp.nodes.txt`
sed -i '' "s|n${_num}_public_ip|${pub_ip}|; s|n${_num}_private_ip|${priv_ip}|" ${_clusteryaml}
done
sed -i '' "s|^cluster_name.*|cluster_name: ${_name}|" ${_clusteryaml}
else
echo "[Error] ${_scope} | Number of nodes is not what we expected, ${_running} when we should have ${_nodes}"
exit 1
fi
rm ${_configdir}/.tmp.nodes.txt
fi
}
waitfornodes () {
_node_ips=`grep ' address:' ${_clusteryaml} | awk '{print $3}'`
echo "Checking nodes are online (.) and Docker is started (_)"
for _ip in ${_node_ips}
do
echo -n "$_ip: "
while ! ping -c1 $_ip >/dev/null 2>&1
do
sleep 2
echo -n "."
done
while ! ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $_ip "docker info > /dev/null 2>&1 | echo $?" >/dev/null 2>&1
do
sleep 2
echo -n "_"
done
echo
done
}
rkeup () {
echo "[Info] ${_scope} | Running RKE to build the rancher server cluster"
rke up --ssh-agent-auth --config ${_clusteryaml}
if [ $? -ne 0 ]
then
echo "[Error] ${_scope} | Something went wrong with 'rke up'"
exit 1
fi
}
checknodes () {
export KUBECONFIG="`pwd`/${_configdir}/kube_config_${_name}-ha.cluster.yml"
kubectl get nodes -o wide
}
installcertmanager () {
echo "[Info] ${_scope} | Starting cert-manager install"
kubectl create namespace cert-manager
helm repo add jetstack https://charts.jetstack.io
helm repo update
helm install cert-manager jetstack/cert-manager \
--namespace cert-manager \
--version v0.12.0
# wait for the webhook to be ready
kubectl wait -n cert-manager pod --selector app=webhook --for condition=ready --timeout=60s
sleep 2
}
installrancher () {
echo "[Info] ${_scope} | Starting Rancher Server install"
helm repo add rancher-latest https://releases.rancher.com/server-charts/latest
helm repo update
kubectl create namespace cattle-system
helm install rancher rancher-latest/rancher \
--namespace cattle-system \
--set hostname=${_serverdomain} \
--set ingress.tls.source=letsEncrypt \
--set letsEncrypt.email=${_email} \
--set rancherImageTag=${_ranchertag}
kubectl -n cattle-system rollout status deploy/rancher -w
helm ls
}
rkeremove () {
echo "[Info] ${_scope} | Running RKE to remove the rancher server cluster"
_clusteryaml="${_configdir}/${_name}-ha.cluster.yml"
_rkestate="${_configdir}/${_name}-ha.cluster.rkestate"
_kubeconfig="${_configdir}/kube_config_${_name}-ha.cluster.yml"
cp ${_clusteryaml} ${_configdir}/.bkp.`date "+%F-%T"`.${_name}.cluster.yml
cp ${_rkestate} ${_configdir}/.bkp.`date "+%F-%T"`.${_name}.cluster.rkestate
cp ${_kubeconfig} ${_configdir}/.bkp.`date "+%F-%T"`.kube_config_${_name}.cluster.yml
rke remove --ssh-agent-auth --config ${_clusteryaml}
if [ $? -ne 0 ]
then
echo "[Error] ${_scope} | Something went wrong with 'rke remove'"
exit 1
fi
rm ${_clusteryaml}
}
terraformdestroy () {
if [ -z ${_imfeelinglucky} ]
then
echo "Careful there, are you sure you want to destory everything?"
echo -n " y/n: "
read _reply
if [[ ! ${_reply} =~ ^[Yy]$ ]]
then
echo -e "\n Ok, lets revist this later..."
exit 1
fi
fi
terraform destroy -var-file=./.${_region}.${_name}.ha.terraform.tfvars -state=./terraform.ha.${_region}.${_name}.tfstate ${_imfeelinglucky} terraform/${_provider}/rancher-ha
if [ $? -ne 0 ]
then
echo "[Error] ${_scope} | Something went wrong with terraform"
exit 1
fi
rm .${_region}.${_name}.ha.terraform.tfvars
rm terraform.ha.${_region}.${_name}.tfstate*
}
deletekey () {
case ${_provider} in
aws)
aws ec2 delete-key-pair --region ${_region} --key-name ${_name}-keypair
;;
*)
echo "Sorry, on the to do list"
exit 1
;;
esac
}
case "$1" in
create)
_create=1
shift 1
;;
delete)
_delete=1
shift 1
;;
esac
while getopts "khlsahot:n:i:r:p:d:c:e:z:" opt
do
case ${opt} in
a)
_imfeelinglucky="-auto-approve"
;;
n)
_opt_name=${OPTARG}
;;
i)
_opt_pubsshkey=${OPTARG}
;;
r)
_opt_region=${OPTARG}
;;
p)
_opt_provider=${OPTARG}
;;
c)
_opt_nodes=${OPTARG}
;;
k)
_dontinstallrancher=1
;;
o)
_terraformonly=1
;;
s)
_rancheronly=1
;;
t)
_opt_ranchertag=${OPTARG}
;;
d)
_serverdomain=${OPTARG}
;;
e)
_opt_email=${OPTARG}
;;
z)
_opt_instancetype=${OPTARG}
;;
l)
_opt_nlb=0
;;
h)
usage
exit 1
;;
*)
usage
exit 1
;;
esac
done
# Defaults
_nlb=${_opt_nlb:-1}
_region=${_opt_region:-us-east-1}
_name=${_opt_name:-rancher-lab}
_pubsshkey=${_opt_pubsshkey:-~/.ssh/id_rsa.pub}
_provider=${_opt_provider:-aws}
_nodes=${_opt_nodes:-3}
_ranchertag=${_opt_ranchertag:-stable}
_email=${_opt_email:-postmaster@$_serverdomain}
_instancetype=${_opt_instancetype:-t3a.medium}
_configdir=config
_scope=HA
if ! [[ -n "${_create}" || -n "${_delete}" ]]
then
echo "[Error] ${_scope} | Sorry, I need a create or delete operation, check out the -h usage."
exit 1
fi
if [ -n "${_create}" ]
then
if [ -z "${_serverdomain}" ]
then
echo "[Error] ${_scope} | Hey, looks like we didn't get a server domain, to create valid certs I'll need a valid domain, please rerun with the '-d' flag"
exit 1
fi
if [ -n "${_rancheronly}" ]
then
checknodes
installcertmanager
installrancher
exit 0
fi
testcredentials
testprereqs
adminip
importkey
if [ -z "${_nodes}" ]
then
_nodes=3 # default
fi
terraformvars
if [ -n "${_terraformonly}" ]
then
terraformapply
exit 0
fi
if [ -z "${_imfeelinglucky}" ]
then
for _function in terraformapply buildclusteryml rkeup checknodes
do
echo "[Info] ${_scope} | We're ready to start the ${_function} function, are we good to go?"
echo -n " y/n: "
read _reply
if [[ ! ${_reply} =~ ^[Yy]$ ]]
then
echo -e "\nOk, lets revist this later..."
exit 1
else
echo -e "\n[Info] ${_scope} | Starting ${_function} now"
${_function}
if [ ${_function} = "buildclusteryml" ]
then
waitfornodes
fi
fi
done
else
terraformapply
buildclusteryml
waitfornodes
rkeup
checknodes
if [ -z "${_dontinstallrancher}" ]
then
installcertmanager
installrancher
fi
fi
echo; terraform output -state=./terraform.ha.${_region}.${_name}.tfstate
echo; echo "--- Use the following command to interact with the new cluster:"
echo; echo "export KUBECONFIG="`pwd`/${_configdir}/kube_config_${_name}-ha.cluster.yml""
fi
if [ -n "${_delete}" ]
then
if [ -n "${_terraformonly}" ]
then
terraformdestroy
exit 0
fi
if [ -z "${_imfeelinglucky}" ]
then
for _function in rkeremove terraformdestroy deletekey
do
echo "[Info] ${_scope} | We\'re ready to start the ${_function} function, are we good to go?"
echo -n " y/n: "
read _reply
if [[ ! ${_reply} =~ ^[Yy]$ ]]
then
echo -e "\nOk, lets revist this later..."
exit 1
else
echo -e "\n[Info] ${_scope} | Starting ${_function} now"
${_function}
fi
done
else
rkeremove
terraformdestroy
deletekey
fi
fi