-
Notifications
You must be signed in to change notification settings - Fork 1
/
novelloshell-v3.sh
executable file
·705 lines (630 loc) · 15.7 KB
/
novelloshell-v3.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
#!/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=".novelloshell.cfg"
if [ ! -f "$CONFIGFILE" ]
then
echo -e "ERROR: $CONFIGFILE does not exist"
exit 1
fi
PUBLICNETWORK=$(grep PUBLICNETWORK $CONFIGFILE)
ADMINRC=$(grep ADMINRC $CONFIGFILE)
APPSDIR=$(grep APPSDIR $CONFIGFILE)
BPSDIR=$(grep BPSDIR $CONFIGFILE)
TAG=$(grep TAG $CONFIGFILE)
ADMINUSERSFILE=$(grep ADMINUSERSFILE $CONFIGFILE)
IMAGEFILESPATH=$(grep IMAGEFILESPATH $CONFIGFILE | grep -v ^#)
eval $PUBLICNETWORK
eval $ADMINRC
eval $APPSDIR
eval $BPSDIR
eval $TAG
eval $ADMINUSERSFILE
eval $IMAGEFILESPATH
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`
function PrintMenuOptions1
{
clear
echo -e "\tNovelloShell - Shell based tool for Ravello like functionality on OpenStack cloud
\t=================================================================================\n
${bold}User:\t $USERNAME ${normal}\n
Lab environment options:
--------------------------
new - Launch a new lab from blueprint
list - List launched labs"
if [ $ADMINUSER -eq 1 ]
then
echo -e "DELETE - Delete the lab environment blueprint"
echo -e "IMAGE - Upload images to Novello cluster"
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 a lab
stop - Stop a lab
delete - Delete a lab
shell - Shell for a lab
"
if [ $ADMINUSER -eq 1 ]
then
echo -e "SAVE - Save the application as blueprint
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
"
fi
echo -ne "back - Go back to previous menu
exit - Exit from NovelloShell
Enter your choice: "
}
function MenuOptionActions1
{
read choice
case "$choice" in
'new')
NewLab
;;
'list')
ListLabs
;;
'DELETE')
if [ $ADMINUSER -eq 1 ]
then
LabBlueprintDELETE
else
echo -e "Administrative rights required for this function\nHit enter to continue"
read p
DisplayScreen1
fi
;;
'IMAGE')
if [ $ADMINUSER -eq 1 ]
then
UploadIMAGE
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 MenuOptionActions2
{
read choice
case "$choice" in
'start')
StartLab
;;
'stop')
StopLab
;;
'delete')
DeleteLab
;;
'status')
StatusLab
;;
'console')
ShowConsole
;;
'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')
LabShell
;;
'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
;;
'back')
DisplayScreen1
;;
'exit')
ExitNovelloShell
;;
*)
echo -e "Invalid option, hit Enter to try again."
read p
DisplayScreen2
;;
esac
}
function PauseDisplayScreen1
{
echo -e "Hit Enter to continue.."
read p
DisplayScreen1
}
function PauseDisplayScreen2
{
echo -e "Hit Enter to continue.."
read p
DisplayScreen2
}
function StopLab
{
SetUserCredentialsFor $lab
echo -e "Suspending $lab"
openstack stack suspend $lab
echo -e "All the VMs of $lab will be suspended in some time"
PauseDisplayScreen2
}
function StartLab
{
SetUserCredentialsFor $lab
echo -e "Starting $lab"
openstack stack resume $lab
echo -e "All the VMs of $lab will be resumed in some time"
PauseDisplayScreen2
}
function StatusLab
{
SetUserCredentialsFor $lab
echo -e "Probing status of $lab"
openstack server list
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
{
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
PauseDisplayScreen2
fi
#mkdir $NEWLAB
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 server list -c Name -f value`
do
openstack server stop $i
done
echo -ne "Waiting for all the server to stop. "
while [ -n "$(openstack server list -c Name -c Status -f value | grep -v SHUTOFF)" ]
do
echo -n " . "
#sleep 5
done
echo -e " "
openstack server list -c Name -c Status -f value
OLDLAB=$(openstack stack list -c 'Stack Name' -f value)
OLDLAB=$(echo $OLDLAB | cut -d '-' -f 2- | 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"
OLDIFS=$(echo $IFS)
IFS=$'\n'
for i in $(openstack server list -c Name -c Image -f value)
do
echo $i
servername=$(echo $i | awk '{print $1}')
curimgname=$(echo $i | awk '{print $2}')
newimgname=$(echo "${curimgname/$OLDLAB/$NEWLAB}")
echo -e "Processing $servername with image $curimgname"
##Avoid cloaning common images
if [[ $curimgname =~ "pntaecommon" ]]
then
echo -e "Skipping image $image"
continue
fi
echo -e "Creating new image: $newimgname"
openstack server image create --name $newimgname $servername > /dev/null 2>&1
sed -i "s/$curimgname/$newimgname/g" $newstackfile
done
echo -e "Waiting for 2 minutes for images to be active..."
sleep 120
echo -e "Setting new images public..."
SetAdminCredentials
for image in $(grep 'image:' $newstackfile)
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
echo -e "Setting $image as public image"
openstack image set --property visibility=public $image
done
SetUserCredentialsFor $lab
PauseDisplayScreen2
}
function LabUPDATE
{
SetUserCredentialsFor $lab
echo -e "Updating lab environment $lab"
labdir="${APPSDIR}/${lab}"
openstack stack update -t $labdir/stack_user.yaml $lab --parameter project_name=$lab --parameter public_net_id=$PUBLICNETWORK --parameter project_guid=xxx
PauseDisplayScreen2
}
function ShowConsole
{
SetUserCredentialsFor $lab
echo -e "Probing console access details for $lab\n"
for vm in `openstack server list -c Name -f value`
do
echo -ne "$vm \t"
openstack console url show $vm -c url -f value
echo -e "\n"
done
PauseDisplayScreen2
}
function ExitNovelloShell
{
echo -e "Exiting NovelloShell"
exit
}
function DisplayScreen1
{
PrintMenuOptions1
MenuOptionActions1
}
function DisplayScreen2
{
PrintMenuOptions2
MenuOptionActions2
}
function SetUserCredentialsFor
{
lab=$1
export OS_USERNAME=$lab
export OS_PASSWORD=redhat
export OS_PROJECT_NAME=$lab
}
function SetAdminCredentials
{
source $ADMINRC
}
function LaunchLabStack
{
lab=$USERNAME-$1
SetAdminCredentials
appendno=0
count=1
runninglabs=$(openstack stack list | grep $lab | wc -l)
if [ $runninglabs -gt 0 ]
then
runninglabnos=$(openstack stack list -c 'Stack Name' | grep $lab | tr -d '|' | tr -d '[:blank:]' | rev | cut -d '_' -f 1)
while [ $appendno -eq 0 ]
do
if [[ $runninglabnos == *$count* ]]
then
count=$(( count + 1 ))
else
appendno=$count
fi
done
else
appendno=$count
fi
lab="${lab}_${appendno}"
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"
openstack project create $lab > /dev/null 2>&1
openstack user create --password redhat --project $lab $lab > /dev/null 2>&1
openstack role add --project $lab --user $lab _member_ > /dev/null 2>&1
openstack role add --project $lab --user admin admin > /dev/null 2>&1
openstack quota set --cores -1 --instances -1 --ram -1 $lab > /dev/null 2>&1
SetUserCredentialsFor $lab
openstack stack create -t $stackfile $lab --parameter project_name=$lab --parameter public_net_id=$PUBLICNETWORK --parameter project_guid=xxx
return
}
function ListLabs
{
echo -e "Checking list of your labs.."
SetAdminCredentials
openstack stack list -c 'Stack Name' -c 'Stack Status' -c 'Creation Time' | grep $USERNAME
SelectLab
echo -e "checking presence of $lab"
openstack stack list -c 'Stack Name' | tr -d '|' | tr -d '[:blank:]' | grep ^$lab$
if [ $? -ne 0 ]
then
echo -e "Invalid lab"
PauseDisplayScreen1
fi
DisplayScreen2
}
function SelectImage
{
echo -e "===^===^===^===^===^===^===^===^===^===^===^===^==="
echo -e "Select the name of the images from above list"
echo -e "Multiple images can be selected by seperating them with space"
echo -e "Enter '<' to go back to previous menu or 'x' to exit"
bind 'set mark-directories off' > /dev/null 2>&1
choice=""
count=0
while [[ $choice = "" ]]
do
read -ea choice
count=$(( count + 1))
if [ $count -eq 5 ]
then
choice='<'
fi
done
ImageArrayReq=("${choice[@]}")
if [[ $choice == '<' ]]
then
DisplayScreen1
fi
if [[ $choice == 'x' ]]
then
ExitNovelloShell
fi
return
}
function GetImageNameTypeFromFile
{
extn=$(echo $ImageFile | rev | cut -d '.' -f 1 | rev)
ImageName=$(echo $ImageFile | rev | cut -d '.' -f 2- | rev)
###echo -e "ImageName: $ImageName"
###echo -e "extn: $extn"
if [[ $extn == "img" || $extn == "raw" ]]
then
ImageType="raw"
else
ImageType=$extn
fi
}
function UploadIMAGE
{
clear
echo -e "Upload the Image(s) in lab environment"
echo -e "======================================"
cd $IMAGEFILESPATH
ls
SelectImage
SetAdminCredentials
echo -ne "\nGettng list of available images . . . "
ImageArrayAvail=($(openstack image list -c Name -f value))
echo -e "done"
###echo -e ${ImageArrayReq[@]}
###echo -e ${ImageArrayReq[0]}
for ImageFile in "${ImageArrayReq[@]}"
do
###echo $ImageFile
###read p
if [[ ! -f $ImageFile ]]
then
echo -e "$ImageFile does not exist, skipping..."
continue
fi
GetImageNameTypeFromFile
if [[ $ImageType != "raw" && $ImageType != "qcow2" && $ImageType != "iso" ]]
then
echo -e "Skipping $ImageFile of invalid image type: $ImageType"
continue
fi
if [[ " ${ImageArrayAvail[@]} " =~ " ${ImageName} " ]]
then
echo -e "Image $ImageName already exists, skipping $ImageFile . . . "
else
echo -ne "Image $ImageName does not exist, creating image . . . "
openstack image create --disk-format $ImageType --container-format bare --public --file $ImageFile $ImageName
echo -e "done"
fi
done
}
function SelectLab
{
echo -e "===^===^===^===^===^===^===^===^===^===^===^===^==="
echo -e "Select the name of lab environment from above list"
echo -e "Enter '<' to go back to previous menu or 'x' to exit"
bind 'set mark-directories off' > /dev/null 2>&1
choice=""
count=0
while [[ $choice = "" ]]
do
read -e choice
count=$(( count + 1))
if [ $count -eq 5 ]
then
choice='<'
fi
done
lab=$choice
if [ $choice == '<' ]
then
DisplayScreen1
fi
if [ $choice == 'x' ]
then
ExitNovelloShell
fi
return
}
function LabBlueprintDELETE
{
clear
echo -e "Delete the lab environment blueprint"
echo -e "===================================="
cd $BPSDIR
ls
SelectLab
if [ -d $choice ]
then
echo -e "Are you sure you want to delete the lab environment blueprint $choice ? (YES|NO)"
read confirm
if [[ $confirm != 'YES' ]]
then
echo -e "Not deleting the lab environment blueprint"
PauseDisplayScreen1
fi
echo -e "All associated images will be permanently deleted. Do you want to proceed? (YES|NO)"
read confirm
if [[ $confirm != 'YES' ]]
then
echo -e "Not deleting the lab environment blueprint"
PauseDisplayScreen1
fi
echo -e "Deleting the lab environment blueprint: $choice"
LabDataDELETE $choice
PauseDisplayScreen1
fi
echo -e "Ivalid option, hit Enter to try again"
read p
LabBlueprintDELETE
}
function LabDataDELETE
{
SetAdminCredentials
stackfile="$BPSDIR/$choice/stack_user.yaml"
OLDIFS=$(echo $IFS)
IFS=$'\n'
for image in $(grep 'image:' $stackfile)
do
image=$(echo $image | awk -F ':' '{print $2}' | tr -d '[:space:]' | tr -d '"')
##Avoid deleting common images
if [[ $image =~ "pntaecommon" ]]
then
echo -e "Skipping image $image"
continue
fi
echo -e "Deleting image $image"
openstack image delete $image
done
echo -e "Deleting heat template for $choice"
cmd="rm -rf $BPSDIR/$choice"
eval $cmd
if [ $? -ne 0 ]
then
echo -e "FAILED"
fi
}
function NewLab
{
clear
echo -e "Launch a new lab environment"
echo -e "============================"
cd $BPSDIR
ls
SelectLab
if [ -d $choice ]
then
echo -e "Launching lab environment for $choice"
LaunchLabStack $choice
PauseDisplayScreen1
fi
echo -e "Ivalid option, hit Enter to try again"
read p
NewLab
}
function DeleteLab
{
echo -e "Are you sure you want to delete the lab environment for $lab ? (YES|NO)"
read confirm
if [[ $confirm != 'YES' ]]
then
echo -e "Not deleting the lab environment"
PauseDisplayScreen2
fi
echo -e "Deleting $lab"
SetUserCredentialsFor $lab
openstack stack delete $lab --wait --yes
if [ $? -eq 0 ]
then
SetAdminCredentials
openstack project delete $lab
openstack user delete $lab
echo -e "\nSuccessfully deleted the lab environment"
echo -ne "Deleating associated files..."
rm -rf "${APPSDIR}/${lab}"
echo -e "done\n"
else
echo -e "\nFailed to delete the lab environment"
fi
PauseDisplayScreen1
}
function LabShell
{
SetUserCredentialsFor $lab
openstack
DisplayScreen2
}
DisplayScreen1