From cc6f0a4eeac7f1239cbdbe359d3e4d11e33d6def Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 7 Dec 2021 11:32:07 -0500 Subject: [PATCH] vm.zsh: Various changes - Change timeouts to 5 seconds - Add a "restart" command - Kill any background jobs on exit Signed-off-by: Anna Schumaker --- completions/_vm.zsh | 2 +- vm.zsh | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/completions/_vm.zsh b/completions/_vm.zsh index 9f6a5c7..168f027 100644 --- a/completions/_vm.zsh +++ b/completions/_vm.zsh @@ -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))' } diff --git a/vm.zsh b/vm.zsh index a374ca5..947bc6c 100755 --- a/vm.zsh +++ b/vm.zsh @@ -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 & ;; *)