################################################################################
# Filename: ./src/plugins/vmware/include/vmware-functions
# 
# The contents of this file are subject to the Qlusters Public License
# Version 1.1 (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.openqrm.org/content/view/33/69/.
# 
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# 
# The Original Code is openQRM.
# 
# The Initial Developer of the Original Code is Qlusters Corporation. Portions
# created by Qlusters Corporation are Copyright (C) 2003-2005. All Rights
# Reserved.
# 
# Contributor(s):
# 
# Description:
# 
# Comments:
################################################################################
QRM_CLI=$QRM_SERVER_BASE_DIR/qrm/bin/qrm-cli
VMWARE_FS_IMAGE_NAME=VMware_GSX_image_1_0
VMWARE_BOOT_IMAGE_NAME=VMware_GSX_kernel_1_0
function vmware_install() {
	echo "Creating vmware boot package"
# creating the plugin package
	tar -C $QRM_SERVER_BASE_DIR/qrm/ -czf $QRM_SERVER_BASE_DIR/qrm/usr/tomcat/webapps/ROOT/unsecure/vmware-plugin.tgz plugins/vmware
if [ -z $QRM_VMWARE_SOURCE_SYSTEM ];then
        echo "SOURCE_SYSTEM parameter is missing or empty in vmware.ini"
        exit 1
fi
if [ -z $QRM_VMWARE_STORAGE_SERVER ];then
        echo "STORAGE_SERVER parameter is missing or empty in vmware.ini"
        exit 1
fi
if [ -z $QRM_VMWARE_STORAGE_FILESYSTEM_PATH ];then
        echo "STORAGE_FILESYSTEM_PATH parameter is missing or empty in vmware.ini"
        exit 1
fi
if [ -z $QRM_VMWARE_SOURCE_KERNEL_VERSION ];then
        echo "SOURCE_KERNEL_VERSION parameter is missing or empty in vmware.ini"
        exit 1
fi
if [ -z $QRM_VMWARE_BOOT_FLAGS ];then
        echo "BOOT_FLAGS parameter is missing or empty in vmware.ini"
        exit 1
fi
if [ -z $QRM_VMWARE_SYSTEM_ARCH ];then
        echo "SYSTEM_ARCH parameter is missing or empty in vmware.ini"
        exit 1
fi
if [ -z $QRM_VMWARE_STORAGE_TYPE ];then
	echo "STORAGE_TYPE parameter is missing or empty in vmware.ini"
	exit 1
fi
if [ -z $QRM_VMWARE_STORAGE_SERVER_NODE_ACCESS_IP ];then
	echo "STORAGE_SERVER_NODE_ACCESS_IP parameter is missing or empty in vmware.ini"
	exit 1
fi
#creating filesystem image for vmware
echo "Creating vmware filesystem image"
if ! $QRM_SERVER_BASE_DIR/qrm/bin/qrm-filesystem-image create -s $VMWARE_FS_IMAGE_NAME -i $QRM_VMWARE_SOURCE_SYSTEM -n $QRM_VMWARE_STORAGE_SERVER_NODE_ACCESS_IP:$QRM_VMWARE_STORAGE_FILESYSTEM_PATH --storage-server $QRM_VMWARE_STORAGE_SERVER --private-add /etc/vmware/  --hostname $VMWARE_FS_IMAGE_NAME -o;then
	exit 1
fi

#creating boot image for vmware
echo "Creating vmware boot image"
if ! $QRM_SERVER_BASE_DIR/qrm/bin/qrm-boot-image create -k $QRM_VMWARE_SOURCE_KERNEL_VERSION -b $VMWARE_BOOT_IMAGE_NAME -y $VMWARE_BOOT_IMAGE_NAME -i $QRM_VMWARE_SOURCE_SYSTEM -l / -o;then
	exit 1
fi
}

function vmware_uninstall() {
	: 
}

function vmware_start() {
	: 
}

function vmware_stop() {
	: 
}

function vmware_run_once() {
# register the nodes-service
$QRM_CLI storage add -n $QRM_VMWARE_STORAGE_SERVER -t $QRM_VMWARE_STORAGE_TYPE -i $QRM_VMWARE_STORAGE_SERVER_NODE_ACCESS_IP
        RET=$?
        case "$RET" in
                0)
                        RET=0
                        ;;
                7)
                        $QRM_CLI event add -g -k QRM.Plugin.Error.Requirements -t pluginName=vmware -t details="Storage Server $QRM_VMWARE_STORAGE_SERVER exist"
                        ;;
                *)
                        echo "ERROR: The Storage server could not  be added. The error code is $RET "
                        ;;
        esac

        $QRM_CLI filesystem add -n $VMWARE_FS_IMAGE_NAME -s $QRM_VMWARE_STORAGE_SERVER -i $QRM_VMWARE_STORAGE_FILESYSTEM_PATH/$VMWARE_FS_IMAGE_NAME -a $QRM_VMWARE_SYSTEM_ARCH
        RET=$?
case "$RET" in
                0)
                        RET=0
                        ;;
                7)
                        $QRM_CLI event add -g -k QRM.Plugin.Error.Requirements -t pluginName=vmware -t details="Filesystem image $VMWARE_FS_IMAGE_NAME already exist inside the DB"
                        RET=0
                        ;;
                *)
                        echo "ERROR: The Filesystem image could not be added. The error code is $RET "
                        ;;
        esac

        $QRM_CLI kernel add -n $VMWARE_BOOT_IMAGE_NAME -k $QRM_VMWARE_SOURCE_KERNEL_VERSION -y $VMWARE_BOOT_IMAGE_NAME -m $QRM_VMWARE_BOOT_FLAGS -a $QRM_VMWARE_SYSTEM_ARCH
        RET=$?
        case "$RET" in
                0)
                        RET=0
                        ;;
                7)
                        $QRM_CLI event add -g -k QRM.Plugin.Error.Requirements -t pluginName=vmware -t details="Boot image $VMWARE_FS_IMAGE_NAME already exist inside the DB"
                        RET=0
                        ;;
                *)
                        echo "ERROR: The Boot image could not be added. The error code is $RET "
                        ;;
        esac
	$QRM_CLI boot associate -k vmware -t image -n $VMWARE_FS_IMAGE_NAME
	RET=$?
	case "$RET" in
		0)
			;;
		6)
			$QRM_CLI event add -g -k QRM.Plugin.Error.Requirements -t pluginName=vmware -t details="Required filesystem image $VMWARE_FS_IMAGE_NAME does not exist. Please add it and then run 'sbin/qrm-plugin vmware run_once'"
			RET=0
			;;
		*)
			echo "ERROR: The boot-service for the vmware-plugin could not be associated. The error code is $RET"
			;;
	esac
	return "$RET"
}
