#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2025, 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_IMG_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")
DST_HOST_LVM="$DST_HOST"

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

if is_undeployed "$VM_ID" "$DST_HOST"; then

    # get BRIDGE_LIST from image datastore
    XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"
    IFS= read -r -d '' BRIDGE_LIST < <(onedatastore show -x "$DS_IMG_ID" \
        | $XPATH /DATASTORE/TEMPLATE/BRIDGE_LIST )

    if [ -n "$BRIDGE_LIST" ]; then # If BRIDGE_LIST is not defined FE must have access to LVM cluster
        DST_HOST_LVM=$(get_destination_host)
    fi
fi

# Get DEV
DEV=$($SSH "$DST_HOST" "readlink $DST_PATH 2> /dev/null | grep \"^/dev/\"")

if [ -n "$DEV" ]; then

    LOCK="tm-fs_lvm-${DS_SYS_ID}.lock"

    if [ "${ZERO_LVM_ON_DELETE}" = "yes" ]; then
        # Activate device
        ACTIVATE_CMD=$(cat <<EOF
            set -ex -o pipefail
            ${SYNC}
            ${SUDO} ${LVSCAN}
            ${SUDO} ${LVCHANGE} -ay "${DEV}"
EOF
)

        exclusive "${LOCK}" 120 ssh_exec_and_log "$DST_HOST_LVM" "$ACTIVATE_CMD" \
            "Error activating disk $SRC_PATH"

        # It's separeted from ACTIVATE_CMD to avoid blocking while writing in the device
        ssh_exec_and_log_no_error "$DST_HOST_LVM" "${DD} if=/dev/zero of=\"${DEV}\" bs=${DD_BLOCK_SIZE:-64k}" \
            "Error cleaning $DST_PATH"
    fi


    exclusive "${LOCK}" 120 ssh_exec_and_log "$DST_HOST_LVM" "$SUDO $LVREMOVE -f \"$DEV\"" \
    "Error deleting $DST_PATH"
fi

ssh_exec_and_log "$DST_HOST" "rm -rf \"$DST_PATH\"" \
    "Error removing \"$DST_PATH\" from $DST_HOST"
