#!/bin/bash
#
# Resource script for haproxy daemon
#
# Description:  Manages haproxy daemon as an OCF resource in
#               an High Availability setup.
#
# HAProxy OCF script's Author: Andreas Grueninger
# License: BSD
# Copyright 2016 Andreas Grueninger, Grueninger GmbH, (grueni). All rights reserved.
#
#
#	usage: $0 {start|stop|status|monitor|validate-all|meta-data}
#
#	The "start" arg starts haproxy.
#
#	The "stop" arg stops it.
#
# OCF parameters:
# OCF_RESKEY_binpath
# OCF_RESKEY_conffile
# OCF_RESKEY_extraconf
#
# Note:This RA requires that the haproxy config files has a "pidfile"
# entry so that it is able to act on the correct process
##########################################################################
# Initialization:

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

USAGE="Usage: $0 {start|stop|status|monitor|validate-all|meta-data}";

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

usage()
{
	echo $USAGE >&2
}

meta_data()
{
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="haproxy">
<version>1.0</version>
<longdesc lang="en">
This script manages haproxy daemon
</longdesc>
<shortdesc lang="en">Manages an haproxy daemon</shortdesc>

<parameters>

<parameter name="binpath">
<longdesc lang="en">
The haproxy binary path.
For example, "/usr/sbin/haproxy"
</longdesc>
<shortdesc lang="en">Full path to the haproxy binary</shortdesc>
<content type="string" default="/usr/sbin/haproxy"/>
</parameter>

<parameter name="conffile">
<longdesc lang="en">
The haproxy daemon configuration file name with full path.
For example, "/etc/haproxy/haproxy.cfg"
</longdesc>
<shortdesc lang="en">Configuration file name with full path</shortdesc>
<content type="string" default="/etc/haproxy/haproxy.cfg" />
</parameter>

<parameter name="extraconf">
<longdesc lang="en">
Extra command line arguments to pass to haproxy.
For example, "-f /etc/haproxy/shared.cfg"
</longdesc>
<shortdesc lang="en">Extra command line arguments for haproxy</shortdesc>
<content type="string" default="" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="20s"/>
<action name="stop" timeout="20s"/>
<action name="monitor" depth="0" timeout="20s" interval="60s" />
<action name="validate-all" timeout="20s"/>
<action name="meta-data"  timeout="5s"/>
</actions>
</resource-agent>
END
exit $OCF_SUCCESS
}

get_pid_and_conf_file()
{
	if [ -n "$OCF_RESKEY_conffile" ]; then
		CONF_FILE=$OCF_RESKEY_conffile
	else
		CONF_FILE="/etc/haproxy/haproxy.cfg"
	fi

	PIDFILE="`grep -v \"#\" ${CONF_FILE} | grep \"pidfile\" | sed 's/^[ \t]*pidfile[ \t]*//'`"
	if [ "${PIDFILE}" = '' ]; then
		PIDFILE="/var/run/${OCF_RESOURCE_INSTANCE}.pid"
	fi
}

haproxy_status()
{
	if [ -n "$PIDFILE" -a -f "$PIDFILE" ]; then
		# haproxy is probably running
		PID=`cat $PIDFILE`
		if [ -n "$PID" ]; then
			if ps -p $PID | grep haproxy >/dev/null ; then
#				ocf_log info "haproxy daemon running"
				return $OCF_SUCCESS
			else
				ocf_log info "haproxy daemon is not running but pid file exists"
				return $OCF_NOT_RUNNING
			fi
		else
			ocf_log err "PID file empty!"
			return $OCF_ERR_GENERIC
		fi
	fi

	# haproxy is not running
	ocf_log info "haproxy daemon is not running"
	return $OCF_NOT_RUNNING
}

haproxy_start()
{
	# if haproxy is running return success
	haproxy_status
	retVal=$?
	if [ $retVal -eq $OCF_SUCCESS ]; then
		exit $OCF_SUCCESS
	elif [ $retVal -ne $OCF_NOT_RUNNING ]; then
		ocf_log err "Error. Unknown status."
		exit $OCF_ERR_GENERIC
	fi

	if [ -n "$OCF_RESKEY_binpath" ]; then
		COMMAND="$OCF_RESKEY_binpath"
	else
		COMMAND="/usr/sbin/haproxy"
	fi
	
	mkdir -p /var/run/haproxy
	$COMMAND $OCF_RESKEY_extraconf -D -f $CONF_FILE -p $PIDFILE;
	if [ $? -ne 0 ]; then
		ocf_log err "Error. haproxy daemon returned error $?."
		exit $OCF_ERR_GENERIC
	fi

	ocf_log info "Started haproxy daemon."
	exit $OCF_SUCCESS
}


haproxy_stop()
{
	if haproxy_status ; then
		PID=`cat $PIDFILE`
		if [ -n "$PID" ] ; then
			kill $PID
			if [ $? -ne 0 ]; then
				kill -SIGKILL $PID
				if [ $? -ne 0 ]; then
					ocf_log err "Error. Could not stop haproxy daemon."
					return $OCF_ERR_GENERIC
				fi
			fi
			rm $PIDFILE 2>/dev/null
		fi
	fi
	ocf_log info "Stopped haproxy daemon."
	exit $OCF_SUCCESS
}

haproxy_monitor()
{
	haproxy_status
}

haproxy_validate_all()
{
	if [ -n "$OCF_RESKEY_binpath" -a ! -x "$OCF_RESKEY_binpath" ]; then
		ocf_log err "Binary path $OCF_RESKEY_binpath does not exist."
		exit $OCF_ERR_ARGS
	fi
	if [ -n "$OCF_RESKEY_conffile" -a ! -f "$OCF_RESKEY_conffile" ]; then
		ocf_log err "Config file $OCF_RESKEY_conffile does not exist."
		exit $OCF_ERR_ARGS
	fi

	if  grep -v "^#" "$CONF_FILE" | grep "pidfile" > /dev/null ; then
		:
	else
		ocf_log err "Error. \"pidfile\" entry required in the haproxy config file by haproxy OCF RA."
		return $OCF_ERR_GENERIC
	fi

	return $OCF_SUCCESS
}


#
# Main
#

if [ $# -ne 1 ]; then
	usage
	exit $OCF_ERR_ARGS
fi

case $1 in
	start)	get_pid_and_conf_file
		haproxy_start
		;;

	stop)	get_pid_and_conf_file
		haproxy_stop
		;;

	status)	get_pid_and_conf_file
		haproxy_status
		;;

	monitor)get_pid_and_conf_file
		haproxy_monitor
		;;

	validate-all)	get_pid_and_conf_file
			haproxy_validate_all
			;;

	meta-data)	meta_data
			;;

	usage)	usage
		exit $OCF_SUCCESS
		;;

	*)	usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac