forked from boundary/tsi-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm
executable file
·458 lines (383 loc) · 10.5 KB
/
vm
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
#!/usr/bin/env bash
# If LOG_FILE variable is not set then set to default
LOG_FILE="${LOG_FILE:="$PWD/vm.log"}"
# Global variables
PID=""
RUNNING=""
AbsolutePath() {
echo "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
}
AbsoluteDir() {
echo $(dirname AbsolutePath)
}
VirtualMachineStatus() {
vm_status=$(vagrant status)
case ${vm_status} in
*"aborted"*) echo "ABORTED" ;;
*"not created"*) echo "NOT_CREATED" ;;
*"running"*) echo "RUNNING" ;;
*"poweroff"*) echo "POWERED_OFF";;
esac
}
VirtualMachineRunning() {
typeset -i rc
vm_status=$(VirtualMachineStatus)
if [ "$vm_status" == "RUNNING" ]
then
rc=1
else
rc=0
fi
return ${rc}
}
DisplayCommandMenu() {
# Preserve the current state of the COLUMNS
# variable and set to 1 so that our menu shows
# all options vertically
save_columns=$COLUMNS
COLUMNS=1
select option in "Create" "Help" "Login" "Remove" "Restart" "Start" "Status" "Stop" "Update" "Version" "Quit"
do
case $REPLY in
1) command="create" ; break ;;
2) command="help" ; break ;;
3) command="login" ; break ;;
4) command="remove" ; break ;;
5) command="restart" ; break ;;
6) command="start" ; break ;;
7) command="start" ; break ;;
8) command="stop" ; break ;;
8) command="update" ; break ;;
8) command="version" ; break ;;
9) command="quit" ; break ;;
*) ;;
esac
done
# Restore the COLUMNS
COLUMNS=${save_columns}
# return the selected command
echo ${command}
}
AlreadyProvisioned() {
# Check to see if running
:
# Okay not running, check to see if provisioned
}
spinner()
{
local pid=$1
local delay=0.75
local spinstr='|/-\'
while [ "$(ps a | awk '{print $1}' | grep ${pid})" ]; do
local temp=${spinstr#?}
printf " [%c] " "$spinstr"
local spinstr=${temp}${spinstr%"$temp"}
sleep ${delay}
printf "\b\b\b\b\b\b"
done
printf " \b\b\b\b"
}
StopAndDestroy() {
if [ ${RUNNING} -eq 1 ]
then
(kill -9 ${PID} 2>&1 >> "${LOG_FILE}")
(vagrant destroy -f 2>&1 >> "${LOG_FILE}")
fi
echo
}
SigHandler() {
StopAndDestroy
exit 1
}
EnableSignalHandlers() {
RUNNING=0
for signal in "INT QUIT TERM"
do
trap "SigHandler ${signal}" ${signal}
done
}
# Prompt and read a value from the terminal
GetValue() {
typeset -r prompt=$1
have_value=0
while [ ${have_value} -eq 0 ]
do
read -p "$prompt: " VALUE
[ ! -z "${VALUE}" ] && have_value=1
done
echo "${VALUE}"
}
GetEmail() {
TSP_EMAIL=$(GetValue "Enter your E-mail")
}
GetApiToken() {
TSP_API_TOKEN=$(GetValue "Enter your API Token")
}
GetApplicationId() {
TSI_APP_ID=$(GetValue "Enter your Application Id")
}
# Get the required configuration from the user
GetConfiguration() {
GetEmail
GetApiToken
GetApplicationId
}
VerifyConfiguration() {
COLUMNS=1
active=1
while [ ${active} -eq 1 ]
do
clear
echo "Create Virtual Machine:"
select option in "Create Virtual Machine" \
"Change E-mail: \"$TSP_EMAIL\"" \
"Change API Token: \"$TSP_API_TOKEN\"" \
"Change Application Id: \"$TSI_APP_ID\"" \
"Quit"
do
case $REPLY in
1) active=0 ; break ;;
2) GetEmail ; break ;;
3) GetApiToken ; break ;;
4) GetApplicationId ; break ;;
5) Quit ;;
esac
done
done
}
ExportConfiguration() {
export TSP_EMAIL
export TSP_API_TOKEN
export TSP_API_HOST="api.truesight-staging.bmc.com"
export TSI_APP_ID
}
ShowConfiguration() {
local prefix=$1
echo "$prefix E-mail: \"$TSP_EMAIL\""
echo "$prefix API Token: \"$TSP_API_TOKEN\""
echo "$prefix Application Id: \"$TSI_APP_ID\""
}
CreateVirtualMachine() {
# Clear the screen to remove clutter
clear
echo "Using the following configuration:"
ShowConfiguration " "
echo -n "to create virtual machine"
echo "Virtual machine provisioning began at $(date)" >> "${LOG_FILE}"
ShowConfiguration >> "${LOG_FILE}"
# Set flag indicating the provisioning background
# process is running
RUNNING=1
# Run the virtual machine provisioning as background process
(vagrant up 2>&1 >> "${LOG_FILE}") &
# Save a copy of the process id for later use
PID=$!
# Provide visual feedback that the provisioning process is running
spinner ${PID}
echo "Virtual machine provisioning ended at $(date)" >> "${LOG_FILE}"
# Complete line from spinner
echo
# Get the background process exit code
wait ${PID}
vagrant status | grep "running" 2>&1 > /dev/null
result=$?
if [ ${result} -eq 0 ]
then
echo "Virtual machine successfully created"
echo "Run './vm-login' to login"
else
echo "Virtual machine provisioning failed"
echo "See ${LOG_FILE} for details"
fi
}
BuildVirtualMachine() {
# Enable signal handlers in case script is interrupted during creation
EnableSignalHandlers
# Prompt the user for required configuration
GetConfiguration
# Allow the user to view configuration and change if necessary
VerifyConfiguration
# Export the configuration through environment variables so they can be passed to vagrant
ExportConfiguration
# Call vagrant to create and provision the virtual machine
CreateVirtualMachine
}
Login() {
case $(VirtualMachineStatus) in
NOT_CREATED) echo "Virtual machine not created, run 'vm-create'" ;;
POWERED_OFF|ABORTED) echo "Virtual machine not running, run 'vm-start' first" ;;
RUNNING) exec vagrant ssh ;;
*) echo "Error logging into virtual machine run 'vm-restart' " ;;
esac
}
Create() {
case $(VirtualMachineStatus) in
NOT_CREATED) BuildVirtualMachine ;;
RUNNING) echo "Virtual machine is running, run 'vm-login' to access" ;;
ABORTED|POWERED_OFF) echo "Virtual machine already created, run 'vm-start'" ;;
esac
}
Remove() {
case $(VirtualMachineStatus) in
NOT_CREATED) echo "Virtual machine is not created" ;;
ABORTED|POWERED_OFF|RUNNING) vagrant destroy -f 2>&1 > /dev/null ;;
esac
}
Restart() {
Stop
Start
}
Start() {
case $(VirtualMachineStatus) in
NOT_CREATED) echo "Virtual machine not created, run 'vm-create'" ;;
RUNNING) echo "Virtual machine is already running, run 'vm-login' to access'" ;;
ABORTED|POWERED_OFF) vagrant up 2>&1 > /dev/null ;;
esac
}
Status() {
case $(VirtualMachineStatus) in
RUNNING) echo "Virtual machine is running" ;;
NOT_CREATED) echo "Virtual machine is not created" ;;
ABORTED|POWERED_OFF) echo "Virtual machine is stopped" ;;
esac
}
Stop() {
case $(VirtualMachineStatus) in
RUNNING) vagrant halt 2>&1 > /dev/null || echo "Error stopping virtual machine" ;;
ABORTED| NOT_CREATED|POWERED_OFF) echo "Virtual machine not running" ;;
esac
}
Update() {
case $(VirtualMachineStatus) in
RUNNING) echo "Virtual machine running" ;;
NOT_CREATED) echo "Virtual Machine not created. Run './vm-create' first" ; exit 0 ;;
ABORTED|POWERED_OFF) echo "Virtual machine not running starting" ; vagrant up ;;
esac
echo "Saving any changes"
git stash
echo "Updating virtual machine contents"
git pull origin master
echo "Applying changes to virtual machine"
vagrant provision
}
Version() {
dir=$(AbsoluteDir)
cat "$dir/docs/index.md" | grep version | tr -d '__' | tr -d [a-z] | tr -d ' '
}
Quit() {
exit 0
}
BadCommand() {
echo "${1} is not a valid command"
Help
exit 1
}
HelpCreate() {
echo "Create"
echo "======"
echo "Builds a virtual machine"
}
HelpHelp() {
echo "Help"
echo "===="
echo " help - Displays this help"
}
HelpLogin() {
echo "Login"
echo "====="
echo "Opens a command line to the virtual machine"
}
HelpRemove() {
echo "Remove"
echo "======"
echo "Deletes the virtual machine"
}
HelpRestart() {
echo "Restart"
echo "======="
echo "Starts and then stops the virtual machine"
}
HelpStart() {
echo "Start"
echo "====="
echo "Turns off the virtual machine"
}
HelpStatus() {
echo "Status"
echo "======"
echo "Current status of the virtual machine"
}
HelpStop() {
echo "Stop"
echo "===="
echo "Turns on the virtual machine"
}
HelpUpdate() {
echo "Update"
echo "======"
echo "Updates the virtual machine with latest content"
}
HelpVersion() {
echo "Version"
echo "======="
echo "Displays the current version of the virtual machine"
}
Help() {
topic="${1}"
if [ -z "${topic}" ]
then
echo "Virtual Machine Management Tool"
echo ""
echo "Runs the following commands:"
echo
echo " create - Builds a virtual machine"
echo " help - Displays this help"
echo " login - Opens a command line to the virtual machine"
echo " remove - Deletes the virtual machine"
echo " restart - Starts and then stops the virtual machine"
echo " start - Turns off the virtual machine"
echo " status - Current status of the virtual machine"
echo " stop - Turns on the virtual machine"
echo " update - Updates the virtual machine with latest content"
echo " version - Displays the current version of the virtual machine"
echo ""
else
case ${topic} in
create) HelpCreate ;;
login) HelpLogin ;;
remove) HelpRemove ;;
restart) HelpRestart ;;
start) HelpStart ;;
start) HelpStatus ;;
stop) HelpStop ;;
update) HelpUpdate ;;
version) HelpVersion ;;
esac
fi
}
ExecuteCommand() {
case $1 in
create) Create ;;
help) Help "${2}" ;;
login) Login ;;
remove) Remove ;;
restart) Restart ;;
start) Start ;;
status) Status ;;
stop) Stop ;;
update) Update ;;
version) Version ;;
quit) Quit ;;
*) BadCommand "${1}" ;;
esac
}
# Assign the command passed on the command line if any
command=$1
# If not passed command then display a menu to the user and
# let them select a command from the menu
if [ -z ${command} ]
then
echo "Virtual Machine Management Tool"
command=$(DisplayCommandMenu)
fi
ExecuteCommand ${command} ${2}