####################################################################
#  Copyright 2004-2008 Hewlett-Packard Development Company, L.P.  
# Confidential commercial computer software.  Valid license required.
#
# This is the hpvm serviceguard module package external script
#####################################################################
# @(#) $Id: hpvmsg_ext 8078 2008-08-13 01:19:43Z williams $
# set -x
###########################
# Source utility functions. 
###########################

if [[ -z $SG_UTILS ]]
then
    . /etc/cmcluster.conf
    SG_UTILS=$SGCONF/scripts/mscripts/utils.sh
fi

if [[ -f ${SG_UTILS} ]]; then
    . ${SG_UTILS}
    if (( $? != 0 ))
    then
        echo "ERROR: Unable to source package utility functions file: ${SG_UTILS}"
        exit 1
    fi
else
    echo "ERROR: Unable to find package utility functions file: ${SG_UTILS}"
    exit 1
fi

###################################################################
#
# Get the environment for this package through utility function
# sg_source_pkg_env().
#
###################################################################

sg_source_pkg_env $*

#########################################################################
#
# validate_command
#
# This function should define actions to take when the package is
# being validated during cmcheckconf/cmapplyconf on all the configured
# package nodes.
#
#########################################################################

function validate_command
{
    sg_log 5 "validate_command"

    return 0
}


#########################################################################
#
# start_command
#
# This function starts the HPVM virtual machine using the hpvmstart command
#
#########################################################################

function start_command
{
    #set -x
    # rtn_val: 1 = failure do NOT try others nodes; 2 = failure try other nodes
    typeset rtn_val=1
    typeset pid=-1
    typeset retries=30
    typeset state

    sg_log 5 "start_command"

    # handle case where package is still running on another system
    while :; do
        state=`/opt/hpvm/bin/hpvmstatus -P ${SG_PACKAGE} -M | /usr/bin/awk -F: '{print $11}'`
	if [[ ${state} = On ]] ; then
	    if [[ -e  /var/opt/hpvm/guests/${SG_PACKAGE}/guest_status ]] ; then
	        pid=`/usr/bin/sed -n '1s/ .*//p' /var/opt/hpvm/guests/${SG_PACKAGE}/guest_status`
	        if  /usr/bin/ps -f -p ${pid} | /usr/bin/grep -q ${SG_PACKAGE} ; then
	            sg_log 0 "Virtual machine already started on this node"
	            rtn_val=0
		    break
                fi
            fi
	    if [[ ${retries} -gt 0 ]] ; then
	        sleep 3
		let retries-=1
            else
		break
            fi
        else
	    if /opt/hpvm/bin/hpvmstart -P ${SG_PACKAGE} -Q ; then
	        rtn_val=0
	    fi
	    break
        fi 
    done
    return ${rtn_val}
}

#########################################################################
#
# stop_command
#
# This function should define actions to take when the package halts
#
# It's recommended that the actions performed in this function should be 
# made idempotent, that is if the halt action has already been performed 
# successfully before, it should not generate an error, but exit successfully.
#
# All SG provided scripts are currently idempotent.
#
#########################################################################

function stop_command
{
    #set -x
    # rtn_val: 1 = failure do NOT start on others nodes; 
    #          2 = failure attempt to start on other nodes
    typeset rtn_val=0
    typeset wait=10
    typeset gstat=/var/opt/hpvm/guests/${SG_PACKAGE}/guest_status
    typeset state
    typeset pid

    sg_log 5 "stop_command"

    state=`/opt/hpvm/bin/hpvmstatus -P ${SG_PACKAGE} -M | awk -F: '{print $11}'`
    if [[ ${state} = Off ]] ; then
	sg_log 0 "Virtual machine already stopped on this node"
	rtn_val=0
    else
        # First attempt to stop the virtual machine
	/opt/hpvm/bin/hpvmstop -F -g -P ${SG_PACKAGE}
	sleep  ${wait}
	
	if [[ -s ${gstat} ]] ; then
	    pid=`sed -n '1s/ .*//' ${gstat}`
            # Kill hpvm processes
	    if kill -0 ${pid} 2>/dev/null ; then
		kill ${pid} 2>/dev/null
		sleep ${wait}
                # Kill harder
		if kill -0 ${pid} 2>/dev/null ; then
		    kill -9 ${pid} 2>/dev/null
		    sleep ${wait}
		fi
	    fi
	    if kill -0 ${pid} 2>/dev/null ; then
		rtn_val=1
	    fi
	fi
    fi
    return ${rtn_val}
}

################
# main routine
################

sg_log 5 "customer defined script"

#########################################################################
#
# Customer defined external script must be specified with three required 
# entry points: start, stop, and validate. 
#
# It's not recommended to add additional entry points to the script
# due to potential name space collision with future Serviceguard releases.
#
#########################################################################

typeset -i exit_val=0

case ${1} in
     start)
           start_command $*
           exit_val=$?
           ;;

     stop)
           stop_command $*
           exit_val=$?
           ;;

     validate)
           validate_command $*
           exit_val=$?
           ;;

     *)    
           sg_log 0 "Unknown entry point $1"
           ;;
esac

exit $exit_val
