forked from dfarrell07/skynet-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·624 lines (489 loc) · 17.5 KB
/
run.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
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
banner() {
echo "-------------------------------------------------------------------------------"
echo " __ __ __ __ "
echo " _____/ /____ ______ ___ / /_ / /_____ ____ / /____ "
echo " / ___/ //_/ / / / __ \/ _ \/ __/ / __/ __ \/ __ \/ / ___/ "
echo " (__ ) ,< / /_/ / / / / __/ /_ / /_/ /_/ / /_/ / (__ ) "
echo " /____/_/|_|\__, /_/ /_/\___/\__/ \__/\____/\____/_/____/ "
echo " /____/ "
echo "-------------------------------------------------------------------------------"
}
usage () {
cat <<EOF
Usage: $0 <action> ...
$0 deploy <deployment-type> ... Deploy on the specified cloud.
$0 destroy <deployment-type> ... Destroy the specified deployment
$0 tunnel <deployment-type> ... Create ssh tunnels to reach the remote API
$0 ping Ping all instances on inventory
Deployment types:
toolbox Used to manage a development/deployment toolbox with the team
development tools, plus openshift-installer, plus ansible.
toolbox-kind Used to deploy 3 kind clusters + submariners in the toolbox VM
openshift-cluster Used to manage a single cluster
rdo-networks Used to cleanup or create default working RDO networks and
resources once you have finished with your toolbox or clusters
EOF
common_options
}
common_options() {
cat <<EOF
Common options:
-c, --cloud AWS|RDO
-d, --debug enable ansible/debugging
-E, --environment
environment file with variable overrides for ansible
-e, --extra-vars <key=value>
additional variables for the ansible scripts
-k, --ssh-key-name
the ssh key name you want to use for deployment
on the cloud, will get saved to ansible/inventory/.ssh_key_name
for next runs
-h help for the specific deployment type/action
-n, --name unique name of the deployment
-s, --skip-networks
skip the creation/check of network resources on the cloud
to speed up the playbook execution.
-S, --skip-tags skip ansible playbook tags.
-t, --toolbox-as-manager
use your toolbox instance as your cluster manager, or the
host the openstack commands to RDO.
EOF
}
help_toolbox_deploy() {
cat <<EOF
Usage: $0 toolbox deploy
This action deploys a toolbox machine, by default on RDO
with openshift-installer, ansible, go compilers, etc.., to
be used as a openshift cluster manager, development machine,
etc.
Options:
-p, --personalized-playbook <path/to/playbook.yml>
path to a personalized ansible playbook to install extra
stuff on your development machine, as every developer
will have own tastes and preferences for editors
configurations, etc.
EOF
common_options
}
help_toolbox_destroy() {
cat <<EOF
Usage: $0 toolbox destroy
This action removes an existing deployment for the toolbox machine
Options:
-f Force without asking for confirmation
EOF
common_options
}
help_openshift_cluster_deploy() {
cat <<EOF
Usage: $0 openshift-cluster deploy
This action deploys an openshift cluster.
Options:
-v, --version <version> (defaults to 4.00)
4.00 using openshift-installer (only supports AWS now)
3.11 using openshift-ansible (only supports RDO cloud now)
-P, --pod-cidr <cidr> (default is 10.128.0.0/14)
This is the desired Pod CIDR for the cluster
-X, --service-cidr <cidr> (default is 172.30.0.0/16, avoid 172.17.0.0/16 docker0 range)
This is the desidred Service CIDR for the cluster
EOF
common_options
}
help_openshift_cluster_destroy() {
cat <<EOF
Usage: $0 openshift-cluster destroy
This action removes an existing openshift-cluster
Options:
-f, --force Force without asking for confirmation
EOF
common_options
}
help_ping_() {
cat <<EOF
Usage: $0 ping
This action will ping all hosts registerd in your ansible/inventory
EOF
common_options
}
check_deployment_type() {
case "$DEPLOYMENT_TYPE" in
openshift-cluster) OPT_CLOUD=AWS
OPT_VERSION=4
;;
toolbox) OPT_CLOUD=RDO
;;
rdo-networks) OPT_CLOUD=RDO
;;
toolbox-kind) OPT_CLOUD=RDO
;;
*) echo "ERROR: deployment type $DEPLOYMENT_TYPE unknown" >&2
usage >&2
exit 2
esac
}
##########################################################################
# Checking for the minimal amount of parameters (deployment and action) #
##########################################################################
if (( "$#" < 1 )); then
banner
usage
exit 1
fi
ACTION=$1; shift
case "$ACTION" in
deploy) DEPLOYMENT_TYPE=$1
shift;
check_deployment_type
;;
destroy) DEPLOYMENT_TYPE=$1
shift;
check_deployment_type
;;
tunnel) DEPLOYMENT_TYPE=$1
check_deployment_type
if [ "$DEPLOYMENT_TYPE" != "toolbox-kind" ]; then
echo "ERROR: tunnel action is only supported with toolbox-kind" >&2
exit 2
fi
shift
;;
ping) PING_GROUP=$1;
shift
;;
*) echo "ERROR: action $ACTION unknown" >&2
usage >&2
exit 2
esac
###############################
# Basic parsing of parameters #
###############################
OPT_VARS=()
OPT_ENVIRONMENT=()
OPT_SKIP_TAGS=()
OPT_DEBUG=0
OPT_FORCE=0
OPT_NAME=
OPT_VERSION=4
OPT_MANAGER_HOST="localhost"
OPT_SSH_KEY=$(cat $DIR/ansible/inventory/.ssh_key_name 2>/dev/null)
OPT_POD_CIDR=10.128.0.0/14
OPT_SERVICE_CIDR=172.30.0.0/16
while [ "x$1" != "x" ]; do
case "$1" in
--debug|-d)
set +x
OPT_DEBUG=1
ANSIBLE_VERBOSITY="-v -v -v"
;;
--version|-v)
OPT_VERSION=$2
shift
;;
--name|-n)
OPT_NAME=$2
shift
;;
--force|-f)
OPT_FORCE=1
;;
--cloud|-c)
OPT_CLOUD=$(echo $2 | tr '[a-z]' '[A-Z]')
shift
;;
--skip-networks|-s)
OPT_SKIP_TAGS+=("--skip-tags=create-rdo-networks")
;;
--skip-tags|-S)
OPT_SKIP_TAGS+=("--skip-tags=$2")
shift
;;
--environment|-E)
OPT_ENVIRONMENT+=("-e")
OPT_ENVIRONMENT+=("@$2")
shift
;;
--extra-vars|-e)
OPT_VARS+=("-e")
OPT_VARS+=("$2")
shift
;;
--personalized-playbook|-p)
OPT_PERSONALIZED_PLAYBOOK=$(realpath $2)
shift
;;
--help|-h)
help_${DEPLOYMENT_TYPE/-/_}_${ACTION/-/_}
;;
--toolbox-as-manager|-t)
OPT_MANAGER_HOST="toolbox"
;;
--ssh-key-name|-k)
OPT_SSH_KEY=$2
echo $OPT_SSH_KEY > $DIR/ansible/inventory/.ssh_key_name
shift
;;
--pod-cidr|-P)
OPT_POD_CIDR=$2
shift
;;
--service-cidr|-X)
OPT_SERVICE_CIDR=$2
shift
;;
*) echo "ERROR: unknown option: $1" >&2
usage >&2
exit 2
;;
esac
shift
done
# Auxiliary functions used by actions/deployments
verify_ssh_key() {
if [ "x$OPT_SSH_KEY" == "x" ]; then
echo "No ssh key defined in ansible/inventory/.ssh_key_name, please" >&2
echo "use option -k at least once to generate such file" >&2
exit 2
fi
}
verify_destroy() {
if [[ OPT_FORCE != 1 ]]; then
read -p "Are you sure you want to destroy $1? " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 2
fi
fi
}
verify_cluster_name() {
if [ "x$OPT_NAME" == "x" ]; then
echo "ERROR: you need to specify a name for the cluster with -n <name>" >&2
exit 2
fi
}
######################################################
# Functions for the different deployment actions #
######################################################
deploy_toolbox_kind() {
ansible-playbook $ANSIBLE_VERBOSITY \
${OPT_ENVIRONMENT[@]} \
${OPT_VARS[@]} \
-e manager_host=$OPT_MANAGER_HOST \
-e ssh_key_name=$OPT_SSH_KEY \
-e kindsubm_host=toolbox \
${OPT_SKIP_TAGS[@]} \
$DIR/ansible/kindsubm.yml \
-i $DIR/ansible/inventory \
-t kindsubm-deploy \
${OPT_SKIP_TAGS[@]}
}
destroy_toolbox_kind() {
ansible-playbook $ANSIBLE_VERBOSITY \
${OPT_ENVIRONMENT[@]} \
${OPT_VARS[@]} \
-e manager_host=$OPT_MANAGER_HOST \
-e ssh_key_name=$OPT_SSH_KEY \
-e kindsubm_host=toolbox \
${OPT_SKIP_TAGS[@]} \
$DIR/ansible/kindsubm.yml \
-i $DIR/ansible/inventory \
-t kindsubm-destroy \
${OPT_SKIP_TAGS[@]}
}
deploy_toolbox() {
verify_ssh_key
ansible-playbook $ANSIBLE_VERBOSITY \
${OPT_ENVIRONMENT[@]} \
${OPT_VARS[@]} \
-e manager_host=$OPT_MANAGER_HOST \
-e ssh_key_name=$OPT_SSH_KEY \
${OPT_SKIP_TAGS[@]} \
$DIR/ansible/toolbox-vm.yml \
-i $DIR/ansible/inventory \
-t create-rdo-vms \
-t create-rdo-networks \
${OPT_SKIP_TAGS[@]}
ansible-playbook $ANSIBLE_VERBOSITY \
${OPT_ENVIRONMENT[@]} \
${OPT_VARS[@]} \
-e manager_host=$OPT_MANAGER_HOST \
-e ssh_key_name=$OPT_SSH_KEY \
${OPT_SKIP_TAGS[@]} \
$DIR/ansible/toolbox.yml \
-i $DIR/ansible/inventory \
-t toolbox \
${OPT_SKIP_TAGS[@]}
}
destroy_toolbox() {
verify_destroy toolbox
ansible-playbook $ANSIBLE_VERBOSITY \
${OPT_ENVIRONMENT[@]} \
${OPT_VARS[@]} \
-e manager_host=$OPT_MANAGER_HOST \
-e ssh_key_name=$OPT_SSH_KEY \
${OPT_SKIP_TAGS[@]} \
$DIR/ansible/toolbox-vm.yml \
-i $DIR/ansible/inventory \
-t delete-rdo-vms \
${OPT_SKIP_TAGS[@]}
}
deploy_openshift_cluster() {
verify_ssh_key
verify_cluster_name
if [ "$OPT_VERSION" == "4.00" ]; then
#TODO: add OPT_POD_CIDR and OPT_SERVICE_CIDR support
ansible-playbook $ANSIBLE_VERBOSITY \
${OPT_ENVIRONMENT[@]} \
${OPT_VARS[@]} \
-e manager_host=$OPT_MANAGER_HOST \
-e ssh_key_name=$OPT_SSH_KEY \
-e cluster_name=$OPT_NAME \
${OPT_SKIP_TAGS[@]} \
$DIR/ansible/os-cluster-4.yml \
-i $DIR/ansible/inventory \
-t create-os-cluster
elif [ "$OPT_VERSION" == "3.11" ]; then
# make sure we can see the ansible-in-ansible execution later down the road
rm /tmp/openshift-ansible-3.11.${OPT_NAME}.log 2>/dev/null
touch /tmp/openshift-ansible-3.11.${OPT_NAME}.log
tail -f /tmp/openshift-ansible-3.11.${OPT_NAME}.log &
ansible-playbook $ANSIBLE_VERBOSITY \
${OPT_ENVIRONMENT[@]} \
${OPT_VARS[@]} \
-e manager_host=$OPT_MANAGER_HOST \
-e ssh_key_name=$OPT_SSH_KEY \
-e cluster_name=$OPT_NAME \
-e pod_cidr=$OPT_POD_CIDR \
-e service_cidr=$OPT_SERVICE_CIDR \
-i $DIR/ansible/inventory \
$DIR/ansible/os-cluster-3.yml \
-t create-os-cluster \
-t create-rdo-vms \
-t create-rdo-networks \
${OPT_SKIP_TAGS[@]}
# kill the tail
kill %1
else
echo "ERROR: openshift $OPT_VERSION not supported yet" >&2
usage
exit 2
fi
}
destroy_openshift_cluster() {
verify_cluster_name
verify_destroy $OPT_NAME
if [ "$OPT_VERSION" == "4.00" ]; then
ansible-playbook $ANSIBLE_VERBOSITY \
${OPT_ENVIRONMENT[@]} \
${OPT_VARS[@]} \
-e manager_host=$OPT_MANAGER_HOST \
-e ssh_key_name=$OPT_SSH_KEY \
-e cluster_name=$OPT_NAME \
${OPT_SKIP_TAGS[@]} \
$DIR/ansible/os-cluster-4.yml \
-i $DIR/ansible/inventory \
-t delete-os-cluster
elif [ "$OPT_VERSION" == "3.11" ]; then
# make sure we can see the ansible-in-ansible execution later down the road
rm /tmp/openshift-ansible-3.11.${OPT_NAME}.log 2>/dev/null
touch /tmp/openshift-ansible-3.11.${OPT_NAME}.log
tail -f /tmp/openshift-ansible-3.11.${OPT_NAME}.log &
ansible-playbook $ANSIBLE_VERBOSITY \
${OPT_ENVIRONMENT[@]} \
${OPT_VARS[@]} \
-e manager_host=$OPT_MANAGER_HOST \
-e ssh_key_name=$OPT_SSH_KEY \
-e cluster_name=$OPT_NAME \
-i $DIR/ansible/inventory \
$DIR/ansible/os-cluster-3.yml \
-t delete-os-cluster \
-t delete-rdo-vms \
${OPT_SKIP_TAGS[@]}
else
echo "ERROR: openshift $OPT_VERSION not supported yet" >&2
usage
exit 2
fi
}
destroy_rdo_networks() {
echo "Deleting network resoures on RDO, please note that it will fail"
echo "if existing instance still depend on the networks"
echo ""
verify_destroy rdo-networks
ansible-playbook $ANSIBLE_VERBOSITY \
${OPT_ENVIRONMENT[@]} \
${OPT_VARS[@]} \
-e manager_host=$OPT_MANAGER_HOST \
-i $DIR/ansible/inventory \
$DIR/ansible/os-cluster-3.yml \
-t delete-os-cluster \
-t delete-rdo-networks \
${OPT_SKIP_TAGS[@]}
}
find_redirect_ports() {
grep -h -E -o localhost:[0-9]+ creds/kind-config-cluster* | grep -E -o [0-9]+
}
find_toolbox_ip() {
grep -h -E -o ansible_ssh_host=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ ansible/inventory/toolbox-inventory \
| grep -E -o [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+
}
get_abs_filename() {
# $1 : relative filename
echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
}
tunnel_toolbox_kind() {
KUBE_PORTS="$(find_redirect_ports)"
if [[ "x$KUBE_PORTS" == "x" ]]; then
echo "No remote kubernetes ports found in creds/kind-config-cluster*" >&2
exit 1
fi
TOOLBOX_IP="$(find_toolbox_ip)"
if [[ "x$TOOLBOX_IP" == "x" ]]; then
echo "Couldn't find the IP address from the toolbox-inventory" >&2
exit 1
fi
SSH_REDIRECT="-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ExitOnForwardFailure=yes"
for port in $KUBE_PORTS; do
SSH_REDIRECT="${SSH_REDIRECT} -L${port}:localhost:${port}"
done
# kill any preexisting tunnel
if [[ -f /tmp/kind_tunnel.pid ]]; then
kill -9 $(cat /tmp/kind_tunnel.pid); rm /tmp/kind_tunnel.pid
fi
ssh centos@$TOOLBOX_IP $SSH_REDIRECT "while true; do sleep 3600; done;" &
echo $! > /tmp/kind_tunnel.pid
echo Started backround ssh redirecting ports $KUBE_PORTS
sleep 4
ABS=$(get_abs_filename creds/kind-config-cluster1)
for i in 2 3; do
ABS="${ABS}:$(get_abs_filename creds/kind-config-cluster${i})"
done
echo ""
echo "you can stop the ssh tunnel by running: kill -9 $(cat /tmp/kind_tunnel.pid); rm /tmp/kind_tunnel.pid"
echo ""
echo "use:"
echo " export KUBECONFIG=$ABS"
echo ""
export KUBECONFIG=$ABS
kubectl config get-contexts
}
############################################
# Actions #
############################################
deploy() {
deploy_${DEPLOYMENT_TYPE/-/_}
}
destroy() {
destroy_${DEPLOYMENT_TYPE/-/_}
}
ping() {
ansible -m ping -i ansible/inventory/ ${PING_GROUP:-all}
}
tunnel() {
tunnel_${DEPLOYMENT_TYPE/-/_}
}
############################################
# Call the specific deployment action #
############################################
${ACTION/-/_}