#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2017,2018,2021,2022.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/lib/ksh93/hacmp/KLIB_HACMP_offline_resourcegroup.sh 1.6 
#  
# 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 
# @(#)  72b3472 43haes/lib/ksh93/hacmp/KLIB_HACMP_offline_resourcegroup.sh, 61aha_r726, 2205A_aha726, Mar 25 2022 11:59 AM
# Start of POD-formatted documentation. Viewing suggestions:
#      perldoc <FILENAME>
#      pod2text -c <FILENAME>
#      pod2text -c --code <FILENAME>
#      pod2html <FILENAME>
function devDoc {
    : <<'=cut' >/dev/null 2>&1

=head1 NAME

 KLIB_HACMP_offline_resourcegroup

=head1 SYNOPSIS

 NON-CONCURRENT RESOURCE GROUPS:
 clmgr offline resource_group { <resource_group>[,<rg#2>,<rg#n>,...] | ALL } \
             [ NODE=<node> ] \
             [ PRIMARY={true} ] \
             [ SECONDARY={true} ]

 CONCURRENT RESOURCE GROUPS:
 clmgr offline resource_group { <resource_group>[,<rg#2>,<rg#n>,...] | ALL } \
             [ NODE={<node>[,<node#2>,...] | ALL} ] \
             [ SECONDARY={true} ]

 NOTE: the alias for "resource_group" is "rg".

=head1 DESCRIPTION

Attempts to bring one or more resource groups offline.

=head1 ARGUMENTS

 1. rgs [REQUIRED] [string]
    One or more resource groups to be brought offline.

 2. specified_nodes [OPTIONAL] [string]
    The label of one or more (for concurrent RGs only) nodes within
    the cluster that the resource group should be brought offline on.

 3. primary [OPTIONAL] [boolean]
    The label of a primary node within the cluster that the
    resource group should be brought offline Defaults to "false".
 
 4. secondary [OPTIONAL] [string]
    The label of a secondary node within the cluster that the
    resource group should be brought offline Defaults to "false".

=head1 RETURN

 0: no errors were detected; the operation appears to have been successful
 1: a general error has occurred
 2: a specified resource does not exist, or could not be found
 3: some required input was missing
 4: some detected input was incorrect in some way
 5: a required dependency does not exist
 6: a specified search failed to match any data

=cut
} # End of POD-formatted documentation.


function KLIB_HACMP_offline_resourcegroup {
    LINENO=2 . $HALIBROOT/log_entry "$0()" "$CL"
    : @(#)  72b3472 43haes/lib/ksh93/hacmp/KLIB_HACMP_offline_resourcegroup.sh, 61aha_r726, 2205A_aha726, Mar 25 2022 11:59 AM
    : INPUTS: $*

    typeset rgs=${1//\"/}
    typeset specified_nodes=${2//\"/}
    #remove duplicates before any other processing takes place on specified_nodes string
    if [[ -n $specified_nodes ]]; then
        specified_nodes=$(echo $specified_nodes | tr "," "\n" | sort -u | tr "\n" " ")
        specified_nodes=${specified_nodes##+([[:space:]])}
        specified_nodes=${specified_nodes%%+([[:space:]])}
    fi
    
    typeset primary=${3//\"/}        
    typeset secondary=${4//\"/}

    [[ $CLMGR_LOGGING == 'med' ]] && set +x  # Only trace param values

    #===================================
    : Declare and initialize variables
    #===================================
    typeset rg= nodes= node=
    typeset -A attr
    typeset -i rc=$RC_UNKNOWN timeout=120 inc=10
    typeset -u uc_val=$specified_nodes

    #=================
    : Validate input
    #=================
    rgs=${rgs#+([[:space:]])}
    rgs=${rgs%+([[:space:]])}
    rgs=${rgs//,/ }
    if [[ -z $rgs ]]; then
        /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 509 "\nERROR: at least one resource group must be specified.\n\n" 1>&2
        rc=$RC_MISSING_DEPENDENCY

    else
        if [[ $primary == @(t|T|y|Y)* && $secondary == @(t|T|y|Y)* && -n $specified_nodes ]]; then
            /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 537 "\nERROR: node information should not be passed to bring down both PRIMARY and SECONDARY.\n\n"
            rc=$RC_MISSING_DEPENDENCY
        elif [[ (-n $primary && $primary != @(t|T|y|Y)*) || (-n $secondary && $secondary != @(t|T|y|Y)*) ]]; then
            if [[ -n $primary && $primary != @(t|T|y|Y)* ]]; then
                /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" PRIMARY "$primary" 1>&2
            elif [[ -n $secondary && $secondary != @(t|T|y|Y)* ]]; then
                /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 110 "\nERROR: invalid value specified for \"%1\$s\":  \"%2\$s\".\n" SECONDARY "$secondary" 1>&2
            fi
            /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 3 "Valid values: %1\$s\n\n" "true, yes" 1>&2
            rc=$RC_INCORRECT_INPUT
        fi
        typeset -u uctext=$rgs
        if [[ $uctext == "ALL" ]]; then
            rgs=
            typeset available
            CL=$LINENO KLIB_HACMP_list_resourcegroups available
            for (( i=0; i<${#available[*]}; i++ )); do
                rgs="$rgs ${available[$i]}"
            done
        fi

        if [[ $uc_val == "ALL" ]]; then
            : Set to the special value of "All_Nodes_in_Group"
            specified_nodes="All_Nodes_in_Group"
        fi
        rgs_orig=$rgs
        #==========================================================================
        : Verify if user passed any dependent RGs as arguments
        : Creating DependentRG list from the list of RGs passed.
        : if RG is not a part of any of the depedency mark it as IndependentRG
        #==========================================================================
        if [[ -n $rgs ]]; then
            for rg in $rgs
            do
                checkparent=$(clodmget -f group_parent -q"group_parent=$rg" HACMPrgdependency)
                checkchild=$(clodmget -f group_parent -q"group_child=$rg" HACMPrgdependency)
                sameNodeDep=$(clrgdependency -t NODECOLLOCATION -sl | grep -w $rg)
                checkAntiCol=$(clodmget -q"group_name = $rg" HACMPrg_loc_dependency)
                if [[ -n $checkparent && -n $checkchild ]]; then
                    rg_par_child="$rg_par_child $rg"
                elif [[ -n $checkchild ]]; then
                    if [[ -z $child_list ]]; then
                        child_list=$rg
                    else
                        child_list="$child_list $rg"
                    fi
                elif [[ -n $checkparent ]]; then
                    if [[ -z $par_list ]]; then
                        par_list=$rg
                    else
                       par_list="$par_list $rg"
                    fi
                elif [[ -n $sameNodeDep ]]; then
                    if [[ -z $node_col ]];then
                        node_col=$rg
                    else
                        node_col="$node_col $rg"
                    fi
                elif [[ -n $checkAntiCol ]]; then
                    if [[ $node_anticol == *$rg* ]]; then
                        continue
                    fi
                    OUTPUT=$(clrgdependency -t ANTICOLLOCATION -sl| grep -w $rg)
                    print -- "$OUTPUT" | grep -v '#' |\
                    while IFS=: read high int low; do
                        if [[ -z int ]];then
                            node_anticol="$node_anticol $high $low"
                        else
                            node_anticol="$node_anticol $high $int $low"
                        fi
                    done
                    #remove leading space
                    if [[ -n node_anticol ]]; then
                        node_anticol=${node_anticol#+([[:space:]])}
                        node_anticol=${node_anticol%+([[:space:]])}
                    fi
                else
                    if [[ -z $IndependentRG ]]; then
                        IndependentRG=$rg
                    else
                        IndependentRG="$IndependentRG $rg"
                    fi
                fi
            done # end of rgs loops
            #
            : RGs will be released in the below order
            : priority 1:RG which is configured as Child
            : priority 2:RG which is configured as both parent and child
            : priority 3:RG configured as parent
            : priority 4:Rg which is configured in same node dependencies.
            : priority 5:RG which is configured in different node dependencies
            : priority 6:RG in serial release order
            #
            dependencyRG="$child_list $rg_par_child $par_list $node_col $node_anticol"
            # Remove leading, trailing space , remove if any duplicate RGs present in DependentRG.
            # Replace multiple spaces in DependentRG with single space if any.
            if [[ -n $dependencyRG ]]; then
                dependencyRG=${dependencyRG#+([[:space:]])}
                dependencyRG=${dependencyRG%+([[:space:]])}
                dependencyRG=$(echo $dependencyRG | awk '{for (i=1;i<=NF;i++) if (!a[$i]++) printf("%s%s",$i,FS)}{printf("\n")}')
                dependencyRG=$(echo $dependencyRG | sed 's/  */ /g')
            fi
            rgs=$IndependentRG
            if [[ -n $IndependentRG ]]; then
                # Condier Serial order processing in case if Configured.
                OUTPUT1=$(clrgorder -s)
                print -- "$OUTPUT1" | grep -v '#' |\
                  IFS=: read sel_acq par_acq ser_rel par_rel
                ser_dep="$ser_dep $ser_rel"
                if [[ -n $ser_dep ]]; then
                    for rg in $rgs
                    do
                        if [[ -z $( echo $ser_dep | grep -w $rg) ]]; then
                            rg_temp="$rg_temp $rg"
                        fi
                    done
                    rgs="$ser_dep $rg_temp"
                    #remove leading space and trailing space
                    if [[ -n $rgs ]]; then
                        rgs=${rgs#+([[:space:]])}
                        rgs=${rgs%+([[:space:]])}
                    fi

                fi
            fi
            # consider dependencyRG followed by serial order processing of IndependentRGs
            rgs="$dependencyRG $rgs"
            # Removing leading and trailing spaces, also remove multiple spaces between RGs.
            rgs=$(echo $rgs | sed -e 's/^[[:space:]]*//')
            rgs=$(echo $rgs | awk '{for (i=1;i<=NF;i++) if (!a[$i]++) printf("%s%s",$i,FS)}{printf("\n")}')
            rgs=$(echo $rgs | sed 's/  */ /g')
        fi # end of rgs check

        if [[ $primary == @(t|T|y|Y)* ]]; then
            pOpt="-s false"
        else
            pOpt="-s true"
        fi
 
        if [[ $secondary == @(t|T|y|Y)* ]]; then
            sOpt="-s true"
        else
            sOpt="-s false"
        fi

        #===========================================================
        : Check the status of the cluster manager. Must be STABLE.
        : Since clRGmove only checks the cluster manager status on
        : the local node, that is all we need to do, too.
        #===========================================================
        STATE=$(CL=$LINENO KLIB_HACMP_get_node_state 2>>$CLMGR_TMPLOG)
        for (( i=0; $i<timeout; i+=$inc )); do
            [[ $STATE == "ST_STABLE" ]] && break
            if (( $i < $timeout - $inc )); then
                STATE=$(CL=$LINENO KLIB_HACMP_get_node_state 2>>$CLMGR_TMPLOG)
                sleep $inc
            fi
        done

        #====================================================================
        : Loop through the specified groups, and attempt to bring each down
        #====================================================================
        for rg in $rgs; do
            # if rg is not present in the original rg list which we passed then we can skip it.
            if [[ -z $( echo $rgs_orig | grep -w $rg ) ]]; then
                continue
            fi
            # check if RG is not ONLINE or ONLINE SECONDARY we can skip bringing the RG down.
            CL=$LINENO KLIB_HACMP_get_resourcegroup_state "$rg" rgattrs
            if [[ ${rgattrs[STATE]} != "ONLINE" && ${rgattrs[STATE]} != "ONLINE SECONDARY" && ${rgattrs[SECONDARY_STATE]} != "ONLINE SECONDARY" ]]; then
                dspmsg -s 100 command.cat 482 "\nResource Group \"%1\$s\" is not online.\n\n" "$rg"
                continue
            fi

            nodes= node=
            if [[ -n $specified_nodes ]]; then
                if [[ $specified_nodes == "All_Nodes_in_Group" || \
                    $specified_nodes != *+([[:space:]])* ]]
                then
                    nodes=$specified_nodes
                else
                    CL=$LINENO KLIB_HACMP_get_rg_attributes "$rg" attr
                    nodes=" ${attr[NODES]} "

                    for node in $specified_nodes; do
                        nodes=${nodes// $node / }
                    done
                    #
                    # If user has specified a list of all the nodes in cluster,
                    # send All_Nodes_in_Group to clRGmove.
                    # This avoids calling clRGmove multiple times
                    #
                    if [[ $nodes == *([[:space:]]) ]]; then
                        nodes="All_Nodes_in_Group"
                    else
                        nodes=$specified_nodes
                    fi
                fi
            else
                CL=$LINENO KLIB_HACMP_get_rg_attributes "$rg" attr
                if [[ ${attr[STARTUP]} == "OAAN" ]]; then
                    nodes="All_Nodes_in_Group"
                else
                    nodes=${attr[CURRENT_NODE]%%+([[:space:]])*}
                fi
                
                #
                # This check is added to bring primary and secondary nodes in resource group
                # should be bring to offline
                #
                if [[ ($primary == @(t|T|y|Y)* && $secondary == @(t|T|y|Y)*) ||  ${attr[STARTUP]} == "OAAN" ]]; then
                    nodes="${attr[NODES]} ${attr[SECONDARYNODES]}" 
                fi
                if [[ -z $nodes ]]; then
                    /usr/bin/dspmsg -s $CLVT_SET $CLVT_MSGS 510 "\nWarning: unable to identify a node where resource group\n         \"%1\$s\" is currently online.\n\n" "$rg"
                    rc=$RC_MISSING_DEPENDENCY
                fi
            fi # end of specified_nodes check

            if (( $rc == RC_UNKNOWN || $rc == RC_SUCCESS )) && \
               [[ -n $nodes ]]
            then
                typeset -i failures=0
                typeset rgstate=""
                for node in $nodes; do
                    rgstate=$(LC_ALL=C clRGinfo -s $rg | awk -F: '($3 == "'$node'") {print $2}')
                    if [[ "$rgstate" == "OFFLINE" ]];then
                        dspmsg -s 100 command.cat 478 "\nResource Group \"%1\$s\" is already offline on node \"%2\$s\".\n\n" "$rg" "$node"
                    fi
                    if [[ $primary == @(t|T|y|Y)* && $secondary == @(t|T|y|Y)* ]]; then
                        for node in $nodes; do
                            rgstate=$(LC_ALL=C clRGinfo -s $rg | awk -F: '($3 == "'$node'") {print $2}')
                            if [[ $rgstate == "ONLINE" ]]; then
                                pOpt="-s false"
                            elif [[ $rgstate == "ONLINE SECONDARY" ]]
                            then
                                pOpt="-s true"
                            fi
                            if [[ $rgstate != "OFFLINE" ]]; then
                                print "$0()[$LINENO]($SECONDS): clRGmove $pOpt -d -i -g \"$rg\" -n \"$node\"" >>$CLMGR_TMPLOG  # Always log commands
                                VERBOSE_LOGGING="" clRGmove $pOpt -d -i -g "$rg" -n "$node"
                                rc=$?
                                print "$0()[$LINENO]($SECONDS): clRGmove RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
                            fi
                        done
                        break
                    elif [[ $primary == @(t|T|y|Y)* && "$rgstate" != "ONLINE" ]]; then
                        dspmsg -s 100 command.cat 480 "\nResource Group \"%1\$s\" is not ONLINE on node \"%2\$s\".\n\n" "$rg" "$node"
                        rc=$RC_ERROR
                    elif [[ $secondary == @(t|T|y|Y)* && "$rgstate" != "ONLINE SECONDARY" ]]; then
                        dspmsg -s 100 command.cat 481 "\nResource Group \"%1\$s\" is not ONLINE SECONDARY on node \"%2\$s\".\n\n" "$rg" "$node"
                        rc=$RC_ERROR
                    else
                        # bring the rg offline only if it is in ONLINE state or ONLINE SECONDARY state
                        if [[ ( $secondary == @(t|T|y|Y)* && "$rgstate" == "ONLINE SECONDARY" ) || ( "$rgstate" == "ONLINE" ) ]]; then
                            print "$0()[$LINENO]($SECONDS): $HAUTILS/clRGmove $sOpt -d -i -g \"$rg\" -n \"$node\"" >>$CLMGR_TMPLOG  # Always log commands
                            VERBOSE_LOGGING="" $HAUTILS/clRGmove $sOpt -d -i -g "$rg" -n "$node"
                            rc=$?
                            print "clRGmove RC: $rc" >>$CLMGR_TMPLOG  # Always log command result
                        elif [[ "$rgstate" != "ONLINE" ]]; then
                            dspmsg -s 100 command.cat 480 "\nResource Group \"%1\$s\" is not ONLINE on node \"%2\$s\".\n\n" "$rg" "$node"
                            rc=$RC_ERROR
                        fi   
                    fi
                    (( $rc != RC_SUCCESS )) && (( failures++ ))

                done # end of nodes loop
                (( failures > 0 )) && rc=$RC_ERROR || rc=$RC_SUCCESS
            fi
        done
    fi

    #=======================================================================
    : If a user input error was detected, provide some helpful suggestions
    #=======================================================================
    if (( $rc == RC_MISSING_INPUT || $rc == RC_INCORRECT_INPUT )) && \
       [[ $CLVT_GUI == *([[:space:]]) ]]
    then
        CL=$LINENO cl_dspmsg -s $CLVT_SET $CLVT_MSGS 104 "For more information about available options and syntax, try\n\"$HAUTILS/clmgr %1\$s\". As an\nalternative, if the PowerHA SystemMirror man pages have been installed, invoke\n\"$HAUTILS/clmgr -hv\" (or \"/usr/bin/man clmgr\"),\nsearching for \"%2\$s\" in the displayed text.\n\n" \
        "offline resource_group -h" "RESOURCE GROUP:" "$CLVT_PROGNAME" 1>&2
    fi

    log_return_msg "$rc" "$0()" "$LINENO"
    return $?
} # End of "KLIB_HACMP_offline_resourcegroup()"
