summaryrefslogtreecommitdiff
path: root/remove-vm
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2017-10-27 16:04:54 +0300
committerLars Wirzenius <liw@liw.fi>2017-10-27 16:04:54 +0300
commit8787cfc30714766e091652ee00c14580a316d4ce (patch)
treec746aceba6be4f905ad9f9e17187cb136fa7ffe8 /remove-vm
parent78bf9ce90827407510fdedd33ab7d3e8a8338380 (diff)
downloadansibleness-8787cfc30714766e091652ee00c14580a316d4ce.tar.gz
Refactor: rewrite to allow any number of VMs to remove
Diffstat (limited to 'remove-vm')
-rwxr-xr-xremove-vm60
1 files changed, 39 insertions, 21 deletions
diff --git a/remove-vm b/remove-vm
index 363f2a0..2ac9e53 100755
--- a/remove-vm
+++ b/remove-vm
@@ -4,28 +4,46 @@
set -eu
-# Command line parameters.
-name="$1"
+run_virsh()
+{
+ virsh -c qemu:///system "$@"
+}
+
+vm_exists()
+{
+ run_virsh list --all | grep -F "$name" > /dev/null
+}
+
+remove_vm()
+{
+ local name="$1"
+
+ # Shut down and remove the VM, if it exists.
+ if vm_exists "$name"
+ then
+ run_virsh destroy "$name" || true
+ run_virsh undefine "$name"
+ fi
+
+ # Remove the LV, if it exists.
+ lvpath="/dev/$vg/$name"
+ if [ -e "$lvpath" ]
+ then
+ sudo lvremove --force "$lvpath"
+ fi
+
+ # Remove the host from /etc/hosts, if there.
+ awk -v "name=$name" '$2 != name' /etc/hosts | sudo sponge /etc/hosts
+
+ # Done.
+ echo "Virtual machine $name is gone."
+}
+
# Read config variables.
. "$HOME/.config/ansibleness/vm.conf"
-# Shut down and remove the VM, if it exists.
-if virsh -c qemu:///system list --all | grep -F "$name" > /dev/null
-then
- virsh -c qemu:///system destroy "$name" || true
- virsh -c qemu:///system undefine "$name"
-fi
-
-# Remove the LV, if it exists.
-lvpath="/dev/$vg/$name"
-if [ -e "$lvpath" ]
-then
- sudo lvremove --force "$lvpath"
-fi
-
-# Remove the host from /etc/hosts, if there.
-awk -v "name=$name" '$2 != name' /etc/hosts | sudo sponge /etc/hosts
-
-# Done.
-echo "Virtual machine $name is gone."
+for name in "$@"
+do
+ remove_vm "$name"
+done