#!/bin/bash
#
# Corosync daemon init script for LSB-compliant Linux distributions.
#
# openais       Start the openais (corosync) cluster service
#
# chkconfig: - 20 20
# processname:  corosync
# pidfile:      /var/run/corosync.pid
# description:  Corosync/OpenAIS
### BEGIN INIT INFO
# Description: Manages the openais cluster services.
#
# Short-Description: openais cluster services.
# Provides: openais
# Required-Start: $network
# Should-Start: $syslog sshd drbd $named $remote_fs logd xendomains xend iscsi libvirtd portmap rpcbind
# Required-Stop: $network
# Default-Start: 3 5
# Default-Stop: 0 6
# Should-Stop: $syslog sshd drbd $named $remote_fs logd xendomains xend iscsi libvirtd portmap rpcbind
### END INIT INFO

START_ON_BOOT=Yes
do_force=0
prog="corosync"
exec="/usr/sbin/corosync"
lockfile="/var/lock/subsys/$prog"

OPENAIS_SYSCONFIG=/etc/sysconfig/openais
if [ -f $OPENAIS_SYSCONFIG ]; then
	. $OPENAIS_SYSCONFIG
fi
PACEMAKER_SYSCONFIG=/etc/sysconfig/pacemaker
if [ -f $PACEMAKER_SYSCONFIG ]; then
        . $PACEMAKER_SYSCONFIG
fi

SBD_CONFIG=/etc/sysconfig/sbd
SBD_BIN="/usr/sbin/sbd"
if [ -f $SBD_CONFIG ]; then
        . $SBD_CONFIG
fi

[ -x "$exec" ] || exit 0

SBD_DEVS=${SBD_DEVICE%;}
SBD_DEVICE=${SBD_DEVS//;/ -d }

: ${SBD_DELAY_START:="no"}

env_is_true() {
	case "$1" in
	yes|true|1|YES|TRUE|ja|on|ON) true ;;
	*)	false ;;
	esac
}

StartSBD() {
        test -x $SBD_BIN || return
        if [ -n "$SBD_DEVICE" ]; then
		if ! pidofproc $SBD_BIN >/dev/null 2>&1 ; then
			echo -n "Starting SBD - "
			if ! $SBD_BIN -d $SBD_DEVICE -D $SBD_OPTS watch ; then
				echo "SBD failed to start; aborting."
				exit 1
			fi
			if env_is_true ${SBD_DELAY_START} ; then
				sleep $(sbd -d "$SBD_DEVICE" dump | grep -m 1 msgwait | awk '{print $4}') 2>/dev/null
			fi
		fi
        fi
}

StopSBD() {
        test -x $SBD_BIN || return
        if [ -n "$SBD_DEVICE" ]; then
		echo -n "Stopping SBD - "
                if ! $SBD_BIN -d $SBD_DEVICE -D $SBD_OPTS message LOCAL exit ; then
			echo "SBD failed to stop; aborting."
			exit 1
                fi
        fi
	while pidofproc $SBD_BIN >/dev/null 2>&1 ; do
		sleep 1
	done
	echo -n "done "
}

export_pacemaker_config() {
	if [ -n "$LRMD_MAX_CHILDREN" ]; then
	    export LRMD_MAX_CHILDREN
	fi
}

internal_status() {
    checkproc $exec > /dev/null 2>&1
    return $?
}

status() {
	if internal_status; then
		echo "Running"
		return 0
    else
		echo "Stopped"
		return 7
	fi
}

start() {
	start_on_boot=$(echo "$START_ON_BOOT" | tr '[A-Z]' '[a-z]')
	#check whether it is run from runlevel or manually
	runlevel="yes"
	base=${0##*/}
	link=${base#*[SK][0-9][0-9]}
	if [ $link == $base ] ; then
		runlevel="no"
	fi

	if [ $start_on_boot == "no" ] && [ $runlevel == "yes" ]; then
		echo "Not starting openais because it was manually disabled"
		return 1
	fi

    export COROSYNC_DEFAULT_CONFIG_IFACE
    : ${COROSYNC_DEFAULT_CONFIG_IFACE="openaisserviceenableexperimental:corosync_parser"}
    echo -n $"Starting OpenAIS/Corosync daemon ($prog): "
    if ! internal_status; then
	StartSBD
	export_pacemaker_config
	echo -n "starting... "
	startproc $exec
    fi

    sleep 2 # give it time to fail... $? isn't definitive

    if internal_status; then
	echo "OK"
    touch "$lockfile"
	return 0
    fi

    echo "Failed"
    return 1
}

do_force=0

stop() {
    local escalation_delay=20
    local iter=0
    local stopsbd=1
    local sig=TERM

    echo -n $"Stopping OpenAIS/corosync daemon ($prog): "

    while internal_status; do
        c_pid="`pidofproc $exec`"
        if [ $iter -eq $escalation_delay -a $do_force = 1 ]; then
            echo -n " Escalating... "
            sig=KILL
            # Do not stop SBD if we forcibly killed corosync; other
            # nodes may still have a legitimate desire to STONITH us.
            stopsbd=0
        fi
        kill -$sig $c_pid
        iter=$[iter+1]

        echo -n "."
        sleep 2
    done

    if [ $stopsbd = 1 ]; then
    	StopSBD
    fi

    rm -f "$lockfile"
    echo "OK"
    return 0
}

restart() {
    stop
    start
}

case "$1" in
    start|stop|restart)
        $1
        ;;
    force-start)
	SBD_OPTS="$SBD_OPTS -S 0"
	start
	;;
    force-stop)
	do_force=1
        stop
        ;;
    reload|force-reload)
        restart
        ;;
    condrestart|try-restart)
        [ ! -f "$lockfile" ] || restart
        ;;
    status)
        status $prog
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|try-restart|condrestart|reload|force-reload|force-start|force-stop|status}"
        exit 2
esac
