#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                     
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2021.  All rights reserved.         	
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                       
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/utilities/cl_terminate_rsct.sh 1.11 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2006,2012 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)  20c8e9d 43haes/usr/sbin/cluster/utilities/cl_terminate_rsct.sh, 726, 2147A_aha726, Oct 30 2021 04:20 PM
###############################################################################
#
#  Name:  cl_terminate_rsct.sh
#
#  Stops the RSCT services on the node that are forced down.
#
#  Arguments:    force - if passed, stops RSCT even if ECH VGs are found
#  active on the node.
#
#  Returns:     0
#
###############################################################################

PROGNAME=${0##*/}
if [[ $VERBOSE_LOGGING == "high" ]]
then
    set -x
    version='1.11'
fi

STATUS=0
FORCE="no"
LOGFILE="/var/hacmp/log/shutdown_rsct.out"
RSCT_BIN="/usr/sbin/rsct/bin"
NODENAME=""
CLRMUPDATE="/usr/es/sbin/cluster/events/utils/clRMupdate"

if (( $# < 2 ))
then
    dspmsg scripts.cat 10015 "Usage: cl_terminate_rsct -n <node name> [YES]"
    exit -1
fi

###############################################################################
# We need the node name. 
###############################################################################
if (( $# == 2 ))
then
    if [[ $1 == "-n" ]]
    then
        NODENAME=$2
    fi
fi

###############################################################################
# If the third parameter is passed this it is force option.
###############################################################################
if (( $# == 3 ))
then
    # The nodename needs to be set whether $3 is YES or NO.
    NODENAME=$2
    if [[ $3 == "YES" ]]
    then
        FORCE="force"
    fi
fi


###############################################################################
# Allow stopping RSCT only on the local node.
###############################################################################
LOCALNODENAME=$(clodmget -f "nodename" -n HACMPcluster)

if [[ $LOCALNODENAME != $NODENAME ]]
then
    # SMIT should prevent this so no need for ILS
    dspmsg scripts.cat 10016 "ERROR: Stopping RSCT services is allowed only on the local node."
    exit -1;
fi

###############################################################################
# What to stop
###############################################################################

STOP_GROUP_SERVICES="false"

if LC_ALL=C lssrc -s grpsvcs | grep -w -q active
then
    STOP_GROUP_SERVICES="true"
fi

if [[ $STOP_GROUP_SERVICES == "false" ]]
then
    dspmsg scripts.cat 10018 "WARNING: Group Services on the local node are not currently active."
    dspmsg scripts.cat 10017 "ERROR: RSCT services are not active on the local node."
    exit -1;
fi


###############################################################################
# Ensure the node is FORCED DOWN
###############################################################################

# Get the force down nodes list.
FORCEDOWN_NODES=$(LC_ALL=C lssrc -ls clstrmgrES | while read line
do
  if [[ $line == "Forced down node list: "* ]]
  then
      echo "${line##*: }"
  fi
done)

FOUND="false"
# check local node is forced down.
for forced_node in $FORCEDOWN_NODES; do
    if [[ $forced_node == $LOCALNODENAME ]]
    then
	FOUND="true"
    fi
done

# if the local node is not forced down, display error message.
if [[ $FOUND == "false" ]]
then
    dspmsg scripts.cat 10001 "ERROR: The RSCT services cannot be stopped on a node where PowerHA SystemMirror is actively \n\
managing the resources. Please note that stopping RSCT services will also \n\
affect the other RSCT dependent services such as the Enhanced Concurrent \n\
Volume Groups.\n"
    exit -1;

fi



###############################################################################
# Check whether ECH VGs are active
###############################################################################


lsvg -o | while read vg  
do
    #   Check the active volume groups for any in enhanced concurrent  mode
    if [[ -z $(clodmget -q "value = $vg" HACMPresource) ]]
    then
        continue	    #	skip non-HA volume groups
    fi

    vgid=$(/usr/sbin/getlvodm -v $vg) 
    integer mode=$(lqueryvg -g $vgid -X)
    integer rc=$?
    if (( $rc == 0 )) && [[ -n $mode ]] && (( 32 == $mode ))
    then
        dspmsg scripts.cat  10002 "WARNING: There are Enhanced Concurrent Volume Groups \n\
        varied'on on this node. Stopping the RSCT services will varyoff the \n\
        Enhanced Concurrent Volume Groups which may affect the running \n\
        applications.  \n"

        if [[ $FORCE != "force" ]]
        then
            dspmsg scripts.cat  10003 "If you want to continue with stopping RSCT services, please retry \n\
            by selecting the 'Force' option in the SMIT screen.\n"
	    exit -1;
	else
	    varyoffvg $vg
	    break;
	fi
    fi
done

#	If a volume group is currently vary'd on in passive mode, it
#	won't show up in the 'lsvg -o' list.  Get a list of such
#	volume groups to check for passive mode
INACTIVE_VGS=$(lsvg -L | grep -w -v -x -E $(lsvg -L -o | paste -s -d'|' - ) )
for vg in $INACTIVE_VGS ; do
    if LC_ALL=C lsvg -L $vg 2>/dev/null | grep -i -q 'passive-only' ; then
	dspmsg scripts.cat  10002 "WARNING: There are Enhanced Concurrent Volume Groups \n\
        varied'on on this node. Stopping the RSCT services will varyoff the \n\
        Enhanced Concurrent Volume Groups which may affect the running \n\
        applications.  \n"
	
	if [[ $FORCE != "force" ]]
	    then
	    dspmsg scripts.cat  10003 "If you want to continue with stopping RSCT services, please retry \n\
            by selecting the 'Force' option in the SMIT screen.\n"
	    exit -1;
	else
	    #
	    :   Reset any read only fence height prior to vary off
	    #
	    cl_set_vg_fence_height -c $vg rw
	    RC=$?
	    if (( $RC != 0 ))
	    then
		#
		:   cl_set_vg_fence_height -c $vg rw return code is $RC
		:   Log any error, but continue.  If this is a real problem, the varyoffvg will fail
		#
		rw=$(dspmsg -s 103 cspoc.cat 350 'read/write' | cut -f2 -d,)
		cl_log 10511 "$PROGNAME: Volume group $vg fence height could not be set to read/write" $PROGNAME $vg $rw
	    fi

	    # 'lsvg <vg name>' will show if a volume group is vary'd
	    # on in passive mode.  Any such are vary'd off
	    varyoffvg $vg
	fi
    fi
done

# If we reach here proceed with stopping services

# trap SIGHUP to ensure proper stopping
trap "" 1 

# Indicate the cluster manager to exit
$CLRMUPDATE clrm_stop_request stopdaemon
if (( $? != 1 ))
then
    dspmsg scripts.cat 10020 "ERROR: Failed to send stop request to the cluster manager process."
    exit -1;
fi


# Give some time for the cluster manager process to react
RC=0
typeset  -i count=0

for (( count=0 ; $count < 10 ; count++ ))
do
    sleep 1;
    lssrc -ls clstrmgrES >/dev/null
    RC=$?
    if [[ $RC != 0 ]]
    then
        break;
    fi
done

if [[  $RC ==  0 ]]
then
    dspmsg scripts.cat 10021 "ERROR: The cluster manager process did not stop as expected. The RSCT services will not be stopped."
    exit -1;
fi

exit $STATUS;