#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2017, 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.                                             #
#--------------------------------------------------------------------------- #
#-#rbt version-------------------------------------------------------------- #

source $(dirname $0)/../../etc/vmm/kvm/kvmrc
source $(dirname $0)/../../scripts_common.sh

deploy_id=$1
dest_host=$2
host=$3
NEW_DS_ID=$4
VM_ID=$5
REQUEST_ID=$7
STDIN=$(cat -)

echo "$STDIN" | tr -d '\n' | grep -q '^ *<.*> *$' || STDIN="$(echo "$STDIN" | base64 -d)"

DRIVER_PATH=$(dirname $0)
XPATH="$DRIVER_PATH/../../datastore/xpath.rb"

unset i j XPATH_ELEMENTS
while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <(echo $STDIN | $XPATH \
            /VMM_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/TM_MAD)

TM_MAD="${XPATH_ELEMENTS[j++]}"

if [ "$dest_host" == "$host" ]; then
    MOVE_CMD=$(cat <<EOF
        set -e -o pipefail
        OUT="\$(mktemp)"
        virsh -c qemu:///system domblklist "$deploy_id" --details | tail -n+3 > \$OUT
        rm -f /var/lib/one/datastores/$NEW_DS_ID/$VM_ID/before_ds
        while read disk; do
            if [ -n "\$disk" ]; then
             tdisk=(\$disk)
             DISK_ID=\${disk##*.}
             NEW_DISK_PATH=/var/lib/one/datastores/$NEW_DS_ID/$VM_ID/disk.\${DISK_ID}
             if [ "$TM_MAD" == "brest_lvm" ]; then
                if [ "\${tdisk[0]}" == "block" ]; then
                    if echo "\${tdisk[3]}" | grep "lv-one-vm" &>/dev/null; then
                        DISK_ID=\${disk##*-}
                        NEW_DISK_PATH="/dev/vg-one-$NEW_DS_ID/lv-one-vm-$VM_ID-\${DISK_ID}"
                        virsh -c qemu:///system blockcopy --domain $deploy_id --path \${tdisk[3]} --dest \$NEW_DISK_PATH --blockdev  --wait --verbose --pivot
                        echo "\$disk" >> /var/lib/one/datastores/$NEW_DS_ID/$VM_ID/before_ds
                    fi
                else
                    #context file
                    echo "\$disk" >> /var/lib/one/datastores/$NEW_DS_ID/$VM_ID/before_ds
                    cp \${tdisk[3]} \$NEW_DISK_PATH
                    virsh -c qemu:///system change-media --domain $deploy_id \${tdisk[2]} \$NEW_DISK_PATH --live
                fi
             elif [ "$TM_MAD" == "lvm_lvm" ]; then
                if [ "\${tdisk[0]}" == "block" ]; then
                    virsh -c qemu:///system blockcopy --domain $deploy_id --path \${tdisk[3]} --dest \$NEW_DISK_PATH --blockdev  --wait --verbose --pivot
                else
                    cp \${tdisk[3]} \$NEW_DISK_PATH
                    virsh -c qemu:///system change-media --domain $deploy_id \${tdisk[2]} \$NEW_DISK_PATH --live
                fi
                echo "\$disk" >> /var/lib/one/datastores/$NEW_DS_ID/$VM_ID/before_ds
             else
                if [ "\${tdisk[0]}" == "block" ]; then
                    virsh -c qemu:///system blockcopy --domain $deploy_id --path \${tdisk[3]} --dest \$NEW_DISK_PATH --blockdev  --wait --verbose --pivot
                else

                    virsh -c qemu:///system blockcopy --domain $deploy_id --path \${tdisk[3]} --dest \$NEW_DISK_PATH --wait --verbose --pivot
                fi
                echo "\$disk" >> /var/lib/one/datastores/$NEW_DS_ID/$VM_ID/before_ds
             fi
            fi
        done < \$OUT
EOF
)
    ssh_exec_and_log "$dest_host" "$MOVE_CMD" "Error move disks between datastore for $deploy_id"

    exit 0
fi
