
Currently this leaves a group of baremetalbrbm_$n VMs defined in my environment, while deleting the baremetal_$n ones. The "regex" is really just a prefix for the expected naming convention, so instead just look for the expected prefix, then all baremetal* nodes are cleaned up. Change-Id: Id895ebc7334545b847939c67d818f9eaedc8313e
107 lines
3.7 KiB
Bash
Executable File
107 lines
3.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# Copyright 2013 Red Hat
|
|
# All Rights Reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
# not use this file except in compliance with the License. You may obtain
|
|
# a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
# License for the specific language governing permissions and limitations
|
|
# under the License.
|
|
|
|
SCRIPT_NAME=$(basename $0)
|
|
|
|
LIBVIRT_VOL_POOL=${LIBVIRT_VOL_POOL:-"default"}
|
|
|
|
function show_options {
|
|
echo "Usage: $SCRIPT_NAME [-n NUM]"
|
|
echo
|
|
echo "Cleanup vm state left behind by previous runs"
|
|
echo
|
|
echo " -b -- Baremetal bridge name(s)."
|
|
echo " The create-nodes script names nodes and"
|
|
echo " volumes based on the attached"
|
|
echo " bridge name(s). This parameter provides"
|
|
echo " a way to cleanup nodes attached to the"
|
|
echo " associated bridge name(s). NOTE: when"
|
|
echo " cleaning up environments with multiple"
|
|
echo " bridges all bridge names must be"
|
|
echo " specified."
|
|
echo " -n -- Test environment number to clean up."
|
|
echo " -a -- Clean up all environments."
|
|
echo " Will delete all libvirt defined domains"
|
|
echo " that start with baremetal* and seed*"
|
|
echo " and their storage"
|
|
echo
|
|
echo "If provided, NUM is the environment number to be cleaned up."
|
|
echo "If not provided, the default environment will be cleaned."
|
|
echo ""
|
|
echo "If both baremetal bridge names and NUM (-n) are provided the NUM"
|
|
echo "is appended to the bridge names when searching for VMs to delete."
|
|
exit 1
|
|
}
|
|
|
|
NUM=
|
|
BRIDGE_NAMES=brbm
|
|
CLEANUP_ALL=
|
|
|
|
TEMP=$(getopt -o h,b:,n:,a -n $SCRIPT_NAME -- "$@")
|
|
if [ $? != 0 ]; then
|
|
show_options;
|
|
fi
|
|
|
|
# Note the quotes around `$TEMP': they are essential!
|
|
eval set -- "$TEMP"
|
|
|
|
while true ; do
|
|
case "$1" in
|
|
-h) show_options ;;
|
|
-b) BRIDGE_NAMES="$2" ; shift 2 ;;
|
|
-n) NUM="$2" ; shift 2 ;;
|
|
-a) CLEANUP_ALL=1 ; shift ;;
|
|
--) shift ; break ;;
|
|
*) echo "Error: unsupported option $1." ; show_options ;;
|
|
esac
|
|
done
|
|
|
|
SEED_NAME=seed
|
|
BAREMETAL_PREFIX="baremetal"
|
|
NUMBERED_BRIDGE_NAMES=
|
|
|
|
if [ -n "$NUM" ]; then
|
|
SEED_NAME="seed_${NUM}"
|
|
fi
|
|
for NAME in $BRIDGE_NAMES; do
|
|
NUMBERED_BRIDGE_NAMES="$NUMBERED_BRIDGE_NAMES$NAME${NUM}_"
|
|
done
|
|
# remove the last underscore
|
|
NUMBERED_BRIDGE_NAMES=${NUMBERED_BRIDGE_NAMES%_}
|
|
|
|
if [ -z "$CLEANUP_ALL" ]; then
|
|
BAREMETAL_PREFIX="baremetal${NUMBERED_BRIDGE_NAMES}"
|
|
fi
|
|
|
|
for NAME in $(sudo virsh list --name | grep "^\($SEED_NAME\|${BAREMETAL_PREFIX}\)"); do
|
|
sudo virsh destroy $NAME
|
|
done
|
|
|
|
for NAME in $(sudo virsh list --name --all | grep "^\($SEED_NAME\|${BAREMETAL_PREFIX}\)"); do
|
|
if [ $NAME == $SEED_NAME ]; then
|
|
# handle seeds differently since their storage is not managed by libvirt
|
|
sudo virsh undefine --managed-save $NAME
|
|
sudo rm /var/lib/libvirt/images/$NAME.qcow2
|
|
else
|
|
sudo virsh undefine --managed-save --remove-all-storage $NAME
|
|
fi
|
|
done
|
|
|
|
for NAME in $(sudo virsh vol-list $LIBVIRT_VOL_POOL 2>/dev/null | grep /var/ | awk '{print $1}' | grep "^\($SEED_NAME\|${BAREMETAL_PREFIX}\)"); do
|
|
sudo virsh vol-delete --pool $LIBVIRT_VOL_POOL $NAME
|
|
done
|