Fold destroy, pristine and virshall into 'command'. Add a lock to avoid destroying active/inuse LABs

This commit is contained in:
Pim van Pelt
2023-05-06 22:03:42 +00:00
parent 9ead69a5f6
commit 8109ef4b1c
6 changed files with 55 additions and 67 deletions

49
command Executable file
View File

@ -0,0 +1,49 @@
#!/bin/sh
BUILD=${BUILD:=default}
LAB=${LAB:=0}
[ -r .lock${LAB} ] && {
echo "Lock file for LAB${LAB}, bailing"
exit
}
## Do not touch below this line
LABDIR=/var/lab
STAGING=$LABDIR/staging
HVN="hvn${LAB}.lab.ipng.ch"
CMD=$1
[ "$CMD" != "start" -a "$CMD" != "shutdown" -a "$CMD" != "destroy" -a "$CMD" != "pristine" ] && {
echo "Usage: LAB=0 ./command start|shutdown|destroy|pristine"
exit
}
VMS=$(ls -1 build/${BUILD}/${HVN}/)
for VM in $VMS; do
echo "[$VM] Executing $CMD"
case $CMD in
pristine)
echo "[$VM] Destroying VM"
ssh root@$HVN "/usr/bin/virsh list --all | grep $VM.*running >/dev/null && virsh destroy $VM"
echo "[$VM] Rolling VM back to pristine snapshot"
ssh root@$HVN "zfs rollback ssd-vol0/${VM}@pristine"
;;
start|shutdown)
echo "[$VM] Sending $CMD to VM"
ssh root@$HVN "virsh $CMD $VM"
;;
destroy)
echo "[$VM] Destroying VM"
ssh root@$HVN "/usr/bin/virsh list --all | grep $VM.*running >/dev/null && virsh destroy $VM"
echo "[$VM] Destroying ZFS image"
ssh root@$HVN "zfs destroy -r ssd-vol0/$VM"
;;
esac
done
[ "$CMD" = "start" ] && {
echo "* Committing OVS config"
scp overlays/$BUILD/ovs-config.sh root@$HVN:$LABDIR
ssh root@$HVN "set -x; LAB=${LAB} $LABDIR/ovs-config.sh"
}