Files
training-labs/labs/osbash/tools/restore-cluster.sh
Roger Luethi 3ddb8081f0 Make restore-cluster.sh use VBM_EXE instead of vboxmanage
On OS X, "vboxmanage" works only if the file system is not
case-sensitive, because the file is called VBoxManage (on Linux,
VirtualBox installs an additional link named vboxmanage).

Change-Id: I3d72b2736bc38d17127627fd1918fc19eb239f58
2016-01-01 09:16:40 +01:00

173 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
set -o errexit -o nounset
TOP_DIR=$(cd "$(dirname "$0")/.." && pwd)
source "$TOP_DIR/config/paths"
source "$CONFIG_DIR/deploy.osbash"
source "$CONFIG_DIR/provider.$PROVIDER"
source "$OSBASH_LIB_DIR/functions-host.sh"
source "$OSBASH_LIB_DIR/$PROVIDER-functions.sh"
OSBASH=exec_cmd
VM_LIST="controller network compute"
function usage {
# Setting to empty string selects latest (current snapshot)
echo "Usage: $0 {-l|-c|-t <SNAP>} [-s]"
echo ""
echo "-h Help"
echo "-l List snapshots for node VMs"
echo "-c Restore cluster node VMs to current snapshot"
echo "-t SNAP Restore cluster to target snapshot"
echo "-s Start each node VMs after restoring it"
exit
}
function list_snapshots {
for vm_name in $VM_LIST; do
if ! vm_exists "$vm_name"; then
echo "VM $vm_name does not exist. Skipping..."
continue
fi
echo -e "Snapshot list for $vm_name node:"
"$VBM_EXE" snapshot "$vm_name" list
echo
done
exit 0
}
while getopts :chlst: opt; do
case $opt in
c)
CURRENT=yes
;;
h)
usage
;;
l)
list_snapshots
;;
s)
START=yes
;;
t)
TARGET_SNAPSHOT=$OPTARG
;;
:)
echo "Error: -$OPTARG needs argument"
;;
?)
echo "Error: invalid option -$OPTARG"
echo
usage
;;
esac
done
# Remove processed options from arguments
shift $(( OPTIND - 1 ));
if [ $# -ne 0 ]; then
usage
elif [ -z "${TARGET_SNAPSHOT:-}" -a -z "${CURRENT:-""}" ]; then
echo
echo "Error: no target snapshot given."
echo
usage
elif [ -n "${TARGET_SNAPSHOT:-}" -a -n "${CURRENT:-""}" ]; then
echo
echo "Error: conflicting options: target snapshot name and -c."
echo
usage
fi
if [ "$PROVIDER" != virtualbox ]; then
if [ -n "$TARGET_SNAPSHOT" -o -n "$CURRENT" ]; then
echo >&2 "ERROR: snapshots currently not supported with $PROVIDER."
exit 1
fi
fi
# Find target_snapshot in scripts_cfg and set global *_SNAPSHOT variables
# to the correct snapshot name for each node (to allow building from there)
function set_snapshot_vars {
local target_snapshot=$1
local found=0
local scripts_cfg="$TOP_DIR/config/scripts.ubuntu_cluster"
while read -r line; do
if [[ $line =~ ^cmd\ snapshot.*-n\ ([^ ]*)\ (.*) ]]; then
# Node name (e.g. controller)
node=${BASH_REMATCH[1]}
# Snapshot name (e.g. keystone_installed)
snapshot=${BASH_REMATCH[2]}
# Global variable name (e.g. CONTROLLER_SNAPSHOT)
# Can't use ${node,,} (OS X bash version is only 3.2)
var_name=$(echo "$node"|tr "a-z" "A-Z")_SNAPSHOT
if [ "$snapshot" = "$target_snapshot" ]; then
# Can't use associative arrays (OS X bash version is only 3.2)
eval "${var_name}=$snapshot"
found=1
elif [ $found -eq 0 ]; then
eval "${var_name}=$snapshot"
fi
fi
done < "$scripts_cfg"
if [ $found -eq 0 ]; then
echo "ERROR: snapshot '$target_snapshot' not found"
exit 1
fi
}
function restore_current_snapshot {
local vm_name=$1
"$VBM_EXE" snapshot "$vm_name" restorecurrent
}
function restore_named_snapshot {
local vm_name=$1
local snapshot=$2
"$VBM_EXE" snapshot "$vm_name" restore "$snapshot"
}
if [ -n "${TARGET_SNAPSHOT:-}" ]; then
set_snapshot_vars "$TARGET_SNAPSHOT"
fi
for vm_name in $VM_LIST; do
if ! vm_exists "$vm_name"; then
echo "VM $vm_name does not exist. Skipping..."
continue
fi
vm_power_off "$vm_name"
vm_wait_for_shutdown "$vm_name"
if [ "${CURRENT:-""}" = "yes" ]; then
restore_current_snapshot "$vm_name"
if [ "${START:-""}" = "yes" ]; then
vm_boot "$vm_name"
fi
else
# Global variable name (e.g. CONTROLLER_SNAPSHOT)
# (use tr due to OS X bash limitation)
var_name=$(echo "$vm_name"|tr "a-z" "A-Z")_SNAPSHOT
if [ -z "${!var_name:=""}" ]; then
vm_delete "$vm_name"
else
restore_named_snapshot "$vm_name" "${!var_name}"
if [ "${START:-""}" = "yes" ]; then
vm_boot "$vm_name"
fi
fi
fi
done