#!/bin/bash
#
# chkconfig: 345 21 79
# description: Starts and stops cman
#
#	       
### BEGIN INIT INFO
# Provides: 
### END INIT INFO

# CMAN_CLUSTER_TIMEOUT -- amount of time to wait for joinging a cluster
#     before giving up.  If CMAN_CLUSTER_TIMEOUT is positive, then we will
#     wait CMAN_CLUSTER_TIMEOUT seconds before giving up and failing when
#     a cluster is not joined.  If CMAN_CLUSTER_TIMEOUT is zero, then
#     wait indefinately for a cluster join.  If CMAN_CLUSTER_TIMEOUT is
#     negative, do not check to see that the cluster has been joined
CMAN_CLUSTER_TIMEOUT=120

# CMAN_QUORUM_TIMEOUT -- amount of time to wait for a quorate cluster on 
#     startup quorum is needed by many other applications, so we may as 
#     well wait here.  If CMAN_QUORUM_TIMEOUT is less than 1, quorum will 
#     be ignored.
CMAN_QUORUM_TIMEOUT=300

# CMAN_SHUTDOWN_TIMEOUT -- amount of time to wait for cman to become a 
#     cluster member before calling cman_tool leave during shutdown.  
#     default is 60 seconds
CMAN_SHUTDOWN_TIMEOUT=60

. /etc/init.d/functions
[ -f /etc/sysconfig/cluster ] && . /etc/sysconfig/cluster

LOCK_FILE="/var/lock/subsys/cman"

[ -n "$CLUSTERNAME" ] && cman_join_opts="-c $CLUSTERNAME"

in_cluster()
{
	grep -q "Cluster Member: Yes" /proc/cluster/status
}

start()
{
	# If gulm is in ccs, don't start cman
	# FIXME -- Should this be silent?  I think users should get some
	#          feedback, but others might not want added verbosity to
	#          the boot process.  Oh well... it's only one line :)
	if grep -qE "<[[:space:]]*gulm([[:space:]]|[>]|$)" /etc/cluster/cluster.conf
	then
		echo "<gulm> section detected in /etc/cluster/cluster.conf"
		return 1
	fi


	echo -n "Starting cman:"
	rtrn=1

	for try in block
	do
		# load the camn module (modprobe won't error if the modules is already
		# loaded
		modprobe cman || break
		
		# TODO -- configure tunable paramters?
		# [ -n "$CMAN_TRANSITION_RESTARTS" ] && 
		#	echo $CMAN_TRANSITION_RESTARTS > /proc/cluster/config/cman/transition_restarts

		if in_cluster
		then
			rtrn=0
			break
		fi

		# specify -w to make sure we have joined the cluster
		# TODO -- should we call cman_tool leave if this times out?
		cman_tool -t $CMAN_CLUSTER_TIMEOUT -w join $cman_join_opts || break

		# make sure that we are quorate?  
		if [ $CMAN_QUORUM_TIMEOUT -gt 0 ]
		then
			cman_tool -t $CMAN_QUORUM_TIMEOUT -q wait || break
		fi

		rtrn=0
	done

	if [ $rtrn -eq 0 ]
	then
		# try to load the dlm module
		modprobe dlm &>/dev/null
		success
	else
		failure
	fi
		
	# need the extra echo to properlly terminate the line
	echo
	return $rtrn
}

stop()
{
	echo -n "Stopping cman:"
	rtrn=0
	if [ -r /proc/cluster/status ]
	then
		rtrn=1

		cman_tool -t $CMAN_SHUTDOWN_TIMEOUT -w leave 

		# allow cman time to clean up BZ 149282
		sleep 3

		# try to unload dlm module
		modprobe -r dlm &>/dev/null
 
		modprobe -r cman && rtrn=0
	fi
	
	if [ $rtrn -eq 0 ]
	then
		success
	else
		failure
	fi
		
	# need the extra echo to properlly terminate the line
	echo
	return $rtrn
}

rtrn=1

# See how we were called.
case "$1" in
  start)
	start
	rtrn=$?
	[ $rtrn = 0 ] && touch $LOCK_FILE
	;;

  stop)
	stop
	rtrn=$?
	[ $rtrn = 0 ] && rm -f $LOCK_FILE
	;;

  restart)
	$0 stop
	$0 start 
	rtrn=$?
	;;

  status)
	cat /proc/cluster/status
	rtrn=0
	;;

  *)
	echo $"Usage: $0 {start|stop|restart|status}"
	;;
esac

exit $rtrn
