#!/usr/bin/sh

#pbx_hacmp_config.sh
#
# HACMP configuration script for PBX 
#

# Set up script environment

#PBX root path.
PBX_ROOT=/opt/VRTSpbx/
PBX_CLUSTER_PATH=${PBX_ROOT}/bin/cluster
RSP=${PBX_CLUSTER_PATH}/PBX_RSP
ROLLBACK_CMDFILE=${PBX_CLUSTER_PATH}/rollback.sh
STAGE2_CMDFILE=${PBX_CLUSTER_PATH}/hacmp_postconfig.sh
host_name=`hostname`


verify_cluster() {
##############################################################################
# 
# Verify/synchronize the cluster, then block until cldump indicates
# that the cluster is in a stable state.
#
   [[ "$SCRIPT_DEBUG" = "true" ]] && set -x

   cldare -rt -V 'normal' -C 'interactive'
   echo "Waiting for cluster to stabilize...\n"

   sleep 10
   STABLE="NOT"
   until [[ "$stable" = "STABLE" ]] ; do
      sleep 2
      stable=`cldump |awk '/Cluster Substate:/ { print $3 }'`
      echo "Cluster Substate = $stable"
   done
}

##############################################################################
#
# Roll back config changes. (TODO - get this working!)
#
rollback_config() {
        [[ "$SCRIPT_DEBUG" = "true" ]] && set -x
        /usr/es/sbin/cluster/utilities/clRGmove -s 'false'  -d -i -g 'pbx_group' -n 'blazer'
	/usr/es/sbin/cluster/utilities/clRGmove -s 'false'  -d -i -g 'pbx_group' -n 'cavalier'
        /usr/es/sbin/cluster/utilities/clrmserv 'pbx_server'
        /usr/es/sbin/cluster/utilities/clrmnode  -a 'chevyserv1'
        /usr/es/sbin/cluster/utilities/clrmgrp -g 'pbx_group'
}


check_hacmp_version() {
##############################################################################
#
# Check that a supported version of HACMP software is installed and that this 
# node is a member of an HACMP cluster. 
#
   [[ "$SCRIPT_DEBUG" = "true" ]] && set -x

   hacmpver=`lslpp -q -c -L 'cluster.*.server.rte' 2>/dev/null |  cut -f3 -d':'`

   if [[ -z ${hacmpver} ]] ; then
        echo "***** HACMP not configured on this system! *****\n"
        echo ""
        exit 1
   fi

   echo "HACMP version is : $hacmpver \n"

   if [[ "`/usr/es/sbin/cluster/utilities/clresactive -V $MIN_HACMP_VERSION`" = "no" ]] ; then
	echo "***** Warning: supported HACMP version is : $MIN_HACMP_VERSION\n"
	resp=0
	until [ ${resp} = "y" -o ${resp} = "n" ] ; do
		#echo "Do you want to continue with configuration (y/n)? : \c "
		resp="y"
		if [ ${resp} = "n" ] ; then
			exit 1
		fi
	done
   fi
}

configure_resgrp() {
###############################################################################
#
# configure the resource group for PBX. This will be called early 
# in the configuration to allow us to use hacmp utilities to check 
# resource availability, etc. Resources are not configured into the group
# until later.
# On entry : 
# On Exit: $PBX_GROUP = resource group name
#          $PBX_NODES = node list on which resource group is configured.
#
   [[ "$SCRIPT_DEBUG" = "true" ]] && set -x

   valid_entry=false
  # until [[ "$valid_entry" = "true" ]] ; do

      #PBX_GROUP="pbx_group"_$host_name
      valid_entry=true
      #echo "Enter PBX HACMP resource group name [${PBX_GROUP}]: \c"
      #read TMP_PBX_GROUP

      #if [ -n "${TMP_PBX_GROUP}" ]; then
      #   PBX_GROUP=${TMP_PBX_GROUP}
      #fi


      if [[  -n "`cllsgrp 2>/dev/null |grep -w ${PBX_GROUP}`" ]] ; then
	   echo "***** WARNING - HACMP Resource Group ${PBX_GROUP} already exists. Configuration must be to a non-preexisting resource group\n "
	   valid_entry=false
      fi

      if [[ "$valid_entry" = "true" ]] ; then 
      #
      #	- ok so far. now get node list.
      #
         nodelist=`/usr/es/sbin/cluster/utilities/clnodename|sort -u |awk '{ printf $1 " "} END { printf "\n" }'`
	 valid_nodelist=""
         PBX_NODES=""
   
        # echo "Enter the list of nodes :[${nodelist}] \c"

         #read TMP_PBX_NODES
	 [[ -z ${TMP_PBX_NODES} ]] && TMP_PBX_NODES=$nodelist

         if [ -n "${TMP_PBX_NODES}" ]; then
           ok_nodes=true
           for chk in $TMP_PBX_NODES ; do
   	       ok_thisnode=false
   	       for chknode in $nodelist ; do
                 if [ "$chk" = "${chknode}" ] ; then
                    ok_thisnode=true
                    if [ $? -eq 0 ] ; then
                       #The local host was found in the list. Move it to the
		       #front to avoid complicating the configuration process
		       #later on.
		       PBX_NODES=${chk}
                    else
                       valid_nodelist="${valid_nodelist} ${chk}"
                    fi
		 fi
   	       done
   	       if [[ "$ok_thisnode" = "false" ]] ; then
                 ok_nodes=false
   	         echo "***** WARNING - node $chk does not appear to be valid. ***** \n"
   	       fi
   	   done
	   if [ -z "${PBX_NODES}" ]; then
		echo "***** WARNING - the local node does not appear to be on the list"
                echo "          this may prevent this script from completing the"
                echo "          configuration process. *****\n"
		ok_nodes=false
	   fi
   	   if [[ "$ok_nodes" = "false" ]]; then
   	      #echo "Do you want to continue (y/n)?: \c"
              resp="y"
              [[ "$resp" != "y" ]] && valid_entry=false
           fi
           PBX_NODES="${PBX_NODES} ${valid_nodelist}"
         fi
      fi

      if [[ "$valid_entry" = "true" ]] ; then

     	   echo "********************************************************************"
     	   echo "HACMP PBX Resource Group name: ${PBX_GROUP}"
	   echo "HACMP PBX Application Server name: ${PBX_SERVER}"
	   echo "Nodes configured for ${PBX_GROUP}: ${PBX_NODES}"
	   echo "********************************************************************\n"
	#   echo "Do you want to continue (y/n)? \c"
	#   read resp
	#   [[ "$resp" != "y" ]] && valid_entry=false
      fi

   #done

   /usr/es/sbin/cluster/utilities/claddgrp  -s ignore  -g "$PBX_GROUP" -n "$host_name" -S 'OHN' -O 'FNPN' -B 'FBHPN'
   #/usr/es/sbin/cluster/utilities/claddgrp  -s ignore  -g "$PBX_GROUP" -n "$PBX_NODES" -S 'OAAN' -O 'BO' -B 'NFB'
   echo clrmgrp -g $PBX_GROUP >> ${ROLLBACK_CMDFILE}

}

configure_pbxserv() {
###############################################################################
#
# Get name for PBX application server to HACMP config. Note that 
# we don't configure this into the pbx resource group until after this script 
# has exited and the database setup, etc has been performed. This is
# to allow the pbx resource group to be brought online without starting 
# PBX.
#
# on entry: 
# on exit : $PBX_SERVER contains the name of the PBX application server
#

   [[ "$SCRIPT_DEBUG" = "true" ]] && set -x

   #echo "Enter HACMP PBX application server [${PBX_SERVER}]: \c"
   #read TMP_PBX_SERVER

   #if [ -n "${TMP_PBX_SERVER}" ]; then
   #     PBX_SERVER=${TMP_PBX_SERVER}
   #fi

   /usr/es/sbin/cluster/utilities/claddserv -s${PBX_SERVER} -b'/opt/VRTSpbx/bin/cluster/hacmp_online' -e'/opt/VRTSpbx/bin/cluster/hacmp_offline'
   /usr/es/sbin/cluster/utilities/claddappmon MONITOR_TYPE=user name=${PBX_SERVER} RESOURCE_TO_MONITOR=${PBX_SERVER} INVOCATION='longrunning' MONITOR_METHOD='/opt/VRTSpbx/bin/cluster/hacmp_monitor' MONITOR_INTERVAL='30' STABILIZATION_INTERVAL='200' RESTART_COUNT='3' FAILURE_ACTION='notify' NOTIFY_METHOD='/opt/VRTSpbx/bin/cluster/hacmp_offline' CLEANUP_METHOD='/opt/VRTSpbx/bin/cluster/hacmp_offline' RESTART_METHOD='/opt/VRTSpbx/bin/cluster/hacmp_online' HUNG_MONITOR_SIGNAL='9' RESTART_INTERVAL=396
  # /usr/es/sbin/cluster/utilities/claddappmon MONITOR_TYPE=user name=${PBX_SERVER} RESOURCE_TO_MONITOR=${PBX_SERVER} INVOCATION='longrunning' MONITOR_METHOD='/opt/VRTSpbx/bin/cluster/hacmp_monitor' MONITOR_INTERVAL='60' STABILIZATION_INTERVAL='300' RESTART_COUNT='1' FAILURE_ACTION='fallover' CLEANUP_METHOD='/opt/VRTSpbx/bin/cluster/hacmp_offline' RESTART_METHOD='/opt/VRTSpbx/bin/cluster/hacmp_online' HUNG_MONITOR_SIGNAL='9' RESTART_INTERVAL=396


   echo "\n Added ${PBX_SERVER} and application monitor to HACMP configuration.\n"
}

##############################################################################
#Main program starts here
##############################################################################

SCRIPT_DEBUG=false
[[ "$SCRIPT_DEBUG" = "true" ]] && set -x
MIN_HACMP_VERSION=5300

echo "\nExecuting PBX HACMP configuration script\n"

export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all):$PATH"
echo "export PATH=${PATH}" > $ROLLBACK_CMDFILE

PBX_SERVER=""
PBX_GROUP=""
option=$1

if [ "$1" = "-c" -a  $# != 7 ]; then
 exit 2
fi
if [ "$1" = "-u" -a $# != 5 ]; then
exit 2
fi
 while [ "$*" != "" ]
 do
 	case $2 in
	-r)
	   # Extract the PBX Server Name	
	   shift 
	   if [ "$option" = "-c" ]; then
	     PBX_SERVER=$2_$host_name
	   else
	     PBX_SERVER=$2
	   fi
	   ;;
	-g)
	   # Extract PBX Group Name
	   shift 
	   if [ "$option" = "-c" ]; then
	     PBX_GROUP=$2_$host_name
	   else
	     PBX_GROUP=$2
	   fi
	   ;;
	-n)
	  # Extract The nodes where group should get configure
	  shift
	  if [ "$2" != "$host_name" ]; then 
	    echo "Node Name is not matching with the host name. Assigning host name to node"
	    TMP_PBX_NODES=$host_name
	  else
	    TMP_PBX_NODES=$2
	  fi
	  ;;
	esac
	shift
 done

if [ "$PBX_SERVER" = "" -o "$PBX_GROUP" = "" ]; then
#echo "Please provide valid argument set...\n"
exit 2
fi
	
case $option in
     -c)
	
	# if resource group has been already configured; Remove it. 
	isExists=`/usr/es/sbin/cluster/utilities/clgetgrp -g $PBX_GROUP`
        if [ $? -eq 0 ] ; then
 	
	    /usr/es/sbin/cluster/utilities/clRGmove -s 'false' -d -i -g $PBX_GROUP -n $host_name 2>/dev/null
	    /usr/es/sbin/cluster/utilities/clrmserv $PBX_SERVER 2>/dev/null
	    /usr/es/sbin/cluster/utilities/clrmgrp -g  $PBX_GROUP 2>/dev/null
	    
	    /usr/es/sbin/cluster/utilities/cldare -rt -V 'normal' -C 'interactive'
	fi    
	
	check_hacmp_version
	configure_pbxserv
	configure_resgrp

###############################################################################
#
# Create script for adding PBX application server + monitoring to the
# PBX resource group. This script will be run by cluster_config 
# immediately before bringing pbx online.
#
cat > $STAGE2_CMDFILE <<EOF
#!/usr/bin/sh
#
# Add PBX application server $pbx_server to resource group $PBX_GROUP
# This script created automatically by hacmp_config on `date`
#

echo "Bringing down $PBX_GROUP prior to configuring application server...\n"
#DC: May not be needed
#/usr/es/sbin/cluster/utilities/clRGmove -s 'false' -d -i -g $PBX_GROUP -n $host_name

#DC: TODO COMMENTING DO WE NEED TO WAIT ???
#echo "Waiting for cluster to Stabilize..."
#sleep 30
#STABLE="NOT"
#until [[ "\$stable" = "STABLE" ]] ; do
#   sleep 2
#   stable=\`cldump |awk '/Cluster Substate:/ { print \$3 }'\`
#   echo "Substate = \$stable"
#done 

echo "Adding application server $PBX_SERVER to resource group $PBX_GROUP\n"
/usr/es/sbin/cluster/utilities/claddres -g "$PBX_GROUP" SERVICE_LABEL= APPLICATIONS="$PBX_SERVER" VG_AUTO_IMPORT=false VOLUME_GROUP= FORCED_VARYON='false' FILESYSTEM=

echo "Verifying and synchronizing cluster configuration....\n"
/usr/es/sbin/cluster/utilities/cldare -rt -V 'normal' -C 'interactive'

#DC: TODO COMMENTING DO WE NEED TO WAIT ???
#echo "Waiting for cluster to stabilize..."
#sleep 30
#STABLE="NOT"
#until [[ "\$stable" = "STABLE" ]] ; do
#   sleep 2
#   stable=\`cldump |awk '/Cluster Substate:/ { print \$3 }'\`
#   echo "Substate = \$stable"
#done 
#Bring the resource group online
#/usr/es/sbin/cluster/utilities/clRGmove -s 'false' -u -i -g $PBX_GROUP -n $host_name 
EOF
chmod 755 $STAGE2_CMDFILE

###############################################################################
#
# add configured resources to the pbx group  
#
# We do not add the pbx server to the resource group at this time. This is	
# because we don't want it to come up until after the configuration is complete.
	#
	# 
	/usr/es/sbin/cluster/utilities/claddres -g "$PBX_GROUP" SERVICE_LABEL= APPLICATIONS= VG_AUTO_IMPORT=false VOLUME_GROUP= FORCED_VARYON='false' FILESYSTEM=

	#DC: Commented
        #echo "Verifying and synchronizing cluster configuration....\n"
        #verify_cluster

	echo "Bringing $PBX_GROUP online on local node for initial configuration."
        /usr/es/sbin/cluster/utilities/clRGmove -u -g $PBX_GROUP -n $host_name 
	echo "Done with pbx_hacmp_config script \n"
     	exit 0
 	;;
     -u)
	    # if resource group has been already configured; Remove it. 
	    isExists=`/usr/es/sbin/cluster/utilities/clgetgrp -g $PBX_GROUP`
        if [ $? -eq 0 ] ; then
			/usr/es/sbin/cluster/utilities/clRGmove -s 'false' -d -i -g $PBX_GROUP -n $host_name
			/usr/es/sbin/cluster/utilities/clrmserv $PBX_SERVER
			/usr/es/sbin/cluster/utilities/clrmgrp -g  $PBX_GROUP
			#Remove log files if any
			rm -f "/var/tmp/hacmp_pbx_online.log"
			rm -f "/var/tmp/hacmp_pbx_monitor.log"
			rm -f "/var/tmp/hacmp_pbx_offline.log"
			rm -f $HACMP_CLUSTER_PATH/rollback.sh
			rm -f $HACMP_CLUSTER_PATH/hacmp_postconfig.sh
	
			/usr/es/sbin/cluster/utilities/cldare -rt -V 'normal' -C 'interactive'
		    exit 0	
	    else
			exit 1
	    fi
	  ;;
     *)
     #echo "Invalid options"
     exit 2
esac


