#!/usr/bin/env bash

# export_vm - Exports a VM from a PVC cluster to local files
# Part of the Parallel Virtual Cluster (PVC) system
#
#    Copyright (C) 2018-2021 Joshua M. Boniface <joshua@boniface.me>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
###############################################################################

set -o errexit
set -o pipefail

usage() {
    echo -e "Export a VM from a PVC cluster to local files."
    echo -e "Usage:"
    echo -e "  $0 <vm> <source_cluster> [<destination_directory>]"
    echo -e ""
    echo -e "Important information:"
    echo -e " * The local user must have valid SSH access to the primary coordinator in the source_cluster."
    echo -e " * The user on the cluster primary coordinator must have 'sudo' access."
    echo -e " * If the VM is not in 'stop' state, it will be shut down."
    echo -e " * Do not switch the cluster primary coordinator while the script is running."
    echo -e " * Ensure you have enough space in <destination_directory> to store all VM disk images."
}

fail() {
    echo -e "$@"
    exit 1
}

# Arguments
if [[ -z ${1} || -z ${2} ]]; then
    usage
    exit 1
fi
source_vm="${1}"
source_cluster="${2}"
if [[ -n "${3}" ]]; then
    destination_directory="${3}"
else
    destination_directory="."
fi

# Verify the cluster is reachable
pvc -c ${source_cluster} status &>/dev/null || fail "Specified source_cluster is not accessible"

# Determine the connection IP
cluster_address="$( pvc cluster list 2>/dev/null | grep -i "^${source_cluster}" | awk '{ print $2 }' )"

# Attempt to connect to the cluster address
ssh ${cluster_address} which pvc &>/dev/null || fail "Could not SSH to source_cluster primary coordinator host"

# Verify that the VM exists
pvc -c ${source_cluster} vm info ${source_vm} &>/dev/null || fail "Specified VM is not present on the cluster"

echo "Verification complete."

# Shut down the VM
echo -n "Shutting down VM..."
set +o errexit
pvc -c ${source_cluster} vm shutdown ${source_vm} &>/dev/null
shutdown_success=$?
while ! pvc -c ${source_cluster} vm info ${source_vm} 2>/dev/null | grep '^State' | grep -q -E 'stop|disable'; do
    sleep 1
    echo -n "."
done
set -o errexit
echo " done."

# Dump the XML file
echo -n "Exporting VM configuration file... "
pvc -c ${source_cluster} vm dump ${source_vm} 1> ${destination_directory}/${source_vm}.xml 2>/dev/null
echo "done".

# Determine the list of volumes in this VM
volume_list="$( pvc -c ${source_cluster} vm info --long ${source_vm} 2>/dev/null | grep -w 'rbd' | awk '{ print $3 }' )"
for volume in ${volume_list}; do
    volume_pool="$( awk -F '/' '{ print $1 }' <<<"${volume}" )"
    volume_name="$( awk -F '/' '{ print $2 }' <<<"${volume}" )"
    volume_size="$( pvc -c ${source_cluster} storage volume list -p ${volume_pool} ${volume_name} 2>/dev/null | grep "^${volume_name}" | awk '{ print $3 }' )"
    echo -n "Exporting disk ${volume_name} (${volume_size})... "
    ssh ${cluster_address} sudo rbd map ${volume_pool}/${volume_name} &>/dev/null || fail "Failed to map volume ${volume}"
    ssh ${cluster_address} sudo dd if="/dev/rbd/${volume_pool}/${volume_name}" bs=1M 2>/dev/null | dd bs=1M of="${destination_directory}/${volume_name}.img" 2>/dev/null
    ssh ${cluster_address} sudo rbd unmap ${volume_pool}/${volume_name} &>/dev/null || fail "Failed to unmap volume ${volume}"
    echo "done."
done
