#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2015, 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)

source $TMCOMMON
source ${DRIVER_PATH}/../../datastore/ceph/ceph.conf

#-------------------------------------------------------------------------------
# Process destination
#-------------------------------------------------------------------------------

DST_PATH=`arg_path $DST`
DST_HOST=`arg_host $DST`

#-------------------------------------------------------------------------------
# Delete and exit if 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

#-------------------------------------------------------------------------------
# Get Image information
#-------------------------------------------------------------------------------

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

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

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]/SOURCE \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CLONE \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CEPH_USER)

SRC="${XPATH_ELEMENTS[j++]}"
CLONE="${XPATH_ELEMENTS[j++]}"
CEPH_USER="${XPATH_ELEMENTS[j++]}"

# No need to delete no cloned images
if [ "$CLONE" = "NO" ]; then
    exit 0
fi

if [ -n "$CEPH_USER" ]; then
    RBD="$RBD --id ${CEPH_USER}"
fi

# cloned, so the name will be "<pool>/one-<imageid>-<vmid>-<diskid>"
RBD_SRC="${SRC}-${VM_ID}-${DISK_ID}"
RBD_SNAP="${VM_ID}-${DISK_ID}"

#-------------------------------------------------------------------------------
# Delete the device
#-------------------------------------------------------------------------------

log "Deleting $DST_PATH"

# Note that this command, as opposed to the rest of $RBD commands in this set of
# drivers, is executed in the worker node and not in the CEPH frontend.

DELETE_CMD=$(cat <<EOF
    rm_children(){
        local rbd_snap rbd snap_id child snap_list

        rbd_snap=\$1
        rbd=\${rbd_snap%%@*}

        snap_list=\$(set +e; rbd snap ls \$rbd)

        if [ -n "\$snap_list" ]; then
            CHILDREN=\$(set +e; rbd children \$rbd_snap 2>/dev/null)

            for child in \$CHILDREN; do
                snap_id=\${child##*-}
                child=\$child@\$snap_id
                rm_children \$child
            done

            $RBD snap unprotect \$rbd_snap
            $RBD snap rm \$rbd_snap
        fi

        $RBD rm \$rbd
    }

    RBD_FORMAT=\$($RBD info $RBD_SRC | sed -n 's/.*format: // p')

    if [ "\$RBD_FORMAT" = "2" ]; then
        has_snap_shots=\$($RBD info $RBD_SRC-0@0 2>/dev/null)

        if [ -n "\$has_snap_shots" ]; then
            rm_children $RBD_SRC-0@0
        else
            $RBD rm $RBD_SRC
        fi
    else
        $RBD rm $RBD_SRC
    fi

    # Remove the snapshot of the original image used to create a CLONE
    if [ "\$RBD_FORMAT" = "2" ]; then
        $RBD snap unprotect $SRC@$RBD_SNAP
        $RBD snap rm $SRC@$RBD_SNAP
    fi
EOF
)

ssh_exec_and_log "$DST_HOST" "$DELETE_CMD" \
                 "Error deleting $RBD_SRC in $DST_HOST"

exit 0
