#!/bin/bash

# Vitastor OpenNebula driver
# Copyright (c) Vitaliy Filippov, 2024+
# License: Apache-2.0 http://www.apache.org/licenses/LICENSE-2.0

# This script is used to create a VM image (SRC) of size (SIZE) and formatted as (FS)

# -------- Set up the environment to source common tools & conf ------------

if [ -z "${ONE_LOCATION}" ]; then
    LIB_LOCATION=/usr/lib/one
else
    LIB_LOCATION=$ONE_LOCATION/lib
fi

. $LIB_LOCATION/sh/scripts_common.sh

DRIVER_PATH=$(dirname $0)
source ${DRIVER_PATH}/../libfs.sh
source ${DRIVER_PATH}/../../etc/datastore/datastore.conf

# -------- Get mkfs and datastore arguments from OpenNebula core ------------

DRV_ACTION=`cat -`
ID=$1

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

unset i XPATH_ELEMENTS

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <($XPATH \
    /DS_DRIVER_ACTION_DATA/DATASTORE/BASE_PATH \
    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/RESTRICTED_DIRS \
    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/SAFE_DIRS \
    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/BRIDGE_LIST \
    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/POOL_NAME \
    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/IMAGE_PREFIX \
    /DS_DRIVER_ACTION_DATA/IMAGE/FORMAT \
    /DS_DRIVER_ACTION_DATA/IMAGE/SIZE \
    /DS_DRIVER_ACTION_DATA/IMAGE/FS \
    /DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VITASTOR_CONF)

unset i

BASE_PATH="${XPATH_ELEMENTS[i++]}"
RESTRICTED_DIRS="${XPATH_ELEMENTS[i++]}"
SAFE_DIRS="${XPATH_ELEMENTS[i++]}"
BRIDGE_LIST="${XPATH_ELEMENTS[i++]}"
POOL_NAME="${XPATH_ELEMENTS[i++]}"
IMAGE_PREFIX="${XPATH_ELEMENTS[i++]:-one}"
FORMAT="${XPATH_ELEMENTS[i++]}"
SIZE="${XPATH_ELEMENTS[i++]}"
FS="${XPATH_ELEMENTS[i++]}"
VITASTOR_CONF="${XPATH_ELEMENTS[i++]}"

DST_HOST=`get_destination_host $ID`

if [ -z "$DST_HOST" ]; then
    error_message "Datastore template missing 'BRIDGE_LIST' attribute."
    exit -1
fi

CLI=
if [ -n "$VITASTOR_CONF" ]; then
    CLI="$CLI --config_path ${VITASTOR_CONF}"
fi
if [ -n "$POOL_NAME" ]; then
    CLI="$CLI --pool ${POOL_NAME}"
fi

set_up_datastore "$BASE_PATH" "$RESTRICTED_DIRS" "$SAFE_DIRS"

IMAGE_NAME="${IMAGE_PREFIX}-${ID}"

# ------------ Image to save_as disk, no need to create a new image ------------

if [ "$FORMAT" = "save_as" ]; then
    echo "$IMAGE_NAME"
    exit 0
fi

# ------------ Create the image in the repository ------------

# FIXME: Duplicate code with tm/vitastor/mkimage

MKIMAGE_CMD=$(cat <<EOF
    set -e -o pipefail
    export PATH=/usr/sbin:/sbin:\$PATH
    vitastor-cli $CLI create --pool "${POOL_NAME}" "$IMAGE_NAME" --size "${SIZE}M"
EOF
)

if [ -n "$FS" -o "$FORMAT" = "swap" ]; then
    MKFS_CMD=`mkfs_command '$NBD' raw "$SIZE" "$SUPPORTED_FS" "$FS" "$FS_OPTS" | grep -v $QEMU_IMG`
fi

MKIMAGE_CMD=$(cat <<EOF
    set -e -o pipefail
    export PATH=/usr/sbin:/sbin:\$PATH
    vitastor-cli $CLI create --pool "${POOL_NAME}" "$IMAGE_NAME" --size "${SIZE}M"
EOF
)
if [ ! -z $FS ]; then
    set -e -o pipefail

    IMAGE_HASH=`generate_image_hash`
    FS_OPTS=$(eval $(echo "echo \$FS_OPTS_$FS"))

    MKFS_CMD=`mkfs_command '$NBD' raw "$SIZE" "$SUPPORTED_FS" "$FS" "$FS_OPTS" | grep -v $QEMU_IMG`
    MKIMAGE_CMD=$(cat <<EOF
    $MKIMAGE_CMD
    NBD=\$(sudo vitastor-nbd $CLI map --image "$IMAGE_NAME")
    trap "sudo vitastor-nbd $CLI unmap \$NBD" EXIT
    $MKFS_CMD
EOF
)
fi

ssh_exec_and_log "$DST_HOST" "$MKIMAGE_CMD" "Error registering $IMAGE_NAME in $DST_HOST"

echo "$IMAGE_NAME"
