Subversion Repositories DevTools

Rev

Rev 1530 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/bin/sh
#set -x
#========================================================
# **** Program Information ****
#
# Program Name      : reqlib.sh
#
# Program Type      : Bourne Shell Scripts(.sh)
#
# Original Author(s)  : Vasili Chatzimichail(vchatzim)
#
# Description / Purpose:
#     Called by the request script of a package providing a suite of
#     common functions to get user input.
#
# Exit Values:
#     NA. 
#
# References:
#     -None-
#
#========================================================


# Global Variables
#
REQLIB_ORA_TAB=/var/opt/oracle/oratab


# Function to get the module enable flag.
#
# Input is the module type and a string to describe the modlue.
#
reqlib_getOLSModuleEnable ()
{
    MODTYPE=$1
    MODSTR=$2
    MODDEF=$3

    MODDEF=`echo $MODDEF | tr '[A-Z]' '[a-z]'`

    printf "\n\n\n\n"
    printf "Will the $MODSTR module be enabled on this tier/node?\n"

    while [ 1 ]
    do
        case "$m_resp" in

        # Simple or Expert
        'y' | 'n')
            break
        ;;
        'q')
            exit 3;
        ;;
        *)
            m_resp=`ckstr -l 1 -d "$MODDEF" -p "(default: $MODDEF) [y,n,q=quit] :"`
            m_resp=`echo $m_resp | tr '[A-Z]' '[a-z]'`
        ;;
        esac
    done

    m_resp=`echo $m_resp | tr '[a-z]' '[A-Z]'`
    m_str="${MODTYPE}_ENABLED=$m_resp"
    eval $m_str
}



# Function to get the install type.
#
reqlib_getInstallType ()
{
    M_VAR=$1
    M_DEF=$2

    printf "\n\n\n\n"
    printf "Please enter the type of installation you would like,\n"
    printf "Note: s = SIMPLE install\n"
    printf "      e = EXPERT install\n"
    printf "      q = quit\n"

    while [ 1 ]
    do
        case "$m_resp" in

        # Simple or Expert
        's' | 'e')
            break
        ;;
        'q')
            exit 3;
        ;;
        *)
        m_resp=`ckstr -l 1 -d "$M_DEF" -p "(default: $M_DEF) [s,e,q=quit] :"`
        m_resp=`echo $m_resp | tr '[A-Z]' '[a-z]'`
        ;;
        esac
    done

    m_str="${M_VAR}=$m_resp"
    eval $m_str

    printf "\n\n\n\n"
    if [ "x$m_resp" = "xe" ]
    then
        printf "You have chosen an EXPERT installation.\n"
    else
        printf "You have chosen a SIMPLE installation.\n"
    fi
    printf "Please answer the following questions or accept the default paramaters.\n"

}


# ---------------------------------------------------------------------------------------
reqlib_testDBConnection ()
#
# this function tests the database connection based on the
# data entered by the user. Variables used are
# sid, user, passowrd and description.
#
# ---------------------------------------------------------------------------------------
{
    M_SID=$1
    M_USER=$2
    M_PASSWORD=$3
    M_DESC=$4

    printf "\n\n\n\n"

    # lets verify we can connect to the database
    #
    printf "$M_DESC\n"

    # do we even have access to sqlplus?
    #
    su - oracle -c ". .profile; sqlplus -? > /dev/null 2>&1"
    if [ "$?" -ne "0" ]
    then
        printf "Cannot connect to database $M_SID - check database before continuing.\n"
        exit 1;
    fi

    # we have access to sqlplus, lets try the database entered
    #
    rm -f /tmp/xx_lastsql.txt
    su - oracle -c ". .profile; sqlplus ${M_USER}/${M_PASSWORD}@${M_SID} <<EOF 1>/tmp/xx_lastsql.txt 2>&1
      select 1 from dual;
      exit
    EOF"

    if [ -f /tmp/xx_lastsql.txt ]
    then
        OK=`cat /tmp/xx_lastsql.txt | grep ORA- | wc -l`
        if [ "$OK" -ne "0" ]
        then
            printf "Cannot connect to database $M_SID - check database before continuing.\n"
            exit 1;
        else
            printf "Database connection OK.\n"
        fi
    else
        printf "Cannot connect to database $M_SID - check database before continuing.\n"
        exit 1;
    fi
}


# ---------------------------------------------------------------------------------------
reqlib_setVariableNoCheck ()
#
# ---------------------------------------------------------------------------------------
{
    M_VAR=$1
    M_DEF=$2
    M_DESC=$3
    
    printf "\n\n\n\n"
    printf "$M_DESC" 
    m_resp=`ckstr -d "$M_DEF" -p "(default: $M_DEF) [q=quit] :"`
    if [ "x$m_resp" = "xq" -o "x$m_resp" = "xQ" ]
    then
        exit 3;
    fi
    m_str="${M_VAR}=$m_resp"
    eval $m_str
}


# ---------------------------------------------------------------------------------------
reqlib_setVariableNoCheck_lc ()
#
# ---------------------------------------------------------------------------------------
{
    M_VAR=$1
    M_DEF=$2
    M_DESC=$3
    
    printf "\n\n\n\n"
    printf "$M_DESC" 
    m_resp=`ckstr -d "$M_DEF" -p "(default: $M_DEF) [q=quit] :"`
    if [ "x$m_resp" = "xq" -o "x$m_resp" = "xQ" ]
    then
        exit 3;
    fi
    m_resp=`echo $m_resp | tr '[A-Z]' '[a-z]'`
    m_str="${M_VAR}=$m_resp"
    eval $m_str
}


reqlib_setVariableNoCheck_uc ()
{
    M_VAR=$1
    M_DEF=$2
    M_DESC=$3
    
    printf "\n\n\n\n"
    printf "$M_DESC" 
    m_resp=`ckstr -d "$M_DEF" -p "(default: $M_DEF) [q=quit] :"`
    if [ "x$m_resp" = "xq" -o "x$m_resp" = "xQ" ]
    then
        exit 3;
    fi
    m_resp=`echo $m_resp | tr '[a-z]' '[A-Z]'`
    m_str="${M_VAR}=$m_resp"
    eval $m_str
}


reqlib_getIntegerResponse ()
{
    M_VAR=$1
    M_DESC=$2
    M_DEF=$3
    
    printf "\n\n\n\n"
    printf "$M_DESC" 
    m_resp=`ckint -d "$M_DEF" -p "(default: $M_DEF) [q=quit] :"`
    if [ "x$m_resp" = "xq" -o "x$m_resp" = "xQ" ]
    then
        exit 3;
    fi
    m_str="${M_VAR}=$m_resp"
    eval $m_str
}


# lets just test if the oracle config exists.
#
reqlib_testOratabConfig ()
{
    if [ ! -f "$REQLIB_ORA_TAB" ]
    then
        printf "ERROR: Failed to find file [$REQLIB_ORA_TAB].\n"
        printf "Check oracle installation before proceeding.\n"
        exit 1
    fi
}


reqlib_getRsOracleSidNoCheck ()
{
     reqlib_getRsOracleSid "$1" "$2" "YES"
}


# Function to get the report server Oracle SID.
#
reqlib_getRsOracleSid ()
{
    M_VAR=$1
    M_DEF=$2
    M_NOCHK=$3    # if empty then a check is done as per normal

    printf "\n\n\n\n"
    printf "Please enter the report server Oracle SID.\n"

    if [ -z "$M_NOCHK" ]; then
        m_SIDs=`egrep -v "#|\*" $REQLIB_ORA_TAB | cut -d: -f 1 | tr '[:space:]' | tr '[a-z] [A-Z]'`
        printf "The current list of active database SIDs on this machine include:\n"
        printf "$m_SIDs\n"
    fi

    m_resp=`ckstr -d "$M_DEF" -p "(default: $M_DEF) [q=quit] :"`
    if [ "x$m_resp" = "xq" -o "x$m_resp" = "xQ" ]
    then
        exit 3;
    fi

    if [ -z "$M_NOCHK" ]; then
        foundIt=0
        if [ -n "$m_SIDs" ]
        then
            for i in $m_SIDs
            do
                m_Match=`echo $i | egrep "^$m_resp$" | wc -l`
                if [ "$m_Match" -eq "1" ]
                then
                    foundIt=1
                fi
            done
        fi

        if [ "$foundIt" -eq "0" ]
        then
            printf "\n\n\n<<<WARNING: [$m_resp] SID entered does not match any in current active list.>>>\n"
        fi
    else
        printf "\n\n\n<<<NOTE:  Make sure the SID [$m_resp] entered is defined in the tnsnames.ora file>>>\n"
    fi

    m_str="${M_VAR}=$m_resp"
    eval $m_str
}


# Function to get a generic Oracle SID.
#
reqlib_getOracleSid ()
{
    M_VAR=$1
    M_DEF=$2
    M_TYPE=$3
    

    # first we need to get a list of the current SIDs on this machine.
    #
    m_SIDs=`egrep -v "#|\*" $REQLIB_ORA_TAB | cut -d: -f 1 | tr '[:space:]' | tr '[a-z] [A-Z]'`

    printf "\n\n\n\n"
    printf "Please enter the $M_TYPE server Oracle SID,\n"
    printf "the current list of active database SIDs on this machine include:\n"
    printf "$m_SIDs\n"

    m_resp=`ckstr -d "$M_DEF" -p "(default: $M_DEF) [q=quit] :"`
    if [ "x$m_resp" = "xq" -o "x$m_resp" = "xQ" ]
    then
        exit 3;
    fi

    # so the user has entered something..

    # convert what was entered to upper case
    m_resp=`echo $m_resp | tr '[:lower:]' '[:upper:]'`

    foundIt=0
    if [ -n "$m_SIDs" ]
    then
        for i in $m_SIDs
        do
            m_Match=`echo $i | egrep "^$m_resp$" | wc -l`
            if [ "$m_Match" -eq "1" ]
            then
                foundIt=1
            fi
        done
    fi

    if [ "$foundIt" -eq "0" ]
    then
        printf "\n\n\n<<<WARNING: [$m_resp] SID entered does not match any in current active list.>>>\n"
    fi

    m_str="${M_VAR}=$m_resp"
    eval $m_str
}



# ---------------------------------------------------------------------------------------
reqlib_getYesNoQuitResponse ()
#
# a simple function that determines yes/no respones
# we pass to the function:
#   1) the variable name as a string
#   2) the description displayed to the user
#   3) the default value
#
# ---------------------------------------------------------------------------------------
{
    m_VAR=$1
    m_DESC=$2
    m_DEFAULT=$3

    m_DEFAULT=`echo $m_DEFAULT | tr '[A-Z]' '[a-z]'`

    printf "\n\n\n\n"
    printf "$m_DESC\n"

    while [ 1 ]
    do
        case "$m_resp" in

        'y' | 'n')
            break
        ;;
        'q')
            exit 3;
        ;;
        *)
            m_resp=`ckstr -l 1 -d "${m_DEFAULT}" -p "(default: ${m_DEFAULT}) [y,n,q=quit] :"`
            m_resp=`echo $m_resp | tr '[A-Z]' '[a-z]'`
        ;;
        esac
    done

    m_resp=`echo $m_resp | tr '[a-z]' '[A-Z]'`
    m_str="${m_VAR}=$m_resp"
    eval $m_str
}


# Function to get proxy peer hostname.
#
reqlib_getHostname ()
{
    m_VAR=$1
    m_DESC=$2
    m_DEFAULT=$3

    printf "\n\n\n\n"
    printf "$m_DESC\n"

    m_resp=`ckstr -d "${m_DEFAULT}" -p "(default: ${m_DEFAULT}) [q=quit] :"`
    if [ "x$m_resp" = "xq" -o "x$m_resp" = "xQ" ]
    then
        exit 3;
    fi

    # lets test to see if the host is contactable
    #
    printf "\n"
    # lets ping the host with 5 sec timeout.
    /usr/sbin/ping $m_resp 5
    retVal=$?

    if [ $retVal != 0 ]
    then
        printf "WARNING: Host [$m_resp] not contactable, ENSURE you check the network config.\n"
    fi

    m_str="${m_VAR}=$m_resp"
    eval $m_str
}


# ---------------------------------------------------------------------------------------
reqlib_setProxyClientConnectionType ()
#
#    This function asks the user to select the proxy client connection type.
#
#    It assumes the input parameters of:
#
#    1.  connection type variable name
#    2.  conection type descriptive string variable name
#    3.  description presented to the user
#    4.  the currently set default value.
#
#    Currently the user can only select from two types of connections these include:
#    1. IPMS
#    2. RPC
#   
# ---------------------------------------------------------------------------------------
{
    m_CONN_TYPE=$1
    m_CONN_TYPE_STR=$2
    m_DESC=$3
    m_DEFAULT=$4

    m_DEFAULT=`echo $m_DEFAULT | tr '[A-Z]' '[a-z]'`

    printf "\n\n\n\n"
    printf "$m_DESC\n"
    printf "Note: i = IPMS proxy client connection type\n"
    printf "      r = RPC proxy client connection type\n"
    printf "      q = quit\n"

    while [ 1 ]
    do
        case "$m_resp" in

        # ipms or rpc
        'i' | 'r')
            break
        ;;
        'q')
            exit 3;
        ;;
        *)
        m_resp=`ckstr -l 1 -d "$m_DEFAULT" -p "(default: $m_DEFAULT) [i,r,q=quit] :"`
        m_resp=`echo $m_resp | tr '[A-Z]' '[a-z]'`
        ;;
        esac
    done

    m_str="${m_CONN_TYPE}=$m_resp"
    eval $m_str

    if [ "x$m_resp" = "xi" ]
    then
        m_str="${m_CONN_TYPE_STR}=IPMS"
    else
        m_str="${m_CONN_TYPE_STR}=RPC"
    fi
    eval $m_str

}


# ---------------------------------------------------------------------------------------
reqlib_updateFileTag ()
#
#    This function is used to update a tag in a file with a new string, globally.
#
#    It assumes the input parameters of:
#
#    1.  the location of the file to be updated
#    2.  the tag to replaced
#    3.  the value to be substitued
#
# ---------------------------------------------------------------------------------------
{
    m_file=$1
    m_tag=$2
    m_nStr=$3

    if [ "x$m_nStr" = "x" ]
    then
        m_tStr=""
    else
        m_tStr=`echo $m_nStr | sed 's/\//\\\\\//g'`
    fi


    echo "Updating file [$m_file] tag [$m_tag] with string [$m_nStr] ..."


    # in here we need to inform the pkg database that we have
    # updated the file from the original
    #
    installf -c none $PKGINST $m_file f


    sed "s/<$m_tag>/$m_tStr/g" $m_file > /tmp/xx.tmp
    if [ $? -eq 0 ]
    then

        cp /tmp/xx.tmp $m_file
        rm /tmp/xx.tmp

        echo "Updating file [$m_file] tag [$m_tag] completed."

    else

        echo "sed command failed, file [$m_file] not updated."

    fi


    # lets inform the pkg database that files have been updated.
    #
    installf -f $PKGINST

}


# ---------------------------------------------------------------------------------------
reqlib_updateFileTag2 ()
#
#    This function is used to update a tag in a file with a new string, globally.
#
#    It assumes the input parameters of:
#
#    1.  the location of the file to be updated
#    2.  the tag to replaced
#    3.  the value to be substitued
#
# ---------------------------------------------------------------------------------------
{
    m_file=$1
    m_tag=$2
    m_nStr=$3

    if [ "x$m_nStr" = "x" ]
    then
        m_tStr=""
    else
        m_tStr=`echo $m_nStr | sed 's/\//\\\\\//g'`
    fi

    sed "s/$m_tag/$m_tStr/g" $m_file > /tmp/xx.tmp
    if [ $? -eq 0 ]
    then
        cp /tmp/xx.tmp $m_file
        rm /tmp/xx.tmp
    else
        echo "sed command failed, file [$m_file] not updated."
    fi
}



# ---------------------------------------------------------------------------------------
reqlib_updateAllFileTags ()
#
#    This function is used to update tags in a file with a new string, globally.
#
#    It assumes the input parameters of:
#
#    1.    the location of the file to be updated
#    2 ... A number of args of type A=B where string A in File will be replaced by B
#
# ---------------------------------------------------------------------------------------
{
    m_file=$1
    shift

    sedScript="/tmp/updateAllFileTags.sed.$$"

    echo "Updating Tags in file [$m_file] as follows"
    while [ -n "$1" ] 
    do
        m_tag=`echo $1 | sed -e 's|^ *\(.*\)=.*$|\1|' -e 's| *$||'`
        m_rep=`echo $1 | sed -e 's|^.*= *\(.*\)$|\1|' -e 's| *$||'`
        echo "        [$m_tag] = [$m_rep]"
        echo "s|<$m_tag>|$m_rep|g" >> $sedScript
        shift
    done
    
    # in here we need to inform the pkg database that we have
    # updated the file from the original
    #
    installf -c none $PKGINST $m_file f


    eval sed -f $sedScript $m_file > /tmp/xx.tmp
    if [ $? -eq 0 ]
    then

        cp /tmp/xx.tmp $m_file
        rm /tmp/xx.tmp

        echo "Updating Tags for file [$m_file] completed."

    else

        echo "sed command failed, file [$m_file] not updated."

    fi
    rm $sedScript

    # lets inform the pkg database that files have been updated.
    #
    installf -f $PKGINST

}


# ---------------------------------------------------------------------------------------
reqlib_cleanLocalFiles ()
#
#   this function is used to clean any new local.profile, local.login
#   and local.cshrc files that are created by the system when you create
#   a new user. we never use these files.
#
#    It assumes the input parameters of:
#
#    1.  the location of the user's home directory
#
# ---------------------------------------------------------------------------------------
{
    m_HOMEDIR=$1

    if [ "x$m_HOMEDIR" = "x" ]
    then
        echo "ERROR: incorrect number of parameters supplied to reqlib_cleanLocalFiles () function."
        exit 1;
    fi

    rm -f $m_HOMEDIR/local.profile
    rm -f $m_HOMEDIR/local.login
    rm -f $m_HOMEDIR/local.cshrc
}



# ---------------------------------------------------------------------------------------
reqlib_getStrWithSpacesResponse ()
#
# ---------------------------------------------------------------------------------------
{
    M_VAR=$1
    M_DESC=$2
    M_DEF=$3
    
    printf "\n\n\n\n"
    printf "$M_DESC" 
    m_resp=`ckstr -d "$M_DEF" -r ".*" -p "(default: $M_DEF) [q=quit] :"`
    if [ "x$m_resp" = "xq" -o "x$m_resp" = "xQ" ]
    then
        exit 3;
    fi
    m_str="${M_VAR}=\"$m_resp\""
    eval $m_str
}


# ---------------------------------------------------------------------------------------
reqlib_getRsIDatasrc ()
#
# Function to get the report server data source interface type.
#
# ---------------------------------------------------------------------------------------
{
    M_VAR=$1
    M_DESC=$2

    printf "\n\n\n\n"
    printf "$M_DESC\n"

    m_resp=`ckitem -o -l "Data Source Type List" "oracle9i - Oracle 9i" "mssqlserver2000 - MS SQLServer 2000"`
    if [ "x$m_resp" = "xq" -o "x$m_resp" = "xQ" ]
    then
        exit 3;
    fi

    m_str="${M_VAR}=$m_resp"
    eval $m_str

    if [ "x$m_resp" = "xoracle9i" ]
    then

        # lets just be sure oracle exists on this machine
        #
        reqlib_testOratabConfig

    fi
}


# ---------------------------------------------------------------------------------------
reqlib_getListEntry ()
#
# Function to get a list item selected by the user.
# The list is provided in a simple text file
# with the format:
#
# item - description
# 
# if the user enters a 'q' or 'Q' the funtion exits.
#
# ---------------------------------------------------------------------------------------
{

    M_VAR=$1
    M_DESC=$2
    M_IFILE=$3
    M_SORT=$4

    printf "\n\n\n\n"
    printf "$M_DESC\n"

    if [ -f "./$M_IFILE" ]
    then
        _file="./$M_IFILE"
    else
        _file="$INFODIR/$M_IFILE"
    fi


    if [ "x$M_SORT" = "xNO_SORT" ]
    then
        _sortOption="-n"
    else
        _sortOption=""
    fi


    m_resp=`ckitem -o -f $_file $_sortOption`
    if [ "x$m_resp" = "xq" -o "x$m_resp" = "xQ" ]
    then
        exit 3;
    fi

    m_str="${M_VAR}=$m_resp"
    eval $m_str
}


# ---------------------------------------------------------------------------------------
reqlib_testDBConnection2 ()
#
# this function tests the database connection based on the
# data entered by the user. 
# 
# The input Variables used are:
#
#     datasrc,
#     sid, 
#     user, 
#     passowrd
#     description.
#
# ---------------------------------------------------------------------------------------
{
    M_DATASRC=$1
    M_SID=$2
    M_USER=$3
    M_PASSWORD=$4
    M_DESC=$5



    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/easysoft/lib
    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/easysoft/oob/client
    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/easysoft/unixODBC/lib
    export LD_LIBRARY_PATH



    printf "\n\n\n\n"

    # lets verify we can connect to the database
    #
    printf "$M_DESC\n"

    # we know what to do with various datasrc types
    #
    if [ "x$M_DATASRC" = "xoracle9i" ]
    then

        # do we even have access to sqlplus?
        #
        su - oracle -c ". .profile; sqlplus -? > /dev/null 2>&1"
        if [ "$?" -ne "0" ]
        then
            printf "Cannot connect to database $M_SID - check database before continuing.\n"
            exit 1;
        fi

        # we have access to sqlplus, lets try the database entered
        #
        rm -f /tmp/xx_lastsql.txt
        su - oracle -c ". .profile; sqlplus ${M_USER}/${M_PASSWORD}@${M_SID} <<EOF 1>/tmp/xx_lastsql.txt 2>&1
          select 1 from dual;
          exit
        EOF"

        if [ -f /tmp/xx_lastsql.txt ]
        then
            OK=`cat /tmp/xx_lastsql.txt | grep ORA- | wc -l`
            if [ "$OK" -ne "0" ]
            then
                printf "Cannot connect to database $M_SID - check database before continuing.\n"
                exit 1;
            else
                printf "Database connection OK.\n"
            fi
        else
            printf "Cannot connect to database $M_SID - check database before continuing.\n"
            exit 1;
        fi

    elif [ "x$M_DATASRC" = "xmssqlserver2000" ]
    then

        # here we test the SQL connection

        myEasySoftLoc="/usr/local/easysoft"
        myPinger="$myEasySoftLoc/bin/oobpingd"
        if [ -d "$myEasySoftLoc" ]
        then

            if [ -x "$myPinger" ]
            then

                $myPinger -d "DSN=$M_SID;UID=$M_USER;PWD=$M_PASSWORD" > /dev/null 2>&1
                if [ $? -ne 0 ]
                then

                    printf "Cannot connect to database $M_SID - check database before continuing.\n"
                    exit 1;

                else

                    printf "Database connection OK.\n"

                fi

            else

                printf "ERROR cannot locate easysoft tool [$myPinger].\n"
                exit 1;

            fi

        else

            printf "ERROR cannot locate easysoft default installation location [$myEasySoftLoc].\n"
            exit 1;

        fi


    else

        echo "\n\n*** Database connection test for datasource [$M_DATASRC] - NOT SUPPORTED (yet!). ***"

    fi

}


# EOF, done.