#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2017,2018,2019,2020,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/utilities/rc.init.sh 1.31 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2004,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/utilities/rc.init.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

###############################################################################
#
# rc.init - Called from inittab at (AIX) boot time.
# This utility starts the automatic release of ROHA resources of PowerHA 
# SystemMirror then starts the agents for PowerHA SystemMirror.
#
# Arguments: -u  : Called during PowerHA migration or upgrade
#            none: Called from inittab
# Usage: none - called by inittab
#
###############################################################################

###############################################################################
#
# Main entry point
#
###############################################################################

# Including Availability metrics library file
. /usr/es/lib/ksh93/availability/cl_amlib

[[ "$VERBOSE_LOGGING" == "high" ]] && set -x
PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
export PATH

PROGNAME=${0##*/}
NODENAME=""

typeset update_flag="false"

# set update_flag="true" if rc.init is called during PowerHA Migration or upgrade
if (( $# != 0 ));then
   if [[ "$1" == "-u" ]];then
         update_flag="true"
   else
         cl_echo 9654 "Usage: $PROGNAME" $PROGNAME >&2
         exit 1
   fi
fi

#
# Cluster services should only be run in the global WPAR.  uname -W and
# wparquery -w will return 0 if this script is running in the global WPAR.
# Note that wparquery is deprecated and uname -W is the preferred command
# to use, however since not all versions of uname support the -W parameter,
# we use wparquery as a fallback.
#
# Note, we treat failures of these commands as indicating a lack of WPAR support
# which essentially means we are executing in the "global WPAR".
if wpar_index=$(/usr/bin/uname -W 2> /dev/null) ; then
    if [[ "$wpar_index" != "0" ]] ; then
        exit 0
    fi
else
    if [[ -x /usr/lib/wpars/wparquery ]] ; then
        if wpar_index=$(/usr/lib/wpars/wparquery -w 2> /dev/null) ; then
            if [[ "$wpar_index" != "0" ]] ; then
                exit 0
            fi
        fi
    fi
fi

# set the environment to access the correct odm and libraries
ODMDIR=/etc/es/objrepos
export ODMDIR
unset LIBPATH

NODE_HALT_CONTROL_FILE="/usr/es/sbin/cluster/etc/ha_nodehalt.lock"
WPAR_MAPPING_DIR="/var/hacmp/adm/wpar"
OP_SEP=$(cl_get_path -S)
HA_LOG_DIR=$(clodmget -q"name=hacmp.out" -n -f value HACMPlogs)
HA_LOG=$HA_LOG_DIR/hacmp.out
IPV6_BOOT_INTERFACES=""

# Availability metrics tracks the shutdown/reboot process which "ends"
# at (AIX) boot time when rc.init script execution begins.
amlog_trace $AM_AIX_DOWN_END "AIX down end"

ALL_PARAMS="none"

####################################################################
# Log Start-Up
####################################################################
cl_echo 220 "Starting execution of $PROGNAME\nwith parameters: ${ALL_PARAMS}\n\n" $PROGNAME "${ALL_PARAMS}"

###############################################################################
# rc.init is called from inittab or during powerHA update, but just in case, make sure we have no
# extra params
###############################################################################
if (( $# > 1 ))
then
    cl_echo 9654 "Usage: $PROGNAME" $PROGNAME >&2
    exit 1
fi

####################################################################################
# Invoke a script that clears PVID of all target disks defined in consistency groups
# which are configured for backup.
####################################################################################
if [[ -n $(clodmget -q "name=BACKUP_ENABLED" -f value HACMPresource) ]]; then
    cl_cbm_clear_pvids 2>/dev/null
fi

####################################################################
# If this node is a server, take some additional actions
####################################################################
lslpp -L cluster.es.server.rte >/dev/null 2>&1
if (( $? == 0 ))
then

    ##########################################################
    # Register PowerHA script for AIX Live Kernel Update (LKU)
    ##########################################################
    Util_DIR=/usr/es/sbin/cluster/utilities
    RegScript_CMD=/usr/sbin/lvupdateRegScript

    ##############################################################################
    # Register the PowerHA script for LKU LVUP_COMPLETE event
    ##############################################################################
    if [[ -x $RegScript_CMD ]]; then
        typeset -i V R M F
        lslpp -lcq "bos.rte.commands" | head -1 | cut -f3 -d':' | IFS=. read V R M F
	# This is only supported with AIX version 7.2.2 and above
	if (( $V > 7 )) || (( $V == 7 && (( $R > 2 || (( $R == 2 && $M >= 2 )) )) )); then
            if ! $RegScript_CMD -l | grep -qw PowerHA_SURR_COMPLETE; then
                $RegScript_CMD -a -n PowerHA_SURR_COMPLETE -P LVUP_COMPLETE -p 1 -d surr \
                    -s "${Util_DIR}/cl_lvupdate -P LVUP_COMPLETE -d surr" >/dev/null 2>&1
                if (( $? != 0 )); then
                   # Log the error and continue
                   cl_echo 10711 "$PROGNAME: Failed to register script for AIX Live Update (PowerHA_SURR_COMPLETE).\n" $PROGNAME
                else
                   cl_echo 10712 "$PROGNAME: Registering script for AIX Live Update (PowerHA_SURR_COMPLETE) is successful.\n" $PROGNAME
                fi
            fi
        fi
    fi
    # don't call clmanageroha as we might be in unmanage where resources are still being used by the application
    if [[ "$update_flag" == "false" ]]; then
        ####################################################################
        # Launch the automatic release of any ROHA resources
        ####################################################################
        print "[ROHALOG:$$] Automatic Release of Resource: Start" >>${HA_LOG} 2>&1
        VERBOSE_LOGGING="high" clmanageroha -o release -s -t >> ${HA_LOG} 2>&1 3>&2
        print "[ROHALOG:$$] Automatic Release of Resource: End" >>${HA_LOG} 2>&1
        ####################################################################
        # Remove the cthags exit file
        ####################################################################
        print "$PROGNAME: Removed /usr/es/sbin/cluster/.cthags.exit file.\n" >>${HA_LOG} 2>&1
        rm -f /usr/es/sbin/cluster/.cthags.exit

    fi

    ######################################################################################################### 
    # REL_APPS contains application names for which resources need to be released for ROHA async release.
    # There is a scenario where REL_APPS is not cleared and no async release instance in place,
    # Due to this subsequent acquire process will get delayed as it thinks that async process is still going on.
    # This ODM need to be cleared to avoid delay in acquire during node start
    ##########################################################################################################
    ps -eo 'args' | grep -vw grep |grep -qw "clasyncroha"
    if (($? != 0))
    then
        # No need to reset cluster handle as HACMPdynresop is node level ODM
        print -- "HACMPdynresop:\\nvalue=\"\"" | ODMDIR=/etc/es/objrepos odmchange -o HACMPdynresop -q "key=REL_APPS"
    fi

    ####################################################################
    # Check to see if our agents are already active
    ####################################################################
    clcheck_server clcomd
    CLCOMD_ACTIVE=$?

    # Note that we do NOT use clcheck_server here for checking clstrmgr
    # since we do not want any of the retries that clcheck_server does
    ps -eo "%c"|grep -qx clstrmgr
    CLSTRMGR_INACTIVE=$?

    if [[ $CLCOMD_ACTIVE == 0 || $CLSTRMGR_INACTIVE == 1 ]]
    then
    
    	####################################################################
    	# Check to see if srcmstr is running; if so, we try to use it;
    	# otherwise, we exit with error
    	####################################################################
    	typeset -i i=3  # check for srcmstr 3 times 
    
    	while (( $i != 0 ))
        do
            src_running=$(ps -e | awk '$NF == "srcmstr" { print $1; exit }')
        
            [[ -n "$src_running" ]] && break
            (( i-- ))  # decrement count
        done
        
        if (( i == 0 ))
        then
            cl_echo 234 "$PROGNAME: srcmstr is not running.\n" $PROGNAME
            exit 1
        fi
        
        ####################################################################
        # If srcmstr is running, ensure that it is active before issuing the
        # startsrc commands
        ####################################################################
        
        if [[ -n "$src_running" ]]
        then
           cl_echo 235 "Checking for srcmstr active..." 
           i=10  # try ten times to contact it
        
    	    while (( $i != 0 ))
            do
                lssrc -s inetd >/dev/null 2>&1 && break  # break out on success
                sleep 1  # otherwise wait a second and try again
                echo ".\c"
                (( i-- ))  # decrement count
            done
            
            if (( i == 0 ))
            then
                cl_echo 236 "ERROR: srcmstr is not accepting connections\n" 
                exit 1
            fi
            
            cl_echo 237 "complete.\n" 
        fi
        
        ####################################################################
        # See if syslogd already running.  If not, start it
        ####################################################################
        
        ps -e | grep -s syslog
        
        if (( $? != 0 ))
        then
            typeset syslog_type=$(clodmget -n -q "subsysname = 'syslogd'" -f path SRCsubsys)
            if [[ ${syslog_type} == "/usr/sbin/rsyslogd" ]]
            then
                cl_echo 10737 "Starting rsyslogd" 
                startsrc -s syslogd
            else
                cl_echo 221 "Starting syslogd" 
                startsrc -s syslogd
            fi
        fi

        ####################################################################
        # Start snmpd 
        ####################################################################
        startsrc -s snmpd 2>/dev/null

        ####################################################################
        # Update clcomd with the timers from HACMPtimersvc odm.
        ####################################################################
	clupdatetimers 0 2>/dev/null

        #################################################################
        # Remove the resource group --> wpar name mappings
        #################################################################
        rm -rf $WPAR_MAPPING_DIR/*

        #################################################################
        # Make sure that the special (hidden) directories for WPAR exist
        # These directories are namefs-mounted inside the WPAR to ensure
        # that the PowerHA binaries do not show up inside the WPAR.
        # (Note: A WPAR can be configured such that it shares a 
        # read-only copy of the Global enviroment's /usr /etc filesystems
        #################################################################
        mkdir -p ${WPAR_MAPPING_DIR}/.usr_es ${WPAR_MAPPING_DIR}/.etc_es

        #################################################################
        # Remove the file created if the clstrmgr is stopped intentionally.
        # The path is defined in (and must match) clstrmgr.h
        #################################################################
        rm -f /var/hacmp/log/clstrmgrES_stopdaemon

    	#################################################################
	# Create the lock file that inhibits node halt before HA startup
    	#################################################################
        touch $NODE_HALT_CONTROL_FILE

    	#################################################################
	# Start the cluster manager daemon only if the lock file got 
        # created. Otherwise failure to start daemon might halt the node
    	#################################################################
        if (( $? == 0 ))
        then
            startsrc -s clstrmgrES 2>/dev/null
        fi

    fi # one or more agents inactive
        
    #
    # If this node is part of any IPv6 network, retain IPv6 addresses
    # if they are configured from router.
    #
    NODENAME=$(clodmget -f nodename -n HACMPcluster)
    #
    # If we have only IPv6 address configured on any interface,
    # the particular interface will be in defined state once
    # the node reboots. So we have to run 'autoconf6 -i <interface>'
    # for all interfaces whose state is defined.
    # Here we are considering the interfaces which has IPv6 address
    # configured on this node and the same has been configured as boot
    # label in cluster.
    #
    IPV6_BOOT_INTERFACES=$(cllsif -S -J "$OP_SEP" | grep "${OP_SEP}$NODENAME${OP_SEP}" |
			grep "${OP_SEP}boot${OP_SEP}" | grep AF_INET6 | cut -d"$OP_SEP" -f9)
    [[ -n "$IPV6_BOOT_INTERFACES" ]] && {
	for INTERFACE in $IPV6_BOOT_INTERFACES
	do
		autoconf6 -i $INTERFACE
	done

	startsrc -s ndpd-host
        # Give it some time to finish
	sleep 60
    }

    # Now configue any persistent IPs
    cl_configure_persistent_address config_all -d >>$HA_LOG 2>&1
    
    #
    # if there are any core files, try to grab them
    #
    core_list=$(ls /core* 2>/dev/null)
    if (( $? == 0 ))
    then
        for name in $core_list
        do
            # Only grab it if created by clstrmgr
            if what $name | grep -q hacmp.pe
            then
                base=$(basename ${name})
                mv $name /var/hacmp/log/clstrmgr.$base &
            fi
        done
    fi

    #
    # retain only the last 3 clstrmgr core files
    #
    saved_cores=$(ls -1 /var/hacmp/log/clstrmgr.core* 2>/dev/null | wc -l)
    if (( saved_cores > 3 ))
    then
        (( number_cores = ( $saved_cores - 3 ) ))
        core_list=$(ls -rt1 /var/hacmp/log/clstrmgr.core* 2>/dev/null | head -${number_cores})
        for name in $core_list
        do
            rm -f $name
        done
    fi

    #
    # Start the clxd daemon
    #
    lslpp -L cluster.es.genxd.cmds > /dev/null 2>&1
    if (( $? == 0 ))
    then
        startsrc -s clxd 2>/dev/null
    fi

    #
    # If SAN communication has been restricted during an incomplete LPM,
    # enable them
    # 
    #
    TMP_IFRESTRICT_FILE="/var/hacmp/ifrestrict"
    CAA_IFRESTRICT_FILE="/etc/cluster/ifrestrict"
    if [[ -s $TMP_IFRESTRICT_FILE ]]
    then
        mv $TMP_IFRESTRICT_FILE $CAA_IFRESTRICT_FILE
    fi
fi

####################################################################
# Log end of start-up
####################################################################
cl_echo 32 "Completed execution of $PPROGNAME\nwith parameters: ${ALL_PARAMS}.\nExit status = 0\n\n" $PPROGNAME "${ALL_PARAMS}"

exit 0
