forked from TritonDataCenter/triton-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup.sh
executable file
·733 lines (693 loc) · 34.2 KB
/
setup.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#!/usr/bin/env bash
set -o errexit
set -o pipefail
# set -o xtrace
db_name=cattle
db_user=cattle
db_pass=cattle
TERRAFORM=$(pwd)/bin/terraform
main() {
if [ ! -e $TERRAFORM ]; then
echo "Getting the correct version of terraform ..."
# Detect the platform
rm -rf bin || true
mkdir bin
cd bin
OS="`uname`"
case $OS in
'Linux')
wget https://releases.hashicorp.com/terraform/0.9.11/terraform_0.9.11_linux_amd64.zip
unzip -o terraform_0.9.11_linux_amd64.zip
rm terraform_0.9.11_linux_amd64.zip
cd ..
;;
'FreeBSD')
wget https://releases.hashicorp.com/terraform/0.9.11/terraform_0.9.11_freebsd_amd64.zip
unzip terraform_0.9.11_freebsd_amd64.zip
rm terraform_0.9.11_freebsd_amd64.zip
cd ..
;;
'Darwin')
wget https://releases.hashicorp.com/terraform/0.9.11/terraform_0.9.11_darwin_amd64.zip
unzip terraform_0.9.11_darwin_amd64.zip
rm terraform_0.9.11_darwin_amd64.zip
cd ..
;;
'SunOS')
wget https://releases.hashicorp.com/terraform/0.9.11/terraform_0.9.11_solaris_amd64.zip
unzip terraform_0.9.11_solaris_amd64.zip
rm terraform_0.9.11_solaris_amd64.zip
cd ..
;;
*)
cd ..
echo "Couldn't determine os type."
exit 1
;;
esac
echo ""
echo "Terraform for $OS added to $(pwd)/bin/ directory."
echo ""
fi
if [[ ! -z "$1" && "$1" == "-c" ]]; then
cleanRunner
exit 0
fi
if [ -e terraform/rancher.tf ]; then
echo "error: configuration for a previous run has been found"
echo " clean the configuration (./setup -c)"
exit
fi
mkdir ansible/tmp > /dev/null 2>&1 || true
# SET default variables
setVarDefaults
# SET configuration from current triton profile (triton account information)
setConfigFromTritonENV
# GET updated configuration from user
getConfigFromUser
# VERIFY with user that parameters are correct
verifyConfig
# UPDATE config file with parameters
setConfigToFile
exportVars
echo "################################################################################"
echo "### Starting terraform tasks..."
echo "################################################################################"
sleep 2
runTerraformTasks
echo "################################################################################"
echo "### Creating ansible configs..."
echo "################################################################################"
sleep 2
createAnsibleConfigs
echo "################################################################################"
echo "### Running ansible tasks..."
echo "################################################################################"
sleep 2
runAnsible
echo ""
echo "Congratulations, your Kubernetes cluster setup has been complete."
echo ""
echo "It will take a few minutes for all the Kubernetes process to start up before you can access Kubernetes Dashboard"
echo "----> To check what processes/containers are coming up, go to http://$(tail -1 terraform/masters.ip):8080/env/$(cat ansible/tmp/kubernetes_environment.id)/infra/containers"
echo " once all these containers are up, you should be able to access Kubernetes by its dashboard or using CLI"
echo "Waiting on Kubernetes dashboard to come up."
echo ""
KUBERNETES_DASHBOARD_UP=false
DASHBOARD_CONTAINER_COUNT=0
while ! "$KUBERNETES_DASHBOARD_UP"; do
echo -ne "."
sleep 1
if ! ((`date +%s` % 15)); then
DASHBOARD_CONTAINER_COUNT=0
cRetVal=$(curl --connect-timeout 5 --max-time 5 -s http://$(tail -1 terraform/masters.ip):8080/r/projects/$(cat ansible/tmp/kubernetes_environment.id)/kubernetes-dashboard:9090/)
if [ $(echo $cRetVal | grep -i kubernetes | wc -l) -ne 0 ]; then
KUBERNETES_DASHBOARD_UP=true
# BUG kill stuck dashboard container
else
for node in $(cat terraform/hosts.ip); do
let DASHBOARD_CONTAINER_COUNT=$DASHBOARD_CONTAINER_COUNT+$(ssh -o StrictHostKeyChecking=no root@$node docker ps | grep kubernetes-dashboard | awk '{print $1}' | wc -w | sed "s/ //g") || true
done
if [[ $DASHBOARD_CONTAINER_COUNT -eq 2 && $(curl --connect-timeout 5 --max-time 5 -s http://$(tail -1 terraform/masters.ip):8080/r/projects/$(cat ansible/tmp/kubernetes_environment.id)/kubernetes-dashboard:9090/ | grep -i "Service Unavailable" | wc -l) -ne 0 ]]; then
for node in $(cat terraform/hosts.ip); do
dashboard_container="$(ssh -o StrictHostKeyChecking=no root@$node docker ps | grep k8s_POD.*dashboard | awk '{print $1}')" || true
if [ "${dashboard_container:-}" != "" ]; then
ssh -o StrictHostKeyChecking=no root@$node "docker stop -t0 $dashboard_container" >> /dev/null 2>&1
fi
echo -ne "."
done
fi
fi
fi
done
echo ""
echo "----> Kubernetes dashboard is at http://$(tail -1 terraform/masters.ip):8080/r/projects/$(cat ansible/tmp/kubernetes_environment.id)/kubernetes-dashboard:9090/"
echo "----> Kubernetes CLI config is at http://$(tail -1 terraform/masters.ip):8080/env/$(cat ansible/tmp/kubernetes_environment.id)/kubernetes/kubectl"
echo ""
echo " CONGRATULATIONS, YOU HAVE CONFIGURED YOUR KUBERNETES ENVIRONMENT!"
}
getArgument() {
# $1 message
# $2 default
while true; do
if [ -z ${2+x} ]; then
read -p "$1 " theargument
else
read -p "$1 ($2) " theargument
if [ -z "$theargument" ]; then
echo $2
else
echo $theargument
fi
break
fi
done
}
runAnsible() {
cd ansible
ansible-playbook -i hosts dockerSetup.yml
if $RANCHER_HA; then
ansible-playbook -i hosts rancherServers.yml
else
ansible-playbook -i hosts rancherServer.yml
fi
echo "Waiting for Rancher to come up ..."
echo ""
while [[ $(curl --connect-timeout 5 --max-time 5 -s http://$(tail -1 ../terraform/masters.ip):8080/ | grep $(tail -1 ../terraform/masters.ip) | wc -l) -eq 0 ]]; do
echo -ne "."
sleep 1
done
echo ""
sleep 5 # wait few seconds before creating environment
if $SEPARATE_PLANE; then
curl -X PUT -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"accountId":null, "data":{"fields":{"stacks":[{"name":"healthcheck", "templateId":"library:infra*healthcheck"}, {"name":"kubernetes", "templateId":"library:infra*k8s", "answers":{"CONSTRAINT_TYPE":"required", "CLOUD_PROVIDER":"rancher", "REGISTRY":"", "DISABLE_ADDONS":"false", "POD_INFRA_CONTAINER_IMAGE":"gcr.io/google_containers/pause-amd64:3.0", "INFLUXDB_HOST_PATH":"", "EMBEDDED_BACKUPS":true, "BACKUP_PERIOD":"15m0s", "BACKUP_RETENTION":"24h", "ETCD_HEARTBEAT_INTERVAL":"500", "ETCD_ELECTION_TIMEOUT":"5000"}}, {"name":"network-services", "templateId":"library:infra*network-services"}, {"name":"ipsec", "templateId":"library:infra*ipsec"}]}}, "description":"Default Kubernetes template", "externalId":"catalog://library:project*kubernetes:0", "id":0, "isPublic":true, "kind":"projectTemplate", "name":"Kubernetes", "removeTime":null, "removed":null, "state":"active", "transitioning":"no", "transitioningMessage":null, "transitioningProgress":0, "stacks":[{"type":"catalogTemplate", "name":"healthcheck", "templateId":"library:infra*healthcheck"}, {"type":"catalogTemplate", "answers":{"CONSTRAINT_TYPE":"required", "CLOUD_PROVIDER":"rancher", "REGISTRY":"", "DISABLE_ADDONS":"false", "POD_INFRA_CONTAINER_IMAGE":"gcr.io/google_containers/pause-amd64:3.0", "INFLUXDB_HOST_PATH":"", "EMBEDDED_BACKUPS":true, "BACKUP_PERIOD":"15m0s", "BACKUP_RETENTION":"24h", "ETCD_HEARTBEAT_INTERVAL":"500", "ETCD_ELECTION_TIMEOUT":"5000"}, "name":"kubernetes", "templateId":"library:infra*k8s"}, {"type":"catalogTemplate", "name":"network-services", "templateId":"library:infra*network-services"}, {"type":"catalogTemplate", "name":"ipsec", "templateId":"library:infra*ipsec"}]}' "http://$(tail -1 ../terraform/masters.ip):8080/v2-beta/projecttemplates/$(cat tmp/kubernetes_template_id.id)" > /dev/null 2>&1
fi
ansible-playbook -i hosts clusterUp.yml
cd ..
}
createAnsibleConfigs() {
if [[ ! -e terraform/masters.ip || ! -e terraform/hosts.ip ]]; then
echo "Terraform had too many errors. Make sure you haven't reached your provisioning limit."
exit 1
fi
echo "Creating ansible hosts file and variable files"
rm -f ansible/hosts 2> /dev/null
if $RANCHER_HA; then
echo "[MYSQLDB]" >> ansible/hosts
cat terraform/mysqldb.ip >> ansible/hosts
fi
echo "[MASTER]" >> ansible/hosts
cat terraform/masters.ip >> ansible/hosts
echo "[HOST]" >> ansible/hosts
cat terraform/hosts.ip >> ansible/hosts
if $SEPARATE_PLANE; then
echo "[K8SHA]" >> ansible/hosts
cat terraform/k8sha.ip >> ansible/hosts
echo "[K8SETCD]" >> ansible/hosts
cat terraform/k8setcd.ip >> ansible/hosts
fi
echo " created: ansible/hosts"
master=$(tail -1 terraform/masters.ip)
echo "master: $master" > ansible/roles/ranchermaster/vars/vars.yml
if $RANCHER_HA; then
mysqldb=$(tail -1 terraform/mysqldb.ip)
echo "mysqldb: $mysqldb" >> ansible/roles/ranchermaster/vars/vars.yml
echo "db_user: $db_user" >> ansible/roles/ranchermaster/vars/vars.yml
echo "db_pass: $db_pass" >> ansible/roles/ranchermaster/vars/vars.yml
echo "db_name: $db_name" >> ansible/roles/ranchermaster/vars/vars.yml
fi
echo "kubernetes_name: \"$(echo $KUBERNETES_NAME | sed 's/"//g')\"" >> ansible/roles/ranchermaster/vars/vars.yml
echo "kubernetes_description: \"$(echo $KUBERNETES_DESCRIPTION | sed 's/"//g')\"" >> ansible/roles/ranchermaster/vars/vars.yml
cd ansible
sed "s;private_key_file = .*$;private_key_file = $(echo $SDC_KEY | sed 's/"//g');g" ansible.cfg > tmp.cfg && mv tmp.cfg ansible.cfg
cd ..
echo " created: ansible/roles/ranchermaster/vars/vars.yml"
}
runTerraformTasks() {
if [ -e terraform/rancher.tf ]
then
echo "warning: a previous terraform configuration has been found"
echo " skipping terraform configuration and execution..."
else
echo "Generating terraform configs for environment..."
updateTerraformConfig provider triton
if $RANCHER_HA; then
echo " Master hostname: ${RANCHER_MASTER_HOSTNAME}1"
echo " ${RANCHER_MASTER_HOSTNAME}2"
echo " ${RANCHER_MASTER_HOSTNAME}db"
updateTerraformConfig master $(echo ${RANCHER_MASTER_HOSTNAME}1 | sed 's/"//g')
updateTerraformConfig master $(echo ${RANCHER_MASTER_HOSTNAME}2 | sed 's/"//g')
updateTerraformConfig mysqldb $(echo ${RANCHER_MASTER_HOSTNAME}db | sed 's/"//g')
else
echo " Master hostname: $RANCHER_MASTER_HOSTNAME"
updateTerraformConfig master $(echo $RANCHER_MASTER_HOSTNAME | sed 's/"//g')
fi
for (( i = 1; i <= $KUBERNETES_NUMBER_OF_NODES; i++ ))
do
echo " Kubernetes node $i: $(echo $KUBERNETES_NODE_HOSTNAME_BEGINSWITH | sed 's/"//g')$i"
updateTerraformConfig host $(echo $KUBERNETES_NODE_HOSTNAME_BEGINSWITH$i | sed 's/"//g')
done
if $SEPARATE_PLANE; then
updateTerraformConfig k8setcd $(echo ${KUBERNETES_NODE_HOSTNAME_BEGINSWITH}etcd1 | sed 's/"//g')
updateTerraformConfig k8setcd $(echo ${KUBERNETES_NODE_HOSTNAME_BEGINSWITH}etcd2 | sed 's/"//g')
updateTerraformConfig k8setcd $(echo ${KUBERNETES_NODE_HOSTNAME_BEGINSWITH}etcd3 | sed 's/"//g')
updateTerraformConfig k8sha $(echo ${KUBERNETES_NODE_HOSTNAME_BEGINSWITH}k8smgmt1 | sed 's/"//g')
updateTerraformConfig k8sha $(echo ${KUBERNETES_NODE_HOSTNAME_BEGINSWITH}k8smgmt2 | sed 's/"//g')
updateTerraformConfig k8sha $(echo ${KUBERNETES_NODE_HOSTNAME_BEGINSWITH}k8smgmt3 | sed 's/"//g')
fi
cd terraform
echo "Starting terraform tasks"
# terraform init --plugin-dir=$GOBIN
$TERRAFORM get
$TERRAFORM apply
echo " terraform tasks completed"
cd ..
fi
}
updateTerraformConfig() {
if [ $1 == "k8sha" ]; then
echo "" >> terraform/rancher.tf
echo "module \"$2\" {" >> terraform/rancher.tf
echo " source = \"k8sha\"" >> terraform/rancher.tf
echo " hostname = \"$2\"" >> terraform/rancher.tf
echo " networks = [\"$(echo $RANCHER_MASTER_NETWORKS | sed 's/,/","/g')\"]" >> terraform/rancher.tf
echo " root_authorized_keys = \"\${file(\"$(echo $SDC_KEY | sed 's/"//g')\")}\"" >> terraform/rancher.tf
# echo " image = \"0867ef86-e69d-4aaa-ba3b-8d2aef0c204e\"" >> terraform/rancher.tf
echo " package = \"$(echo $HOST_PACKAGE | sed 's/"//g')\"" >> terraform/rancher.tf
echo "}" >> terraform/rancher.tf
return
fi
if [ $1 == "k8setcd" ]; then
echo "" >> terraform/rancher.tf
echo "module \"$2\" {" >> terraform/rancher.tf
echo " source = \"k8setcd\"" >> terraform/rancher.tf
echo " hostname = \"$2\"" >> terraform/rancher.tf
echo " networks = [\"$(echo $RANCHER_MASTER_NETWORKS | sed 's/,/","/g')\"]" >> terraform/rancher.tf
echo " root_authorized_keys = \"\${file(\"$(echo $SDC_KEY | sed 's/"//g')\")}\"" >> terraform/rancher.tf
# echo " image = \"0867ef86-e69d-4aaa-ba3b-8d2aef0c204e\"" >> terraform/rancher.tf
echo " package = \"$(echo $HOST_PACKAGE | sed 's/"//g')\"" >> terraform/rancher.tf
echo "}" >> terraform/rancher.tf
return
fi
if [ $1 == "master" ]; then
echo "" >> terraform/rancher.tf
echo "module \"$2\" {" >> terraform/rancher.tf
echo " source = \"master\"" >> terraform/rancher.tf
echo " hostname = \"$2\"" >> terraform/rancher.tf
echo " networks = [\"$(echo $RANCHER_MASTER_NETWORKS | sed 's/,/","/g')\"]" >> terraform/rancher.tf
echo " root_authorized_keys = \"\${file(\"$(echo $SDC_KEY | sed 's/"//g')\")}\"" >> terraform/rancher.tf
# echo " image = \"0867ef86-e69d-4aaa-ba3b-8d2aef0c204e\"" >> terraform/rancher.tf
echo " package = \"$(echo $HOST_PACKAGE | sed 's/"//g')\"" >> terraform/rancher.tf
echo "}" >> terraform/rancher.tf
return
fi
if [ $1 == "mysqldb" ]; then
echo "" >> terraform/rancher.tf
echo "module \"$2\" {" >> terraform/rancher.tf
echo " source = \"mysqldb\"" >> terraform/rancher.tf
echo " hostname = \"$2\"" >> terraform/rancher.tf
echo " networks = [\"$(echo $RANCHER_MASTER_NETWORKS | sed 's/,/","/g')\"]" >> terraform/rancher.tf
echo " root_authorized_keys = \"\${file(\"$(echo $SDC_KEY | sed 's/"//g')\")}\"" >> terraform/rancher.tf
# echo " image = \"0867ef86-e69d-4aaa-ba3b-8d2aef0c204e\"" >> terraform/rancher.tf
echo " package = \"$(echo $HOST_PACKAGE | sed 's/"//g')\"" >> terraform/rancher.tf
echo "}" >> terraform/rancher.tf
return
fi
if [ $1 == "host" ]; then
echo "" >> terraform/rancher.tf
echo "module \"$2\" {" >> terraform/rancher.tf
echo " source = \"host\"" >> terraform/rancher.tf
echo " hostname = \"$2\"" >> terraform/rancher.tf
echo " networks = [\"$(echo $KUBERNETES_NODE_NETWORKS | sed 's/,/","/g')\"]" >> terraform/rancher.tf
echo " root_authorized_keys = \"\${file(\"$(echo $SDC_KEY | sed 's/"//g')\")}\"" >> terraform/rancher.tf
# echo " image = \"0867ef86-e69d-4aaa-ba3b-8d2aef0c204e\"" >> terraform/rancher.tf
echo " package = \"$(echo $HOST_PACKAGE | sed 's/"//g')\"" >> terraform/rancher.tf
echo "}" >> terraform/rancher.tf
return
fi
if [ $1 == "provider" ]; then
echo "provider \"triton\" {" > terraform/rancher.tf
echo " account = \"$(echo $SDC_ACCOUNT | sed 's/"//g')\"" >> terraform/rancher.tf
echo " key_material = \"\${file(\"$(echo $SDC_KEY | sed 's/"//g')\")}\"" >> terraform/rancher.tf
echo " key_id = \"$(echo $SDC_KEY_ID | sed 's/"//g')\"" >> terraform/rancher.tf
echo " url = \"$(echo $SDC_URL | sed 's/"//g')\"" >> terraform/rancher.tf
echo "}" >> terraform/rancher.tf
return
fi
echo "error: problem updating terraform configuration..."
exit 1
}
setConfigToFile() {
echo "RANCHER_MASTER_NETWORKS=$RANCHER_MASTER_NETWORKS" >> config
echo "KUBERNETES_NODE_NETWORKS=$KUBERNETES_NODE_NETWORKS" >> config
echo "KUBERNETES_NUMBER_OF_NODES=$KUBERNETES_NUMBER_OF_NODES" >> config
echo "KUBERNETES_NAME=\"$KUBERNETES_NAME\"" >> config
echo "KUBERNETES_DESCRIPTION=\"$KUBERNETES_DESCRIPTION\"" >> config
echo "RANCHER_MASTER_HOSTNAME=\"$RANCHER_MASTER_HOSTNAME\"" >> config
echo "KUBERNETES_NODE_HOSTNAME_BEGINSWITH=\"$KUBERNETES_NODE_HOSTNAME_BEGINSWITH\"" >> config
echo "HOST_PACKAGE=\"$HOST_PACKAGE\"" >> config
}
setConfigFromTritonENV() {
eval "$(triton env)"
echo "SDC_URL=\"$SDC_URL\"" >> config
echo "SDC_ACCOUNT=\"$SDC_ACCOUNT\"" >> config
echo "SDC_KEY_ID=\"$SDC_KEY_ID\"" >> config
local foundKey=false
for f in $(ls ~/.ssh); do
if [[ "$(ssh-keygen -E md5 -lf ~/.ssh/$(echo $f | sed 's/.pub$//') 2> /dev/null | awk '{print $2}' | sed 's/^MD5://')" == "$SDC_KEY_ID" ]]; then
echo "SDC_KEY=\"~/.ssh/$(echo $f | sed 's/.pub$//')\"" >> config
foundKey=true
break
fi
# old version of ssh-keygen, defaults to md5
if [ "$(ssh-keygen -l -f ~/.ssh/$(echo $f | sed 's/.pub$//') 2> /dev/null | awk '{print $2}' | grep ^SHA256)" == "" ]; then
if [[ "$(ssh-keygen -l -f ~/.ssh/$(echo $f | sed 's/.pub$//') 2> /dev/null | awk '{print $2}')" == "$SDC_KEY_ID" ]]; then
echo "SDC_KEY=\"~/.ssh/$(echo $f | sed 's/.pub$//')\"" >> config
foundKey=true
break
fi
fi
done
if ! "$foundKey" ; then
echo "error: couldn't find the ssh key associated with fingerprint $SDC_KEY_ID in ~/.ssh/ directory..."
echo " Clean the setup and make sure your triton profile is set up."
echo " To confirm your profile is set up, try \`triton info\`."
cleanRunner
exit 1
fi
echo "" >> config
}
setVarDefaults() {
if [ -e config ]; then
echo "error: old configuration found"
cleanRunner
fi
KUBERNETES_NAME="k8s dev"
KUBERNETES_DESCRIPTION=$KUBERNETES_NAME
RANCHER_MASTER_HOSTNAME="kubemaster"
KUBERNETES_NODE_HOSTNAME_BEGINSWITH="kubenode"
KUBERNETES_NUMBER_OF_NODES=1
RANCHER_MASTER_NETWORKS=
KUBERNETES_NODE_NETWORKS=
HOST_PACKAGE=
echo "ANSIBLE_HOST_KEY_CHECKING=False" >> config
}
getConfigFromUser() {
# get networks from the current triton profile to prompt
local networks=$(triton networks -oname,id | grep -v "^NAME.*ID$" | tr -s " " | tr " " "=" | sort)
# get packages for the current triton profile to prompt
local packages=$(triton packages -oname,id | grep "\-kvm-" | grep -v "^NAME.*ID$" | tr -s " " | tr " " "=" | sort)
local tmp=0
local gotValidInput=false
local tmp_ValidatedInput
echo "---------------"
KUBERNETES_NAME=$(getArgument "Name your Kubernetes environment:" "$(echo $KUBERNETES_NAME | sed 's/"//g')")
echo "---------------"
if [[ $KUBERNETES_DESCRIPTION == "" ]]; then
KUBERNETES_DESCRIPTION=$(getArgument "Describe this Kubernetes environment:" "$(echo $KUBERNETES_NAME | sed 's/"//g')")
else
KUBERNETES_DESCRIPTION=$(getArgument "Describe this Kubernetes environment:" "$(echo $KUBERNETES_DESCRIPTION | sed 's/"//g')")
fi
echo "---------------"
gotValidInput=false
while ! $gotValidInput; do
read -p "Would you like HA for Kubernetes Cluster Manager (+3 VMs) (yes | no)? " yn
case $yn in
yes )
RANCHER_HA=true
gotValidInput=true
;;
no )
RANCHER_HA=false
gotValidInput=true
;;
* ) echo "Please answer yes or no.";;
esac
done
echo "---------------"
gotValidInput=false
while ! $gotValidInput; do
read -p "Run Kubernetes Management Services on dedicated nodes (+3 VMs for etcd, +3 VMs for K8s services - apiserver/scheduler/controllermanager...) (yes | no)? " yn
case $yn in
yes )
SEPARATE_PLANE=true
gotValidInput=true
# echo "SEPARATE_PLANE: true" >> ansible/roles/ranchermaster/vars/vars.yml
;;
no )
SEPARATE_PLANE=false
gotValidInput=true
;;
* ) echo "Please answer yes or no.";;
esac
done
echo "---------------"
gotValidInput=false
while ! $gotValidInput; do
tmp_ValidatedInput=$(getArgument "Hostname of the master:" "$(echo $RANCHER_MASTER_HOSTNAME | sed 's/"//g')")
if [[ $tmp_ValidatedInput =~ ^[a-zA-Z][0-9a-zA-Z]+$ ]]; then
gotValidInput=true
else
echo "error: Enter a valid hostname or leave blank to use the default."
echo " Must start with a letter and can only include letters and numbers"
fi
done
RANCHER_MASTER_HOSTNAME=$tmp_ValidatedInput
echo "---------------"
gotValidInput=false
while ! $gotValidInput; do
tmp_ValidatedInput=$(getArgument "Enter a string to use for appending to hostnames of all the nodes:" "$(echo $KUBERNETES_NODE_HOSTNAME_BEGINSWITH | sed 's/"//g')")
if [[ $tmp_ValidatedInput =~ ^[a-zA-Z][0-9a-zA-Z]+$ ]]; then
gotValidInput=true
else
echo "error: Enter a valid value or leave blank to use the default."
echo " Must start with a letter and can only include letters and numbers"
fi
done
KUBERNETES_NODE_HOSTNAME_BEGINSWITH=$tmp_ValidatedInput
echo "---------------"
# HARD LIMIT: 1-9 nodes allowed only since this setup has no HA
gotValidInput=false
while ! $gotValidInput; do
tmp_ValidatedInput=$(getArgument "How many nodes should this Kubernetes cluster have:" "$(echo $KUBERNETES_NUMBER_OF_NODES | sed 's/"//g')")
if [[ $tmp_ValidatedInput =~ ^[1-9]$ ]]; then
gotValidInput=true
else
echo "error: Enter a valid value (1-9) or leave blank to use the default."
fi
done
KUBERNETES_NUMBER_OF_NODES=$tmp_ValidatedInput
echo "---------------"
echo "From the networks below:"
# print options and find location for "Joyent-SDC-Public"
local publicNetworkLocation=1
local countNetwork
tmp=0
for network in $networks; do
tmp=$((tmp + 1))
echo -e "$tmp.\t$(echo $network | sed 's/=/ /g')"
# get default location of public network
if [[ "$network" == "Joyent-SDC-Public="* ]]; then
publicNetworkLocation=$tmp
fi
done
countNetwork=$tmp
# set publicNetworkLocation to RANCHER_MASTER_NETWORKS, if it wasn't set already
if [[ $RANCHER_MASTER_NETWORKS == "" ]]; then
RANCHER_MASTER_NETWORKS=$(getNetworkIDs $publicNetworkLocation)
fi
# get input for network and validate to make sure the input provided is within the limit (number of networks)
gotValidInput=false
while ! $gotValidInput; do
tmp_RANCHER_MASTER_NETWORKS=$(getArgument "What networks should the master be a part of, provide comma separated values:" "$(echo $RANCHER_MASTER_NETWORKS | sed 's/"//g')")
RANCHER_MASTER_NETWORKS=$(echo $RANCHER_MASTER_NETWORKS | tr ',' '\n' | sort | uniq | tr '\n' ',' | sed 's/\(.*\),$/\1/')
tmp_RANCHER_MASTER_NETWORKS=$(echo $tmp_RANCHER_MASTER_NETWORKS | tr ',' '\n' | sort | uniq | tr '\n' ',' | sed 's/\(.*\),$/\1/')
# if valid input was given, move forward, else quit
if [[ $(echo $tmp_RANCHER_MASTER_NETWORKS | grep '^[1-9][0-9]\?\(,[1-9][0-9]\?\)*$' 2> /dev/null) ]]; then
gotValidInput=true
for network in $(echo $tmp_RANCHER_MASTER_NETWORKS | tr "," "\n"); do
if [[ "$network" -gt "$countNetwork" || "$network" -lt 1 ]]; then
echo "error: Enter a valid option or leave blank to use the default."
echo " Values should be comma separated between 1 and $countNetwork."
gotValidInput=false
fi
done
if $gotValidInput; then
RANCHER_MASTER_NETWORKS=$(getNetworkIDs $tmp_RANCHER_MASTER_NETWORKS)
fi
elif [[ $tmp_RANCHER_MASTER_NETWORKS == $RANCHER_MASTER_NETWORKS ]]; then
gotValidInput=true
else
echo "error: Enter a valid option or leave blank to use the default."
echo " Values should be comma separated between 1 and $countNetwork."
fi
done
echo "---------------"
echo "From the networks below:"
# print options
tmp=0
for network in $networks; do
tmp=$((tmp + 1))
echo -e "$tmp.\t$(echo $network | sed 's/=/ /g')"
done
# set publicNetworkLocation to KUBERNETES_NODE_NETWORKS, if it wasn't set already
if [[ $KUBERNETES_NODE_NETWORKS == "" ]]; then
KUBERNETES_NODE_NETWORKS=$(getNetworkIDs $publicNetworkLocation)
fi
# get input for network and validate to make sure the input provided is within the limit (number of networks)
gotValidInput=false
while ! $gotValidInput; do
tmp_KUBERNETES_NODE_NETWORKS=$(getArgument "What networks should the nodes be a part of, provide comma separated values:" "$(echo $KUBERNETES_NODE_NETWORKS | sed 's/"//g')")
KUBERNETES_NODE_NETWORKS=$(echo $KUBERNETES_NODE_NETWORKS | tr ',' '\n' | sort | uniq | tr '\n' ',' | sed 's/\(.*\),$/\1/')
tmp_KUBERNETES_NODE_NETWORKS=$(echo $tmp_KUBERNETES_NODE_NETWORKS | tr ',' '\n' | sort | uniq | tr '\n' ',' | sed 's/\(.*\),$/\1/')
# if valid input was given, move forward, else quit
if [[ $(echo $tmp_KUBERNETES_NODE_NETWORKS | grep '^[1-9][0-9]\?\(,[1-9][0-9]\?\)*$' 2> /dev/null) ]]; then
gotValidInput=true
for network in $(echo $tmp_KUBERNETES_NODE_NETWORKS | tr "," "\n"); do
if [[ "$network" -gt "$countNetwork" || "$network" -lt 1 ]]; then
echo "error: Enter a valid option or leave blank to use the default."
echo " Values should be comma separated between 1 and $countNetwork."
gotValidInput=false
fi
done
if $gotValidInput; then
KUBERNETES_NODE_NETWORKS=$(getNetworkIDs $tmp_KUBERNETES_NODE_NETWORKS)
fi
elif [[ $tmp_KUBERNETES_NODE_NETWORKS == $KUBERNETES_NODE_NETWORKS ]]; then
gotValidInput=true
else
echo "error: Enter a valid option or leave blank to use the default."
echo " Values should be comma separated between 1 and $countNetwork."
fi
done
echo "---------------"
echo "From the packages below:"
# print options and find location for "k4-highcpu-kvm-7.75G"
local packageLocation=1
local countPackages
tmp=0
for package in $packages; do
tmp=$((tmp + 1))
echo -e "$tmp.\t$(echo $package | sed 's/=/ /g')"
# get default location of package
if [[ "$package" == "k4-highcpu-kvm-7.75G="* ]]; then
packageLocation=$tmp
fi
done
countPackages=$tmp
# set packageLocation to HOST_PACKAGE, if it wasn't set already
if [[ $HOST_PACKAGE == "" ]]; then
HOST_PACKAGE=$(getPackageID $packageLocation)
fi
# get input for package and validate to make sure the input provided is within the limit (number of packages)
gotValidInput=false
while ! $gotValidInput; do
tmp_HOST_PACKAGE=$(getArgument "What KVM package should the master and nodes run on:" "$(echo $HOST_PACKAGE | sed 's/"//g')")
# if valid input was given, move forward, else quit
if [[ $(echo $tmp_HOST_PACKAGE | grep '^[1-9][0-9]*$' 2> /dev/null) ]]; then
gotValidInput=true
for package in $(echo $tmp_HOST_PACKAGE | tr "," "\n"); do
if [[ "$package" -gt "$countPackages" || "$package" -lt 1 ]]; then
echo "error: Enter a valid option or leave blank to use the default."
echo " Value should be between 1 and $countPackages."
gotValidInput=false
fi
done
if $gotValidInput; then
HOST_PACKAGE=$(getPackageID $tmp_HOST_PACKAGE)
echo "entered $tmp_HOST_PACKAGE and got $HOST_PACKAGE"
fi
elif [[ $tmp_HOST_PACKAGE == $(echo $HOST_PACKAGE | sed 's/"//g') ]]; then
gotValidInput=true
else
echo "error: Enter a valid option or leave blank to use the default."
echo " Value should be between 1 and $countPackages."
fi
done
HOST_PACKAGE=$(echo $HOST_PACKAGE | sed 's/"//g')
}
verifyConfig() {
echo "################################################################################"
echo "Verify that the following configuration is correct:"
echo ""
echo "Name of kubernetes environment: $KUBERNETES_NAME"
echo "Kubernetes environment description: $KUBERNETES_DESCRIPTION"
if $RANCHER_HA; then
echo "Cluster Manager hosts: ${RANCHER_MASTER_HOSTNAME}1, ${RANCHER_MASTER_HOSTNAME}2, ${RANCHER_MASTER_HOSTNAME}db"
else
echo "Master hostname: $RANCHER_MASTER_HOSTNAME"
fi
if $SEPARATE_PLANE; then
echo " There will be three nodes created for etcd and three for Kubernetes HA."
fi
echo "All node hostnames will start with: $KUBERNETES_NODE_HOSTNAME_BEGINSWITH"
echo "Kubernetes environment will have $KUBERNETES_NUMBER_OF_NODES nodes"
echo "Master server will be part of these networks: $RANCHER_MASTER_NETWORKS"
echo "Kubernetes nodes will be a part of these networks: $KUBERNETES_NODE_NETWORKS"
echo "This package will be used for all the hosts: $HOST_PACKAGE"
echo ""
echo "Make sure the above information is correct before answering:"
echo " to view list of networks call \"triton networks -l\""
echo " to view list of packages call \"triton packages -l\""
echo "WARN: Make sure that the nodes and master are part of networks that can communicate with each other and this system from which the setup is running."
while true; do
read -p "Is the above config correct (yes | no)? " yn
case $yn in
yes )
break
;;
no )
exit 0
;;
* ) echo "Please answer yes or no.";;
esac
done
}
cleanRunner() {
echo "Clearing settings...."
while true; do
if [ -e terraform/masters.ip ]; then
echo "WARNING: You are about to destroy KVMs associated with this Rancher cluster."
read -p "Do you wish to destroy the KVMs and reset configuration (yes | no)? " yn
else
read -p "Do you wish to reset configuration (yes | no)? " yn
fi
case $yn in
yes )
if [ -e terraform/rancher.tf ]; then
cd terraform
echo " destroying images..."
$TERRAFORM destroy -force 2> /dev/null || true
cd ..
fi
if [[ -e terraform/hosts.ip && -e terraform/masters.ip && -e ~/.ssh/known_hosts ]]; then
for host_key in $(cat terraform/hosts.ip terraform/masters.ip); do
ssh-keygen -R $host_key 2>&1 >> /dev/null
done
fi
rm -rf terraform/hosts.ip terraform/masters.ip terraform/k8setcd.ip terraform/k8sha.ip terraform/mysqldb.ip terraform/terraform.* terraform/.terraform* terraform/rancher.tf 2>&1 >> /dev/null
rm -rf ansible/roles/ranchermaster/vars/vars.yml ansible/tmp/kubernetes_* 2>&1 >> /dev/null
echo 'master: 64.30.129.229' > ansible/roles/ranchermaster/vars/vars.yml
echo 'kubernetes_name: "k8s dev"' >> ansible/roles/ranchermaster/vars/vars.yml
echo 'kubernetes_description: "k8s dev"' >> ansible/roles/ranchermaster/vars/vars.yml
sed "s~private_key_file = .*$~private_key_file = ~g" ansible/ansible.cfg > tmp && mv tmp ansible/ansible.cfg
rm -f ansible/hosts ansible/*retry ansible/ansible.cfg.tmp 2>&1 >> /dev/null
rm -rf config tmp/* 2>&1 >> /dev/null
echo " All clear!"
return;;
no ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
}
debugVars() {
echo "KUBERNETES_NAME=$KUBERNETES_NAME"
echo "KUBERNETES_DESCRIPTION=$KUBERNETES_DESCRIPTION"
echo "RANCHER_MASTER_HOSTNAME=$RANCHER_MASTER_HOSTNAME"
echo "KUBERNETES_NODE_HOSTNAME_BEGINSWITH=$KUBERNETES_NODE_HOSTNAME_BEGINSWITH"
echo "KUBERNETES_NUMBER_OF_NODES=$KUBERNETES_NUMBER_OF_NODES"
echo "RANCHER_MASTER_NETWORKS=$RANCHER_MASTER_NETWORKS"
echo "KUBERNETES_NODE_NETWORKS=$KUBERNETES_NODE_NETWORKS"
echo "HOST_PACKAGE=$HOST_PACKAGE"
}
getNetworkIDs() {
values=$(echo $1 | tr "," " ")
local networks
for network in $values; do
networks="$networks,$(triton networks -oname,id | sort | grep -v "^NAME *ID$" | sed -n "$network"p | awk 'NF>1{print $NF}')"
done
echo "$networks" | sed 's/^,\(.*\)$/\1/' | sed 's/\(.*\),$/\1/'
}
getPackageID() {
echo "$(triton packages -oname,id | grep "\-kvm-" | grep -v "^NAME.*ID$" | tr -s " " | sort | sed -n "$1"p | awk 'NF>1{print $NF}')"
}
exportVars() {
grep -v "^$" config > config.tmp
while read line; do
export "$line"
done <config.tmp
rm config.tmp
export SEPARATE_PLANE
export RANCHER_HA
}
main "$@"