/
xeh
executable file
·677 lines (587 loc) · 16.5 KB
/
xeh
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
#!/bin/bash
PROGNAME="xeh"
CONFIG="$HOME/.xeh.conf"
XE_BIN="/usr/bin/xe"
VNC_BIN="/usr/bin/vncviewer"
SSH_BIN="/usr/bin/ssh"
SSHPASS_BIN="/usr/bin/sshpass"
# create "macros" to switch formatting
bold=`tput bold`
normal=`tput sgr0`
# hack so we can kill the whole script from inside a function
trap "exit 1" TERM
export TOP_PID=$$
error(){
echo -e "$PROGNAME: ERROR: $*" >&2
kill -s TERM $TOP_PID
}
warning(){
echo -e "$PROGNAME: WARNING: $*" >&2
}
check_xe(){
if [ ! -f "$XE_BIN" ]; then
if [ "$1" == "warn" ]; then
warning "$XE_BIN is not installed."
else
error "$XE_BIN is not installed."
fi
fi
}
check_vnc(){
if [ ! -f "$VNC_BIN" ]; then
if [ "$1" == "warn" ]; then
warning "$VNC_BIN is not installed."
else
error "$VNC_BIN is not installed."
fi
fi
}
check_ssh(){
if [ ! -f "$SSH_BIN" ]; then
if [ "$1" == "warn" ]; then
warn "$SSH_BIN is not installed."
else
error "$SSH_BIN is not installed."
fi
fi
}
check_sshpass(){
if [ ! -f "$SSHPASS_BIN" ]; then
if [ "$1" == "warn" ]; then
warn "$SSHPASS_BIN is not installed."
else
error "$SSHPASS_BIN is not installed."
fi
fi
}
check_server(){
$xe_get host-list &> /dev/null
if [ "$?" != 0 ]; then
error "Unable to contact server or login into it."
fi
}
print_usage(){
echo -e "$PROGNAME Usage:
$ $PROGNAME [options] {action} [object name] [additional parameters]
Options:
-s, --server='hostname'
Use the specified 'hostname' string as hostname/IP address to login into.
-u, --user='username'
Use the specified 'username' string as username to login into xen server (both XAPI and SSH).
-p, --pasword='pass'
Use the specified 'pass' string as password to login into xen server (both XAPI and SSH).
-r, --ssh-port='port'
Use the specified 'port' number [1-65535] to connect via SSH to xen server (default: 22).
-c, --colour='colour'
Use the specified 'colour' level [0-3] when connecting via VNC (default: 1).
Acceptable values: 0=8 colours, 1=64 colours, 2=256 colours, 3=millions of coulours
-f, --fake
Don't execute any xe commands that may change something. Instead, just print them.
Actions:
search {object-name} [object-type]
Print all objects of type 'object-type' that contains the string 'object-name'.
If 'object-type' is blank, every supported object type that contains the string will be printed.
Acceptable values for object-type: vm snapshot network cd host appliance template
list [object-type]
Print all objects of type 'object-type'.
If 'object-type is blank, every supported object type will be printed.
Acceptable values for object-type: vm snapshot network cd host appliance template
vnc {vm-name}
Open a VNC session connected to the specified VM 'vm-name' VGA output through a SSH tunnel.
console {vm-name}
Open a VT100 terminal connected to the specified VM 'vm-name' serial output.
power {vm-name} [up|down]
Change the VM 'vm-name' power status to either up (running) or down (halted).
memory {vm-name} [value]
Change the static memory value of the VM 'vm-name' to 'value' in MegaBytes.
cpu {vm-name} [value]
Change the static available vCPU cores to the VM 'vm-name' to the positive non-zero integer 'value'.
snapshot {vm-name} {action} {snapshot-name}
Take or restore a snapshot of the halted VM 'vm-name' with name 'snapshot-name'.
Acceptable values for action are: take restore
clone {vm-name} {new-vm-name}
Clone a VM 'vm-name' naming it 'new-vm-name' using storage-level quick copy.
remove {object-name} {object-type} [object-parent]
Remove (uninstall) the specified object 'object-name' of type 'object-type'.
If you specify a snapshot, you must also specify a parent VM that it belongs to.
You will be asked interactively for confirmation.
Accepted values for object-type are: vm snapshot template appliance
Connection:
The config file is $CONFIG. If it doesn't exists, it's obligatory to inform the script the host, user and password. If it does exist, you may inform just the information that is missing from it (eg. you don't need to store the password in the config file, as the script will ask interactively). The config file is a CSV, with the following syntax:
host1,user1,password1,ssh_port1
host2,user2,password2,ssh_port2
If no host is informed in the arguments, it uses the first line as default. If the host is informed, it searches in the config for all information that was not provided in the arguments. If some information is missing from either the arguments or config file, the script will fail, except for the SSH port (the default 22 will be tried).
"
}
get_uuid(){
case "$2" in
snapshot)
if [ -n "$3" ]; then
extra_param="snapshot-of=\"$3\""
fi
if [ -n "$4" ]; then
extra_message="of VM \"$4\""
fi
;;
*) extra_param=""
;;
esac
uuid=`$xe_get $2-list name-label="$1" $extra_param | grep uuid | cut -d ':' -f2 | tr -d ' '`
if [ -z "$uuid" ]; then
error "The $2 \"$1\" $extra_message doesn't exists on server \"$host\"."
fi
echo -e "$uuid"
}
vm_get_domain(){
domain=`$xe_get vm-list uuid="$1" params=dom-id | grep dom-id | cut -d ':' -f2 | tr -d ' '`
if [ -z "$domain" ]; then
error "Unable to identify the VM domain."
fi
echo $domain
}
vm_get_port_vnc(){
port_vnc=`sshpass -p "$password" ssh "$user"@"$host" xenstore-ls /local/domain/$1/console | grep vnc-port | cut -d '"' -f2`
if [ -z "$port_vnc" ]; then
error "Missing console. Either the VM isn't up yet, or this VM wasn't configured to have a VNC console."
fi
echo $port_vnc
}
vm_test_state(){
state=`$xe_get vm-list uuid="$2" params=power-state | grep power-state | cut -d ':' -f2 | tr -d ' '`
if [ "$1" != "$state" ]; then
error "The VM isn't \"$1\"."
fi
}
generate_port_local(){
read lowerPort upperPort < /proc/sys/net/ipv4/ip_local_port_range
port=`shuf -i $lowerPort-$upperPort -n 1`
while true; do
(echo >/dev/tcp/localhost/$port) &>/dev/null && port=`shuf -i $lowerPort-$upperPort -n 1` || break
done
echo -e "$port"
}
search(){
case "$1" in
vm)
out=`$xe_get vm-list | grep -C1 "name-label ( RW): $2"`
if [ "$?" -eq 0 ]; then
echo -e "${bold}---Virtual Machines---${normal}"
echo -e "$out"
echo -e "${bold}---\\Virtual Machines---${normal}\n"
fi
;;
snapshot)
out=`$xe_get snapshot-list | grep -C1 "name-label ( RW): $2"`
if [ "$?" -eq 0 ]; then
echo -e "${bold}---Snapshots---${normal}"
echo -e "$out"
echo -e "${bold}---\\Snapshots---${normal}\n"
fi
;;
network)
out=`$xe_get network-list | grep -C1 "name-label ( RW): $2"`
if [ "$?" -eq 0 ]; then
echo -e "${bold}---Networks---${normal}"
echo -e "$out"
echo -e "${bold}--\\Networks---${normal}\n"
fi
;;
cd)
out=`$xe_get cd-list | grep -C1 "name-label ( RW): $2"`
if [ "$?" -eq 0 ]; then
echo -e "${bold}---CDs---${normal}"
echo -e "$out"
echo -e "${bold}---\\CDs---${normal}\n"
fi
;;
host)
out=`$xe_get host-list | grep -C1 "name-label ( RW): $2"`
if [ "$?" -eq 0 ]; then
echo -e "${bold}---Hosts---${normal}"
echo -e "$out"
echo -e "${bold}---\\Hosts---${normal}\n"
fi
;;
appliance)
out=`$xe_get appliance-list | grep -C1 "name-label ( RW): $2"`
if [ "$?" -eq 0 ]; then
echo -e "${bold}---Appliances---${normal}"
echo -e "$out"
echo -e "${bold}---\\Appliances---${normal}\n"
fi
;;
template)
out=`$xe_get template-list | grep -C1 "name-label ( RW): $2"`
if [ "$?" -eq 0 ]; then
echo -e "${bold}---Templates---${normal}"
echo -e "$out"
echo -e "${bold}---\\Templates---${normal}\n"
fi
;;
*)
error "Object type \"$1\" not recognized."
;;
esac
}
# retrieve args
ARGS=`getopt -o s:u:p:c:hf --long server:,user:,password:,ssh-port:,colour:,help,fake -n "$PROGNAME" -- "$@"`
if [ $? != 0 ] ; then echo -e "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$ARGS"
while true; do
case "$1" in
-s|--server)
if [ -z "$host" ]; then
host="$2"
else
error "You can't set the host twice."
fi
shift 2
;;
-u|--user)
if [ -z "$user" ]; then
user="$2"
else
error "You can't set the user twice."
fi
shift 2
;;
-p|--password)
if [ -z "$password" ]; then
password="$2"
else
error "You can't set the password twice."
fi
shift 2
;;
-r|--ssh-port)
if [ -z "$port_ssh" ]; then
port_ssh="$2"
else
error "You can't set the SSH port twice."
fi
shift 2
;;
-c|--colour)
if [ -z "$vnc_colour" ]; then
case "$2" in
0) vnc_colour="-LowColourLevel 0";;
1) vnc_colour="-LowColourLevel 1";;
2) vnc_colour="-LowColourLevel 2";;
3) vnc_colour="-FullColour";;
esac
else
error "You can't set the VNC colour mode twice."
fi
shift 2
;;
-f|--fake)
export FAKE=1
shift 1
;;
-h|--help)
print_usage
exit 0
;;
--) shift; break;;
*) error "Internal error.";;
esac
done
# retrieve operands
count=0
for arg; do
op[$count]=$arg
count=`expr $count + 1`
done
search_config(){
for linha in `cat $CONFIG`; do
key=`echo $linha | cut -d ',' -f1`
if [ "$key" == "$1" ]; then
info=`echo $linha | cut -d ',' -f$2`
break
fi
done
echo "$info"
}
#parse config
if [ ! -f "$CONFIG" ]; then
warning "The config file $CONFIG does not exist."
if [ -z "$host" ]; then
error "There was no host on command-line and there is no config."
fi
if [ -z "$user" ]; then
error "There was no user on command-line and there is no config."
fi
else
cfg_head=`head -n1 "$CONFIG"`
if [ -z "$host" ]; then
host=`echo "$cfg_head" | cut -d ',' -f1`
if [ -z "$host" ]; then
error "There's no default host on config."
fi
if [ -z "$user" ]; then
user=`echo "$cfg_head" | cut -d ',' -f2`
if [ -z "$user" ]; then
error "There's no default user on config."
fi
if [ -z "$password" ]; then
password=`echo "$cfg_head" | cut -d ',' -f3`
if [ -z "$password" ]; then
error "There's no default password on config."
fi
fi
fi
else
if [ -z "$user" ]; then
user=`search_config "$host" 2`
fi
if [ -z "$password" ]; then
password=`search_config "$host" 3`
fi
if [ -z "$port_ssh" ]; then
port_ssh=`search_config "$host" 4`
fi
fi
fi
#if no password was provided in config nor command line, ask the user interactively
if [ -z "$password" ]; then
read -p "Password for $user@$host: " password
fi
# set defaults
if [ -z "$port_ssh" ]; then
port_ssh=22
fi
if [ -z "$vnc_colour" ]; then
vnc_colour="-LowColourLevel 0"
fi
# give this snippet a nickname
xe_get="eval \$XE_BIN -s \$host -u \$user -pw \$password"
#if we are just testing, we may not want to change anything
if [ $FAKE ]; then
xe_set="eval echo $PROGNAME: FAKE: \$XE_BIN -s \$host -u \$user -pw \$password"
else
xe_set="eval \$XE_BIN -s \$host -u \$user -pw \$password"
fi
case ${op[0]} in
vnc)
check_xe
check_vnc
check_ssh
check_sshpass
check_server
if [ -z "${op[1]}" ]; then
error "VM not specified."
fi
uuid=`get_uuid ${op[1]} vm`
vm_test_state running "$uuid"
domain=`vm_get_domain "$uuid"`
port_vnc=`vm_get_port_vnc "$domain"`
port_local=`generate_port_local`
# create SSH tunnel on random free port
sshpass -p "$password" ssh "$user"@"$host" -p$port_ssh -N -L$port_local:127.0.0.1:$port_vnc &
tunnel_pid=$!
sleep 2 #wait for ssh handshake and tunnel setup
# VNC will block the terminal, and the tunnel will be terminated as soon as the VNC is closed
$VNC_BIN -PreferredEncoding ZRLE $vnc_colour 127.0.0.1:$port_local
kill $tunnel_pid
;;
search)
check_xe
check_server
if [ -z "${op[1]}" ]; then
error "There's no string to search for. Use \"list\" action to list all of them."
fi
if [ -z "${op[2]}" ]; then
warning "No object type specified; searching all types."
for object in vm snapshot network cd host appliance template; do
search $object "${op[1]}"
done
else
search "${op[2]}" "${op[1]}"
fi
;;
list)
check_xe
check_server
if [ -z "${op[1]}" ]; then
warning "No object type specified; listing all types."
for object in vm snapshot network cd host appliance template; do
search $object ""
done
else
search "${op[1]}" ""
fi
;;
console)
check_xe
check_server
if [ -z "${op[1]}" ]; then
error "VM not specified"
fi
uuid=`get_uuid ${op[1]} vm`
vm_test_state running "$uuid"
domain=`vm_get_domain "$uuid"`
$xe_get console uuid="$uuid"
;;
power)
check_xe
check_server
if [ -z "${op[1]}" ]; then
error "VM not specified."
fi
uuid=`get_uuid ${op[1]} vm`
if [ -z "${op[2]}" ]; then
warning "Power action not specified"
value=`$xe_get vm-param-list uuid=$uuid | grep -e 'power-state .*:'`
echo -e "Actual value:\n $value"
exit 0
fi
case "${op[2]}" in
up)
vm_test_state halted "$uuid"
$xe_set vm-start uuid="$uuid"
;;
down)
vm_test_state running "$uuid"
$xe_set vm-shutdown force=true uuid="$uuid"
;;
*)
error "Power action \"${op[2]}\" not recognized."
;;
esac
;;
memory)
check_xe
check_server
if [ -z "${op[1]}" ]; then
error "VM not specified."
fi
uuid=`get_uuid ${op[1]} vm`
if [ -z "${op[2]}" ]; then
warning "Value not specified."
value=`$xe_get vm-param-list uuid=$uuid | grep -e 'memory-\(dynamic\|static\)-\(min\|max\) .*:'`
echo -e "Actual values (in Bytes):\n $value"
exit 0
fi
if ! [ "${op[2]}" -eq "${op[2]}" 2> /dev/null ]; then
error "\"${op[2]}\" is not a valid value (you should specify in MB)."
fi
vm_test_state halted "$uuid"
mem=`expr ${op[2]} '*' 1024 '*' 1024`
$xe_set vm-memory-limits-set uuid="$uuid" static-min=$mem static-max=$mem dynamic-min=$mem dynamic-max=$mem
;;
cpu)
check_xe
check_server
if [ -z "${op[1]}" ]; then
error "VM not specified."
fi
uuid=`get_uuid ${op[1]} vm`
if [ -z "${op[2]}" ]; then
warning "Value not specified."
value=`$xe_get vm-param-list uuid=$uuid | grep -e 'VCPUs-\(max\|at-startup\) .*:'`
echo -e "Actual values: \n $value"
exit 0
fi
if ! [ "${op[2]}" -eq "${op[2]}" -a "${op[2]}" -gt 0 2> /dev/null ]; then
error "\"${op[2]}\" is not a valid value (you should specify in number of vCPUs)."
fi
vm_test_state halted "$uuid"
actual_value=`$xe_get vm-param-get uuid=$uuid param-name=VCPUs-max`
if [ "$?" != 0 ]; then
error "Internal error."
fi
if [ "$actual_value" -lt "${op[2]}" ]; then
$xe_set vm-param-set uuid=$uuid VCPUs-max="${op[2]}" VCPUs-at-startup="${op[2]}"
else
$xe_set vm-param-set uuid=$uuid VCPUs-at-startup="${op[2]}" VCPUs-max="${op[2]}"
fi
;;
snapshot)
check_xe
check_server
if [ -z "${op[1]}" ]; then
error "VM not specified."
fi
if [ -z "${op[2]}" ]; then
error "Snapshot action not specified."
fi
if [ -z "${op[3]}" ]; then
error "Snapshot name not specified."
fi
vm_uuid=`get_uuid ${op[1]} vm`
vm_test_state halted "$vm_uuid"
case "${op[2]}" in
take)
$xe_set vm-snapshot uuid="$vm_uuid" new-name-label="${op[3]}"
;;
restore)
snapshot_uuid=`get_uuid "${op[3]}" snapshot "$vm_uuid" "${op[1]}"`
$xe_set snapshot-revert snapshot-uuid="$snapshot_uuid"
;;
*)
error "Invalid snapshot action."
;;
esac
;;
clone)
check_xe
check_server
if [ -z "${op[1]}" ]; then
error "VM not specified."
fi
if [ -z "${op[2]}" ]; then
error "New VM name not specified."
fi
uuid=`get_uuid ${op[1]} vm`
vm_test_state halted "$uuid"
$xe_set vm-clone uuid="$uuid" new-name-label="${op[2]}"
;;
remove)
check_xe
check_server
if [ -z "${op[1]}" ]; then
error "Object not specified."
fi
if [ -z "${op[2]}" ]; then
error "Type of object not specified."
fi
case "${op[2]}" in
vm)
uuid=`get_uuid "${op[1]}" vm`
vm_test_state halted "$uuid"
$xe_set vm-uninstall force=true uuid="$uuid"
;;
snapshot)
if [ -z "${op[3]}" ]; then
error "You must specify the VM that the Snapshot belongs to."
fi
vm_uuid=`get_uuid "${op[3]}" vm`
snapshot_uuid=`get_uuid "${op[1]}" snapshot "$vm_uuid" "${op[3]}"`
$xe_set snapshot-uninstall force=true uuid="$snapshot_uuid"
;;
template)
uuid=`get_uuid "${op[1]}" template`
$xe_set template-uninstall force=true uuid="$uuid"
;;
appliance)
uuid=`get_uuid "${op[1]}" appliance`
$xe_set appliance uninstall force=true uuid="$uuid"
;;
*)
error "\"${op[2]} is not a valid object type or this script is unable to remove it."
;;
esac
;;
help)
print_usage
exit 0
;;
"")
print_usage
exit 0
;;
*)
error "Action not recognized."
;;
esac
exit 0