#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 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/cspoc/utilities/cli_extendvg.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2003,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 
#
# @(#)  7d4c34b 43haes/usr/sbin/cluster/cspoc/utilities/cli_extendvg.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#
###############################################################################
#
#   COMPONENT_NAME: CSPOC Command Line Interface
#
# Name:
#   cli_extendvg
#
# Description:
#   Use C-SPOC to run the extendvg command with the given parameters and make the
#   volume group definition known on all cluster nodes
#
# Arguments:
#   Same as for the extendvg command; assumed validated by caller
#
#   If '-f' is not supplied, it will be forced
#
#   Physical volume names are assumed valid on the node this command is run
#   on; C-SPOC will handle the case where the disk names are different on
#   different nodes
#
#   Setting the environment variable _DEBUG to 1 -> 9 turns on levels of
#   C-SPOC debugging
#
# Return Values:
#   As set by cl_extendvg
#
# Notes:
#   The '-f' flag is passed on cl_extendvg to surpress unnecessary checking.
#   As a consequenc, the operation will proceed even if not all nodes are
#   accessable, and the '-f' will be passed through to the native extenvg
#   command.
#
################################################################################

[[ "$VERBOSE_LOGGING" == "high" ]] && set -x
typeset DEBUG
typeset cl_extendvg_args

#
#   Variables needed by cl_extendvg
#	_REFNODE - the node on which the hdisk names are chosen, since they
#			may differ across the cluster
#	_CSPOC_MODE - working with shared (as opposed to concurrent) volume
#			groups
#	_CSPOC_CALLED_FROM_SMIT - can skip checks on input
#
export _REFNODE=$(/usr/es/sbin/cluster/utilities/get_local_nodename)
export _CSPOC_MODE="shared"
export _CSPOC_CALLED_FROM_SMIT="true"

# Check if RBAC is enabled
typeset is_rbac_enabled=""
is_rbac_enabled=$(/usr/es/sbin/cluster/utilities/clodmget -nq "group=LDAPClient and name=RBACConfig" -f value HACMPLDAP 2>/dev/null)

# For a non root user, append user name to the output file.
[[ -z "$LOGIN" ]] && user_name=$(id -nu 2>/dev/null) || user_name="$LOGIN"
if [[ $user_name != "root" && $is_rbac_enabled == "YES" ]];then
    CL_DATFILE="/var/hacmp/tmp/cllspvids.out"_$user_name
else
    CL_DATFILE="/var/hacmp/tmp/cllspvids.out"
fi
#
:   Parse command line, to get at the list of disks at the end
#
while getopts 'fp:' option
do
    case $option in
	f ) 
	    ;;
	p )
	    MirrorPool="-p $OPTARG"
	    ;;
    esac
done
shift $(( OPTIND-1 ))

#
:   Pick up volume group name and list of disks
#
VG=$1
shift
DiskList=$*
	
if [[ -z $VG || -z $DiskList ]]
then
    #
    :	Cause error message to be printed
    #
    extendvg
    return 1
fi

#
:   Construct the node list so that the local node shows up as first in the
:   list, since the physical volume names are relative to the local node.
:   This is a comma separated, quote delimited list of the nodes this change
:   is to be made on.  Its set to all cluster nodes.
#
all_nodes=$(/usr/es/sbin/cluster/utilities/clodmget -q "object = VERBOSE_LOGGING" -f name -n HACMPnode)
for node in $all_nodes 
do
    if [[ $node != $_REFNODE ]]
    then
        other_nodes=${other_nodes:+"${other_nodes},"}$node
    fi
done
export _NODE_LIST="$_REFNODE,$other_nodes"
print "$all_nodes" | wc -w | read nodes_cnt	# this many nodes

#
:   Get a list of free hdisk to PVID mappings across the cluster
#
if ! /usr/es/sbin/cluster/sbin/cl_lspvids -cspoc "-n $_NODE_LIST"  > $CL_DATFILE 2>&1
then
    return 1
fi

#
:   Change the ownership of output file, for a non root user.
#
[[ $user_name != "root" ]] && chown $user_name $CL_DATFILE
#
:   The C-SPOC command takes a list of PVIDs, so convert the given 
:   list of disks to a corresponding list of PVIDs.
#
lspv_out=$(LC_ALL=C lspv)
PVID_list=""
for disk in $DiskList
do
    if ! print -- "$lspv_out" | grep -w $disk | read skip PVID VG_name rest
    then
	#
	:   Cause "cannot find $disk" error message to be printed
	:   if the disk is not known on the local node
	#
	lspv $disk
	return 1

    elif [[ $VG_name != "None" ]]
    then
	dspmsg -s 33 cspoc.cat 5 "Disk $disk is already in volume group $VG_name" $disk $VG_name
	return 1
    fi
    found_cnt=$( grep -c $PVID $CL_DATFILE )
    if [[ $found_cnt == $nodes_cnt ]]	# any entry for that PVID on each node
    then
	PVID_list=${PVID_list:+"${PVID_list} "}$PVID
    else
	#
	:   $disk with PVID $PVID is not valid on all nodes
	#
	dspmsg -s 33 cspoc.cat 4 "Disk $disk with PVID $PVID is not valid on all nodes" $disk $PVID
	return 1
    fi
done

#
:   If a _DEBUG value has been set, pass it through
#
if [[ -n $_DEBUG ]]
then
    #
    :	The debug level is a number, 1 through 9.  Those values are passed
    :	through.  Anything else is turned into '1'
    #
    integer dbg_level=$_DEBUG
    if (( $dbg_level < 10 && $dbg_level >= 1 )) ; then
	DEBUG="-d $_DEBUG"
    else
	DEBUG="-d 1"
    fi
fi

#
:   The arguments to cl_extendvg are the same as passed in, with hdisk names
:   converted to PVIDs, and '-f' to skip prompting
#
cl_extendvg_args="-f $MirrorPool $VG $PVID_list"

#
:   Invoke the cspoc code to extend the volume group and make it known on all
:   cluster nodes.  
#
/usr/es/sbin/cluster/sbin/cl_extendvg -cspoc "-f $DEBUG -n $_NODE_LIST" $cl_extendvg_args
return $?
