#!/sbin/sh
#
# @(#)pdbadmin 1.16 95/03/14 SMI
#
# 	Copyright (c) 1994 Sun Microsystems, Inc.
#
# pdbadmin - SPARCcluster PDB administration script.
#
# Usage:
#	pdbadmin {startnode|stopnode} clustname
#	pdbadmin reldisks clustname
#

prog=`basename $0`


B=/opt/SUNWcluster/bin
E=/etc/opt/SUNWcluster
V=/var/opt/SUNWcluster
PKGINFO=/bin/pkginfo
GREP=/bin/grep
SED=/bin/sed

# print usage and exit
usage() {
	echo "Usage:"
	echo "	$0 [-a] [-f] startnode [clustname]"
	echo "	$0 [-a] stopnode [clustname]"
	exit 2
}

# validate that the proper pdb packages have been installed
# before we try to start the cluster.
validate_installation(){

  bring_up="YES"
  req_pkgs="SUNWccm SUNWcmm SUNWdlm"

  for i in $req_pkgs 
  do
   pkgs=`${PKGINFO} | ${GREP} $i | ${SED} 's/.*\(SUNW.* \) .*$/\1/`
   if [ -z "$pkgs" ];then
     echo "Error: Required PDB package '$i' not installed!"
     bring_up="NO"
   fi
  done
   if [ "$bring_up" = "NO" ]; then
     echo "       Aborting cluster startup."
     exit 2
   fi
}

# lookup a value in the configuration file
enmatch() {
	${B}/cdbmatch $* ${cdbfile} || \
		(echo "enmatch" "cdbmatch $* ${cdbfile} failed" 1>&2; return 1) 
}

# find the nodeid of the local node
get_nodeids () {
	myuname=`uname -n`
	if [ `enmatch cluster.node.0.hostname` = ${myuname} ]
	then
		myid=0
		otherid=1
	elif [ `enmatch cluster.node.1.hostname` = ${myuname} ]
	then
		myid=1
		otherid=0
	else
		echo "Host ${myuname} is not a cluster member"
		exit 1
	fi
}

# get program options
set -- `getopt af $*`
if [ $? != 0 ]
then
        usage
fi
for i in  $*
do
        case $i in
        -a) async="-a"; shift ;;
        -f) forcestart="-f"; shift ;;
        --) shift; break;;
        esac
done

if [ $# -lt 1 ]; then
 echo "Missing required parameter!"
 usage
 exit 2
fi

cmd=$1
if [  ${cmd} != "startnode" -a ${cmd} != "stopnode" -a ${cmd} != "reldisks" ]
then
	echo "invalid command: ${cmd}"
	usage
	exit 2
fi

clustname=$2
if [ -z "$clustname" ]; then
	if [ -f ${E}/conf/default_clustername ]; then
		clustname=`cat ${E}/conf/default_clustername`
		echo "Assuming a default cluster name of ${clustname}"
	else
		usage
	fi
fi


cdbfile=${E}/conf/${clustname}.cdb

if [ ! -f $cdbfile ]; then
	echo "Error: Invalid cluster name: $clustname"
	echo "       The file \"$cdbfile\" was not found"
	usage
fi
# -f option is legal only with the 'startnode' command
if [ "${forcestart}" = -f -a ${cmd} != "startnode" ] ; then
	echo "Error: The -f option is only legal when used with the 'startnode' sub-command"
	usage
fi

# warn operator about the consequences of the -f option
if [ "${forcestart}" = -f ] ; then
	get_nodeids
	mynode=`eval enmatch cluster.node.${myid}.hostname`
	othernode=`eval enmatch cluster.node.${otherid}.hostname`
	qctl=`eval enmatch ctlreserve.node.${myid}.quorumctl`

	# XXX - allow running a customized script here

	echo "=========================== WARNING ================================="
	echo "=      Multiple Failures have been detected in this cluster         ="
	echo "====================================================================="
	echo
	echo "You are attempting to start up the cluster node '${mynode}' using"
	echo "the -f option.  The -f option allows '${mynode}' to come online when"
	echo "both the node '${othernode}' and the quorum controller " \
		 "'${qctl:-UNKNOWN}' are not reachable."
	echo "This action could corrupt the database and/or otherwise compromise"
	echo "cluster integrity if used incorrectly. Please read the following"
	echo "instructions carefully and refer to XXX documentation."
	echo 
	echo "Before you proceed, you must verify that:"
	echo "	(1) that node '${othernode}' is offline (i.e. halted)"
	echo "	(2) the quorum control is offline (i.e. power is off)"
	echo
	echo "Note that you *must* bring the node '${mynode}' offline before"
	echo "restarting the quorum controller or the node '${othernode}'." \
		" This can be"
	echo "done by executing the command"
	echo
	echo "	/opt/SUNWcluster/bin/pdbadmin stopnode ${clustname}"
	echo
	echo "Please enter \"yes\" only after you have verified (1) and (2)."
	echo "Do you want to continue? \c"

	while read reply
	do
		case $reply in
			yes | YES)
				break ;;
			no | NO)
				echo "exiting..." ; exit 0 ;;
			*)
				echo "Please enter yes or no: \c"
		esac
	done
fi

# dispatch into reconf_ener

validate_installation

case ${cmd} in
	startnode | stopnode)
		${B}/reconf_ener ${forcestart} ${async} ${cmd} ${clustname} || \
			(echo "$0: errors encountered."; \
			exit 1) || exit 1;;
	reldisks)
		${B}/reconf_ener ${cmd} ${clustname} || \
			(echo "$0: errors encountered."; \
			exit 1) || exit 1;;
	*)
		usage; exit 1 ;;
esac
exit 0
