#!/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
#   - vm_id is the id of the VM
#   - ds_id is the target datastore (the original datastore for the image)

SRC=$1
DST=$2
SNAP_ID=$3
VM_ID=$4
DS_ID=$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)

source $TMCOMMON
source ${DRIVER_PATH}/../../etc/vmm/kvm/kvmrc
source ${DRIVER_PATH}/../../datastore/libfs.sh
source ${DRIVER_PATH}/../../etc/datastore/linstor_un/linstor_un.conf
source ${DRIVER_PATH}/../../datastore/linstor_un/linstor_utils.sh

#-------------------------------------------------------------------------------
# Set dst path and dir
#-------------------------------------------------------------------------------

SRC_PATH=`arg_path $SRC`
SRC_HOST=`arg_host $SRC`
DST_PATH=`arg_path $DST`

#-------------------------------------------------------------------------------
# Get Datastore information
#-------------------------------------------------------------------------------

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

unset i j XPATH_ELEMENTS

while IFS= read -r -d '' element; do
        XPATH_ELEMENTS[i++]="$element"
done < <(onedatastore show -x $DS_ID | $XPATH \
                    /DATASTORE/TEMPLATE/LS_CONTROLLERS \
                    /DATASTORE/TEMPLATE/LS_CERTFILE \
                    /DATASTORE/TEMPLATE/LS_KEYFILE \
                    /DATASTORE/TEMPLATE/LS_CAFILE \
                    /DATASTORE/TEMPLATE/RESOURCE_GROUP \
                    /DATASTORE/TEMPLATE/NODE_LIST \
                    /DATASTORE/TEMPLATE/LAYER_LIST \
                    /DATASTORE/TEMPLATE/PROVIDERS \
                    /DATASTORE/TEMPLATE/REPLICAS_ON_SAME \
                    /DATASTORE/TEMPLATE/REPLICAS_ON_DIFFERENT \
                    /DATASTORE/TEMPLATE/AUTO_PLACE \
                    /DATASTORE/TEMPLATE/REPLICA_COUNT \
                    /DATASTORE/TEMPLATE/DO_NOT_PLACE_WITH \
                    /DATASTORE/TEMPLATE/DO_NOT_PLACE_WITH_REGEX \
                    /DATASTORE/TEMPLATE/STORAGE_POOL \
                    /DATASTORE/TEMPLATE/DISKLESS_POOL \
                    /DATASTORE/TEMPLATE/ENCRYPTION)

LS_CONTROLLERS="${XPATH_ELEMENTS[j++]}"
LS_CERTFILE="${XPATH_ELEMENTS[j++]}"
LS_KEYFILE="${XPATH_ELEMENTS[j++]}"
LS_CAFILE="${XPATH_ELEMENTS[j++]}"
RESOURCE_GROUP="${XPATH_ELEMENTS[j++]}"
NODE_LIST="${XPATH_ELEMENTS[j++]}"
LAYER_LIST="${XPATH_ELEMENTS[j++]}"
PROVIDERS="${XPATH_ELEMENTS[j++]}"
REPLICAS_ON_SAME="${XPATH_ELEMENTS[j++]}"
REPLICAS_ON_DIFFERENT="${XPATH_ELEMENTS[j++]}"
AUTO_PLACE="${XPATH_ELEMENTS[j++]}"
REPLICA_COUNT="${XPATH_ELEMENTS[j++]:-$AUTO_PLACE}"
DO_NOT_PLACE_WITH="${XPATH_ELEMENTS[j++]}"
DO_NOT_PLACE_WITH_REGEX="${XPATH_ELEMENTS[j++]}"
STORAGE_POOL="${XPATH_ELEMENTS[j++]}"
DISKLESS_POOL="${XPATH_ELEMENTS[j++]:-DfltDisklessStorPool}"
ENCRYPTION="${XPATH_ELEMENTS[j++]}"

linstor_load_keys

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

DISK_ID=$(echo "$SRC_PATH" | $AWK -F. '{print $NF}')
DST_DEV="$DST_PATH"
DST_RES="$(echo "$DST_DEV" | $AWK -F/ '{print $(NF-1)}')"

unset i j XPATH_ELEMENTS

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

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

SRC_RES="$(echo "$SRC_DEV" | $AWK -F/ '{print $(NF-1)}')"

if [ "$CLONE" != "NO" ]; then
    SRC_RES="one-vm-${VM_ID}-disk-${DISK_ID}"
    SRC_DEV="/dev/drbd/by-res/${SRC_RES}/0"
fi

#-------------------------------------------------------------------------------
# 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
    CPDS_CMD_EXPORT=$(cat <<EOF
    if ! virsh -c ${LIBVIRT_URI} blockcopy ${DEPLOY_ID} ${DISK_TARGET} ${DST_DEV} --wait --finish; then
        if virsh -c ${LIBVIRT_URI} domfsfreeze ${DEPLOY_ID}; then
            trap "virsh -c ${LIBVIRT_URI} domfsthaw ${DEPLOY_ID}" EXIT TERM INT HUP
        elif virsh -c ${LIBVIRT_URI} suspend ${DEPLOY_ID}; then
            trap "virsh -c ${LIBVIRT_URI} resume ${DEPLOY_ID}" EXIT TERM INT HUP
        else
            echo "Could not domfsfreeze or suspend domain" >&2
            exit 1
        fi
        $DD if=$SRC_DEV of=$DST_DEV bs=${DD_BLOCK_SIZE:-64k} conv=${DD_CONV:-sparse}
        fi
EOF
)
else
    CPDS_CMD_EXPORT="$DD if=$SRC_DEV of=$DST_DEV bs=${DD_BLOCK_SIZE:-64k} conv=${DD_CONV:-sparse}"
fi

#-------------------------------------------------------------------------------
# Copy Image back to the datastore
#-------------------------------------------------------------------------------

# Load cleanup trap
trap linstor_cleanup_trap EXIT

# Deploy new volume
linstor_exec_and_log \
    "resource-definition create $DST_RES $RD_CREATE_ARGS"
LINSTOR_CLEANUP_RD+=" $DST_RES"
linstor_exec_and_log \
    "volume-definition create $DST_RES ${SIZE}M $VOL_CREATE_ARGS"
if [ -n "$RESOURCE_GROUP" ]; then
    linstor_exec_and_log \
        "resource-definition auto-place $DST_RES $RD_AUTO_PLACE_ARGS"
else
    linstor_exec_and_log \
        "resource create $NODE_LIST $DST_RES $RES_CREATE_ARGS"
fi

# Set properties
linstor_exec_and_log \
    "resource-definition set-property $DST_RES Aux/one/DS_ID"

# Attach diskless resource for DST_RES on SRC_HOST
if linstor_attach_diskless "$SRC_HOST" "$DST_RES" "$DISKLESS_POOL"; then
    LINSTOR_CLEANUP_RES+=" $SRC_HOST:$DST_RES"
fi

if [ "$SNAP_ID" = "-1" ]; then

    # Clone image from SRC_RES to DST_RES
    ssh_exec_and_log \
        "$SRC_HOST" "$CPDS_CMD_EXPORT" \
        "Error cloning $SRC_DEV to $DST_DEV on $SRC_HOST"

    LINSTOR_CLEANUP_RD=
    exit 0
else
    SNAPSHOT="snapshot-${SNAP_ID}"
    TMP_RES="${SRC_RES}-tmp"
    TMP_DEV="/dev/drbd/by-res/${TMP_RES}/0"

    # Deploy snapshot
    linstor_exec_and_log \
        "resource-definition create $TMP_RES $RD_CREATE_ARGS"
    LINSTOR_CLEANUP_RD+=" $TMP_RES"
    linstor_exec_and_log \
        "snapshot volume-definition restore --from-resource $SRC_RES --from-snapshot $SNAPSHOT --to-resource $TMP_RES"
    linstor_exec_and_log \
        "snapshot resource restore --from-resource $SRC_RES --from-snapshot $SNAPSHOT --to-resource $TMP_RES"
    
    # Attach diskless resources for TMP_RES on SRC_HOST
    if linstor_attach_diskless "$SRC_HOST" "$TMP_RES" "$DISKLESS_POOL"; then
        LINSTOR_CLEANUP_RES+=" $SRC_HOST:$TMP_RES"
    fi
    
    # Clone image from TMP_RES to DST_RES
    ssh_exec_and_log "$SRC_HOST" \
        "$DD if=$TMP_DEV of=$DST_DEV bs=${DD_BLOCK_SIZE:-64k} conv=${DD_CONV:-sparse}" \
        "Error cloning $TMP_DEV to $DST_DEV on $SRC_HOST"

    # Leave only TMP_RES in LINSTOR_CLEANUP_RD
    LINSTOR_CLEANUP_RD=$TMP_RES
    exit 0
fi
