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

# DELETE <host:remote_system_ds/disk.i|host:remote_system_ds/>
#   - host is the target host to deploy the VM
#   - remote_system_ds is the path for the system datastore in the host

DST=$1
VM_ID=$2
DS_ID=$3

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)
XPATH="${DRIVER_PATH}/../../datastore/xpath.rb"

source $TMCOMMON
source ${DRIVER_PATH}/../../etc/tm/fs_lvm/fs_lvm.conf
source ${DRIVER_PATH}/../../datastore/libfs.sh

#-------------------------------------------------------------------------------
# Return if deleting a disk, we will delete them when removing the
# remote_system_ds directory for the VM (remotely)
#-------------------------------------------------------------------------------
DST_PATH=`arg_path $DST`
DST_HOST=`arg_host $DST`

if [ `is_disk $DST_PATH` -eq 1 ]; then
    DS_SYS_ID=$(echo $DST_PATH | $AWK -F '/' '{print $(NF-2)}')
else
    DS_SYS_ID=$(echo $DST_PATH | $AWK -F '/' '{print $(NF-1)}')
fi

unset i j XPATH_ELEMENTS
while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <(onevm show -x $VM_ID | $XPATH \
    '/VM/HISTORY_RECORDS/HISTORY[last()]/HOSTNAME')

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


# Change DST_HOST to one of the BRIDGE_LIST to prevent
# running on frontend for undeployed VMs
if [ "$LAST_HOST" != "$DST_HOST" ]; then
    unset i j XPATH_ELEMENTS
    while IFS= read -r -d '' element; do
        XPATH_ELEMENTS[i++]="$element"
    done < <(onedatastore show -x $DS_SYS_ID | $XPATH \
        /DATASTORE/TEMPLATE/BRIDGE_LIST)

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

    if [ -n "$BRIDGE_LIST" ]; then
        DST_HOST=$(get_destination_host)
    fi
fi

# Activate device
ACTIVATE_CMD=$(cat <<EOF
    set -ex -o pipefail
    if [ -L "$DST_PATH" ]; then
        DEV=\$(readlink $DST_PATH)
        if echo "\$DEV" | grep "^/dev/" &>/dev/null; then
            ${SYNC}
            ${SUDO} ${LVSCAN}
            ${SUDO} ${LVCHANGE} -ay "\${DEV}"
        fi
    fi
EOF
)

# Zero space
ZERO_CMD=$(cat <<EOF
    set -ex -o pipefail
    if [ -L "$DST_PATH" ]; then
        DEV=\$(readlink $DST_PATH)
        if echo "\$DEV" | grep "^/dev/" &>/dev/null; then
            ${DD} if=/dev/zero of="\${DEV}" bs=${DD_BLOCK_SIZE:-64k} || :
        fi
    fi
EOF
)

# Delete the device if it's a clone (LVM snapshot)
DELETE_CMD=$(cat <<EOF
    set -ex -o pipefail

    if [ -d "$DST_PATH" ]; then
        rm -rf "$DST_PATH"
        exit 0
    fi

    if [ -L "$DST_PATH" ]; then
        DEV=\$(readlink $DST_PATH)
        if echo "\$DEV" | grep "^/dev/" &>/dev/null; then
            $SUDO $LVREMOVE -f "\$DEV"
        fi
    fi

    rm -f "$DST_PATH"
EOF
)

if [ "${ZERO_LVM_ON_DELETE}" = "yes" ]; then
    LOCK="tm-fs_lvm-${DS_SYS_ID}.lock"
    exclusive "${LOCK}" 120 ssh_exec_and_log "$DST_HOST" "$ACTIVATE_CMD" \
        "Error activating disk $SRC_PATH"

    ssh_exec_and_log "$DST_HOST" "$ZERO_CMD" "Error cleaning $DST_PATH"
fi

LOCK="tm-fs_lvm-${DS_SYS_ID}.lock"
exclusive "${LOCK}" 120 ssh_exec_and_log "$DST_HOST" "$DELETE_CMD" \
    "Error deleting $DST_PATH"

hup_collectd $DST_HOST
