forked from ashish-k-s/NovelloShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
novelloshell.sh
executable file
·289 lines (256 loc) · 5.59 KB
/
novelloshell.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
#!/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]
PUBLICNETWORK=provider-network
ADMINRC="/export/novello/TESTING/do-not-use-this-rc"
WORKINGDIR="/export/novello/TESTING/PROJECTS"
USERNAME=$(whoami)
cd $WORKINGDIR
function PrintMenuOptions
{
clear
echo -ne "\tNovelloShell - a quick and dirty substitute of ravshello for Novello
\t====================================================================\n
Lab environment activities:
--------------------------
1 - Launch a new lab
2 - Start a lab
3 - Stop a lab
4 - Delete a lab
5 - Show status of a lab
6 - Show console access details
7 - Update lab
s - Shell for a lab
x - Exit from NovelloShell
Enter your choice: "
}
function MenuOptionActions
{
read choice
case "$choice" in
1)
LaunchLab
;;
2)
StartLab
;;
3)
StopLab
;;
4)
DeleteLab
;;
5)
StatusLab
;;
6)
ShowConsole
;;
7)
UpdateLab
;;
s)
LabShell
;;
x)
ExitNovelloShell
;;
*)
echo -e "Invalid option, hit Enter to try again."
read p
DisplayScreen1
;;
esac
}
function PauseDisplayScreen1
{
echo -e "Hit Enter to continue.."
read p
DisplayScreen1
}
function StopLab
{
CheckListOfLabs
SelectLab
lab=$choice
SetUserCredentialsFor $lab
echo -e "Suspending $lab"
openstack stack suspend $lab
echo -e "All the VMs of $lab will be suspended in some time"
PauseDisplayScreen1
}
function StartLab
{
CheckListOfLabs
SelectLab
lab=$choice
SetUserCredentialsFor $lab
echo -e "Starting $lab"
openstack stack resume $lab
echo -e "All the VMs of $lab will be resumed in some time"
PauseDisplayScreen1
}
function StatusLab
{
CheckListOfLabs
SelectLab
lab=$choice
SetUserCredentialsFor $lab
echo -e "Probing status of $lab"
openstack server list
PauseDisplayScreen1
}
function UpdateLab
{
CheckListOfLabs
SelectLab
lab=$choice
SetUserCredentialsFor $lab
echo -e "Updating lab environment $lab"
labdir=${lab//$USERNAME-/}
if [[ $(echo $labdir | rev | cut -d '-' -f 1 | rev) =~ ^n ]]
then
labdir=$(echo $labdir | rev | cut -d '-' -f 2- | rev)
fi
openstack stack update -t $labdir/stack_user.yaml $lab --parameter project_name=$lab --parameter public_net_id=$PUBLICNETWORK --parameter project_guid=xxx
}
function ShowConsole
{
CheckListOfLabs
SelectLab
lab=$choice
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
PauseDisplayScreen1
}
function ExitNovelloShell
{
echo -e "Exiting NovelloShell"
exit
}
function DisplayScreen1
{
PrintMenuOptions
MenuOptionActions
}
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
runninglabs=$(openstack stack list | grep $lab | wc -l)
if [ $runninglabs -gt 0 ]
then
no=$(( $runninglabs + 1 ))
lab="$lab-n$no"
fi
openstack project create $lab
openstack user create --password redhat --project $lab $lab
openstack role add --project $lab --user $lab _member_
openstack role add --project $lab --user admin admin
openstack quota set --cores -1 --instances -1 --ram -1 $lab
SetUserCredentialsFor $lab
openstack stack create -t $1/stack_user.yaml $lab --parameter project_name=$lab --parameter public_net_id=$PUBLICNETWORK --parameter project_guid=xxx
return
}
function CheckListOfLabs
{
echo -e "Checking list of your labs.."
SetAdminCredentials
openstack stack list -c 'Stack Name' -c 'Stack Status' -c 'Creation Time' | grep $USERNAME
}
function SelectLab
{
echo -e "================"
echo -e "Select the name of lab environment"
echo -e "Enter '<' to go back to previous menu or 'x' to exit"
read -e choice
if [ $choice == '<' ]
then
DisplayScreen1
fi
if [ $choice == 'x' ]
then
ExitNovelloShell
fi
}
function LaunchLab
{
clear
echo -e "Launch a new lab environment"
echo -e "============================"
ls
SelectLab
if [ -d $choice ]
then
echo -e "Launching stack for $choice"
LaunchLabStack $choice
PauseDisplayScreen1
fi
echo -e "Ivalid option, hit Enter to try again"
read p
LaunchLab
}
function DeleteLab
{
CheckListOfLabs
SelectLab
lab=$choice
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"
PauseDisplayScreen1
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"
else
echo -e "\nFailed to delete the lab environment"
fi
PauseDisplayScreen1
}
function LabShell
{
CheckListOfLabs
SelectLab
lab=$choice
SetUserCredentialsFor $lab
openstack
DisplayScreen1
}
DisplayScreen1