#!/bin/bash

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

DRIVER_PATH=$(dirname $0)
DOMAIN=$1

# Exit if no stdin data is available
if [ -t 0 ]; then
    exit 0
fi

# Read data from stdin data. Extracting the VCPU and MEMORY
XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"

unset i XPATH_ELEMENTS

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <($XPATH     /VMM_DRIVER_ACTION_DATA/VM/TEMPLATE/VCPU \
                    /VMM_DRIVER_ACTION_DATA/VM/TEMPLATE/MEMORY \
                    /VMM_DRIVER_ACTION_DATA/VM/TEMPLATE/RESIZE/VCPU \
                    /VMM_DRIVER_ACTION_DATA/VM/TEMPLATE/RESIZE/MEMORY)

unset i

VCPU="${XPATH_ELEMENTS[i++]}"
MEM="${XPATH_ELEMENTS[i++]}"
VCPU_OLD="${XPATH_ELEMENTS[i++]}"
MEM_OLD="${XPATH_ELEMENTS[i++]}"

source $(dirname $0)/../../etc/vmm/kvm/kvmrc
source $(dirname $0)/../../scripts_common.sh

# Resize mem
if [ ! -z "$MEM" -a "$MEM" -ne "$MEM_OLD" ]; then
    # Compact memory
    if [ "x$CLEANUP_MEMORY_ON_START" = "xyes" ]; then
        sudo -n sysctl vm.drop_caches=3 vm.compact_memory=1 >/dev/null
    fi

    exec_and_log "virsh --connect ${LIBVIRT_URI} setmem ${DOMAIN} $((${MEM}*1024))"

    # Compact memory
    if [ "x$CLEANUP_MEMORY_ON_STOP" = "xyes" ]; then
        sudo -n sysctl vm.drop_caches=3 vm.compact_memory=1 &>/dev/null &
    fi
fi

# Resize VCPU
if [ ! -z "$VCPU" -a "$VCPU" -ne "$VCPU_OLD" ]; then
     data_check=$(virsh -c ${LIBVIRT_URI} vcpucount --guest ${DOMAIN} --live 2>&1)
     if [ "x$?" = "x0" ]; then
         data=$(virsh -c ${LIBVIRT_URI} setvcpus ${DOMAIN} ${VCPU} --live --guest 2>&1)
         if [ "x$?" != "x0" ]; then
             virsh -c qemu:///system setvcpus ${DOMAIN} ${VCPU} --live >/dev/null 2>&1
             data=$(virsh -c ${LIBVIRT_URI} setvcpus ${DOMAIN} ${VCPU} --guest 2>&1)
             if [ "x$?" != "x0" ]; then
                 error_message "Could not change VCPU for domain ${DOMAIN} $data"
                 exit -1
             fi
         fi
     else
        error_message "Could not change VCPU for domain ${DOMAIN} $data_check"
        exit -1
     fi
fi
