#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2019, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# 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.                                             #
#--------------------------------------------------------------------------- #

# cpds host:remote_system_ds/disk.i fe:SOURCE snapid vmid dsid
#   - fe is the front-end hostname
#   - SOURCE is the path of the disk image in the form DS_BASE_PATH/disk
#   - host is the target host to deploy the VM
#   - remote_system_ds is the path for the system datastore in the host
#   - snapid is the snapshot id. "-1" for none
#   - dsid is the target datastore

SRC=$1
DST=$2
SNAP_ID=$3
VMID=$4
DSID=$5

if [ -z "${ONE_LOCATION}" ]; then
    TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
else
    TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
fi

DRIVER_PATH=$(dirname $0)

. $TMCOMMON
. ${DRIVER_PATH}/../../etc/vmm/kvm/kvmrc

#-------------------------------------------------------------------------------
# Get Image information
#-------------------------------------------------------------------------------

DISK_ID=$(basename ${SRC} | cut -d. -f2)

XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"

unset i j XPATH_ELEMENTS

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <(onevm show -x $VMID| $XPATH \
                    /VM/DEPLOY_ID \
                    /VM/LCM_STATE \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/TARGET)

DEPLOY_ID="${XPATH_ELEMENTS[j++]}"
LCM_STATE="${XPATH_ELEMENTS[j++]}"
DISK_TARGET="${XPATH_ELEMENTS[j++]}"

#-------------------------------------------------------------------------------
# Set src path
#-------------------------------------------------------------------------------

SRC_PATH=`arg_path $SRC`
SRC_HOST=`arg_host $SRC`
SRC_TEMP_PATH=$(mktemp -u ${SRC_PATH}.XXXXXXXX)

if [ "${SNAP_ID}" != "-1" ]; then
    SRC="${SRC}.snap/${SNAP_ID}"
fi

#-------------------------------------------------------------------------------
# Move the image back to the datastore
#-------------------------------------------------------------------------------
# For current image of the running VMs, don't touch the image directly,
# but export the content via blockcopy. If that's not possible (old QEMU),
# domfsfreeze or suspend the domain before.

if [ "${LCM_STATE}" = '26' ] && [ "${SNAP_ID}" = '-1' ]; then
    log "VM is running, trying blockcopy, fsfreeze, suspend"

    CPDS_CMD_EXPORT=$(cat <<EOF
touch ${SRC_TEMP_PATH}

if ! virsh -c ${LIBVIRT_URI} blockcopy ${DEPLOY_ID} ${DISK_TARGET} ${SRC_TEMP_PATH} --wait --finish; then
    set -e -o pipefail

    if virsh -c ${LIBVIRT_URI} domfsfreeze ${DEPLOY_ID}; then
        trap "virsh -c ${LIBVIRT_URI} domfsthaw ${DEPLOY_ID}" EXIT TERM INT HUP
        $CP $SRC_PATH $SRC_TEMP_PATH
    elif virsh -c ${LIBVIRT_URI} suspend ${DEPLOY_ID}; then
        trap "virsh -c ${LIBVIRT_URI} resume ${DEPLOY_ID}" EXIT TERM INT HUP
        $CP $SRC_PATH $SRC_TEMP_PATH
    else
        echo "Could not domfsfreeze or suspend domain" >&2
        exit 1
    fi
fi
EOF
)

    CPDS_CMD_ERR="Error creating export for domain ${DEPLOY_ID} of disk ${DISK_TARGET} at ${SRC_TEMP_PATH}"

    # Always try delete temp export
    trap "ssh_exec_and_log $SRC_HOST \"$RM ${SRC_TEMP_PATH}\"" EXIT TERM INT HUP

    if ssh_exec_and_log_no_error $SRC_HOST "$CPDS_CMD_EXPORT" "$CPDS_CMD_ERR"; then
        # Export creation succeeded, point SRC there
        SRC="${SRC_HOST}:${SRC_TEMP_PATH}"
    else
        log_error "Exporting failed"
        exit 1
    fi
fi

log "Moving $SRC to datastore as $DST"
exec_and_log "$SCP -r $SRC $DST" "Error copying $SRC to $DST"
hup_collectd $SRC_HOST

exit 0
