vm.zsh: Add support for operating on multiple machines in parallel

Signed-off-by: Anna Schumaker <Anna.Schumaker@Netapp.com>
This commit is contained in:
Anna Schumaker 2021-12-02 10:23:44 -05:00
parent 04927311ef
commit 0ca7ac8d86
2 changed files with 20 additions and 12 deletions

View File

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

30
vm.zsh
View File

@ -2,8 +2,8 @@
autoload colors
colors
if [ "$#" -ne 2 ]; then
echo "Usage: $0 {boot,halt,reboot,shutdown,start,stop} {libvirt VM}"
if [ "$#" -lt 2 ]; then
echo "Usage: $0 {boot,halt,reboot,shutdown,start,stop} {libvirt VM, ...}"
exit 1
fi
@ -22,25 +22,33 @@ function vm_shutdown() {
while nc -z -w 60 $1 22 2>/dev/null; do
sleep 1
done
while virsh list --name | grep $1 >/dev/null; do
while virsh list --name | grep ^$1$ >/dev/null; do
sleep 1
done
fi
}
machines=$(virsh list --all --name)
if [[ ${machines[(ie)$2]} -le ${#machines} ]]; then
function vm_reboot() {
vm_shutdown $1
vm_boot $1
}
for vm in "${@:2}"; do
case $1 in
"boot" | "start") vm_boot $2 ;;
"shutdown" | "stop" | "halt") vm_shutdown $2 ;;
"boot" | "start")
vm_boot $vm &
;;
"shutdown" | "stop" | "halt")
vm_shutdown $vm &
;;
"reboot")
vm_shutdown $2
sleep 1
vm_boot $2
vm_reboot $vm &
;;
*)
echo "Boot, reboot, or shudown?"
exit 1
;;
esac
fi
done
wait $(jobs -pr)