#!/bin/sh
#
#
#	Haproxy Backend Server OCF RA enables and disables individual servers
#	of haproxy backend. It is intended for usage when pacemaker is
#	used as a replacement for haproxy monitor functions
#
# Copyright (c) 2004 Hosting90 Systems s.r.o, Jiri Lunacek
#                    All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like.  Any license provided herein, whether implied or
# otherwise, applies only to this software file.  Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#

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

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

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

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

<longdesc lang="en">

</longdesc>
<shortdesc lang="en">HA proxy backend server</shortdesc>

<parameters>
<parameter name="socket" unique="1">
<longdesc lang="en">
Location of haproxy socket.
</longdesc>
<shortdesc lang="en">Socket file</shortdesc>
<content type="string" default="/var/run/haproxy.sock" />
</parameter>
<parameter name="server_id" unique="1" required="1">
<longdesc lang="en">
Server identification in form backend/server
</longdesc>
<shortdesc lang="en">Server ID</shortdesc>
</parameter>

</parameters>

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

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

haproxy_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
}

haproxy_start() {
    haproxy_monitor
    if [ $? =  $OCF_SUCCESS ]; then
	return $OCF_SUCCESS
    fi
    RESULT=`echo "enable server ${OCF_RESKEY_server_id}" | socat stdio ${OCF_RESKEY_socket} | head -n1 | tr -d '[:space:]'`
	if [ -z "$RESULT" ]; then
		return $OCF_SUCCESS
	else
		return $OCF_ERR_GENERIC
	fi
}

haproxy_stop() {
    haproxy_monitor
    if [ $? =  $OCF_NOT_RUNNING ]; then
	return $OCF_SUCCESS
    fi
    RESULT=`echo "disable server ${OCF_RESKEY_server_id}" | socat stdio ${OCF_RESKEY_socket} | head -n1 | tr -d '[:space:]'`
	if [ -z "$RESULT" ]; then
		return $OCF_SUCCESS
	else
		return $OCF_ERR_GENERIC
	fi
}

haproxy_monitor() {
	# Monitor _MUST!_ differentiate correctly between running
	# (SUCCESS), failed (ERROR) or _cleanly_ stopped (NOT RUNNING).
	# That is THREE states, not just yes/no.
	
	if [ -z "`echo "show stat" | socat stdio ${OCF_RESKEY_socket} | grep BACKEND`" ]; then
		return $OCF_NOT_RUNNING
	fi
	
	SRV_STATUS=`echo "show stat" | socat stdio ${OCF_RESKEY_socket} | egrep "^$(echo ${OCF_RESKEY_server_id} | sed 's/\//,/'),"`
	if [ -z "$SRV_STATUS" ]; then
		return $OCF_ERR_CONFIGURED
	fi
	
	if echo "$SRV_STATUS" | grep -q -e ',UP' -e ',DOWN'; then
	    return $OCF_SUCCESS
	elif echo "$SRV_STATUS" | grep -q ',MAINT'; then
		return $OCF_NOT_RUNNING
	else
		ocf_exit_reason "Server status: $SRV_STATUS"
		return $OCF_ERR_GENERIC
	fi
}

haproxy_validate() {
	# TODO validate parameters, socket exists?

    return $OCF_SUCCESS
}

: ${OCF_RESKEY_state=${HA_RSCTMP}/Dummy-${OCF_RESOURCE_INSTANCE}.state}
: ${OCF_RESKEY_fake="dummy"}

case $__OCF_ACTION in
meta-data)	meta_data
		exit $OCF_SUCCESS
		;;
start)		haproxy_start;;
stop)		haproxy_stop;;
monitor)	haproxy_monitor;;
migrate_to)	ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} to ${OCF_RESKEY_CRM_meta_migrate_target}."
	        haproxy_stop
		;;
migrate_from)	ocf_log info "Migrating ${OCF_RESOURCE_INSTANCE} from ${OCF_RESKEY_CRM_meta_migrate_source}."
	        haproxy_start
		;;
reload)		ocf_log info "Reloading ${OCF_RESOURCE_INSTANCE} ..."
		;;
validate-all)	haproxy_validate;;
usage|help)	haproxy_usage
		exit $OCF_SUCCESS
		;;
*)		haproxy_usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac
rc=$?
ocf_log debug "${OCF_RESOURCE_INSTANCE} $__OCF_ACTION : $rc"
exit $rc

