#!/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.                                             #
#--------------------------------------------------------------------------- #

DRIVER_PATH=$(dirname $0)

SRC_PATH=$1
DST_HOST=$2
DEPLOY_ID=$3
VM_ID=$4

DRV_ACTION=$(cat)

#-------------------------------------------------------------------------------

source $(dirname $0)/../../scripts_common.sh

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

unset i j XPATH_ELEMENTS

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <(echo "$DRV_ACTION" | base64 -d | $XPATH \
                    /VMM_DRIVER_ACTION_DATA/DATASTORE/TM_MAD \
                    /VMM_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/LS_CONTROLLERS \
                    /VMM_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/LS_CERTFILE \
                    /VMM_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/LS_KEYFILE \
                    /VMM_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/LS_CAFILE \
                    /VMM_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/DISKLESS_POOL)

TM_MAD="${XPATH_ELEMENTS[j++]}"
LS_CONTROLLERS="${XPATH_ELEMENTS[j++]}"
LS_CERTFILE="${XPATH_ELEMENTS[j++]}"
LS_KEYFILE="${XPATH_ELEMENTS[j++]}"
LS_CAFILE="${XPATH_ELEMENTS[j++]}"
DISKLESS_POOL="${XPATH_ELEMENTS[j++]:-DfltDisklessStorPool}"

SRC_RES="one-vm-${VM_ID}-checkpoint"
SRC_DEV="/dev/drbd/by-res/${SRC_RES}/0"
SRC_XML=${SRC_PATH}.xml
DS_ID=$(echo $SRC_PATH | $AWK -F/ '{print $(NF-2)}')
DS_LOCATION=$(echo "$SRC_DIR" | $SED 's|(/[^/]*){3}$||g')
DS_LOCATION_NON_DOUBLE_SLASH=$(echo "$DS_LOCATION" | $SED 's|//|/|g')

#-------------------------------------------------------------------------------
# TM_MAD is not 'linstor_un'
#-------------------------------------------------------------------------------

if [ "$TM_MAD" != "linstor_un" ]; then
    echo "$DRV_ACTION" | $SSH "$DST_HOST" /var/tmp/one/vmm/kvm/restore "$@"
    exit $?
fi

#-------------------------------------------------------------------------------
# TM_MAD is 'linstor_un'
#-------------------------------------------------------------------------------

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

linstor_load_keys

# Attach diskless resource for SRC_RES on DST_HOST
linstor_attach_diskless "$DST_HOST" "$SRC_RES" "$DISKLESS_POOL"

RECALCULATE_CMD=$(cat <<EOF
set -e -o pipefail

# extract the xml from the checkpoint

virsh --connect $LIBVIRT_URI save-image-dumpxml $SRC_DEV > $SRC_XML

# Eeplace all occurrences of the DS_LOCATION/<DS_ID>/<VM_ID> with the specific
# DS_ID where the checkpoint is placed. This is done in case there was a
# system DS migration

sed -i "s%$DS_LOCATION/[0-9]\+/$VM_ID/%$DS_LOCATION/$DS_ID/$VM_ID/%g" $SRC_XML
sed -i "s%$DS_LOCATION_NON_DOUBLE_SLASH/[0-9]\+/$VMID/%$DS_LOCATION/$DS_ID/$VMID/%g" $SRC_XML
EOF
)

ssh_exec_and_log "$DST_HOST" "$RECALCULATE_CMD" \
    "Could not recalculate paths in $SRC_XML on $DST_HOST"

### Restore with retry

# On RHEL/CentOS 7 with qemu-kvm (1.5), it may happen the QEMU
# segfaults on the very first try to restore from checkpoint.
# We retry 3 times before failing completely.

function restore_domain {
    ssh_exec_and_log "$DST_HOST" \
        "virsh --connect $LIBVIRT_URI restore $SRC_DEV --xml $SRC_XML" \
        "Could not restore from $SRC_DEV on $DST_HOST"
}

retry 3 restore_domain

if [ $? -ne 0 ]; then
    exit 1
fi

ssh_exec_and_log "$DST_HOST" \
    "virsh --connect $LIBVIRT_URI resume $DEPLOY_ID" \
    "Could not resume $DEPLOY_ID on $DST_HOST"

linstor_exec_and_log \
    "resource-definition delete $SRC_RES"

exit 0
