#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r721 src/43haes/usr/sbin/cluster/events/node_up_remote.sh 1.1.12.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,2016 
# 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 
# @(#)  7d4c34b 43haes/usr/sbin/cluster/events/node_up_remote.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM


#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################

#########################################################################
#                                                                       #
#       Name:           node_up_remote                                  #
#                                                                       #
#	Description:	This script is called when a remote node 	#
#			has finished joining the cluster.  The          #
#			local node must release all resources it took	#
#			over from that node, such as: service address,	#
#			filesystem, volume group, disk, and AIX         #
#			Connections realm/service pairs.          	#
#									#
#	Called by:	node_up						#
#									#
#	Calls to:	release_takeover_addr, stop_server,		#
#                       release_vg_fs, cl_deactivate_nfs,               #
#                       cl_stop_snalink                                 #
#									#
#       Arguments:      nodename                                        #
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#			2	bad argument				#
#                                                                       #
#########################################################################

#########################################################################
#
# Function Release Addr
#
#########################################################################
release_addr()
{
    typeset PS4_FUNC="release_addr"
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    #
    # Check if there is any remote service address we have taken over.
    # If yes, release it.
    #
    if [ -n "$TAKEOVER_LABEL" ] 
    then
	clcallev release_takeover_addr "$TAKEOVER_LABEL"
        RC=$?
        : exit status of release_takeover_addr $TAKEOVER_LABEL is: $RC
	if [ $RC -ne 0 ]
	then
		set_resource_status "ERROR"
                : exit status of set_resource_status is: $?
		exit 1
	fi
    fi
    #
    # Finally, release service address (go to boot address).
    #	
    if [ -n "$SERVICE_LABEL" ]
    then
	clcallev release_service_addr "$SERVICE_LABEL"
        RC=$?
        : exit status of release_service_addr $SERVICE_LABEL is: $RC
	if [ $RC -ne 0 ] 
	then
		set_resource_status "ERROR"
                : exit status of set_resource_status is: $?
		exit 1
	fi
    fi
}

#########################################################################
# 
# Set status of resource in resource location DB.  Note that we do this if
# if NFS_resource is "TRANS" because this means that we are 
# actually relinquishing the resource.
#
#########################################################################
set_resource_status()
{
    typeset PS4_FUNC="set_resource_status"
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x
    set +u
    eval TEMPNFS=\$"NFS_$GROUPNAME"
    set -u
    if [ "$TEMPNFS" = "TRANS" ]
    then
	if [ "$EMULATE" = "EMUL" ]
	then
		cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
		echo	"clchdaemons -d clstrmgr_scripts -t resource_locator -n \""$LOCALNODENAME"\" -o \""$GROUPNAME"\" -v \"$1\"\n"
	else
		clchdaemons -d clstrmgr_scripts -t resource_locator -n "$LOCALNODENAME" -o "$GROUPNAME" -v "$1"
		if [ $? -ne 0 ]
		then
			cl_log 655 "$PROGNAME: Problem with resource location database in HACMPdaemons ODM.\n" $PROGNAME
			STATUS=1
		fi
	fi
    fi

        #
        # Resource Manager Updates
        #
        if [ "$1" = "RELEASING" ]
        then
            if [ "$FOLLOWER_ACTION" = "RELEASE_SECONDARY" ]
            then
                cl_RMupdate releasing_secondary $GROUPNAME $PROGNAME
                [ $? -ne 0 ] && STATUS=1
            elif [ "$FOLLOWER_ACTION" = "SECONDARY_BECOMES_PRIMARY" ]
            then
                cl_RMupdate releasing_secondary $GROUPNAME $PROGNAME
                [ $? -ne 0 ] && STATUS=1
            elif [ "$FOLLOWER_ACTION" = "PRIMARY_BECOMES_SECONDARY" ]
            then
                cl_RMupdate acquiring_secondary $GROUPNAME $PROGNAME
                [ $? -ne 0 ] && STATUS=1
            elif [ "$FOLLOWER_ACTION" = "ACQUIRE_SECONDARY" ]
            then
                cl_RMupdate acquiring_secondary $GROUPNAME $PROGNAME
                [ $? -ne 0 ] && STATUS=1
	    else
                if [ "$PRINCIPAL_ACTION" = "RELEASE" ]; then
                  cl_RMupdate releasing $GROUPNAME $PROGNAME
                  [ $? -ne 0 ] && STATUS=1
                fi
            fi
        else # if error was experienced, argument is ERROR
            if [ "$FOLLOWER_ACTION" = "RELEASE_SECONDARY" ]
            then
                cl_RMupdate rg_error_secondary $GROUPNAME $PROGNAME
                [ $? -ne 0 ] && STATUS=1
            elif [ "$FOLLOWER_ACTION" = "SECONDARY_BECOMES_PRIMARY" ]
            then
                cl_RMupdate rg_error_secondary $GROUPNAME $PROGNAME
                [ $? -ne 0 ] && STATUS=1
	    else
                cl_RMupdate rg_error $GROUPNAME $PROGNAME
                [ $? -ne 0 ] && STATUS=1
            fi
        fi
}

#########################################################################
#
# Main Starts Here
#
#########################################################################

typeset PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"

if [[ $VERBOSE_LOGGING == "high" ]]; then
    eval export $(cllsparam -x)
    set -x
    version='1.2.1.25'
fi

STATUS=0

if [ $# -ne 1 ]
then
	cl_echo 1037 "Usage: $PROGNAME nodename\n" $PROGNAME
	exit 2
fi

if [ ! -n "$EMULATE" ]
then
	EMULATE="REAL"
fi

set +u
OEM_FILESYSTEM=${OEM_FILESYSTEM:-""}
OEM_VOLUME_GROUP=${OEM_VOLUME_GROUP:-""}
set -u

#
# First, indicate that the resource is leaving.
#
set_resource_status "RELEASING"
: exit status of set_resource_status is: $?

#
# The order of the operations was changed, causing the
# application servers to be stopped first, and then releasing the 
# service address the node took.
#
if [ -n "$APPLICATIONS" ] 
then
    TMPLIST=""
    let cnt=0
    set -A appnames $( print ${APPLICATIONS} )

    while ((  cnt < ${#appnames[*]} )); do
        TMPLIST="${APPLICATIONS%% *} ${TMPLIST}"
        APPLICATIONS="${APPLICATIONS#* }"
        let cnt=cnt+1
    done

    APPLICATIONS="${TMPLIST}"


    if [ "$EMULATE" = "EMUL" ]
    then
        cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
        echo "clcallev stop_server "$APPLICATIONS"\n"
        RC=$?
    else
        clcallev stop_server "$APPLICATIONS"
        RC=$?
        : exit status of stop_server $APPLICATIONS is: $RC
    fi

    if [ $RC -ne 0 ]
    then
        STATUS=1
    fi

    clmanageroha -o release -s -l "${APPLICATIONS// /,}" 3>&2
    : exit status of \"clmanageroha -o release -s -l \"${APPLICATIONS// /,}\"\" is: $?
fi
    #
    # Stop SMB/Fast Connect resources
    #
if [ -n "$AIX_FAST_CONNECT_SERVICES" ]
then
    if [ "$EMULATE" = "EMUL" ]
    then
	cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
	echo "release_fast_connect_rs \n"
        RC=$?
    else
	release_fast_connect_rs
        RC=$?
        : exit status of release_fast_connect_rs is: $RC
    fi
	    
    if [ $RC -ne 0 ]
    then
	STATUS = 1
    fi
fi

#
# Stop AIX Connections services
#
if [ -n "$AIX_CONNECTIONS_SERVICES" ]
then
	if [ "$EMULATE" = "EMUL" ]
	then
		cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
		echo "clcallev release_aconn_rs $AIX_CONNECTIONS_SERVICES\n"
                RC=$?
	else
		clcallev release_aconn_rs "$AIX_CONNECTIONS_SERVICES"
                RC=$?
                : exit status of release_aconn_rs $AIX_CONNECTIONS_SERVICES is: $RC
	fi

	if [ $RC -ne 0 ]
	then
	    STATUS=1
	fi
fi

#
# Stop tape resources
#
if [ -n "$SHARED_TAPE_RESOURCES" ]
then
	if [ "$EMULATE" = "EMUL" ]
	then
		cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
		echo "cl_tape_resource_release_multi $SHARED_TAPE_RESOURCES\n"
                RC=$?
	else
		cl_tape_resource_release_multi "$SHARED_TAPE_RESOURCES"
                RC=$?
                : exit status of cl_tape_resource_release_multi $SHARED_TAPE_RESOURCES is: $RC
	fi
	if [ $RC -ne 0 ]
	then
	    STATUS=1
	fi
fi

CROSSMOUNT=0
export CROSSMOUNT

if [ -n "$MOUNT_FILESYSTEM" ]
then
    echo $MOUNT_FILESYSTEM | grep -q "\;/"
    RC=$?
    if [ $RC = 0 ]
    then
	CROSSMOUNT=1
    fi
fi

#
# Release IP address first if the order of takeover is to take
# FS before IPaddr.
#
if [[ "$FS_BEFORE_IPADDR" == "true" ]]
then
	release_addr;
        : exit status of release_addr is: $?
fi
#
#
# Release filesystem(s) and volume group(s).
#
clcallev release_vg_fs "$FILESYSTEM" "$VOLUME_GROUP" "$OEM_FILESYSTEM" "$OEM_VOLUME_GROUP"
RC=$?
: exit status of release_vg_fs $FILESYSTEM $VOLUME_GROUP $OEM_FILESYSTEM $OEM_VOLUME_GROUP is: $RC
if [ $RC -ne 0 ]
then
	STATUS=1
fi


if [ "$PRINCIPAL_ACTION" = "RELEASE" -a "$AUXILLIARY_ACTION" = "ACQUIRE_SECONDARY" ]
then    
  #
  # Call replicated resource clear-primary method associated
  # with any replicated resource defined in the resource group
  # we arecurrently processing. 
  #
        
  call_replicated_methods  "clear_primary" ""
  METHODS=$(cl_rrmethods2call clear_primary)
  STATUS=$?
fi
    
if [ -n "$MOUNT_FILESYSTEM" ] 
then
    if [ $RC = 1 ]
    then
        cl_deactivate_nfs "$MOUNT_FILESYSTEM"
        RC=$?
        : exit status of cl_deactivate_nfs $MOUNT_FILESYSTEM is: $RC	
	if [ $RC -ne 0 ]
        then
                STATUS=1
        fi
    fi
fi
#
#If order of takeover is to take IPaddr than FS,
# then release address here.

if [[ "$FS_BEFORE_IPADDR" != "true" ]]
then
	release_addr;
        : exit status of release_addr is: $?
fi

# Stop the WPAR.  Due to the fact that WPAR enablement/disablement is
# done in a lazy fashion, the actual state of WPAR activity will not
# necessarily match our ODM state.  Consequently, we can't simply look
# at the WPAR_NAME environment variable.
#
# The command clstop_wpar will check if the resource group actually has
# a WPAR so we don't need to check for that here.
clstop_wpar
RC=$?
: exit status of clstop_wpar is: $RC

if (( $RC != 0 ))
then
    echo "Failed to stop the WPAR associated with resource group '${GROUPNAME}'"
    (( $STATUS == 0 )) && STATUS=1
fi

#
# If there is a lingering error, set error status, then exit.
#
if [ $STATUS -ne 0 ]
then
    set_resource_status "ERROR"
    : exit status of set_resource_status is: $?
    exit $STATUS
fi


# start of commlink processing.

	if [[ -n $COMMUNICATION_LINKS ]]
	then
		if [ "$EMULATE" = "EMUL" ]
    		then
				cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
				echo "cl_stop_commlinks\n"
                                RC=$?
    		else
				cl_stop_commlinks "$COMMUNICATION_LINKS"
                                RC=$?
                                : exit status of cl_stop_commlinks $COMMUNICATION_LINKS is: $RC
        if [ $RC -ne 0 ]
          			then
          			  STATUS=1
          			fi
		fi
	fi

#
# 
# Finally, indicate that the resource has made it DOWN by removing the 
# marker in the resource location DB (ODM HACMPdaemons).  
#
# Note that we only do this if NFS_resource is "TRANS" or nonexistent, 
# because this means that we are actually releasing the resource.
#
set +u
eval TEMPNFS=\$"NFS_$GROUPNAME"
set -u
if [ "$TEMPNFS" = "" -o "$TEMPNFS" = "TRANS" ]
then
    if [ "$EMULATE" = "EMUL" ]
    then
	    cl_echo 3020 "NOTICE >>>> The following command was not executed <<<< \n"
	    echo	"clchdaemons -r -d clstrmgr_scripts -t resource_locator -o \""$GROUPNAME"\"\n"
    else
	    clchdaemons -r -d clstrmgr_scripts -t resource_locator -o "$GROUPNAME"
	    if [ $? -ne 0 ]
	    then
		    cl_log 655 "$PROGNAME: Problem with resource location database in HACMPdaemons ODM.\n" $PROGNAME
		    STATUS=1
	    fi
    fi
fi
	
exit $STATUS

