#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2017, 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.                                             #
#--------------------------------------------------------------------------- #

# mkimage size format host:remote_system_ds/disk.i vmid dsid
#   - size in MB of the image
#   - format for the image
#   - 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)

SIZE=$1
FSTYPE=$2
DST=$3

VMID=$4
DSID=$5

#-------------------------------------------------------------------------------

if [ -z "${ONE_LOCATION}" ]; then
    TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
    LIB_LOCATION=/usr/lib/one
else
    TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
    LIB_LOCATION=$ONE_LOCATION/lib
fi

DRIVER_PATH=$(dirname $0)

source $TMCOMMON

source ${DRIVER_PATH}/../../datastore/ceph/ceph.conf
source ${DRIVER_PATH}/../../datastore/libfs.sh

CEPH_UTILS=${DRIVER_PATH}/../../datastore/ceph/ceph_utils.sh

#-------------------------------------------------------------------------------
# Set dst path and dir
#-------------------------------------------------------------------------------

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

DISK_ID=$(echo $DST|awk -F. '{print $NF}')

#-------------------------------------------------------------------------------
# Make the new image (file-based)
#-------------------------------------------------------------------------------

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 $VMID | $XPATH  \
                            /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CEPH_USER \
                            /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CEPH_KEY \
                            /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CEPH_CONF \
                            /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/POOL_NAME \
                            /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/RBD_FORMAT)

CEPH_USER="${XPATH_ELEMENTS[j++]}"
CEPH_KEY="${XPATH_ELEMENTS[j++]}"
CEPH_CONF="${XPATH_ELEMENTS[j++]}"
POOL_NAME="${XPATH_ELEMENTS[j++]:-$POOL_NAME}"
RBD_FORMAT="${XPATH_ELEMENTS[j++]:-$RBD_FORMAT}"

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

if [ -n "$CEPH_KEY" ]; then
    RBD="$RBD --keyfile ${CEPH_KEY}"
fi

if [ -n "$CEPH_CONF" ]; then
    RBD="$RBD --conf ${CEPH_CONF}"
fi

if [ "$RBD_FORMAT" = "2" ]; then
    FORMAT_OPT="--image-format 2"
fi

RBD_SOURCE="${POOL_NAME}/one-sys-${VMID}-${DISK_ID}"

MKIMAGE_CMD=$(cat <<EOF
    export PATH=/usr/sbin:/sbin:\$PATH
    $RBD create $FORMAT_OPT $RBD_SOURCE --size ${SIZE} || exit \$?

    if [ "$FSTYPE" = "swap" ]; then
        $SUDO $RBD map $RBD_SOURCE || exit \$?
        $SUDO $MKSWAP -L swap /dev/rbd/$RBD_SOURCE
        $SUDO $RBD unmap /dev/rbd/$RBD_SOURCE
    fi
EOF
)

DELIMAGE_CMD=$(cat <<EOF
    export PATH=/usr/sbin:/sbin:\$PATH
    $RBD rm $RBD_SOURCE
EOF
)

log "Making volatile disk of ${SIZE}M at $DST"

ssh_exec_and_log_no_error "$DST_HOST" "$MKIMAGE_CMD" \
    "Error creating volatile disk.$DISK_ID ($RBD_SOURCE) in $DST_HOST into pool $POOL_NAME."

rc=$?

if [ $rc != 0 ]; then
    ssh_exec_and_log_no_error "$DST_HOST" "$DELIMAGE_CMD" \
        "Error removing image"
fi

exit $rc
