#!/bin/sh
#
# evms_failover
#       Description: Manages Private containers on shared storage media.
#  	Original Author: Ram Pai (linuxram@us.ibm.com)
#       Support: evms-devel@lists.sourceforge.net
#
# usage: evms_failover <container> {start|stop|status}
#		<container> : name of private container
#
# Since the evms tool requires cluster-wide locks, it is best so configure
# heartbeat so that all evms_failover resource calls are serialized.
# evms calls will be re-tried up to 6 times, with a random sleep interval
# between 0 and 40 seconds. This should help ensure that the resource doesn't
# fail due to other script invocations on other nodes.
# This means that the timeout setting for this resource should be _at least_
# 120 seconds, but better around 200. It is not a good idea for heartbeat 
# to interrupt this script, as this may lead to evms processes left behind.
#

. /etc/ha.d/shellfuncs

usage() {
cat <<-EOT;
	usage: $0 <container> {start|stop|status}
		<container> : name of private container to be imported
EOT
}

#attempt to make it as much LSB compliant as possible
LSB_STATUS_OK=0
LSB_STATUS_STOPPED=3
LSB_EXIT_OK=0
LSB_EXIT_NOTRUNNING=7
LSB_EXIT_GENERIC=1
LSB_EXIT_EINVAL=2
CSMDEVNODES=/dev/evms/.nodes

container=$1
if [ -z $container ]; then
	usage
	exit 1
fi


evms_report_status()
{
	if [ -d $CSMDEVNODES/$1 ]; then
		return 0
	fi
	return 1
}

evms_import()
{
	#modify the container
	ret=1
	for (( cnt=1; cnt<7; cnt++ )) ; do
		evms -b <<-EOF >/dev/null 2>&1
			modify:"$1",node="$HOSTNAME",type=private
			save
			exit
			EOF
		if [ -d $CSMDEVNODES/$1 ]; then
			ret=0
			break
		fi
		if [ $cnt -lt 6 ]; then
			sleep $(($RANDOM % 40))
		fi
	done
	return $ret
}

evms_discover()
{
	#discover the container
	for (( cnt=1; cnt<7; cnt++ )) ; do
		evms -b <<-EOF >/dev/null 2>&1
			exit
			EOF
		ret=$?
		if [ $ret -eq 0 ]; then
			break
		fi
		if [ $cnt -lt 6 ]; then
			sleep $(($RANDOM % 40))
		fi
	done
	return $ret
}

evms_deport()
{
	#deport the container
	ret=1
	for (( cnt=1; cnt<7; cnt++ )) ; do
		evms -b <<-EOF >/dev/null 2>&1
			modify:"$1",type=deported
			save
			exit
			EOF
		if [ ! -d $CSMDEVNODES/$1 ]; then
			ret=0
			break
		fi
		if [ $cnt -lt 6 ]; then
			sleep $(($RANDOM % 40))
		fi
	done
	return $ret
}


evmsunlock()
{
	rm -f $1
}

#ensure ccm has a stable membership.
#we dont want to run evms command when the
#membership is unstable.
evmsccm
if [ $? -ne 0 ];then
	exit $LSB_EXIT_NOTRUNNING
fi

ret=$LSB_EXIT_OK


# Look for the 'start', 'stop' or 'status' argument
case "$2" in
#
# START: Import the container
#
start)
	# import the container
	evms_report_status $1
	if [ $? -ne 0 ];then
		evms_import $1 && evms_report_status $1
		if [ $? -eq 0 ]; then
			ret=$LSB_EXIT_OK
		else
			ha_log "WARNING: Container $container failed to $2"
			ret=$LSB_EXIT_NOTRUNNING
		fi
	fi
	;;

#
# STOP: Release the container
#
stop)
	# deport the container
	evms_report_status $1
	if [ $? -eq 0 ];then
		evms_deport $1 && evms_report_status $1
		if [ $? -ne 0 ]; then
			ret=$LSB_EXIT_OK
		else
			ha_log "WARNING: Container $container failed to $2"
			ret=$LSB_EXIT_GENERIC
		fi
	fi
	;;

#
# STATUS: is the container already imported?
#
status)
	#  is the container owned by us?
	evms_report_status $1
	if [ $? -eq 0 ] ; then
		ha_log "Container $container is already running ( OK )"
		echo  "Container $container is already running ( OK )"
		ret=$LSB_STATUS_OK
	else
		ret=$LSB_STATUS_STOPPED
	fi
	;;

#
# STARTSPECIAL: is called only by the evmd daemon.
#
startspecial)
	evms_discover
	if [ $? -eq 0 ] ; then
		ret=$LSB_STATUS_OK
	else
		ret=$LSB_EXIT_GENERIC
	fi
	;;

*)
	echo "This script should be run with a second argument of "
	echo " 'start', 'stop', or 'status' "
	usage
	ret=$LSB_EXIT_EINVAL
	;;
esac

exit $ret
