#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org)             #
#                                                                            #
# 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/> vmid dsid
#   - host is the target host to deploy the VM
#   - remote_system_ds is the path for the system datastore in the host
#   - vmid is the id of the VM
#   - dsid is the target datastore (0 is the system datastore)

DST=$1

VMID=$2
DSID=$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

. $TMCOMMON

DRIVER_PATH=$(dirname $0)

source ${DRIVER_PATH}/../../datastore/iscsi/iscsi.conf

#-------------------------------------------------------------------------------
# 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`

#-------------------------------------------------------------------------------
# Get IQN information
#-------------------------------------------------------------------------------

DISK_ID=$(echo "$DST_PATH" | $AWK -F. '{print $NF}')

XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"

unset i XPATH_ELEMENTS

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

IQN="${XPATH_ELEMENTS[0]}"
PERSISTENT="${XPATH_ELEMENTS[1]}"

if [ -z "$PERSISTENT" ]; then
    NEW_IQN=$IQN-$VMID
else
    NEW_IQN=$IQN
fi

TARGET=`arg_path $IQN`
VG_NAME=`echo $TARGET|$AWK -F. '{print $(NF-1)}'`
LV_NAME=`echo $TARGET|$AWK -F. '{print $(NF)}'`
DEV="/dev/$VG_NAME/$LV_NAME"
TARGET_HOST="${TARGET%.$VG_NAME.$LV_NAME}"

TARGET_LV_NAME=`echo $NEW_IQN|$AWK -F. '{print $(NF)}'`

#-------------------------------------------------------------------------------
# Remove directory if dst_path is a directory
#-------------------------------------------------------------------------------

if [ `is_disk $DST_PATH` -eq 0 ]; then
    # Directory
    log "Deleting $DST_PATH"
    ssh_exec_and_log "$DST_HOST" "rm -rf $DST_PATH" "Error deleting $DST_PATH"
    exit 0
fi

#-------------------------------------------------------------------------------
# Logout from the iSCSI target in the hypervisor
#-------------------------------------------------------------------------------

LOGOUT_CMD=$(cat <<EOF
    set -e
    $SUDO $(iscsiadm_logout $NEW_IQN)
EOF
)

ssh_exec_and_log "$DST_HOST" "$LOGOUT_CMD" \
    "Error logging out $NEW_IQN"

if [ "$PERSISTENT" = "YES" ]; then
    exit 0
fi

#-------------------------------------------------------------------------------
# Remove target and LV in the iSCSI server
#-------------------------------------------------------------------------------

DELETE_CMD=$(cat <<EOF
    set -e
    # get tid for IQN
    TID=\$($SUDO $(tgtadm_get_tid_for_iqn "$NEW_IQN"))

    # remove iscsi target
    $SUDO $(tgtadm_target_delete "\$TID")

    # dump configuration
    $SUDO $(tgt_admin_dump_config "$TARGET_CONF")

    # remove lv
    $SUDO $LVREMOVE -f $VG_NAME/$TARGET_LV_NAME
EOF
)

ssh_exec_and_log "$TARGET_HOST" "$DELETE_CMD" \
    "Error logging out $IQN"

exit 0
