From 8109ef4b1cd44ab8e775eceb24938dbaf8d632f0 Mon Sep 17 00:00:00 2001
From: Pim van Pelt <pim@ipng.nl>
Date: Sat, 6 May 2023 22:03:42 +0000
Subject: [PATCH] Fold destroy, pristine and virshall into 'command'. Add a
 lock to avoid destroying active/inuse LABs

---
 .gitignore |  1 +
 command    | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 create     |  5 +++++
 destroy    | 26 --------------------------
 pristine   | 14 --------------
 virshall   | 27 ---------------------------
 6 files changed, 55 insertions(+), 67 deletions(-)
 create mode 100755 command
 delete mode 100755 destroy
 delete mode 100755 pristine
 delete mode 100755 virshall

diff --git a/.gitignore b/.gitignore
index 07410ae..9f50c0c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 *.json
 __pycache__/
 .peeringdb/
+.lock*
diff --git a/command b/command
new file mode 100755
index 0000000..5645182
--- /dev/null
+++ b/command
@@ -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"
+}
diff --git a/create b/create
index 89d063b..4a3006d 100755
--- a/create
+++ b/create
@@ -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
diff --git a/destroy b/destroy
deleted file mode 100755
index ce0c8de..0000000
--- a/destroy
+++ /dev/null
@@ -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
diff --git a/pristine b/pristine
deleted file mode 100755
index f1ea8e0..0000000
--- a/pristine
+++ /dev/null
@@ -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
diff --git a/virshall b/virshall
deleted file mode 100755
index be78ca7..0000000
--- a/virshall
+++ /dev/null
@@ -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"
-}