#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2018,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/utils/cl_activate_vgs.sh 1.46 
#  
# 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/utils/cl_activate_vgs.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
###############################################################################
#
#   COMPONENT_NAME: EVENTUTILS
#
#   FUNCTIONS: none
#
###############################################################################

###############################################################################
#  
#  Name:  cl_activate_vgs
#
#  Returns:
#       0 - All of the volume groups are successfully varied on
#       1 - varyonvg of at least one volume group failed
#       2 - Zero arguments were passed
#
#  This function will activate the volume groups passed in as arguments.
#
#  Arguments:  -n don't sync the volume groups when varyon is called
#              Volume group list 
#              
#  Environment: VERBOSE_LOGGING, PATH
#
#  Questions?  Comments?  Expressions of Astonishment?   mailto:hafeedbk@us.ibm.com
#
###############################################################################

###############################################################################
#  
#  Name:  vgs_chk
#
#  vgs_chk will varyon individual volume groups. 
#  status will be appended to the status file $TMP_FILENAME
#
#  Returns:
#       0 - volume group is successfully varied on
#       1 - varyonvg of volume group failed
#
#  Arguments:
#       $1 = volume group to be varied on
#       $2 = nosync option
#       $3 = Program name - shell script name for error messages
#              
#  Environment: VERBOSE_LOGGING, PATH, TMP_FILENAME, CLENV
#
###############################################################################

function vgs_chk
{
    typeset PS4_TIMER="true"
    export PS4_TIMER PS4_LOOP
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    typeset VG="$1"
    typeset SYNCFLAG=$2
    typeset PROGNAME="$3"
    integer STATUS=0

    if [[ -n $CLENV ]] &&		#   Known concurrent volume groups
       [[ $CLENV == @(?(* )$VG?( *)) ]]	#   This one is in that list
       then
           #   This is a concurrent volume group.  Pass it off to the appropriate
           #   routine, which will update the resource manager with the result.
           SYNCFLAG=${SYNCFLAG:+'-s'}
           if ! cl_mode3 $SYNCFLAG $VG
           then
               STATUS=1			#   Note error and keep going
           fi
    
    else				
        #   Not concurrent volume group
        #   Try to varyon a non-concurrent volume group, capturing any error
        #   message from clvaryonvg.
        
        # Format for consumption by cl_am utility
        amlog_trace $AM_VG_VARYON_BEGIN "Activating Volume Group|$VG"
        
        typeset -x ERRMSG
        ERRMSG=$(clvaryonvg $SYNCFLAG $VG)
        RC=$?
        # In case of any disk of a VG is missing and force varyon option is false, clvaryonvg returns 20 causing failure in VG acquire.
        # Supporting this return code, to prevent RG to come ONLINE even when VG is not varyed ON.
        if (( $RC == 1 || $RC == 20 ))
            then
                #	Update the resource manager with the information that this one
                #	did not come on line
                cl_RMupdate resource_error $VG $PROGNAME
                cl_echo 21 "$PROGNAME: Failed clvaryonvg of $VG." $PROGNAME $VG
                STATUS=1			#   Note error and keep going
        fi
        : exit status of clvaryonvg $SYNCFLAG $VG: $RC

        if [[ -n $ERRMSG ]] && (( $STATUS != 1 ))
        then
            cl_echo 286 "$PROGNAME: Successful clvaryonvg of $VG with message $ERRMSG." $PROGNAME $VG $ERRMSG
        fi
        
        # Format for consumption by cl_am utility
        if [[ $STATUS != 0 ]]
        then
            amlog_err $AM_VG_VARYON_FAILURE "Activating Volume Group|$VG"
        else
            amlog_trace $AM_VG_VARYON_END "Activating Volume Group|$VG"
        fi
    fi
    
    #	Append status to the status file
    echo $VG $STATUS >> $TMP_FILENAME
    return $STATUS
}

# this method activates the OEM vgs 
# using the user configured methods.
activate_oem_vgs ()
{
    typeset PS4_LOOP="" PS4_TIMER="true"
    [[ "$VERBOSE_LOGGING" == "high" ]] && set -x

    for OEM_VG in $*
    do
	PS4_LOOP="$OEM_VG"
        # get OEM type
        OEM_TYPE=$(cl_get_oem_type -v $OEM_VG)

        # get custom method to activate this
        OEM_METHOD_TO_ACTIVATE=$(cl_get_oem_method -m "ONLINE" -t $OEM_TYPE)
        
        ($OEM_METHOD_TO_ACTIVATE "$OEM_VG")
	RC=$?
        if (( $RC == 1 ))
        then
            # Update the resource manager with the information that this one
            # did not come on line
            cl_RMupdate resource_error $OEM_VG $PROGNAME
            echo "$PROGNAME: User defined method returned non-zero exit code"
            STATUS=1
        fi
	: exit status of $OEM_METHOD_TO_ACTIVATE "$OEM_VG": $RC
        # This status file is used in the high-level scripts. Mark the failure
        # or success
        echo "$OEM_VG $STATUS" >> $TMP_FILENAME
    done
    unset PS4_LOOP PS4_TIMER
    return $STATUS
}

###############################################################################
#
#   Vary on a list of volume groups
#
###############################################################################

vgs_list() {
    typeset PS4_LOOP=""

    for vg in $*
    do
	PS4_LOOP="$vg"
	#   Is $vg varied on?  Check both the list of vary'd on volume groups,
	#   and to see if LVM can actually read it.  The latter is needed for
	#   dealing with quorum close.
	if [[ $VGSTATUS == @(?(* )$vg?( *)) ]] &&
	    lqueryvg -g $(getlvodm -v $vg)
	then
	    cl_echo 23 "$PROGNAME: Volume group $vg already varied on." $PROGNAME $vg
	else
            # 
	    :	call varyon for the volume group in Foreground
            #
	    vgs_chk $vg $SYNCFLAG $PROGNAME 
	fi
        
    done
    unset PS4_LOOP PS4_TIMER
}


###############################################################################
#
# Start of main
#
###############################################################################

PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
# Including Availability metrics library file
. /usr/es/lib/ksh93/availability/cl_amlib
[[ $VERBOSE_LOGGING == high ]] && set -x
[[ $VERBOSE_LOGGING == high ]] && version='1.46'

integer STATUS=0
SYNCFLAG=""
CLENV="$CONCURRENT_VOLUME_GROUP"
TMP_FILENAME="/tmp/_activate_vgs.tmp"
USE_OEM_METHODS="false"

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


#   Set the sync flag, so that it can be propagated to the appropriate varyon
#   commands.
if [[ $1 == "-n" ]] 
then
    SYNCFLAG="-n"
    shift			#   shiftout
fi


# Use OEM custom methods if called with -c flag
if (( $# != 0 )) && [[ $1 == "-c" ]]
then
    USE_OEM_METHODS="true"
    shift			#  shiftout 
fi

# set -u will report an error if any flag used in the script is not set
set -u

#   Remove the status file if already exists
rm -f $TMP_FILENAME 

#   See what volume groups are currently vary'd on, so that they can be
#   skipped.
VGSTATUS=$(print $(lsvg -L -o))	#   turn new lines to blanks in list

#   Update resource manager to record the fact that varyons are being done
ALLVGS="All_volume_groups"
cl_RMupdate resource_acquiring $ALLVGS $PROGNAME

# if we are not called from process_resources, we have the old-style
# environment and parameters
if [[ $PROC_RES == false ]]; then

    #   Check for valid input.  If not called from process_resources, which passes
    #   parameters through the environment, there should be some volume group
    #   names as formal parameters.
    if (( $# == 0 ))
    then
	cl_echo 22 "usage: $PROGNAME volume_groups_to_varyon" $PROGNAME
	exit 2
    fi

    #	In the case where the volume group names were passed in explicitly,
    #	try to vary them on.
    if [[ "$USE_OEM_METHODS" == "false" ]]
    then
        vgs_list $*
    else
        activate_oem_vgs $*
    fi

else
    #	We were called from process_resources; have to extract the volume
    #	groups to vary on from the environment.
    LIST_OF_VOLUME_GROUPS_FOR_RG=""

    for GROUPNAME in $RESOURCE_GROUPS ; do

        export GROUPNAME

	echo $VOLUME_GROUPS | IFS=: read LIST_OF_VOLUME_GROUPS_FOR_RG VOLUME_GROUPS
	
	# forward sorting
	LIST_OF_VOLUME_GROUPS_FOR_RG=$(echo $LIST_OF_VOLUME_GROUPS_FOR_RG |
					tr ',' '\n' |
					sort -u)

	vgs_list $LIST_OF_VOLUME_GROUPS_FOR_RG

    done # end for GROUPNAME
fi

#   Wait to sync all the processes
wait

#   Update resource manager with the unsurprising statement that all volume
#   groups which have not had errors are now vary'd on.  This is done with the
#   expectation that most volume group will come on line most of the time, and
#   only the exceptions need be reported.
ALLNOERRVGS="All_nonerror_volume_groups"
cl_RMupdate resource_up $ALLNOERRVGS $PROGNAME

#
#   If there's a status file, see if it contains reports of any errors.
#   Failure to vary on a volume group will result in the resource group going
#   into error state.
#
if [[ -f $TMP_FILENAME ]]
then
    if grep " 1" $TMP_FILENAME
    then
        if [[ $PROC_RES == true ]]; then
            STATUS=11
        else
            STATUS=1
        fi
    fi

    rm -f $TMP_FILENAME
fi

exit $STATUS
