#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2019,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/events/start_udresource.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009,2010 
# 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 
# sccsid = @(#)  7d4c34b 43haes/usr/sbin/cluster/events/start_udresource.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################

#########################################################################
#												#
#	Name:		start_udresource 							#
#												#
#	Description:	This script is called to start the 			#
#			user defined resource(s) for given type when the local#
#			node is completely up or a remote node 			#
#			has finished leaving the cluster.				#
#												#
#	Called by:	node_up_local, node_down_remote				#
#												#
#	Calls to:	None                   						#
#												#
#	Arguments: 	[userdefined-resource-name]					#
#												#
#	Returns:	0	success							#
#			1	failure							#
#												#
#########################################################################

start_and_monitor_udresource()
{
    typeset PS4_FUNC="start_and_monitor_udresource"
    [[ "$VERBOSE_LOGGING" = "high" ]] && set -x

    RETURN_STATUS=0
   
    udres=$1
    
    # first check if the userdefined resource is already running


    dspmsg scripts.cat 99999 "Checking whether $udres is already running...\n"  $udres

        if [[ -n ${WPARNAME} ]]
        then
            cl_app_startup_monitor -s $udres -a -w ${WPARNAME}
        else
            cl_app_startup_monitor -s $udres -a
        fi
  
        RETURN_STATUS=$?
        : exit status of cl_app_startup_monitor is: $RETURN_STATUS

        if [[ $RETURN_STATUS  == 0 ]] ; # cl_app_startup_monitor monitor is RUNNING
        then
            dspmsg scripts.cat 99999 "The resource monitor detected a running instance of $udres. PowerHA SystemMirror will not start another instance of $udres on this node. \n" $udres  $udres
            echo "$1 $RETURN_STATUS" >> $TMP_FILE
            return
        fi

   # The application is not running or we could  not determine if its running	
   # or we are in emulation mode
   dspmsg scripts.cat 99999 "Resource monitor(s) indicate that $udres is not active. Continuing with resource startup.\n"
   
   
    #
    # First get the start resource script.
    # Then prepare the pattern which we will write to an app. server
    # status file to keep track of the running server (we only need
    # to start the resource. resource once on one node.)
    #
    udrestype=$(cludres -l -n $udres -ch | awk ' FS=":" { print $2 }')
    START=$(cludrestype -l -n $udrestype | awk ' FS="=" { if( $1 == "START_METHOD" ) print $2 }')
    START_SCRIPT=$(echo $START | cut -d' ' -f1 | /usr/bin/sed s/\"//g )

    PATTERN="$LOCALNODENAME $udres"

    #export resource specific attributes as env variables 
    eval export `cludres -l -n $udres`

    #
    # If the script is executable, start the server,
    # If not executable, echo error message.
    #
    if [[ ( -n ${WPARNAME} && -x ${WPARDIR}/${START_SCRIPT} ) || \
          ( -z ${WPARNAME} && -x $START_SCRIPT ) ]]
    then
            ODMDIR=/etc/es/objrepos ${EXEC} $START $udres 
    else
       cl_log 310 "$START_SCRIPT does not exist or is not executable." $START_SCRIPT
       RETURN_STATUS=1
       
        if [[ $PROC_RES = true ]]
        then
            RETURN_STATUS=11  #shows that the resource manager got updated
        fi

        # update the resource manager with the results
            cl_RMupdate resource_error $udres $PROGNAME
        echo "$1 $RETURN_STATUS" >> $TMP_FILE
        return $RETURN_STATUS
    fi


        if [[ -n ${WPARNAME} ]]
        then
            cl_app_startup_monitor -s $udres -w ${WPARNAME}
        else
            cl_app_startup_monitor -s $udres
        fi

        RETURN_STATUS=$?
        : exit status of cl_app_startup_monitor is: $RETURN_STATUS
        if [[ $RETURN_STATUS != 0 ]]
        then
            if [[ $PROC_RES = true ]]; then
                RETURN_STATUS=11  #shows that the resource manager got updated
            fi
            cl_RMupdate resource_error $udres $PROGNAME
            echo "ERROR: Application Startup did not succeed."
        fi

        # write STATUS into file
        echo "$1 $RETURN_STATUS" >> $TMP_FILE
    return $RETURN_STATUS
}



##############################################################
#
# MAIN starts here
#
##############################################################

PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
if [[ $VERBOSE_LOGGING == high ]]
then
    set -x
    version='%I%'
fi

export TMP_FILE="/var/hacmp/log/.start_udresource.$$"

rm -f $TMP_FILE

STATUS=0

# Usage: start_udresource [userdefined-resource-names...]

PROC_RES=false

# if JOB_TYPE is set, and it doesn't equal to "GROUP", then
# we are processing for process_resources
if [[ ${JOB_TYPE:-0} != 0 && $JOB_TYPE != "GROUP" ]]; then
   PROC_RES=true
fi

# handle simulator mode up front.
if [[ -n "$SIMULATENODE" ]]; then
  cl_echo 3020 "NOTICE >>>> Simulator Command <<<< \n"
  for each_udres in $UDRESOURCES
  do
      echo "Simulator call: cl_RMupdate resource_acquiring $udres $PROGNAME"
      cl_RMupdate resource_acquiring $each_udres $PROGNAME
      echo "Simulator call: cl_RMupdate resource_up $each_udres $PROGNAME"
      cl_RMupdate resource_up $each_udres $PROGNAME
  done
  exit 0
fi

set -u
typeset WPARNAME EXEC WPARDIR
export  WPARNAME EXEC WPARDIR

EXEC=""
WPARNAME=""
WPARDIR=""


# update the resource manager with this operation
ALLUDRESOURCES="All_userdefined_resources"
ALLNOERRUDRESOURCES="All_nonerror_servers"
cl_RMupdate resource_acquiring $ALLUDRESOURCES $PROGNAME

WPARNAME=$(clwparname ${GROUPNAME})

if (( $? == 0 )) && [[ -n ${WPARNAME} ]]
then
    WPARDIR=$(clwparroot ${GROUPNAME})

    if [[ $? != 0 ]]
    then
        echo "$0 ERROR: Failed to get the WPAR directory for ${WPARNAME}"
        exit 1
    fi

    EXEC="clwparexec ${WPARNAME}"
fi

for each_udres in $UDRESOURCES
do 
  start_and_monitor_udresource $each_udres &
done

wait #wait for everybody to finish 

# see if anybody was successful

cat $TMP_FILE | awk '{
        if ($2 == 0) {
                exit 1

         }
      }'
SUCCESS=$?

if [[ $SUCCESS = 1 ]]; then

  # update the resource manager with the results
    cl_RMupdate resource_up $ALLNOERRUDRESOURCES $PROGNAME
fi

# see if everybody was successful
# we should never get 1 returned from here because the resource manager
# has been updated, but we check for it anyway.
cat $TMP_FILE | awk '{
        if ($2 == 1) {
                exit 1
         }
        if ($2 == 11) {
                exit 11
         }

      }'
SUCCESS=$? #11 if there was any error, otherwise 0

if [[ $SUCCESS = 0 ]]; then
  rm -f $TMP_FILE
fi

exit $SUCCESS 

