#!/bin/sh
#
#
#	Hobbit OCF RA. Start, stop, migrate and monitor hobbit
#

#######################################################################
# Initialization:

. ${OCF_ROOT}/resource.d/heartbeat/.ocf-shellfuncs

#######################################################################

meta_data() {
	cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="Hobbit" version="1.0">
<version>1.0</version>

<longdesc lang="en">
This is an agent for the hobbit monitoring system.
</longdesc>
<shortdesc lang="en">Hobbit resource agent</shortdesc>

<parameters>
<parameter name="bbhome" unique="1">
<longdesc lang="en">
Location of the bbhome-directory, default is /home/hobbit/server
</longdesc>
<shortdesc lang="en">BBHOME-Directory</shortdesc>
<content type="string" default="/home/hobbit/server" />
</parameter>

</parameters>

<actions>
<action name="start"        timeout="90" />
<action name="stop"         timeout="100" />
<action name="monitor"      timeout="20" interval="10" depth="0" start-delay="30" />
<action name="reload"       timeout="90" />
<action name="migrate_to"   timeout="100" />
<action name="migrate_from" timeout="90" />
<action name="meta-data"    timeout="5" />
<action name="verify-all"   timeout="30" />
</actions>
</resource-agent>
END
}

#######################################################################

# don't exit on TERM, to test that lrmd makes sure that we do exit
#trap sigterm_handler TERM
#sigterm_handler() {
#	ocf_log info "They use TERM to bring us down. No such luck."
#	return
#}

hobbit_usage() {
	cat <<END
usage: $0 {start|stop|monitor|migrate_to|migrate_from|validate-all|meta-data}

Expects to have a fully populated OCF RA-compliant environment set.
END
}

hobbit_start() {
    hobbit_monitor
    if [ $? =  $OCF_SUCCESS ]; then
	ocf_log info "Hobbit already running"
	return $OCF_SUCCESS
    fi
    su - hobbit -c "${OCF_RESKEY_bbhome}/hobbit.sh start"
    hobbit_monitor
    if [ $? =  $OCF_SUCCESS ]; then
	ocf_log info "Hobbit started"
	return $OCF_SUCCESS
    else
        ocf_log error "Unable to start hobbit"
	return $OCF_ERR_GENERIC
    fi
}

hobbit_stop() {
    hobbit_monitor
    if [ $? =  $OCF_SUCCESS ]; then
        su - hobbit -c "${OCF_RESKEY_bbhome}/hobbit.sh stop"
	hobbit_monitor
        if [ $? =  $OCF_SUCCESS ]; then
            ocf_log error "Hobbit NOT stopped!"
	    return $OCF_ERR_GENERIC
	else
	   ocf_log info "Hobbit stopped"
	   return $OCF_SUCCESS
	fi
    else
       ocf_log info "Hobbit already stopped"
       return $OCF_SUCCESS
    fi
}

hobbit_monitor() {
	# Monitor _MUST!_ differentiate correctly between running
	# (SUCCESS), failed (ERROR) or _cleanly_ stopped (NOT RUNNING).
	# That is THREE states, not just yes/no.
	su - hobbit -c "/home/hobbit/server/hobbit.sh status 2>&1" | /bin/grep -q "Hobbit (hobbitlaunch) running with PID"
        if [ $? = 0 ]; then
	    return $OCF_SUCCESS
	else
	    return $OCF_NOT_RUNNING
	fi
}

hobbit_validate() {
    
    state_dir=`dirname "$OCF_RESKEY_state"`
    touch "$state_dir/$$"
    if [ -x $OCF_RESKEY_bbhome/hobbit.sh ]; then
	return $OCF_SUCCESS
    else
       return $OCF_ERR_ARGS
    fi
}

: ${OCF_RESKEY_bbhome=/home/hobbit/server}

case $__OCF_ACTION in
meta-data)	meta_data
		exit $OCF_SUCCESS
		;;
start)		hobbit_start;;
stop)		hobbit_stop;;
monitor)	hobbit_monitor;;
migrate_to)	ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} to ${OCF_RESKEY_CRM_meta_migrate_to}."
	        hobbit_stop
		;;
migrate_from)	ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} to ${OCF_RESKEY_CRM_meta_migrated_from}."
	        hobbit_start
		;;
reload)		ocf_log err "Reloading..."
	        hobbit_start
		;;
validate-all)	hobbit_validate;;
usage|help)	hobbit_usage
		exit $OCF_SUCCESS
		;;
*)		hobbit_usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc

