#!/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"
DISK_ID=$(basename ${DST_PATH} | cut -d. -f2)

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

unset i j XPATH_ELEMENTS

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <(onevm show -x $VM_ID| $XPATH \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/LVM_THIN_ENABLE )

# This will be undefined when deleting VM dirs
LVM_THIN_ENABLE="${XPATH_ELEMENTS[j++],,}"

# Get DEV
POOL_NAME="lv-one-$VM_ID-pool"
VG_NAME="vg-one-$DS_SYS_ID"
DEV=$($SSH "$DST_HOST" "readlink $DST_PATH 2> /dev/null | grep \"^/dev/\"")

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

# The `delete` operation is called for each disk, and then once for the VM directory
if [ -n "$DEV" ]; then
    # This branch takes care of the per-disk calls (deleting thin LVs)
    if [ "${ZERO_LVM_ON_DELETE}" = "yes" ] && [ "$LVM_THIN_ENABLE" != '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

    RMCMD=$(cat <<EOF
        ${SYNC}
        ${SUDO} ${LVSCAN}
        $SUDO $LVREMOVE -y '$DEV'
        ${SYNC}
EOF
)
    ERR="Error deleting LV '$DEV'"
else
    # And this one takes care of the VM itself. In LVM thin, delete the thin pool.
    RMCMD=$(cat <<EOF
        set -ex -o pipefail
        if [ -n "\$($SUDO $LVS --noheading -S "vg_name = $VG_NAME && lv_name = $POOL_NAME")" ]; then
            $SUDO $LVREMOVE -y '$VG_NAME/$POOL_NAME'
        fi
EOF
)
    ERR="Error deleting LV POOL '$VG_NAME/$POOL_NAME'"
fi

exclusive "${LOCK}" 120 ssh_exec_and_log "$DST_HOST_LVM" "$RMCMD" "$ERR"

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