#!/bin/sh

### BEGIN INIT INFO
# Provides:          cluster manager
# Required-Start:    $network $remote_fs
# Required-Stop:     $network $remote_fs
# Should-Start:      $named $time $syslog ssh
# Should-Stop:       $named $time $syslog ssh
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts and stops cman
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=cman
DESC="cluster manager"
CONF=/etc/cluster/cluster.conf

test -x /usr/sbin/cman_tool || exit 0

# ccsd

if [ -f /etc/defaults/cman ] ; then
	. /etc/defaults/cman
fi

[ -z "$CCSD_OPTS" ] && CCSD_OPTS=

# 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
[ -z "$CMAN_CLUSTER_TIMEOUT" ] && 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.
[ -z "$CMAN_QUORUM_TIMEOUT" ] && CMAN_QUORUM_TIMEOUT=0

# 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
[ -z "$CMAN_SHUTDOWN_TIMEOUT" ] && CMAN_SHUTDOWN_TIMEOUT=60

# FENCED_START_TIMEOUT -- amount of time to wait for starting fenced
#     before giving up.  If FENCED_START_TIMEOUT is positive, then we will
#     wait FENCED_START_TIMEOUT seconds before giving up and failing when
#     fenced does not start.  If FENCED_START_TIMEOUT is zero, then
#     wait indefinately for fenced to start.
[ -z "$FENCED_START_TIMEOUT" ] && FENCED_START_TIMEOUT=300

load_modules() {
	modprobe configfs 2>&1 || return 1
	modprobe dlm 2>&1 || return 1
	modprobe lock_dlm 2>&1 || return 1
}

mount_configfs() {
	if ! awk '{ print $2" "$3 }' /etc/mtab | grep "/sys/kernel/config configfs" > /dev/null ; then
		mount -t configfs none /sys/kernel/config
	fi
}

umount_configfs() {
	if awk '{ print $2" "$3 }' /etc/mtab | grep "/sys/kernel/config configfs" > /dev/null ; then
		umount /sys/kernel/config
	fi
}

check_cluster_conf() {

	if [ ! -e $CONF -o ! -s $CONF ] ; then
		echo ""
		echo "$CONF does not exist or is empty."
		echo "See /usr/share/doc/gfs-tools/examples/min-gfs.txt"
		echo "for instructions on how to setup a minimal cluster."
		echo "$DESC will not be started."
		exit 0
	fi

}

case "$1" in
  start)
	check_cluster_conf

	echo "Starting $DESC:"
	echo -n " Loading kernel modules:"
	load_modules
	echo " done"
	echo -n " Mounting config filesystem:"
	mount_configfs
	echo " done"
	echo -n " Starting cluster configuration system:"
	start-stop-daemon --start --quiet --pidfile /var/run/cluster/ccsd.pid --exec /usr/sbin/ccsd -- $CCSD_OPTS
	sleep 1
	echo " done"
	echo -n " Joining cluster:"
	cman_tool -t $CMAN_CLUSTER_TIMEOUT -w join $cman_join_opts
	echo " done"
	if [ $CMAN_QUORUM_TIMEOUT -gt 0 ]; then
		echo -n " Waiting quorum timeout ($CMAN_QUORUM_TIMEOUT seconds):"
		cman_tool -t $CMAN_QUORUM_TIMEOUT -q wait
		echo " done"
	fi
	echo -n " Starting daemons:"
	start-stop-daemon --start --quiet --pidfile /var/run/groupd.pid --exec /usr/sbin/groupd
	echo -n " groupd"
	start-stop-daemon --start --quiet --pidfile /var/run/fenced.pid --exec /usr/sbin/fenced
	echo -n " fenced"
	start-stop-daemon --start --quiet --pidfile /var/run/dlm_controld.pid --exec /usr/sbin/dlm_controld
	echo -n " dlm_controld"
	start-stop-daemon --start --quiet --pidfile /var/run/gfs_controld.pid --exec /usr/sbin/gfs_controld
	echo " gfs_controld"
	echo -n " Joining fence domain:"
	fence_tool -w -t $FENCED_START_TIMEOUT join
	echo " done"
	echo -n " Starting Quorum Disk daemon:"
	start-stop-daemon --start --quiet --pidfile /var/run/qdiskd.pid --exec /usr/sbin/qdiskd -- -Q || true
	echo " done"
	;;
  stop)
	echo "Stopping $DESC"
	echo -n " Stopping Quorum Disk daemon:"
	start-stop-daemon --stop --oknodo --quiet --pidfile /var/run/qdiskd.pid --exec /usr/sbin/qdiskd
	rm -f /var/run/qdiskd.pid
	echo " done"
	echo -n " Leaving fence domain:"
	fence_tool -w leave &
	for sec in $(seq 1 10); do
		if pidof fence_tool > /dev/null 2>&1; then
			if [ "$sec" = 10 ]; then
				kill $(pidof fence_tool) > /dev/null 2>&1
			else
				sleep 1
			fi
		fi
	done
	echo " done"
	echo -n " Stopping daemons:"
	start-stop-daemon --stop --oknodo --quiet --pidfile /var/run/gfs_controld.pid --exec /usr/sbin/gfs_controld
	rm -f /var/run/gfs_controld.pid
	echo -n " gfs_controld"
	start-stop-daemon --stop --oknodo --quiet --pidfile /var/run/dlm_controld.pid --exec /usr/sbin/dlm_controld
	rm -f /var/run/dlm_controld.pid
	echo -n " dlm_controld"
	start-stop-daemon --stop --oknodo --quiet --pidfile /var/run/fenced.pid --exec /usr/sbin/fenced
	rm -f /var/run/fenced.pid
	echo -n " fenced"
	start-stop-daemon --stop --oknodo --quiet --pidfile /var/run/groupd.pid --exec /usr/sbin/groupd
	rm -f /var/run/groupd.pid
	echo " groupd"
	echo -n " Leaving the cluster:"
	cman_tool -t $CMAN_SHUTDOWN_TIMEOUT -w leave || true
	echo " done"
	echo -n " Stopping cluster configuration system:"
	start-stop-daemon --stop --oknodo --quiet --pidfile /var/run/cluster/ccsd.pid --exec /usr/sbin/ccsd
	rm -f /var/run/cluster/$NAME.pid
	echo " done"
	echo -n " Unmounting config filesystem:"
	umount_configfs
	echo " done"
	;;
  restart)
	$0 stop 
	sleep 1
	$0 start
	;;
  remove)
	echo -n "Stopping $DESC, removing node from cluster"
	cman_tool leave remove
	echo "."
	;;
  force-reload)
	$0 force-stop
	sleep 1
	$0 start
	;;
  force-stop)
	echo -n "Stopping $DESC, force"
	cman_tool leave force
	echo "."
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|remove|restart|force-reload|force-stop}" >&2
	exit 1
	;;
esac

exit 0
