-
Notifications
You must be signed in to change notification settings - Fork 1
/
novelloshell-v5.sh
executable file
·1701 lines (1526 loc) · 46.5 KB
/
novelloshell-v5.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
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
# Copyright 2020, 2021 NovelloShell Author
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
# Report Bugs: [email protected]
CONFIGFILE="/etc/novelloshell.cfg"
if [ ! -f "$CONFIGFILE" ]
then
echo -e "ERROR: $CONFIGFILE does not exist"
exit 1
fi
PUBLICNETWORK=$(grep PUBLICNETWORK $CONFIGFILE | grep -v ^#)
ADMINRC=$(grep ADMINRC $CONFIGFILE | grep -v ^#)
APPSDIR=$(grep APPSDIR $CONFIGFILE | grep -v ^#)
BPSDIR=$(grep BPSDIR $CONFIGFILE | grep -v ^#)
TAG=$(grep TAG $CONFIGFILE | grep -v ^#)
FAQURL=$(grep FAQURL $CONFIGFILE | grep -v ^#)
MOTD=$(grep MOTD $CONFIGFILE | grep -v ^#)
ADMINUSERSFILE=$(grep ADMINUSERSFILE $CONFIGFILE | grep -v ^#)
IMAGEFILESPATH=$(grep IMAGEFILESPATH $CONFIGFILE | grep -v ^#)
STARTUPSCRIPTSPATH=$(grep STARTUPSCRIPTSPATH $CONFIGFILE | grep -v ^#)
WEBURL=$(grep WEBURL $CONFIGFILE | grep -v ^#)
ADMINACCESS=$(grep ADMINACCESS= $CONFIGFILE | grep -v ^#)
DOMAIN=$(grep DOMAIN= $CONFIGFILE | grep -v ^#)
ADMIN_USRROL_SCRIPT=$(grep ADMIN_USRROL_SCRIPT= $CONFIGFILE | grep -v ^#)
ADMIN_STACK_SCRIPT=$(grep ADMIN_STACK_SCRIPT= $CONFIGFILE | grep -v ^#)
ADMIN_PUBLISH_IMAGE_SCRIPT=$(grep ADMIN_PUBLISH_IMAGE_SCRIPT= $CONFIGFILE | grep -v ^#)
CLISUFFIX=$(grep CLISUFFIX= $CONFIGFILE | grep -v ^#)
CLUSTERNAME=$(grep CLUSTERNAME= $CONFIGFILE | grep -v ^#)
PROJECTID=$(grep PROJECTID= $CONFIGFILE | grep -v ^#)
LABNAMESTR=$(grep LABNAMESTR= $CONFIGFILE | grep -v ^#)
MAXLABS=$(grep MAXLABS= $CONFIGFILE | grep -v ^#)
LOGFILE=$(grep LOGFILE= $CONFIGFILE | grep -v ^#)
STATSDIR=$(grep STATSDIR= $CONFIGFILE | grep -v ^#)
KEYFILEPATH=$(grep KEYFILEPATH= $CONFIGFILE | grep -v ^#)
eval $PUBLICNETWORK
eval $ADMINRC
eval $APPSDIR
eval $BPSDIR
eval $TAG
eval $FAQURL
eval $MOTD
eval $ADMINUSERSFILE
eval $IMAGEFILESPATH
eval $STARTUPSCRIPTSPATH
eval $WEBURL
typeset -l ADMINACCESS
eval $ADMINACCESS
eval $DOMAIN
eval $PROJECTID
eval $LABNAMESTR
eval $CLUSTERNAME
eval $MAXLABS
eval $LOGFILE
eval $STATSDIR
eval $KEYFILEPATH
REPORTFILE="ns-usage-stats.csv"
CSVSEP=";"
if [ -z "$CLUSTERNAME" ]
then
CLUSTERNAME="RHOS"
fi
if [ $ADMINACCESS == "no" ]
then
accesstype="non-admin"
echo -e "NovelloShell is running without admin rights"
eval $ADMIN_USRROL_SCRIPT
eval $ADMIN_STACK_SCRIPT
eval $ADMIN_PUBLISH_IMAGE_SCRIPT
echo $ADMIN_USRROL_SCRIPT
echo $ADMIN_STACK_SCRIPT
if [[ -x "$ADMIN_USRROL_SCRIPT" ]]
then
echo -e "NovelloShell will be using $ADMIN_USRROL_SCRIPT for user and role creation"
else
echo -e "File $ADMIN_USRROL_SCRIPT configured to be used for user and role creation does not exist or it is not executable"
exit 1
fi
if [[ -x "$ADMIN_STACK_SCRIPT" ]]
then
echo -e "NovelloShell will be using $ADMIN_STACK_SCRIPT for listing of all stacks"
else
echo -e "File $ADMIN_STK_SCRIPT configured to be used for listing of all stacks does not exist or it is not executable"
exit 1
fi
if [[ -x "$ADMIN_PUBLISH_IMAGE_SCRIPT" ]]
then
echo -e "NovelloShell will be using $ADMIN_PUBLISH_IMAGE_SCRIPT for publishing images"
else
echo -e "File $ADMIN_PUBLISH_IMAGE_SCRIPT configured to be used for publishing image does not exist or it is not executable"
exit 1
fi
else
accesstype="admin"
echo -e "NovelloShell is running with admin rights"
fi
#read p
eval $CLISUFFIX
USERNAME=$(whoami)
# Set the ADMINUSER flag if the user has administrative rights
grep $USERNAME $ADMINUSERSFILE
if [ $? -eq 0 ]
then
ADMINUSER=1
else
ADMINUSER=0
fi
cd $BPSDIR
bold=`tput bold`
normal=`tput sgr0`
italic=`tput sgr3`
## This function may need to be tweaked based on the requirements of the ADMIN_STACK_SCRIPT being called.
## ekg USERNAME argument passed here may or may not be needed based on the ADMIN_STACK_SCRIPT implementation.
function exec_admin_stack_script
{
#echo -e $ADMIN_STACK_SCRIPT
#echo -e $@
eval $ADMIN_STACK_SCRIPT $USERNAME $@
}
function exec_admin_usrrol_script
{
#echo -e $ADMIN_USRROL_SCRIPT
#echo -e $@
eval $ADMIN_USRROL_SCRIPT $@
}
function exec_admin_publish_image_script
{
#echo -e $ADMIN_PUBLISH_IMAGE_SCRIPT
#echo -e $@
WriteLog "exec_admin_publish_image_script"
eval $ADMIN_PUBLISH_IMAGE_SCRIPT $@
}
function PrintMenuOptions1
{
clear
echo -e "\tNovelloShell - Shell based tool for Ravello like functionality on OpenStack cloud
\t=================================================================================\n
NovelloShell access on ${bold} $CLUSTERNAME : $accesstype ${normal}
FAQs : $FAQURL
${bold}$(cat $MOTD)${normal} \n
${bold}User:\t $USERNAME ${normal}\n
Lab environment options:
--------------------------
new - Launch a new lab from blueprint
list - List your launched labs
connect - Connect to the lab environment"
if [ $ADMINUSER -eq 0 ]
then
echo -e "info - Read the information about the lab environment template"
fi
if [ $ADMINUSER -eq 1 ]
then
echo -e "INFO - Read or edit the information about the lab environment template"
echo -e ""
echo -e "BECOME - Use NovelloShell as other user"
echo -e ""
echo -e "BLUEPRINT - Manage blueprints on Novello cluster"
echo -e "IMAGE - Manage images on Novello cluster"
echo -e ""
fi
echo -ne "exit - Exit from NovelloShell
Enter your choice: "
}
function PrintMenuOptions2
{
clear
echo -e "\tNovelloShell - Shell based tool for Ravello like functionality on OpenStack cloud
\t=================================================================================\n
${bold}User:\t $USERNAME ${normal}
${bold}Lab environment:\t $lab ${normal}
\n
Lab environment activities:
--------------------------
status - Show status of a lab
console - Show console access details
start - Start VMs in the lab
stop - Stop VMs in the lab
delete - Delete a lab
toggle - Toggle boot sequence for a VM
"
if [ $ADMINUSER -eq 1 ]
then
echo -e "SHELL - Open Stack shell for the lab
EDIT - Edit the stack template of application
(need to run "UPDATE" for the changes to take effect)
UPDATE - Update lab environment with current heat template
SAVE - Save the application as blueprint
PUBLISH - Publish images within this project
"
fi
echo -ne "back - Go back to previous menu
exit - Exit from NovelloShell
Enter your choice: "
}
function PrintBlueprintOptions
{
clear
echo -e "\tNovelloShell - Shell based tool for Ravello like functionality on OpenStack cloud
\t=================================================================================\n
NovelloShell access on ${bold} $CLUSTERNAME : $accesstype ${normal}
${bold}User:\t $USERNAME ${normal}\n
Blueprint management options:
-----------------------------"
echo -ne "CLONE - Clone a new lab from existing lab environment template
EDIT - Edit lab environment blueprint
RENAME - Rename an existing lab environment
DELETE - Delete lab environment blueprint
STARTUP - Edit startup scripts for the lab
back - Go back to previous menu
exit - Exit from NovelloShell
Enter your choice: "
}
function PrintImageOptions
{
clear
echo -ne "\tNovelloShell - Shell based tool for Ravello like functionality on OpenStack cloud
\t=================================================================================\n
NovelloShell access on ${bold} $CLUSTERNAME : $accesstype ${normal}
${bold}User:\t $USERNAME ${normal}\n
Image management options:
------------------------"
echo -ne "\nUpload - Upload image(s) to Novello cluster
Delete - Delete image from Novello cluster (just mark for deletion, can be retrieved)
Purge - Purge the image marked for deletion (permanent delete, no recovery possible)
Retrieve - Retrieve deleted image from Novello cluster
Show - Show details of image
back - Go back to previous menu
exit - Exit from NovelloShell
Enter your choice: "
}
function MenuOptionActions1
{
read choice
case "$choice" in
'new')
NewLab
;;
'list')
ListLabs
;;
'info')
LabInfo
;;
'INFO')
LabInfo
;;
'connect')
LabBlueprintConnect
;;
'BECOME')
if [ $ADMINUSER -eq 1 ]
then
BecomeUser
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen1
fi
;;
'BLUEPRINT')
if [ $ADMINUSER -eq 1 ]
then
DisplayBlueprintScreen
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen1
fi
;;
'IMAGE')
if [ $ADMINUSER -eq 1 ]
then
DisplayImageScreen
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen1
fi
;;
'exit')
ExitNovelloShell
;;
*)
echo -e "Invalid option, hit Enter to try again."
read p
DisplayScreen1
;;
esac
}
function MenuBlueprintActions
{
read choice
case "$choice" in
'BLUEPRINT')
if [ $ADMINUSER -eq 1 ]
then
DisplayBlueprintScreen
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen1
fi
;;
'DELETE')
if [ $ADMINUSER -eq 1 ]
then
LabBlueprintDELETE
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen1
fi
;;
'CLONE')
if [ $ADMINUSER -eq 1 ]
then
LabBlueprintCLONE
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen1
fi
;;
'RENAME')
if [ $ADMINUSER -eq 1 ]
then
LabBlueprintRENAME
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen1
fi
;;
'EDIT')
if [ $ADMINUSER -eq 1 ]
then
LabBlueprintEDIT
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen1
fi
;;
'STARTUP')
if [ $ADMINUSER -eq 1 ]
then
LabBlueprintSTARTUP
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen1
fi
;;
'back')
DisplayScreen1
;;
'exit')
ExitNovelloShell
;;
*)
echo -e "Invalid option, hit Enter to try again."
read p
DisplayBlueprintScreen
;;
esac
}
function MenuOptionActions2
{
read choice
case "$choice" in
'start')
StartLabVMs
;;
'stop')
StopLabVMs
;;
'delete')
DeleteLab
;;
'status')
StatusLab
;;
'console')
ShowConsole
;;
'toggle')
ToggleBoot
;;
'UPDATE')
if [ $ADMINUSER -eq 1 ]
then
LabUPDATE
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen2
fi
;;
'SHELL')
if [ $ADMINUSER -eq 1 ]
then
LabSHELL
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen2
fi
;;
'SAVE')
if [ $ADMINUSER -eq 1 ]
then
LabSAVE
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen2
fi
;;
'EDIT')
if [ $ADMINUSER -eq 1 ]
then
LabEDIT
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen2
fi
;;
'PUBLISH')
if [ $ADMINUSER -eq 1 ]
then
LabImagePUBLISH
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen2
fi
;;
'back')
DisplayScreen1
;;
'exit')
ExitNovelloShell
;;
*)
echo -e "Invalid option, hit Enter to try again."
read p
DisplayScreen2
;;
esac
}
function MenuImageActions
{
read choice
case "$choice" in
'Upload')
UploadIMAGE
;;
'Delete')
DeleteIMAGE
;;
'Purge')
PurgeIMAGE
;;
'Retrieve')
RetrieveIMAGE
;;
'Show')
ShowIMAGE
;;
'back')
DisplayScreen1
;;
'exit')
ExitNovelloShell
;;
*)
echo -e "Invalid option, hit Enter to try again."
read p
DisplayImageScreen
;;
esac
}
function PauseDisplayScreen1
{
echo -e "Hit Enter to continue.."
read p
DisplayScreen1
}
function PauseDisplayScreen2
{
echo -e "Hit Enter to continue.."
read p
DisplayScreen2
}
function PauseDisplayImageScreen
{
echo -e "Hit Enter to continue.."
read p
DisplayImageScreen
}
function PauseDisplayBlueprintScreen
{
echo -e "Hit Enter to continue.."
read p
DisplayBlueprintScreen
}
function StopLabVMs
{
WriteLog "StopLabVMs $lab"
SetUserCredentialsFor $lab
echo -e "List of VMs in $lab"
openstack $CLISUFFIX server list -c 'Name' -f value -c 'Status' -f value
echo -e "Select VMs to be stopped from above list (provide space seperated list)"
read line
arr=($line)
echo -e "Stopping ${#arr[@]} VM(s): ${arr[@]}"
openstack $CLISUFFIX server stop ${arr[@]} > /dev/null 2>&1
PauseDisplayScreen2
}
function StartLabVMs
{
WriteLog "StartLabVMs $lab"
SetUserCredentialsFor $lab
echo -e "List of VMs in $lab"
openstack $CLISUFFIX server list -c 'Name' -f value -c 'Status' -f value
echo -e "Select VMs to be started from above list (provide space seperated list)"
read line
arr=($line)
echo -e "Starting ${#arr[@]} VM(s): ${arr[@]}"
openstack $CLISUFFIX server start ${arr[@]} > /dev/null 2>&1
PauseDisplayScreen2
}
function StatusLab
{
SetUserCredentialsFor $lab
echo -e "Probing status of $lab"
openstack $CLISUFFIX stack list
echo -e "Probing for list of instances in $lab"
openstack $CLISUFFIX server list -c Name -c Status -c Networks -c Image -c Flavor
echo -e "Probing lab access details..."
# Looking for 7 output values. Change the number if more output values are needed.
for n in {1..7}
do
openstack $CLISUFFIX stack output show $lab output_$n -c output_value -f value 2> /dev/null
done
PauseDisplayScreen2
}
function LabEDIT
{
labdir="${APPSDIR}/${lab}"
stackfile="${labdir}/stack_user.yaml"
vi $stackfile
echo -e "Make sure to UPDATE the stack for changes to take effect"
PauseDisplayScreen2
}
function LabSAVE
{
SetAdminCredentials
SetUserCredentialsFor $lab
read -p "Enter new Project name: " NEWLAB
while [[ -d "$NEWLAB" ]]
do
echo -e "Project with provided name already exists"
read -p "Enter new Project name: " NEWLAB
done
if [[ $NEWLAB == "" ]]
then
PauseDisplayBlueprintScreen
fi
#mkdir $NEWLAB
echo -ne "\nGetting list of available images . . . "
ImageArrayAvail=($(openstack $CLISUFFIX image list -c Name -f value))
echo -e "done"
echo -e "Stopping all the servers in this lab..."
# TODO: Do not stop the server which is already in stopped state
# TODO: Check if stopping the server is really needed
for i in `openstack $CLISUFFIX server list -c Name -f value`
do
openstack $CLISUFFIX server stop $i > /dev/null 2>&1
done
echo -ne "Waiting for all the server to stop. "
while [ -n "$(openstack $CLISUFFIX server list -c Name -c Status -f value | grep -v SHUTOFF)" ]
do
echo -n " . "
#sleep 5
done
echo -e " "
openstack $CLISUFFIX server list -c Name -c Status -f value
OLDLAB=$(openstack $CLISUFFIX stack list -c 'Stack Name' -f value)
OLDLAB=$(echo $OLDLAB | cut -d '-' -f 3- | rev | cut -d '_' -f 2- | rev)
echo -e "Cloaning current status of $lab to $NEWLAB"
cp -r $APPSDIR/$lab $BPSDIR/$NEWLAB
newstackfile="${BPSDIR}/${NEWLAB}/stack_user.yaml"
chmod -R a+w $BPSDIR/$NEWLAB
OLDIFS=$(echo $IFS)
IFS=$'\n'
for i in $(openstack $CLISUFFIX server list -c Name -c Image -f value)
do
echo -e "\n$i"
servername=$(echo $i | awk '{print $1}')
curimgname=$(echo $i | awk '{print $2}')
newimgname=$(echo "${NEWLAB}-${servername}")
echo -e "Processing vm $servername running from image $curimgname"
##Provide option to avoid cloaning common images
if [[ $curimgname =~ "pntaecommon" ]]
then
echo -e "\nPress Enter to skip saving common image $image \nType name of the new image if you wish save it."
echo -e "Copy/Paste the suggested name for new image $newimgname \nor type new name or press Enter to skip"
read newimgname
if [[ $newimgname == '' ]]
then
echo -e "Skipping $image"
continue
fi
fi
if [[ $newimgname != *"$TAG"* ]]; then
newimgname=$newimgname-pntae
fi
if [[ " ${ImageArrayAvail[@]} " =~ " ${newimgname} " ]]
then
echo -e "Image $newimgname already exists, skipping image . . . "
echo -e "Replacing $curimgname with $curimgname-MODIFYTHIS in $newstackfile"
sed -i "s/$curimgname/$curimgname-MODIFYTHIS/g" $newstackfile
else
echo -ne "Image $newimgname does not exist, creating image . . . "
echo -e "Creating new image $newimgname from $servername..."
openstack $CLISUFFIX server image create --name $newimgname $servername > /dev/null 2>&1
echo -e "Replacing $curimgname with $newimgname in $newstackfile"
cat -n $newstackfile > $newstackfile.num
lineno=$(grep name $newstackfile.num | grep $servername | head -1 | awk '{print $1}')
lineno=$(tail -n +$lineno $newstackfile.num | grep image | grep -v '#' | head -1 | awk '{print $1}')
cmd="sed -i \""${lineno}"s/"${curimgname}"/"${newimgname}"/\" "${newstackfile}
eval $cmd
fi
done
echo -e "\nWaiting for 2 minutes for images to be active..."
sleep 120
echo -e "Setting new images public..."
SetAdminCredentials
for image in $(grep 'image:' $newstackfile | grep -v '#')
do
image=$(echo $image | awk -F ':' '{print $2}' | tr -d '[:space:]' | tr -d '"')
##Avoid common images
#if [[ $image =~ "pntaecommon" ]]
#then
# echo -e "Skipping image $image"
# continue
#fi
# new image with pntaecommon tag is getting skipped from publishing. Hence commenting above code
echo -e "\nSetting $image as public image"
if [ $ADMINACCESS == "no" ]
then
exec_admin_publish_image_script $PROJECTID $image
else
openstack $CLISUFFIX image set --property visibility=public $image
fi
done
echo -e "Current status of $lab is frozen as blueprint with name $NEWLAB. "
echo -e "Please launch $NEWLAB and verify that the state of all the VMs is as expected."
echo -e "Do not delete this lab until you verify the new blueprint."
SetUserCredentialsFor $lab
PauseDisplayScreen2
}
function LabUPDATE
{
SetUserCredentialsFor $lab
echo -e "Updating lab environment $lab"
labdir="${APPSDIR}/${lab}"
openstack $CLISUFFIX stack update -t $labdir/stack_user.yaml $lab --parameter project_name=$lab --parameter public_net_id=$PUBLICNETWORK --parameter project_guid=$USERNAME
PauseDisplayScreen2
}
function LabImagePUBLISH
{
echo -e "\nListing images available for publishing..."
SetUserCredentialsFor $lab
openstack $CLISUFFIX image list --shared -c Name -f value
echo -e "please wait..."
openstack $CLISUFFIX image list --private -c Name -f value
read -p "Select (copy-paste) name of the image to be published (one at a time): " image
if [ -z "$image" ]
then
echo -e "No image selected"
PauseDisplayScreen2
fi
echo -e "\nSetting $image as public image..."
if [ $ADMINACCESS == "no" ]
then
exec_admin_publish_image_script $PROJECTID $image
else
openstack $CLISUFFIX image set --property visibility=public $image
fi
PauseDisplayScreen2
}
function ShowConsole
{
SetUserCredentialsFor $lab
echo -e "Probing list of VMs in $lab . . ."
openstack $CLISUFFIX server list -c 'Name' -f value -c 'Status' -f value
echo -e "===^===^===^===^===^===^===^==="
echo -e "Select VMs from above list for which you need console access details (provide space separated list)"
echo -e "You may copy-paste name of the VM(s) from the above list"
echo -e "or\nPress Enter to see console access details of all the VMs in this lab"
read line
if [[ ! -z "$line" ]]
then
arr=($line)
echo -e "Probing console access links for selected VM(s)\n"
for vm in "${arr[@]}"
do
echo -ne "$vm \t"
openstack $CLISUFFIX console url show $vm -c url -f value
echo -e "\n"
done
else
echo -e "Probing console access details for $lab\n"
for vm in `openstack $CLISUFFIX server list -c Name -f value`
do
echo -ne "$vm \t"
openstack $CLISUFFIX console url show $vm -c url -f value
echo -e "\n"
done
fi
PauseDisplayScreen2
}
function ToggleBoot
{
SetUserCredentialsFor $lab
echo -e "Listing vms of this lab, please wait..."
vms=($(openstack server list -c Name -f value))
printf '%s\n' "${vms[@]}"
read -p "Select the VM to toggle boot: " vmname
if [[ " ${vms[@]} " =~ " ${vmname} " ]]
then
echo -ne "VM exists ... "
eval `openstack server show $vmname -f shell | grep os_ext_sts`
if [[ "$os_ext_sts_vm_state" == "rescued" && "$os_ext_sts_task_state" == "None" ]]
then
echo -e "Server $vmname is in rescue mode, putting it in unrescue mode..."
openstack server unrescue $vmname
elif [[ "$os_ext_sts_vm_state" == "active" && "$os_ext_sts_task_state" == "None" ]]
then
echo -ne "checking image ... "
properties=`openstack server show $vmname -c properties -f value`
eval $properties
cdrom=$(echo $cdrom | sed 's/\,//')
if [ -z "$cdrom" ]
then
echo -e "$vmname is not configured to use cdrom"
PauseDisplayScreen2
fi
echo -ne "$cdrom ... "
openstack image list | grep $cdrom > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo -e "exists"
echo -e "Putting VM $vmname in rescue mode with image $cdrom"
openstack server rescue --image $cdrom $vmname
else
echo -e "does not exist"
PauseDisplayScreen2
fi
#echo -e "DETECT CDROM IMAGE AND USE IT FOR RESCUE"
fi
echo -e "PROBE FOR TOGGLE STATUS AND WHEN READY SHOW CONSOLE URL"
else
echo -e "VM does not exists"
fi
PauseDisplayScreen2
}
function ExitNovelloShell
{
WriteLog "ExitNovelloShell"
echo -e "Exiting NovelloShell"
exit
}
function DisplayScreen1
{
PrintMenuOptions1
MenuOptionActions1
}
function DisplayScreen2
{
PrintMenuOptions2
MenuOptionActions2
}
function DisplayBlueprintScreen
{
PrintBlueprintOptions
MenuBlueprintActions
}
function DisplayImageScreen
{
PrintImageOptions
MenuImageActions
}
function SetUserCredentialsFor
{
lab=$1
export OS_USERNAME=$lab
export OS_PASSWORD=redhat
export OS_PROJECT_NAME=$lab
### FIXME: Required config option for domain settings below
export OS_USER_DOMAIN_NAME=$DOMAIN
export OS_PROJECT_DOMAIN_NAME=$DOMAIN
}
function SetAdminCredentials
{
source $ADMINRC
}
function LaunchLabStack
{
CSVSTR=${USERNAME}
CSVSTR=${CSVSTR}${CSVSEP}${1}
uniqid=$(echo $RANDOM | md5sum | head -c 4)
lab=$USERNAME-$TAG-$1-$uniqid
SetAdminCredentials
if [[ "$ADMINACCESS" != "no" ]]
then
runninglabs=$(openstack $CLISUFFIX stack list | grep $USERNAME | wc -l)
else
runninglabs=$(exec_admin_stack_script | grep $USERNAME | wc -l)
fi
if [ $runninglabs -lt $MAXLABS ]
then
mkdir -p "${APPSDIR}/${lab}"
cpsrc="${BPSDIR}/${1}/stack_user.yaml"
cpdst="${APPSDIR}/${lab}"
cp $cpsrc $cpdst
stackfile="${APPSDIR}/${lab}/stack_user.yaml"
echo -e "Stack name: $lab"
if [[ "$ADMINACCESS" == "no" ]]
then
exec_admin_usrrol_script create $lab
WriteLog "exec_admin_usrrol_script create $lab"
else
## FIXME: Required config option for usage of --domain --user-domain and --project-domain options below
openstack $CLISUFFIX project create $lab --domain $DOMAIN > /dev/null 2>&1
openstack $CLISUFFIX user create --password redhat --project $lab $lab --domain $DOMAIN > /dev/null 2>&1
openstack $CLISUFFIX role add --user-domain $DOMAIN --project-domain $DOMAIN --project $lab --user $lab _member_ > /dev/null 2>&1
openstack $CLISUFFIX role add --user-domain $DOMAIN --project-domain $DOMAIN --project $lab --user admin admin > /dev/null 2>&1
openstack $CLISUFFIX role add --user $lab --user-domain $DOMAIN --project $lab --project-domain $DOMAIN member > /dev/null 2>&1
openstack $CLISUFFIX quota set --cores -1 --instances -1 --ram -1 $lab > /dev/null 2>&1
fi
SetUserCredentialsFor $lab
WriteLog "Creating stack for $lab"
openstack $CLISUFFIX stack create -t $stackfile $lab --parameter project_name=$lab --parameter public_net_id=$PUBLICNETWORK --parameter project_guid=$USERNAME
CSVSTR=${CSVSTR}${CSVSEP}${lab}
CSVSTR=${CSVSTR}${CSVSEP}$(date +"%a %b %d %T %Y")
echo -e $CSVSTR >> $STATSDIR/$REPORTFILE
else
echo -e "You can run maximum of $MAXLABS labs. Delete any of your running labs and try again."
fi
return
}
function ListLabs
{
echo -e "Checking list of your labs.."
SetAdminCredentials
if [[ "$ADMINACCESS" != 'no' ]]
then
openstack $CLISUFFIX stack list -c 'Stack Name' -c 'Stack Status' -c 'Creation Time' -f value | grep ^$USERNAME-
else
exec_admin_stack_script -c "'Stack Name'" -c "'Stack Status'" -c "'Creation Time'" -f value | grep ^$USERNAME-
fi
SelectLab
echo -e "checking presence of $lab"
SetUserCredentialsFor $lab
openstack $CLISUFFIX stack list -c 'Stack Name' | tr -d '|' | tr -d '[:blank:]' | grep ^$lab$