#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs        #
#                                                                            #
# 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.                                             #
#--------------------------------------------------------------------------- #

source $(dirname $0)/kvmrc
source $(dirname $0)/../../scripts_common.sh

DRIVER_PATH=$(dirname $0)

DEFAULT_TYPE="raw"

DOMAIN="$1"
SOURCE="$2"
TARGET="$3"
TARGET_INDEX="$4"
DRV_ACTION="$5"

ATTACH_FILE="$SOURCE.attach"

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

unset i j XPATH_ELEMENTS

DISK_XPATH="/VMM_DRIVER_ACTION_DATA/VM/TEMPLATE/DISK[ATTACH='YES']"

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <($XPATH     /VMM_DRIVER_ACTION_DATA/VM/ID \
                    $DISK_XPATH/DRIVER \
                    $DISK_XPATH/TYPE \
                    $DISK_XPATH/READONLY \
                    $DISK_XPATH/CACHE \
                    $DISK_XPATH/SOURCE \
                    $DISK_XPATH/DISK_ID \
                    $DISK_XPATH/CLONE \
                    $DISK_XPATH/CEPH_HOST \
                    $DISK_XPATH/CEPH_SECRET \
                    $DISK_XPATH/CEPH_USER)

VMID="${XPATH_ELEMENTS[j++]}"
DRIVER="${XPATH_ELEMENTS[j++]:-$DEFAULT_TYPE}"
TYPE="${XPATH_ELEMENTS[j++]}"
READONLY="${XPATH_ELEMENTS[j++]}"
CACHE="${XPATH_ELEMENTS[j++]}"
IMG_SRC="${XPATH_ELEMENTS[j++]}"
DISK_ID="${XPATH_ELEMENTS[j++]}"
CLONE="${XPATH_ELEMENTS[j++]}"
CEPH_HOST="${XPATH_ELEMENTS[j++]}"
CEPH_SECRET="${XPATH_ELEMENTS[j++]}"
CEPH_USER="${XPATH_ELEMENTS[j++]}"

TYPE=$(echo "$TYPE"|tr A-Z a-z)

case "$TYPE" in
block)
    TYPE_SOURCE="dev"
    TYPE_XML="block"
    DEVICE="disk"
    ;;
cdrom)
    TYPE_SOURCE="file"
    TYPE_XML="file"
    DEVICE="cdrom"
    ;;
rbd)
    TYPE_SOURCE="protocol"
    TYPE_XML="network"
    SOURCE="rbd"
    DEVICE="disk"

    if [ "$CLONE" = "YES" ]; then
        SOURCE_ARGS="name='${IMG_SRC}-${VMID}-${DISK_ID}'"
    else
        SOURCE_ARGS="name='${IMG_SRC}'"
    fi

    for host in $CEPH_HOST ; do
        BCK_IFS=$IFS
        IFS=':'

        unset k HOST_PARTS

        for part in $host ; do
            HOST_PARTS[k++]="$part"
        done

        SOURCE_HOST="$SOURCE_HOST<host name='${HOST_PARTS[0]}'"

        if [ -n "${HOST_PARTS[1]}" ]; then
            SOURCE_HOST="$SOURCE_HOST port='${HOST_PARTS[1]}'"
        fi

        SOURCE_HOST="$SOURCE_HOST/>"

        IFS=$BCK_IFS
    done

    if [ -n "$CEPH_USER" -a -n "$CEPH_SECRET" ]; then
        AUTH="<auth username='$CEPH_USER'>\
                <secret type='ceph' uuid='$CEPH_SECRET'/>\
              </auth>"
    fi
    ;;
*)
    #NOTE: This includes TYPE=FS and TYPE=SWAP
    TYPE_SOURCE="file"
    TYPE_XML="file"
    DEVICE="disk"
    ;;
esac

[ -z "$CACHE" ] && [ -n "$DEFAULT_ATTACH_CACHE" ] && \
    CACHE="$DEFAULT_ATTACH_CACHE"
[ -n "$CACHE" ] && CACHE="cache='$CACHE'"

if [ "$READONLY" = "YES" ]; then
    READONLY="<readonly/>"
else
    READONLY=""
fi

cat <<EOF > $ATTACH_FILE
<disk type='$TYPE_XML' device='$DEVICE'>
  <driver name='qemu' type='$DRIVER' $CACHE/>
  <source $TYPE_SOURCE='$SOURCE' $SOURCE_ARGS>
  $SOURCE_HOST
  </source>
  $AUTH
  <target dev='$TARGET'/>
  $READONLY
</disk>
EOF

exec_and_log "virsh --connect $LIBVIRT_URI attach-device $DOMAIN $ATTACH_FILE" \
    "Could not attach $SOURCE ($TARGET) to $DOMAIN"

rm -f "$ATTACH_FILE"
