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

source $DRIVER_PATH/../../etc/vmm/kvm/kvmrc
source $DRIVER_PATH/../../scripts_common.sh

FILE=$1
HOST=$2
DEPLOY_ID=$3

VM_ID=$4
DS_ID=$5

FILE_XML=${FILE}.xml

#-------------------------------------------------------------------------------
# Handle DRV_MESSAGE coming from stdin
#-------------------------------------------------------------------------------

if [ ! -t 0 ]; then
    # There is data in stdin, read it
    DRV_MESSAGE=$(cat)

    # The data is the driver message. Extracting the System DS TM_MAD
    XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"
    TM_MAD=$(echo "$DRV_MESSAGE" | $XPATH /VMM_DRIVER_ACTION_DATA/DATASTORE/TM_MAD)

    # If there is a specific hook for this TM_MAD call it:
    RESTORE_TM_FILE="${DRIVER_PATH}/restore.${TM_MAD}"

    if [ -x "$RESTORE_TM_FILE" ]; then
        echo "$DRV_MESSAGE" | $RESTORE_TM_FILE $@
    fi
fi

# Checkpoint file: /var/lib/one//datastores/<DS_ID>/<VM_ID>/checkpoint

DS_ID=$(basename $(dirname $(dirname $FILE)))
DS_LOCATION=$(dirname $(dirname $(dirname $FILE)))
DS_LOCATION_NON_DOUBLE_SLASH=$(echo "$DS_LOCATION" | sed 's|//|/|g')
VM_DIR=$(dirname $FILE)

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

# extract the xml from the checkpoint

virsh --connect $LIBVIRT_URI save-image-dumpxml $FILE > $FILE_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" $FILE_XML
sed -i "s%$DS_LOCATION_NON_DOUBLE_SLASH/[0-9]\+/$VMID/%$DS_LOCATION/$DS_ID/$VMID/%g" $FILE_XML
EOF
)

multiline_exec_and_log "$RECALCULATE_CMD" \
    "Could not recalculate paths in $FILE_XML"

### 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 {
    exec_and_log "virsh --connect $LIBVIRT_URI restore $FILE --xml $FILE_XML" \
        "Could not restore from $FILE"
}

retry ${VIRSH_RETRIES:-3} restore_domain

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

if [ "$SYNC_TIME" = "yes" ]; then
    virsh --connect $LIBVIRT_URI domtime --sync $DEPLOY_ID >/dev/null 2>/dev/null || :
fi

rm "$FILE"
rm "$FILE_XML"

# redefine potential snapshots
for SNAPSHOT_MD_XML in $(ls ${VM_DIR}/snap-*.xml 2>/dev/null); do

	# query UUID, but only once
	UUID=${UUID:-$(virsh --connect $LIBVIRT_URI dominfo $DEPLOY_ID | grep UUID: | awk '{print $2}')}

	# replace uuid in the snapshot metadata xml
	sed -i "s%<uuid>[[:alnum:]-]*</uuid>%<uuid>$UUID</uuid>%" $SNAPSHOT_MD_XML

	# redefine the snapshot using the xml metadata file
	virsh --connect $LIBVIRT_URI snapshot-create $DEPLOY_ID $SNAPSHOT_MD_XML --redefine > /dev/null || true
done

exit 0
