#!/bin/ksh
#  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_r714 src/43haes/usr/sbin/cluster/events/utils/cl_wlm_reconfig.sh 1.14 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2001,2008 
# 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_wlm_reconfig.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
############################################################################
#
#  Name:  cl_wlm_reconfig
#
#  This script is used to determine whether the current cluster configuration
#  requires WLM support for this node, and to copy the WLM configuration
#  files into the appropriate directory.
#
# Arguments:    CALLING_EVENT 
#
# Returns:      0 - Success
#               1 - Failure
#               2 - Usage error
#               3 - No Reconfiguration Required
#
# Called by:    node_up and reconfig_resource_acquire
#
# Environment:  VERBOSE_LOGGING
#
############################################################################

############################################################################
# Function:   build_class_list
# Purpose:    read HACMPgroup and HACMPresource to build the 
#             HA_WLM_CLASSES list
# Parameters: none
# Return:     none
############################################################################
build_class_list()
{
    typeset PS4_FUNC="build_class_list"
    [[ "$VERBOSE_LOGGING" = "high" ]] && set -x
    PRIMARY=""
    SECONDARY=""

    # build the primary and secondary group lists for this node 
    GROUP=""
    NODES=""
    STARTUP_PREF=""
    FALLOVER_PREF=""
    FALLBACK_PREF=""
    clgetgrp -c | grep -v -E '^#' | \
    while read line
    do
        GROUP=`echo ${line} | cut -d: -f1`	# first field is group name
        NODES=`echo ${line} | cut -d: -f4` # node list
        STARTUP_PREF=`echo ${line} | cut -d: -f5` # Startup Pref for custom groups
        FALLOVER_PREF=`echo ${line} | cut -d: -f6` # 
        FALLBACK_PREF=`echo ${line} | cut -d: -f7` # 

        if [[ -z "$GROUP$NODES$STARTUP_PREF$FALLOVER_PREF$FALLBACK_PREF" ]]
	    then
		cl_log 7415 "${PROGNAME}: Failed getting node information from clgetgrp.\n" ${PROGNAME} "clgetgrp"
		exit 1;
	    fi

        # Resource groups with 'Online On HomeNode' & 'Online On First
        # Available Node' can have primary and secondary classes

        if [[ "${STARTUP_PREF}" = "OHN" || "${STARTUP_PREF}" = "OFAN" ]]
        then
            if [[ "${LOCALNODENAME}" = "${NODES%% *}" ]]
            then
                PRIMARY="${PRIMARY} ${GROUP}"
            else
                # use a for loop instead of a grep to make
                # sure that we match the full token
                for node in ${NODES}
                do
                    if [[ "${LOCALNODENAME}" = "${node}" ]]
                    then
                        SECONDARY="${SECONDARY} ${GROUP}"
                        break
                    fi
                done
            fi
            # All other groups can only have primary classes
        else 
            # use a for loop instead of a grep to make
            # sure that we match the full token
            for node in ${NODES}
            do
                if [[ "${LOCALNODENAME}" = "${node}" ]]
                then
                    PRIMARY="${PRIMARY} ${GROUP}"
                    break
                fi
            done
        fi
    done

    # get the primary classes
    for one_group in ${PRIMARY}
    do
        WLM_PRIMARY=`odmget -q \
            "group = ${one_group} and name = 'WLM_PRIMARY'" HACMPresource | \
	     awk '$1 = /value/ { print $3 }' | sed 's/"//g'`
        if [[ -n "${WLM_PRIMARY}" ]]
        then
            HA_WLM_CLASSES="${HA_WLM_CLASSES} ${WLM_PRIMARY}"  
        fi
    done

    # get the secondary classes
    for one_group in ${SECONDARY}
    do
        WLM_SECONDARY=`odmget -q \
            "group = ${one_group} and name = 'WLM_SECONDARY'" HACMPresource | \
             awk '$1 = /value/ { print $3 }' | sed 's/"//g'`
        if [[ -n "${WLM_SECONDARY}" ]]
        then
            HA_WLM_CLASSES="${HA_WLM_CLASSES} ${WLM_SECONDARY}"  
        else
            WLM_PRIMARY=`odmget -q \
               "group = ${one_group} and name = 'WLM_PRIMARY'" HACMPresource | \
	        awk '$1 = /value/ { print $3 }' | sed 's/"//g'`
            if [[ -n "${WLM_PRIMARY}" ]]
            then
                HA_WLM_CLASSES="${HA_WLM_CLASSES} ${WLM_PRIMARY}"  
            fi
        fi
    done
}

############################################################################
# Function:   copy_config_files
# Purpose:    copies config files from /usr/[es/]sbin/cluster/etc/wlm into
#             the HACMP WLM configuration directory without overwriting
#             files that already exist there
# Parameters: none
# Return:     none
############################################################################
copy_config_files()
{
    typeset PS4_FUNC="copy_config_files"
    [[ "$VERBOSE_LOGGING" = "high" ]] && set -x

    # make sure the HA WLM config directory exists
    if [[ ! -d "/etc/wlm/${HA_WLM_CONFIG}" ]]
    then
        mkdir "/etc/wlm/${HA_WLM_CONFIG}"
        if [ $? -ne 0 ]
        then
            cl_log 7410 \
	        "${PROGNAME}: Cannot create subdirectory ${HA_WLM_CONFIG} in /etc/wlm\n" \
                "${PROGNAME}" "${HA_WLM_CONFIG}"
            exit 1
        fi
    fi

    for one_file in ${WLM_CONFIG_FILES}
    do
        # don't lose previously existing configuration files
        if [[ ! -e "/etc/wlm/${HA_WLM_CONFIG}/HA_prev_config_subdir" || \
              "true" = "${HA_WLM_CONFIG_CHANGED}" ]]
        then
                if [[ -e "/etc/wlm/${HA_WLM_CONFIG}/${one_file}" ]]
                then
                        mv "/etc/wlm/${HA_WLM_CONFIG}/${one_file}" \
                           "/etc/wlm/${HA_WLM_CONFIG}/${one_file}.prev"
                        if [ $? -ne 0 ]
                        then
                                cl_log 7411 \
				    "${PROGNAME}: Cannot rename file ${one_file}\n" \
                                    "${PROGNAME}" "${one_file}"
                                exit 1
                        fi
                fi
        else
                if [[ -e "/etc/wlm/${HA_WLM_CONFIG}/${one_file}" ]]
                then
                        mv "/etc/wlm/${HA_WLM_CONFIG}/${one_file}" \
                           "/etc/wlm/${HA_WLM_CONFIG}/${one_file}.hacmp"
                        if [ $? -ne 0 ]
                        then
                                cl_log 7411 \
				    "${PROGNAME}: Cannot rename file ${one_file}\n" \
                                    "${PROGNAME}" "${one_file}"
                                exit 1
                        fi
                fi
        fi

        if [[ -e "/usr/es/sbin/cluster/etc/wlm/${one_file}" ]]
        then
            # copy config files from synch directory to active directory
            if [[ "rules" = "${one_file}" ]]
            then
                write_rules_file
                : exit status of write_rules_file is: $?
            else
                cp "/usr/es/sbin/cluster/etc/wlm/${one_file}" \
                   "/etc/wlm/${HA_WLM_CONFIG}/${one_file}"
                if [ $? -ne 0 ]
                then
                    cl_log 7414 \
                        "${PROGNAME}: Cannot write file ${one_file} in /etc/wlm/${HA_WLM_CONFIG}\n" \
                        "${PROGNAME}" "${one_file}" \
                        "/etc/wlm/${HA_WLM_CONFIG}"
                    exit 1
                fi
            fi
        fi
   done
}

############################################################################
# Function:   write_rules_file
# Purpose:    sets up rules file, filtering out rules for non-HACMP and
#             non-default classes
# Parameters: none
# Return:     none
############################################################################
write_rules_file() 
{
    typeset PS4_FUNC="write_rules_file"
    [[ "$VERBOSE_LOGGING" = "high" ]] && set -x

    # create the empty rules file and test that it can be created properly
    touch "/etc/wlm/${HA_WLM_CONFIG}/rules"
    if [ $? -ne 0 ]
    then
        cl_log 7414 \
            "${PROGNAME}: Cannot write file rules in /etc/wlm/${HA_WLM_CONFIG}\n" \
            "${PROGNAME}" "rules" \
            "/etc/wlm/${HA_WLM_CONFIG}"
        exit 1
    fi
    # read rules file line by line
    cat "/usr/es/sbin/cluster/etc/wlm/${one_file}" | while read line
    do
        if [[ -n $(echo "${line}" | awk '$1 ~ /\*.*/') ]]
        then
            # move any comments straight over into the rules file
            echo "${line}" >> "/etc/wlm/${HA_WLM_CONFIG}/rules"
        else
            # only move real rules if the class name is one from HA_WLM_CLASSES
            CUR_CLASS=`echo "${line}" | \
                awk '{ print $1 }' | awk 'BEGIN { FS = "-" } { print $1 }'`
            # use a for loop instead of a grep to ensure that 
            # we match the full token
            for one_class in ${HA_WLM_CLASSES}
            do
                if [[ "${one_class}" = "${CUR_CLASS}" ]]
                then
                    # the rules for this class should be active
                    echo "${line}" >> "/etc/wlm/${HA_WLM_CONFIG}/rules"
                    break
                fi
            done
        fi
    done
}

############################################################################
# Function:   usage
# Purpose:    display usage information and exit
# Parameters: none
# Returns:    none
############################################################################
usage()
{
    typeset PS4_FUNC="usage"
    echo "Usage: ${progname} <current event name>\n"
    exit 2
}

############################################################################
# Function:   main script body
# Purpose:    primary control mechanism for the script
# Parameters: $1 = name of calling event (node_up or reconfig_resource)
# Returns:    0  = successful reconfiguration 
#             1  = failed reconfiguration or no reconfiguration required
#             2  = Usage error
#             3  = No Reconfiguration Required
############################################################################

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

SCD="/usr/es/sbin/cluster/etc/objrepos/stage"
ACD="/usr/es/sbin/cluster/etc/objrepos/active"

EMULATE="${EMULATE:-REAL}"

CALLING_EVENT="$1"
# list of WLM classes required by RGs this node can acquire
HA_WLM_CLASSES=""
# name of specified HACMP wlm configuration
HA_WLM_CONFIG=`clwlmruntime -l -d "${ACD}" | awk 'BEGIN { FS = ":" } $1 !~ /^#.*/ { print $1 }'`
if [[ -z "$HA_WLM_CONFIG" ]]
then
    cl_log 7416 \
    	"${PROGNAME}: Cannot determine name of HA WLM configuration\n" \
	${PROGNAME}
    exit 1
fi

# important WLM configuration files
WLM_CONFIG_FILES="classes limits shares rules"

# need to read from SCD for reconfig_resources event
if [[ "reconfig_resources" = "${CALLING_EVENT}" ]]
then
        export ODMDIR="${SCD}"
fi

build_class_list
if [[ -z "${HA_WLM_CLASSES}" ]]
then
        exit 3
fi
# add the three default classes to the list
HA_WLM_CLASSES="${HA_WLM_CLASSES} Default System Shared" 

# handle reconfig_resources event
HA_WLM_CONFIG_CHANGED="false"
NEW_HA_WLM_CONFIG=""
if [[ "reconfig_resources" = "${CALLING_EVENT}" ]]
then
        NEW_HA_WLM_CONFIG=`clwlmruntime -l | awk 'BEGIN { FS = ":" } $1 !~ /^#.*/ { print $1 }'`
        if [[ "${HA_WLM_CONFIG}" != "${NEW_HA_WLM_CONFIG}" ]]
        then
                HA_WLM_CONFIG_CHANGED="true"
        fi
fi
if [[ "true" = "${HA_WLM_CONFIG_CHANGED}" ]]
then
        # create new HA configuration subdirectory if it doesn't already exist
        if [[ ! -d "/etc/wlm/${NEW_HA_WLM_CONFIG}" ]]
	then
            if [[ "$EMULATE" = "EMUL" ]]
            then
                cl_echo 3020 "NOTICE >>>> The following command was not executed <<<<  \n"
                echo "mkdir /etc/wlm/${NEW_HA_WLM_CONFIG}\n"
            else
                mkdir "/etc/wlm/${NEW_HA_WLM_CONFIG}"
                if [ $? -ne 0 ]
                then
                    cl_log 7410 \
                        "${PROGNAME}: Cannot create subdirectory ${NEW_HA_WLM_CONFIG} in /etc/wlm\n" \
                        "${PROGNAME}" "${NEW_HA_WLM_CONFIG}"
                    exit 1
                fi
            fi
        fi

        if [[ -e "/etc/wlm/${HA_WLM_CONFIG}/HA_prev_config_subdir" ]]
        then
            if [[ "$EMULATE" = "EMUL" ]]
            then
                cl_echo 3020 "NOTICE >>>> The following command was not executed <<<<  \n"
                echo "mv /etc/wlm/${HA_WLM_CONFIG}/HA_prev_config_subdir\n"
                echo "   /etc/wlm/${NEW_WLM_CONFIG}/HA_prev_config_subdir\n"
            else
                # don't forget non-HA WLM configuration
                mv "/etc/wlm/${HA_WLM_CONFIG}/HA_prev_config_subdir" \
                   "/etc/wlm/${NEW_HA_WLM_CONFIG}/HA_prev_config_subdir"
                if [ $? -ne 0 ]
                then
                        cl_log 7414 \
                            "${PROGNAME}: Cannot write file HA_prev_config_subdir in directory /etc/wlm/${NEW_HA_WLM_CONFIG}\n" \
                            "${PROGNAME}" "HA_prev_config_subdir" \
                            "/etc/wlm/${NEW_HA_WLM_CONFIG}"
                        exit 1
                fi
            fi
        fi
        # we're done with the update, now process everything with
        # the new directory name
        HA_WLM_CONFIG="${NEW_HA_WLM_CONFIG}"
else
        # if configuration hasn't changed, then we only need to
        # create HA configuration subdirectory if it doesn't already exist
        if [[ ! -d "/etc/wlm/${HA_WLM_CONFIG}" ]]
        then
            if [[ "$EMULATE" = "EMUL" ]]
            then
                cl_echo 3020 "NOTICE >>>> The following command was not executed <<<<  \n"
                echo "mkdir /etc/wlm/${HA_WLM_CONFIG}\n"
            else
                mkdir "/etc/wlm/${HA_WLM_CONFIG}"
                if [ $? -ne 0 ]
                then
                    cl_log 7410 \
                        "${PROGNAME}: Cannot create subdirectory ${HA_WLM_CONFIG} in /etc/wlm\n" \
                        "${PROGNAME}" "${HA_WLM_CONFIG}"
                    exit 1
                fi
            fi
        fi
fi

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

exit 0

