vm.zsh: Various changes

- Change timeouts to 5 seconds
- Add a "restart" command
- Kill any background jobs on exit

Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
This commit is contained in:
Anna Schumaker 2021-12-07 11:32:07 -05:00
parent 36e6838dd3
commit cc6f0a4eea
2 changed files with 16 additions and 8 deletions

View File

@ -2,6 +2,6 @@
function _vm.zsh() {
_arguments \
'1:operation:(boot halt reboot shutdown start stop)' \
'1:operation:(boot halt reboot restart shutdown start stop)' \
'*:virtual machine:($(virsh list --all --name))'
}

22
vm.zsh
View File

@ -3,15 +3,15 @@ autoload colors
colors
if [ "$#" -lt 2 ]; then
echo "Usage: $0 {boot,halt,reboot,shutdown,start,stop} {libvirt VM, ...}"
echo "Usage: $0 {boot,halt,reboot,restart,shutdown,start,stop} {libvirt VM, ...}"
exit 1
fi
function vm_boot() {
if virsh start $1 1>/dev/null 2>/dev/null; then
echo "$fg_bold[green]Starting VM: $fg_bold[white]$1$reset_color"
until nc -z -w 60 $1 22 2>/dev/null; do
sleep 1
until nc -z -w 5 $1 22 2>/dev/null; do
sleep 5
done
fi
}
@ -19,11 +19,11 @@ function vm_boot() {
function vm_shutdown() {
if virsh shutdown $1 1>/dev/null 2>/dev/null; then
echo "$fg_bold[red]Stopping VM: $fg_bold[white]$1$reset_color"
while nc -z -w 60 $1 22 2>/dev/null; do
sleep 1
while nc -z -w 5 $1 22 2>/dev/null; do
sleep 5
done
while virsh list --name | grep ^$1$ >/dev/null; do
sleep 1
sleep 5
done
fi
}
@ -33,6 +33,14 @@ function vm_reboot() {
vm_boot $1
}
TRAPEXIT() {
if [[ ! -z "$(jobs -pr)" ]]; then
for job in ($(jobs -pr)); do
kill -9 $job
done
fi
}
for vm in "${@:2}"; do
case $1 in
"boot" | "start")
@ -41,7 +49,7 @@ for vm in "${@:2}"; do
"shutdown" | "stop" | "halt")
vm_shutdown $vm &
;;
"reboot")
"reboot"|"restart")
vm_reboot $vm &
;;
*)