#!/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()
{
	echo -n "Starting cman:"

	# 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 ! [ -r /etc/cluster/cluster.conf ]
	then
		# TODO -- cman can start w/out cluster.conf file.  This 
		#	  should not stop cman from starting up.
		# FIXME -- don't use echo.  but initlog is now deprecated
		# so i need to find a better mechanism for error reporting
		echo "/etc/cluster/cluster.conf was not detected"
	elif grep -qE "<[[:space:]]*gulm([[:space:]]|[>]|$)" \
		/etc/cluster/cluster.conf 
	then
		warning "Skipping because of <gulm> section detected in " \
			"/etc/cluster/cluster.conf"
		echo
		exit 0
	fi

	rtrn=1

	for try in block
	do
		# load the camn module (modprobe won't error if the modules 
		# is already loaded
		errmsg=$(modprobe cman 2>&1) || 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?
		errmsg=$(cman_tool -t $CMAN_CLUSTER_TIMEOUT -w join \
			$cman_join_opts 2>&1) || break

		# make sure that we are quorate?  
		if [ $CMAN_QUORUM_TIMEOUT -gt 0 ]
		then
			errmsg=$( cman_tool -t $CMAN_QUORUM_TIMEOUT \
				-q wait 2>&1 ) || break
		fi

		rtrn=0
	done

	if [ $rtrn -eq 0 ]
	then
		# try to load the dlm module
		modprobe dlm &>/dev/null
		success
		echo
	else
		failure "$errmsg"
		echo
	fi
		
	# need the extra echo to properlly terminate the line
	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 &> /dev/null

		# allow cman time to clean up BZ 149282
		sleep 3

		# try to unload dlm module
		modprobe -r dlm &>/dev/null
 
		modprobe -r cman &>/dev/null && rtrn=0
	fi
	
	if [ $rtrn -eq 0 ]
	then
		success
		echo
	else
		failure "failed to stop cman"
		echo
	fi
		
	# need the extra echo to properlly terminate the line
	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
