#!/bin/bash
#
# This script is inteded to be used as resource script by heartbeat
#
# Copright 2003-2008 LINBIT Information Technologies
# Philipp Reisner, Lars Ellenberg
#
###

DEFAULTFILE="/etc/default/drbd"
DRBDADM="/sbin/drbdadm"

if [ -f $DEFAULTFILE ]; then
  . $DEFAULTFILE
fi

if [ "$#" -eq 2 ]; then
  RES="$1"
  CMD="$2"
else
  RES="all"
  CMD="$1"
fi

## EXIT CODES
# since this is a "legacy heartbeat R1 resource agent" script,
# exit codes actually do not matter that much as long as we conform to
#  http://wiki.linux-ha.org/HeartbeatResourceAgent
# but it does not hurt to conform to lsb init-script exit codes,
# where we can.
#  http://refspecs.linux-foundation.org/LSB_3.1.0/
#	LSB-Core-generic/LSB-Core-generic/iniscrptact.html
####

case "$CMD" in
    start)
	# try several times, in case heartbeat deadtime
	# was smaller than drbd ping time
	try=6
	while true; do
		$DRBDADM primary $RES && break
		let "--try" || exit 1 # LSB generic error
		sleep 1
	done
	;;
    stop)
	$DRBDADM secondary $RES
	ex=$?
	case $ex in
	0)
		exit 0
		;;
	11)
		# see drbdadm_main.c adm_generic and m_system
		# as well as drbdsetup.c:
		# in fact a role change was attempted, but failed.
		echo >&2 "$DRBDADM secondary $RES: exit code $ex, mapping to 1"
		exit 1 # LSB generic error
		;;
	*)
		# other error, may be syntax error in config file,
		# anything else: to not confuse heartbeat further,
		# and avoid reboot due so "failed stop recovery",
		# pretend that we succeeded in stopping this.
		echo >&2 "$DRBDADM secondary $RES: exit code $ex, mapping to 0"
		exit 0
		;;
	esac
	;;
    status)
	if [ "$RES" = "all" ]; then
	    echo "A resource name is required for status inquiries."
	    exit 10
	fi
	ST=$( $DRBDADM role $RES )
	STATE=${ST%/*}
	case $STATE in
		Primary)
			echo "running (Primary)"
			exit 0 # LSB status "service is OK"
			;;
		Secondary|Unconfigured)
			echo "stopped ($STATE)" ;;
		"")
			echo "stopped" ;;
		*)
			# unexpected. whatever...
			echo "stopped ($ST)" ;;
	esac
	exit 3 # LSB status "service is not running"
	;;
    *)
	echo "Usage: drbddisk [resource] {start|stop|status}"
	exit 1
	;;
esac

exit 0
