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

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.json
__pycache__/
.peeringdb/
.lock*

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"
}

5
create
View File

@ -19,6 +19,11 @@ BASE=${BASE:=ssd-vol0/hvn0.chbtl0.ipng.ch/ssd-vol0/vpp-proto-disk0@20230403-rele
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

26
destroy
View File

@ -1,26 +0,0 @@
#!/bin/sh
## Using SSH:
## * Log in to hypervisor, retrieve list of running VMs
## * Log in to hypervisor, retrieve list of ZFS volumes
## * List any VM matching spec ^vpp[\d]+-[\d+]$
## * List any ZFS datasets matching matching spec ^ssd-vol0/vpp[\d]+-[\d+]$
## * If --force is set:
## * Destroy matching VMs
## * Recursively delete any matching ZFS datasets
BASE=${BASE:=ssd-vol0/hvn0.chbtl0.ipng.ch/ssd-vol0/vpp-proto-disk0@20221018-release}
BUILD=${BUILD:=default}
LAB=${LAB:=0}
## Do not touch below this line
HVN="hvn${LAB}.lab.ipng.ch"
VMS=$(ls -1 build/${BUILD}/${HVN}/)
for VM in $VMS; do
echo "[$VM] Destroying VM"
ssh root@$HVN "/usr/bin/virsh list --all | grep $VM.*running >/dev/null && virsh destroy $VM"
echo "[$VM] Destroying ZFS datasets"
ssh root@$HVN "zfs destroy -r ssd-vol0/$VM"
done

View File

@ -1,14 +0,0 @@
#!/bin/sh
BUILD=${BUILD:=default}
LAB=${LAB:=0}
## Do not touch below this line
HVN="hvn${LAB}.lab.ipng.ch"
VMS=$(ls -1 build/${BUILD}/${HVN}/)
for VM in $VMS; do
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"
done

View File

@ -1,27 +0,0 @@
#!/bin/sh
BUILD=${BUILD:=default}
LAB=${LAB:=0}
## 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" ] && {
echo "Usage: LAB=0 ./virshall start|shutdown|destroy"
exit
}
VMS=$(ls -1 build/${BUILD}/${HVN}/)
for VM in $VMS; do
echo "[$VM] Executing $CMD against VMs on lab $LAB"
ssh root@$HVN "virsh $CMD $VM"
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"
}