forked from openshift/training
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-training.sh
More file actions
1416 lines (1345 loc) · 41.7 KB
/
setup-training.sh
File metadata and controls
1416 lines (1345 loc) · 41.7 KB
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
function exec_it() {
if $verbose
then
echo "$@"
eval "$@"
else
eval "$@" &> /dev/null
fi
}
function test_exit() {
if [ $1 -eq 0 ]
then
printf '\033[32m✓ \033[0m'
printf '%s\n' "$2 passed"
if $verbose
then
echo
fi
else
printf '\033[31m✗ \033[0m'
printf '%s\n' "$2 failed"
exit 255
fi
}
function wait_on_build(){
# arg1 = build id, arg2 = namespace, arg3 = time, arg4 = status
test="Waiting up to $3s for build ($1) status $4..."
printf " $test\r"
for i in $(seq 1 $3)
do
sleep 1
exec_it oc get build "$1" -n "$2" --template \''{{.status.phase}}'\' "|" grep -E \""$4"\"
if [ $? -eq 0 ]
then
test_exit 0 "$test"
return
fi
done
test_exit 1 "$test"
}
function wait_on_pod(){
# arg1 = pod id, arg2 = pod namespace, arg3 = time
test="Waiting up to $3s for pod ($1) deployment..."
printf " $test\r"
for i in $(seq 1 $3)
do
sleep 1
exec_it oc get pod "$1" -n "$2" --template \''{{index .status.conditions 0 "type"}}|{{.status.phase}}'\' "|" grep \""Ready|Running"\"
if [ $? -eq 0 ]
then
test_exit 0 "$test"
return
fi
done
test_exit 1 "$test"
}
function wait_on_endpoints(){
# arg1 = service name, arg2 = namespace, arg3 = time
test="Waiting up to $3s for service ($1) endpoints..."
printf " $test\r"
for i in $(seq 1 $3)
do
sleep 1
val=$(oc get endpoints -n "$2" "$1" --template '{{len .subsets}}')
if [ $val -gt 0 ]
then
test_exit 0 "$test"
return
fi
done
test_exit 1 "$test"
}
function wait_on_rc(){
# arg1 = rc id, arg2 = rc namespace, arg3 = time, arg4 = # replicas
test="Waiting up to $3s for rc ($1) deployer..."
printf " $test\r"
for i in $(seq 1 $3)
do
sleep 1
exec_it oc get rc $1 -n $2 --template \''{{.status.replicas}}'\' "|" grep $4
if [ $? -eq 0 ]
then
test_exit 0 "$test"
# need to sleep for a bit because the pod probably isn't really there yet
sleep 5
return
fi
done
test_exit 1 "$test"
}
function wait_on_project(){
# arg1 = project id, arg2 = time
test="Waiting up to $2s for project ($1) to be deleted..."
printf " $test\r"
for i in $(seq 1 $2)
do
sleep 1
exec_it oc get project "|" grep "$1"
if [ $? -eq 1 ]
then
test_exit 0 "$test"
return
fi
done
test_exit 1 "$test"
}
function prepare_dns(){
for node in ose3-master ose3-node1 ose3-node2
do
test="Checking $node resolver..."
exec_it ssh -o StrictHostKeyChecking=no root@$node.example.com \""grep 133.4 /etc/resolv.conf"\"
# need to test whether ssh failed versus whether grep failed
if [ $? -eq 1 ]
then
test="Setting nameserver for $node..."
printf " $test\r"
exec_it ssh -o StrictHostKeyChecking=no root@$node.example.com \""sed -e '/^nameserver .*/i nameserver 192.168.133.4' -i /etc/resolv.conf"\"
test_exit $? "$test"
fi
done
test="Starting dnsmasq..."
printf " $test\r"
exec_it ssh root@ose3-node2.example.com "systemctl start dnsmasq"
test_exit $? "$test"
test="Enabling dnsmasq..."
printf " $test\r"
exec_it ssh root@ose3-node2.example.com "systemctl enable dnsmasq"
test_exit $? "$test"
test="Checking for firewall rule..."
exec_it ssh root@ose3-node2.example.com \""grep 'dport 53' /etc/sysconfig/iptables"\"
# need to test whether ssh failed or grep failed
if [ $? -eq 1 ]
then
test="Adding iptables rule to sysconfig file..."
printf " $test\r"
exec_it ssh root@ose3-node2.example.com \""sed -i /etc/sysconfig/iptables -e '/^-A INPUT -p tcp -m state/i -A INPUT -p udp -m udp --dport 53 -j ACCEPT'"\"
test_exit $? "$test"
fi
test="Checking live firewall..."
exec_it ssh root@ose3-node2.example.com \""iptables-save | grep 'dport 53'"\"
# need to test whether ssh failed or grep failed
if [ $? -eq 1 ]
then
test="Adding iptables rule to live rules..."
printf " $test\r"
exec_it ssh root@ose3-node2.example.com \""iptables -I INPUT -p udp -m udp --dport 53 -j ACCEPT"\"
test_exit $? "$test"
fi
#test="Enabling DNS in firewalld..."
#printf " $test\r"
#exec_it ssh root@ose3-node2.example.com \""firewall-cmd --zone=public --add-service=dns --permanent"\"
#test_exit $? "$test"
#
#test="Reloading firewalld..."
#printf " $test\r"
#exec_it ssh root@ose3-node2.example.com \""firewall-cmd --reload"\"
#test_exit $? "$test"
}
function pull_content(){
cd
# if the directory doesn't exist
if [ ! -d /root/training ]
then
test="Pulling training content..."
printf " $test\r"
exec_it git clone https://github.com/$gituser/training -b $branch ~/training
test_exit $? "$test"
else
test="Updating training content..."
printf " $test\r"
cd ~/training
exec_it git pull $gituser $branch
test_exit $? "$test"
fi
#if [ ! -d /root/openshift-ansible ]
#then
# test="Pulling ansible content..."
# printf " $test\r"
# exec_it git clone https://github.com/openshift/openshift-ansible ~/openshift-ansible
# test_exit $? "$test"
#else
# test="Updating ansible content..."
# printf " $test\r"
# cd ~/openshift-ansible
# exec_it git pull origin master
# test_exit $? "$test"
#fi
#test="Copying hosts file..."
#printf " $test\r"
#exec_it /bin/cp -f ~/training/content/sample-ansible-hosts /etc/ansible/hosts
#test_exit $? "$test"
}
function prepare_things(){
test="Installing atomic-openshift-utils (installer)..."
printf " $test\r"
#exec_it yum -y install atomic-openshift-utils
# temporary workaround
exec_it yum -y install atomic-openshift-utils
test_exit $? "$test"
prepare_dns
pull_content
# just in case
if [ -d /root/.config ]
then
exec_it oc login -u system:admin
exec_it oc project default
fi
}
function just_setup() {
prepare_things
run_install
post_install
setup_dev_users
configure_htpasswd_auth
install_router
prepare_nfs
setup_storage_volumes_claims
install_registry
add_claimed_volume
setup_default_project_template
}
function post_install(){
test="Making master schedulable..."
printf " $test\r"
exec_it oadm manage-node ose3-master.example.com --schedulable=true
test_exit $? "$test"
configure_default_project_selector
label_nodes
configure_routing_domain
configure_default_nodeselector
}
function configure_htpasswd_auth(){
test="Configuring htpasswd authentication..."
printf " $test\r"
exec_it perl -0777 -pi -e \''BEGIN { $match = `cat ~/training/content/oldauth.yaml`; $replace = `cat ~/training/content/auth.yaml` } s/$match/$replace/'\' /etc/origin/master/master-config.yaml
test_exit $? "$test"
test="Restarting master..."
printf " $test\r"
exec_it systemctl restart atomic-openshift-master
test_exit $? "$test"
}
function update_project_template_quota(){
test="Updating project request quota to 5 pods..."
printf " $test\r"
exec_it oc get template/default-project-request -o yaml "|" sed -e \''s/pods: 3/pods: 5/'\' "|" oc replace -f -
test_exit $? "$test"
}
function run_install(){
date=$(date +%d%m%Y)
test="Running installation..."
if $installoutput
then
echo "Installation..."
cd
atomic-openshift-installer -c ~/training/installer.cfg.yaml -u install -f
else
echo "Installation (takes a while - output logged to /tmp/ansible-$date.log)..."
cd
atomic-openshift-installer -c ~/training/installer.cfg.yaml -u install -f > /tmp/ansible-`date +%d%m%Y`.log
fi
test_exit $? "$test"
}
#function copy_ca(){
#test="Copying CA certificate to a user accessible location..."
#printf " $test\r"
#exec_it /bin/cp /etc/origin/master/ca.crt /etc/origin
#test_exit $? "$test"
#}
function label_nodes(){
# let things settle a bit
sleep 15
test="Labeling master..."
printf " $test\r"
exec_it oc label --overwrite node/ose3-master.example.com region=infra zone=default
test_exit $? "$test"
test="Labeling node1..."
printf " $test\r"
exec_it oc label --overwrite node/ose3-node1.example.com region=primary zone=east
test_exit $? "$test"
test="Labeling node2..."
printf " $test\r"
exec_it oc label --overwrite node/ose3-node2.example.com region=primary zone=west
test_exit $? "$test"
}
function configure_routing_domain(){
test="Configure default routing domain..."
printf " $test\r"
exec_it sed -i \''s/^ subdomain.*/\ subdomain: "cloudapps.example.com"/'\' /etc/origin/master/master-config.yaml
test_exit $? "$test"
test="Restart master..."
printf " $test\r"
exec_it systemctl restart atomic-openshift-master
test_exit $? "$test"
# wait for things to settle
sleep 15
}
function configure_default_nodeselector(){
test="Configure default nodeselector for system..."
printf " $test\r"
exec_it sed -i /etc/origin/master/master-config.yaml -e \''s/defaultNodeSelector: ""/defaultNodeSelector: "region=primary"/'\'
test_exit $? "$test"
test="Restart master..."
printf " $test\r"
exec_it systemctl restart atomic-openshift-master
test_exit $? "$test"
# wait for things to settle
sleep 15
}
function configure_default_project_selector(){
test="Configure default namespace selector..."
printf " $test\r"
exec_it oc get namespace default -o json "|" sed -e \''/"openshift.io\/sa.scc.mcs"/i "openshift.io/node-selector": "region=infra",'\' "|" oc replace -f -
test_exit $? "$test"
}
function setup_dev_users(){
test="Setting up joe..."
printf " $test\r"
exec_it getent passwd joe
if [ ! $? -eq 0 ]
then
exec_it useradd joe
test_exit $? "$test"
fi
test="Setting up alice..."
printf " $test\r"
exec_it getent passwd alice
if [ ! $? -eq 0 ]
then
useradd alice
test_exit $? "$test"
fi
test="Creating passwd file..."
printf " $test\r"
exec_it touch /etc/origin/openshift-passwd
test_exit $? "$test"
test="Setting joe password..."
printf " $test\r"
exec_it htpasswd -b /etc/origin/openshift-passwd joe redhat
test_exit $? "$test"
test="Setting alice password..."
printf " $test\r"
exec_it htpasswd -b /etc/origin/openshift-passwd alice redhat
test_exit $? "$test"
}
function setup_default_project_template(){
# check if the template is already there
exec_it oc get template/default-project-request -n default
if [ ! $? -eq 0 ]
then
test="Creating default project template..."
printf " $test\r"
exec_it oc create -f ~/training/content/default-project-template.yaml
test_exit $? "$test"
fi
# check if the setting for default template is set
exec_it grep default-project-request /etc/origin/master/master-config.yaml
if [ ! $? -eq 0 ]
then
test="Configuring OpenShift to use the default project template..."
printf " $test\r"
exec_it sed -i -e \''s/^ projectRequestTemplate:.*/\ projectRequestTemplate: "default\/default-project-request"/'\' /etc/origin/master/master-config.yaml
test_exit $? "$test"
test="Restarting master..."
printf " $test\r"
exec_it systemctl restart atomic-openshift-master
test_exit $? "$test"
fi
}
function create_joe_project(){
# check for project
exec_it oc get project demo
if [ $? -eq 0 ]
then
exec_it oc delete project demo
wait_on_project demo 30
fi
# a little extra time
sleep 3
test="Creating project for joe..."
printf " $test\r"
exec_it su - joe -c \""oc new-project demo --display-name='OpenShift 3 Demo' --description='This is the first demo project with OpenShift v3'"\"
test_exit $? "$test"
}
function set_project_quota_limits(){
# is there already a quota?
exec_it oc get quota -n demo "|" grep quota
if [ $? -eq 1 ]
then
test="Create quota on joe's project..."
printf " $test\r"
exec_it oc create -f ~/training/content/quota.json -n demo
test_exit $? "$test"
fi
# is there already a limit?
exec_it oc get limitrange -n demo "|" grep limits
if [ $? -eq 1 ]
then
test="Create limits on joe's project..."
printf " $test\r"
exec_it oc create -f ~/training/content/limits.json -n demo
test_exit $? "$test"
fi
}
function joe_login_pull(){
test="Login as joe..."
printf " $test\r"
exec_it su - joe -c \""oc login -u joe -p redhat \
--certificate-authority=/etc/origin/master/ca.crt \
--server=https://ose3-master.example.com:8443"\"
test_exit $? "$test"
# make sure to set the right project in case this is a re-run
exec_it su - joe -c \""oc project demo"\"
if [ ! -d /home/joe/training ]
then
test="Pulling training content..."
printf " $test\r"
exec_it su - joe -c \""git clone https://github.com/$gituser/training -b $branch"\"
test_exit $? "$test"
else
test="Updating training content..."
printf " $test\r"
exec_it su - joe -c \""cd ~/training && git pull $gituser $branch"\"
test_exit $? "$test"
fi
}
function hello_pod(){
exec_it su - joe -c \""oc project demo"\"
test="Creating hello-openshift pod..."
printf " $test\r"
exec_it su - joe -c \""oc create -f ~/training/content/hello-pod.json"\"
test_exit $? "$test"
wait_on_pod "hello-openshift" "demo" 30
# if we came out of that successfully, proceed
test="Verifying hello-pod..."
printf " $test\r"
exec_it curl $(oc get pod hello-openshift -n demo --template '{{.status.podIP}}'):8080 "|" grep Hello
test_exit $? "$test"
test="Deleting hello-pod..."
printf " $test\r"
exec_it su - joe -c \""oc delete pod hello-openshift"\"
test_exit $? "$test"
# it takes 10 seconds for quota to update
sleep 15
}
function hello_quota() {
exec_it su - joe -c \""oc project demo"\"
# if there are any pods, nuke 'em and start over
ans=$(oc get pods -n demo | wc -l)
if [ $ans != 1 ]
then
exec_it oc delete pods --all -n demo
# it takes 10 seconds for quota to update
sleep 15
fi
test="Checking if quota is enforced..."
printf " $test\r"
exec_it su - joe -c \""oc create -f ~/training/content/hello-quota.json"\"
if [ $? -eq 1 ]
then
# we failed, which we wanted to, so exit successfully
test_exit 0 "$test"
else
test_exit 1 "$test"
fi
exec_it oc delete pods --all -n demo
# it takes 10 seconds for quota to update
sleep 15
}
function joe_project(){
joe_login_pull
create_joe_project
set_project_quota_limits
hello_pod
hello_quota
setup_default_project_template
}
function create_populate_service(){
# delete hello service
exec_it oc delete service --all -n demo
exec_it oc delete pod --all -n demo
exec_it oc delete route --all -n demo
sleep 15
exec_it su - joe -c \""oc project demo"\"
test="Creating hello-service..."
printf " $test\r"
exec_it su - joe -c \""oc create -f ~/training/content/hello-service.json"\"
test_exit $? "$test"
test="Creating pods..."
printf " $test\r"
exec_it su - joe -c \""oc create -f ~/training/content/hello-service-pods.json"\"
test_exit $? "$test"
# there's probably an easier way to do this, but this is pretty easy
wait_on_pod "hello-openshift-1" "demo" 30
wait_on_pod "hello-openshift-2" "demo" 30
wait_on_pod "hello-openshift-3" "demo" 30
# just in case
sleep 5
test="Checking service endpoints..."
# there should be three
printf " $test\r"
exec_it oc get endpoints hello-service -n demo --template \''{{index .subsets 0 "addresses" | len}}'\' "|" grep 3
test_exit $? "$test"
test="Validating service..."
printf " $test\r"
exec_it curl $(oc get service hello-service -n demo --template \''{{.spec.clusterIP}}:{{index .spec.ports 0 "port"}}'\')
test_exit $? "$test"
}
function install_router(){
# just in case
exec_it oc project default
cd
CA=/etc/origin/master
if [ ! -e /root/cloudapps.router.pem ]
then
test="Creating server certificates..."
printf " $test\r"
exec_it oadm ca create-server-cert --signer-cert=$CA/ca.crt \
--signer-key=$CA/ca.key --signer-serial=$CA/ca.serial.txt \
--hostnames=\''*.cloudapps.example.com'\' \
--cert=cloudapps.crt --key=cloudapps.key
test_exit $? "$test"
test="Combining certificates..."
printf " $test\r"
exec_it cat cloudapps.crt cloudapps.key $CA/ca.crt ">" cloudapps.router.pem
test_exit $? "$test"
fi
# check scc
exec_it oc get scc privileged -o yaml | grep router
if [ $? -eq 1 ]
then
test="Adding router service account to privileged scc..."
printf " $test\r"
exec_it oc get scc privileged -o yaml "|" sed -e \''/openshift-infra:build-controller/a - system:serviceaccount:default:router'\' "|" oc replace -f -
test_exit $? "$test"
fi
# check for router
exec_it oadm router --dry-run --credentials='/etc/origin/master/openshift-router.kubeconfig' --service-account=router
# if no router
if [ $? -eq 1 ]
then
test="Installing router..."
printf " $test\r"
exec_it oadm router router --replicas=1 --default-cert=cloudapps.router.pem --credentials='/etc/origin/master/openshift-router.kubeconfig' --service-account=router
test_exit $? "$test"
fi
# verify that router came up
# first wait for rc to indicate status
wait_on_rc "router-1" "default" 60 1
# now find the router pod and wait for that to be ready
ans=$(oc get pod | awk '{print $1}'| grep -E "^router-1-\w{5}$")
wait_on_pod $ans "default" 60
# add router admin iptables port
check_add_iptables_port 1936 tcp
# add iptables rules to sysconfig file
exec_it grep \""dport 1936"\" /etc/sysconfig/iptables
if [ $? -eq 1 ]
then
test="Adding router iptables rules to sysconfig file..."
printf " $test\r"
exec_it sed -i -e \''/^COMMIT$/i -A OS_FIREWALL_ALLOW -p tcp -m state --state NEW -m tcp --dport 1936 -j ACCEPT\'\' \
/etc/sysconfig/iptables
test_exit $? "$test"
fi
}
function expose_test_service(){
# check for route
exec_it oc get route hello-service -n demo
if [ ! $? -eq 0 ]
then
test="Exposing hello-service service..."
printf " $test\r"
exec_it su - joe -c \""oc expose service hello-service -l name=hello-openshift"\"
test_exit $? "$test"
fi
# wait to settle
sleep 5
test="Verifying the route..."
printf " $test\r"
exec_it curl hello-service-demo.cloudapps.example.com "|" grep Hello
test_exit $? "$test"
}
function complete_pod_service_route(){
# delete everything in the project
exec_it su - joe -c \""oc project demo"\"
exec_it su - joe -c \""oc delete all -l name=hello-openshift -n demo"\"
# wait for quota
sleep 15
# create complete def
test="Creating the complete definition..."
printf " $test\r"
exec_it su - joe -c \""oc create -f ~/training/content/test-complete.json"\"
test_exit $? "$test"
wait_on_rc "hello-openshift-1" "demo" 60 1
ans=$(oc get pod -n demo | awk '{print $1}'| grep -E "^hello-openshift-1-\w{5}$")
wait_on_pod "$ans" "demo" 60
wait_on_endpoints "hello-openshift-service" "demo" 60
sleep 15
test="Testing the new HTTPS route..."
printf " $test\r"
exec_it curl -k https://hello-openshift.cloudapps.example.com "|" grep Hello
test_exit $? "$test"
}
function project_administration(){
test="Add alice to view role..."
printf " $test\r"
exec_it su - joe -c \""oadm policy add-role-to-user view alice"\"
test_exit $? "$test"
# things settle
sleep 5
test="Login as alice..."
printf " $test\r"
exec_it su - alice -c \""oc login -u alice -p redhat \
--certificate-authority=/etc/origin/master/ca.crt \
--server=https://ose3-master.example.com:8443"\"
test_exit $? "$test"
exec_it su - alice -c \""oc project demo"\"
test="Alice should be able to see a pod..."
printf " $test\r"
exec_it su - alice -c \""oc get pod | grep hello-openshift"\"
test_exit $? "$test"
test="Alice can't delete pods..."
printf " $test\r"
ans=$(oc get pod -n demo | awk '{print $1}'| grep -E "^hello-openshift-1-\w{5}$")
exec_it su - alice -c \""oc delete pod $ans"\"
if [ $? -eq 1 ]
then
test_exit 0 "$test"
else
text_exit 1 "$test"
fi
test="Add alice to edit role..."
printf " $test\r"
exec_it su - joe -c \""oadm policy add-role-to-user edit alice"\"
test_exit $? "$test"
test="Set alice project to demo..."
printf " $test\r"
exec_it su - alice -c \""oc project demo"\"
test_exit $? "$test"
test="Alice can delete pods..."
printf " $test\r"
ans=$(oc get pod -n demo | awk '{print $1}'| grep -E "^hello-openshift-1-\w{5}$")
exec_it su - alice -c \""oc delete pod $ans"\"
test_exit $? "$test"
test="Add alice to admin role..."
printf " $test\r"
exec_it su - joe -c \""oadm policy add-role-to-user admin alice"\"
test_exit $? "$test"
test="Alice can remove joe..."
printf " $test\r"
exec_it su - alice -c \""oadm policy remove-user joe"\"
test_exit $? "$test"
test="Alice can delete demo project..."
printf " $test\r"
exec_it su - alice -c \""oc delete project demo"\"
test_exit $? "$test"
}
function check_add_iptables_port(){
# $1 = port
# $2 = protocol
exec_it iptables-save "|" grep \""port $1"\"
if [ $? -eq 1 ]
then
test="Adding live iptables rule for $2 port $1..."
printf " $test\r"
exec_it iptables -I OS_FIREWALL_ALLOW -p $2 -m state --state NEW -m $2 --dport $1 -j ACCEPT
test_exit $? "$test"
fi
}
function prepare_nfs(){
test="Create NFS export folder..."
printf " $test\r"
exec_it install -d -m 0777 -o nfsnobody -g nfsnobody /var/export/regvol
test_exit $? "$test"
test="Create exports file..."
printf " $test\r"
exec_it echo \""/var/export/regvol *(rw,sync,all_squash)"\" ">" /etc/exports
test_exit $? "$test"
# add iptables rules to running iptables
check_add_iptables_port 111 tcp
check_add_iptables_port 2049 tcp
check_add_iptables_port 20048 tcp
check_add_iptables_port 50825 tcp
check_add_iptables_port 53248 tcp
# add iptables rules to sysconfig file
exec_it grep \""dport 53248"\" /etc/sysconfig/iptables
if [ $? -eq 1 ]
then
test="Adding NFS iptables rules to sysconfig file..."
printf " $test\r"
exec_it sed -i -e \''/^COMMIT$/i -A OS_FIREWALL_ALLOW -p tcp -m state --state NEW -m tcp --dport 53248 -j ACCEPT\
-A OS_FIREWALL_ALLOW -p tcp -m state --state NEW -m tcp --dport 50825 -j ACCEPT\
-A OS_FIREWALL_ALLOW -p tcp -m state --state NEW -m tcp --dport 20048 -j ACCEPT\
-A OS_FIREWALL_ALLOW -p tcp -m state --state NEW -m tcp --dport 2049 -j ACCEPT\
-A OS_FIREWALL_ALLOW -p tcp -m state --state NEW -m tcp --dport 111 -j ACCEPT'\' \
/etc/sysconfig/iptables
test_exit $? "$test"
fi
test="Setting NFS args in sysconfig file..."
printf " $test\r"
exec_it sed -i -e \''s/^RPCMOUNTDOPTS.*/RPCMOUNTDOPTS="-p 20048"/'\' -e \''s/^STATDARG.*/STATDARG="-p 50825"/'\' /etc/sysconfig/nfs
test_exit $? "$test"
exec_it grep \""nlm_tcpport"\" /etc/sysctl.conf
if [ $? -eq 1 ]
then
test="Adding sysctl NFS parameters..."
printf " $test\r"
exec_it sed -i -e \""\\\$afs.nfs.nlm_tcpport=53248"\" -e \""\\\$afs.nfs.nlm_udpport=53248"\" /etc/sysctl.conf
test_exit $? "$test"
fi
test="Enable rpcbind and nfs-server..."
printf " $test\r"
exec_it systemctl enable rpcbind nfs-server
test_exit $? "$test"
test="Start rpcbind, nfs-server, nfs-lock..."
printf " $test\r"
exec_it systemctl start rpcbind nfs-server nfs-lock
test_exit $? "$test"
test="Start nfs-idmap..."
printf " $test\r"
exec_it systemctl start nfs-idmap
test_exit $? "$test"
test="Persisting sysctl parameters..."
printf " $test\r"
exec_it sysctl -p
test_exit $? "$test"
test="Restarting nfs..."
printf " $test\r"
exec_it systemctl restart nfs
test_exit $? "$test"
test="Setting NFS seboolean..."
printf " $test\r"
exec_it setsebool -P virt_use_nfs=true
test_exit $? "$test"
}
function setup_storage_volumes_claims(){
exec_it oc project default
# check for volume
exec_it oc get pv registry-volume
if [ $? -eq 1 ]
then
test="Setting up registry storage volume..."
printf " $test\r"
exec_it oc create -f ~/training/content/registry-volume.json
test_exit $? "$test"
fi
exec_it oc get pvc registry-claim
if [ $? -eq 1 ]
then
test="Setting up registry volume claim..."
printf " $test\r"
exec_it oc create -f ~/training/content/registry-claim.json
test_exit $? "$test"
fi
sleep 5
}
function install_registry(){
# check for registry
exec_it oadm registry --dry-run \
--config=/etc/origin/master/admin.kubeconfig \
--credentials=/etc/origin/master/openshift-registry.kubeconfig
# if no registry
if [ $? -eq 1 ]
then
test="Installing Docker registry..."
printf " $test\r"
exec_it oadm registry \
--config=/etc/origin/master/admin.kubeconfig \
--credentials=/etc/origin/master/openshift-registry.kubeconfig
test_exit $? "$test"
# if registry is already scaled to zero we can skip
# check if rc 1 was ever successful
exec_it oc describe rc docker-registry-1 "|" grep successfulCreate
if [ $? -eq 0 ]
then
# check if status = spec = 0
ans=$(oc get rc docker-registry-1 --template '{{.spec.replicas}}{{.status.replicas}}')
if [ $ans -eq 00 ]
then
return
fi
fi
# we need to wait for the registry to get deployed before we can scale it down
wait_on_rc "docker-registry-1" "default" 60 1
fi
}
function add_claimed_volume(){
exec_it oc project default
# check for claim
exec_it oc get dc docker-registry -o yaml "|" grep registry-claim
if [ $? -eq 1 ]
then
test="Adding the claimed volume to the Docker registry..."
printf " $test\r"
exec_it oc volume dc/docker-registry --add --overwrite -t persistentVolumeClaim \
--claim-name=registry-claim --name=registry-storage
test_exit $? "$test"
wait_on_rc "docker-registry-2" "default" 60 1
sleep 5
pod=$(oc get pod | awk '{print $1}' | grep -v deploy | grep -E "^docker-registry-2-\w{5}")
wait_on_pod "$pod" "default" 60
fi
}
function s2i_project(){
# check for project
exec_it oc get project sinatra
if [ $? -eq 0 ]
then
exec_it oc delete project sinatra
wait_on_project sinatra 30
fi
# a little extra time
sleep 3
test="Creating sinatra S2I project..."
printf " $test\r"
exec_it su - joe -c \""oc new-project sinatra --display-name=\""Sinatra Example"\" \
--description=\""This is your first build on OpenShift 3\"""\"
test_exit $? "$test"
test="Using new-app to create content..."
printf " $test\r"
exec_it su - joe -c \""oc new-app https://github.com/openshift/sinatra-example \
--name=example"\"
test_exit $? "$test"
test="Exposing the service..."
printf " $test\r"
exec_it su - joe -c \""oc expose service example"\"
test_exit $? "$test"
# may take up to 120 seconds for build to start
wait_on_build "example-1" "sinatra" 120 "Running"
# now wait up to 2 mins for build to complete
wait_on_build "example-1" "sinatra" 280 "Complete"
wait_on_rc "example-1" "sinatra" 60 1
ans=$(oc get pod -n sinatra | grep -v build | grep example | grep -v deploy | awk {'print $1'})
wait_on_pod "$ans" "sinatra" 60
wait_on_endpoints "example" "sinatra" 60
exec_it sleep 60
test="Testing the service..."
printf " $test\r"
exec_it curl `oc get service -n sinatra example --template '{{.spec.portalIP}}:{{index .spec.ports 0 "port"}}'` "|" grep \""the time"\"
test_exit $? "$test"
sleep 15
test="Testing the route..."
printf " $test\r"
exec_it curl example-sinatra.cloudapps.example.com "|" grep \""the time"\"
test_exit $? "$test"
test="Scaling joe's app..."
printf " $test\r"
exec_it su - joe -c \""oc scale --replicas=3 rc/example-1"\"
test_exit $? "$test"
wait_on_rc "example-1" "sinatra" 60 3
# find the pods
# 3 pods should run
for pod in $(oc get pod -n sinatra | grep example | grep -v build | awk {'print $1'})
do
wait_on_pod "$pod" "sinatra" 30
done
# start new build
exec_it su - joe -c \""oc start-build example"\"
sleep 15
# build will never schedule so we need to look at the events with describe
# forbidden will immediately be show
test="Build should be forbidden..."
printf " $test\r"
exec_it su - joe -c \""oc get event | grep forbidden"\"
# build should be forbidden
test_exit $? "$test"
}
function templates_project() {
update_project_template_quota
test_exit $? "$test"
# check for project
exec_it oc get project quickstart
if [ $? -eq 0 ]
then
exec_it oc delete project quickstart
wait_on_project quickstart 30
fi
# a little extra time
sleep 3
# create the project
test="Creating quickstart project..."
printf " $test\r"
exec_it su - joe -c \""oc new-project quickstart --display-name=\"Quickstart\" \
--description='A demonstration of a \"quickstart/template\"'"\"
test_exit $? "$test"
# add the quickstart sample app template if it's not already there
exec_it oc get template -n openshift quickstart-keyvalue-application
if [ $? -eq 1 ]
then
test="Adding the quickstart template..."
printf " $test\r"
exec_it oc create -f ~/training/content/quickstart-template.json -n openshift
test_exit $? "$test"
fi
# create via joe
test="Instantiating the quickstart application..."
printf " $test\r"
exec_it su - joe -c \""oc new-app quickstart-keyvalue-application"\"
test_exit $? "$test"
# wait for the build to start
wait_on_build "ruby-sample-build-1" "quickstart" 120 "Running"
# wait for build to finish
wait_on_build "ruby-sample-build-1" "quickstart" 280 "Complete"
# wait for rc to deploy
wait_on_rc "frontend-1" "quickstart" 240 2
# find the deployed pods
pods=$(oc get pod -n quickstart | grep frontend | grep -v deploy | awk {'print $1'})
for pod in $pods
do
wait_on_pod "$pod" "quickstart" 60
done
sleep 15
# test the application
test="Testing the application..."
printf " $test\r"
exec_it curl keyvalue-route-quickstart.cloudapps.example.com "|" grep OpenShift
test_exit $? "$test"
}
function wiring_project() {
update_project_template_quota
# check for project
exec_it oc get project wiring
if [ $? -eq 0 ]
then
exec_it oc delete project wiring
wait_on_project wiring 30
fi
# a little extra time
sleep 3
# create the project
test="Creating wiring project..."
printf " $test\r"
exec_it su - alice -c \""oc new-project wiring --display-name='Exploring Parameters' \
--description='An exploration of wiring using parameters'"\"
test_exit $? "$test"
# pull the training material
if [ ! -d /home/alice/training ]
then