#!/bin/sh 
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/haws/sbin/subsys/was/import.sh 1.19 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2004,2011 
# 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 
# @(#)37	1.19 src/43haes/usr/sbin/cluster/haws/sbin/subsys/was/import.sh, hacmp.assist, 61haes_r714 11/28/11 15:05:18
#
###############################################################################
#
# This is an application-specific import script that reads the
# WebSphere configuration files and translates it into HACMP specific 
# commands. The output of this script is another script that can be 
# called to perform the actual import into HACMP. 
###############################################################################

# WAS Package name
WAS_PACKAGE_NAME="WSBAA"

# Version 5.0 WebSphere
WAS_VERSION_5="5.0"

# I don't know if this is the right version, but if not, this is the
# only place that it needs to be changed
WAS_VERSION_6="6.0"

# Global variable for the WAS Version. We carry it around because
# some of the functions may need to do different things depending
# on the version of WAS. This variable is defined to be empty here,
# and it's set correctly in the function 'verify_websphere_version'
WAS_VERSION=""

# The WAS_CELL is set to the name of the cell we're actively configuring
# The cell name could be set on the command line, or it can be 
# determined automatically by searching down through the directories
# of the WebSphere configuration files. If multiple cells are defined,
# then the user must specify the name of the cell to use.
WAS_CELL=""

# The WAS_NODE contains the node we are trying to make highly available
WAS_NODE=""

# The WAS_SERVER_NAME is the name of the server we need to query to determine
# if the app server is healthy. This value will be used as a config
# param to the monitor scripts
WAS_SERVER_NAME=""

# The directory where WebSphere is installed
WAS_INSTALL_DIR=""

# The volume group where WebSphere is configured. We'll need to make sure
# this is made highly available.
WAS_VG=""

# Specifies the name of the node where this application server will fallover
# to.
WAS_FALLOVER_NODE=""

WAS_SERVICE_LABEL=""

IHS_PACKAGE_NAME="IHS"
IHS_INSTALL_DIR=""
IHS_VG=""

#
###############################################################################
#
# SCRIPT DEFINITION SECTION
#
###############################################################################
# Define Functions
###############################################################################

###############################################################################
# Function: verify_websphere_version
###############################################################################
#
# Make sure that WebSphere is at version 5.0.0
###############################################################################
verify_websphere_version() {

    logmsg HAWS_DEBUG "$MSG_DEBUG_MSG" "Checking websphere version...\n"
    WAS_VERSION=`lslpp -Lc "$WAS_PACKAGE_NAME" 2>/dev/null | sed -e '/^#/d' | cut -f 3 -d':' | cut -f 1,2 -d '.'`
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "WebSphere Version = $WAS_VERSION\n"
    
    # We need to be prepared to support version 6 of WebSphere so
    #  
    case $WAS_VERSION in
	$WAS_VERSION_5)
	    logmsg HAWS_INFO "$MSG_WAS_VER5_DETECTED" "WebSphere Version 5.0 detected\n"
	    ;;
	$WAS_VERSION_6)
	    logmsg HAWS_INFO "$MSG_WAS_VER6_DETECTED" "WebSphere Version 6.0 detected\n"
	    logmsg HAWS_ERROR "$MSG_WAS_VER6_NOTSUPPORTED" "Version 6.0 is not supported\n"
	    exit $HAWS_EXIT_FAIL
	    ;;
	"")
	    logmsg HAWS_ERROR "$MSG_WAS_NOT_INSTALLED" "WebSphere could not be found.  Please install WebSphere v5.0\n"
	    exit $HAWS_EXIT_FAIL
	    ;;
	*)
	    logmsg HAWS_ERROR "$MSG_WAS_UNKNOWN_VERSION" "Unsupported version of WebSphere: %s.  Please install WebSphere v5.0\n" $WAS_VERSION
	    exit $HAWS_EXIT_FAIL
    esac
}

###############################################################################
# Function: get_websphere_install_dir
###############################################################################
#
# Get the location where WebSphere is installed. Also, get the volume group
# where websphere has its configuration files.
###############################################################################
get_websphere_install_dir() {
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Getting websphere installation directory...\n"
    WAS_INSTALL_DIR=`lslpp -Lc $WAS_PACKAGE_NAME 2>/dev/null | sed -e '/^#/d' | cut -f9 -d':'`
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Directory = $WAS_INSTALL_DIR\n"
}

###############################################################################
# Function: get_ihs_install_dir
###############################################################################
#
# Get the location where IBM HTTP Server is installed. Also, get the volume group
# where websphere has its configuration files.
###############################################################################
get_ihs_install_dir() {
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Getting IHS installation directory...\n"
    IHS_INSTALL_DIR=`lslpp -Lc $IHS_PACKAGE_NAME 2>/dev/null | sed -e '/^#/d' | cut -f9 -d':'`
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Directory = \"$IHS_INSTALL_DIR\"\n"
}

###############################################################################
# Function: get_websphere_vg
###############################################################################
#
# Gets the volume group where the websphere configuration files exist.
###############################################################################
get_websphere_vg() {

    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Getting the vg for WebSphere...\n"
    VG=""
    if [[ -d "$WAS_INSTALL_DIR" ]]; then
	findvg $WAS_INSTALL_DIR"/bin"
	if [[ $? -eq HAWS_EXIT_FAIL ]]; then
	    exit $HAWS_EXIT_FAIL
	fi
	WAS_VG=$VG
    fi
    if [[ "$WAS_VG" = "" ]]; then
	logmsg HAWS_ERROR "$MSG_WAS_UNKNOWN_VG" "Can't get the volume group for WebSphere directory: %s\n" $WAS_INSTALL_DIR
	exit $HAWS_EXIT_FAIL
    else
	logmsg HAWS_INFO  "$MSG_WAS_VG_FOUND" "WAS Volume group = %s\n" $WAS_VG
    fi
}

###############################################################################
# Function: get_ihs_vg
###############################################################################
#
# Gets the volume group where the ihs configuration files exist.
###############################################################################
get_ihs_vg() {
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Getting the vg for IBM HTTP Server...\n"
    VG=""
    if [[ -d "$IHS_INSTALL_DIR" ]]; then
	findvg $IHS_INSTALL_DIR"/bin"
	if [[ $? -eq HAWS_EXIT_FAIL ]]; then
	    exit $HAWS_EXIT_FAIL
	fi
	IHS_VG=$VG
    fi
    if [[ "$IHS_VG" = "" ]]; then
	logmsg HAWS_WARN "$MSG_IHS_UNKNOWN_VG" "Can't get the volume group for IBM HTTP Server directory: %s\n" "$IHS_INSTALL_DIR"
    else
	logmsg HAWS_INFO  "$MSG_IHS_VG_FOUND" "IHS Volume group = %s\n" $IHS_VG
    fi
}

###############################################################################
# Function: validate_webpshere_config
###############################################################################
#
# Validate the websphere configuration information against HACMP. Make
# sure it makes sense to import this stuff.
###############################################################################
validate_websphere_config() {
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Validating WebSphere configuration..."

    conf_file="$IHS_INSTALL_DIR/conf/httpd.conf"

    if [[ ! -f $conf_file || $IHS_INSTALL_DIR != "" && -z $(grep "^ServerName" $conf_file | grep -w "$WAS_SERVICE_LABEL") ]] ; then 
	logmsg HAWS_WARN "$MSG_IHS_WARN_SERVERNAME" "The supplied PowerHA SystemMirror service label (%s) is not found in the IHS ServerName in httpd.conf.\n" $WAS_SERVICE_LABEL
    fi

    # If not Validating, there is no need to do anything else.
    if [[ $VALIDATE_FLAG -ne 1 ]] ; then
	return
    fi

    # Check for the configuration file
    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking Configuration file $cfg_script"
    if [[ ! -f $cfg_script ]] ; then
	logmsg HAWS_ERROR $MSG_CONFIG_FILE_NOT_FOUND "The configuration file %s was not found.\n" $cfg_script
	return
    fi

    # Check the Configuration script for matching items
    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking WAS_SERVER_NAME $WAS_SERVER_NAME"
    if [[ -z $(grep -w "WAS_SERVER_NAME=\"$WAS_SERVER_NAME\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_WAS_NO_MATCH_SERVER_NAME "The WAS Server Name  %s does not match value in %s\n" $WAS_SERVER_NAME $cfg_script
    fi

    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking WAS_INSTALL_DIR $WAS_INSTALL_DIR"
    if [[ -z $(grep -w "WAS_INSTALL_DIR=\"$WAS_INSTALL_DIR\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_WAS_NO_MATCH_INSTALL_DIR "The WAS Installation Directory %s does not match value in %s\n" $WAS_INSTALL_DIR $cfg_script
    fi

    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking WAS_VG $WAS_VG"
    if [[ -z $(grep -w "WAS_VG=\"$WAS_VG\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_WAS_NO_MATCH_VG "The WAS Volume Group %s does not match value in %s\n" $WAS_VG $cfg_script
    fi

    logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking WAS_SERVICE_LABEL $WAS_SERVICE_LABEL"
    if [[ -z $(grep -w "WAS_SERVICE_LABEL=\"$WAS_SERVICE_LABEL"\" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_WAS_NO_MATCH_SERVICE_LABEL "The WAS Service Label %s does not match value in %s\n" $WAS_SERVICE_LABEL $cfg_script
    fi

    if [[ $IHS_INSTALL_DIR != "" ]] ; then
	logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking IHS_INSTALL_DIR $IHS_INSTALL_DIR"
	if [[ -z $(grep -w "IHS_INSTALL_DIR=\"$IHS_INSTALL_DIR\"" $cfg_script) ]] ; then
	    logmsg HAWS_ERROR $MSG_IHS_NO_MATCH_INSTALL_DIR "The IHS Installation Directory %s does not match value in %s\n" $IHS_INSTALL_DIR $cfg_script
	fi

	logmsg HAWS_DEBUG $MSG_DEBUG_MSG "Checking IHS_VG $IHS_VG"
	if [[ -z $(grep -w "IHS_VG=\"$IHS_VG\"" $cfg_script) ]] ; then
	    logmsg HAWS_ERROR $MSG_IHS_NO_MATCH_VG "The IHS Volume Group %s does not match value in %s\n" $IHS_VG $cfg_script
	fi
    fi

}

###############################################################################
# Function: generate_import_script
###############################################################################
#
# Write the script that will be used to import the websphere configuration
# information into HACMP.
###############################################################################
generate_import_script() {

    # Setup the variable names here
    was_start_script="$HAWS_HOME/sbin/subsys/was/startWAS"
    was_stop_script="$HAWS_HOME/sbin/subsys/was/stopWAS"
    was_monitor_script="$HAWS_HOME/sbin/subsys/was/monitor_was"

    FALLOVER_VG=""
    # Compare IHS_VG and WAS_VG.  If different, then both must be handled by the cluster.
    if [[ "$WAS_VG" = "$IHS_VG" ]] ; then
	FALLOVER_VG="$WAS_VG"
    else
	FALLOVER_VG="$WAS_VG $IHS_VG"
    fi

    # This is the script that does the actual import
    logmsg HAWS_TRACE  "$MSG_CREATE_IMPORT" "Generating the import script...\n"

    echo "#!/bin/sh" > $create_script
    echo "# Script Automatically created on `date`" >> $create_script
    echo "# Any changes made to this file will be lost next time" >> $create_script
    echo "# the webshphere configuration information is imported" >> $create_script
    echo " " >> $create_script
    echo ". $clhaws_functions" >> $create_script
    echo " " >> $create_script

    echo "get_cluster_state" >> $create_script
    echo "if [[ \$CLUSTER_STATE -ne 0 ]] ; then" >> $create_script
    echo "logmsg HAWS_ERROR $MSG_CLUSTER_NOT_STABLE \"Cluster is not in stable state.\\\\n\"" >> $create_script
    echo "exit 1" >> $create_script
    echo "fi" >> $create_script
    echo " " >> $create_script

    echo "# Create the application server " >> $create_script
    echo "$CLADDSERV -s'$asname' -b'$was_start_script $cfg_script' -e'$was_stop_script $cfg_script'" >> $create_script
    echo "logmsg HAWS_INFO $MSG_APP_SERVER_CREATED \"Application Server %s created.\\\\n\" \"$asname\"" >> $create_script
    echo " " >> $create_script

    echo "# Create the resource group " >> $create_script
    echo "$CLADDGRP -g '$rgname' -s 'ignore' -n '`hostname` $WAS_FALLOVER_NODE' -S 'OHN' -O 'FNPN' -B 'FBHPN'" >> $create_script
    echo "logmsg HAWS_INFO $MSG_RESOURCE_GROUP_CREATED \"Resource Group %s created.\\\\n\" \"$rgname\"" >> $create_script
    echo " " >> $create_script

    echo "# Create the application monitor" >> $create_script
    echo "$CLADDAPPMON MONITOR_TYPE=user name='$asname' INVOCATION='$mon_mode' RESOURCE_TO_MONITOR='$asname' MONITOR_METHOD='$app_monitor_script' MONITOR_INTERVAL='120' STABILIZATION_INTERVAL='420' RESTART_COUNT='3' RESTART_INTERVAL='1800' FAILURE_ACTION='fallover' CLEANUP_METHOD='$was_stop_script $cfg_script' RESTART_METHOD='$was_start_script $cfg_script' HUNG_MONITOR_SIGNAL='9'" >> $create_script
    echo "logmsg HAWS_INFO $MSG_APP_MON_CREATED \"Application Monitor for application server %s created.\\\\n\" \"$asname\"" >> $create_script
    echo " " >> $create_script

    echo "# Create the resource to monitor now" >> $create_script
    echo "$CLADDRES -g '$rgname' APPLICATIONS='$asname' VOLUME_GROUP='$FALLOVER_VG' SERVICE_LABEL='$WAS_SERVICE_LABEL'" >> $create_script
    echo "logmsg HAWS_INFO $MSG_RESOURCES_CREATED \"Resources added to resource group %s.\\\\n\" \"$rgname\"" >> $create_script
    echo " " >> $create_script

    echo "logmsg HAWS_INFO $MSG_IMPORT_CREATE_COMPLETE \"%s import create complete.\\\\n\" WAS" >> $create_script
    echo " " >> $create_script

    # This is the script that backs out the changes
    logmsg HAWS_TRACE  "$MSG_CREATE_REMOVE" "Generating the remove script...\n"

    echo "#!/bin/sh" > $delete_script
    echo "# Script Automatically created on `date`" >> $delete_script
    echo "# Any changes made to this file will be lost next time" >> $delete_script
    echo "# the webshphere configuration information is imported" >> $delete_script
    echo " " >> $delete_script
    echo ". $clhaws_functions" >> $delete_script
    echo " " >> $delete_script

    echo "get_cluster_state" >> $delete_script
    echo "if [[ \$CLUSTER_STATE -ne 0 ]] ; then" >> $delete_script
    echo "logmsg HAWS_ERROR $MSG_CLUSTER_NOT_STABLE \"Cluster is not in stable state.\\\\n\"" >> $delete_script
    echo "exit 1" >> $delete_script
    echo "fi" >> $delete_script
    echo " " >> $delete_script

    echo "# Remove the resources " >> $delete_script
    echo "$CLRMRES -g '$rgname'" >> $delete_script
    echo "logmsg HAWS_INFO $MSG_RESOURCES_DELETED \"Resources removed to resource group %s.\\\\n\" \"$rgname\"" >> $delete_script
    echo " " >> $delete_script

    echo "# Remove the application monitor " >> $delete_script
    echo "$CLRMAPPMON $asname" >> $delete_script
    echo "logmsg HAWS_INFO $MSG_APP_MON_DELETED \"Application Monitor for application server %s deleted.\\\\n\" \"$asname\"" >> $delete_script
    echo " " >> $delete_script

    echo "# Remove the application server " >> $delete_script
    echo "$CLRMSERV $asname" >> $delete_script
    echo "logmsg HAWS_INFO $MSG_APP_SERVER_DELETED \"Application Server %s deleted.\\\\n\" \"$asname\"" >> $delete_script
    echo " " >> $delete_script

    echo "# Remove the resource group " >> $delete_script
    echo "$CLRMGRP -g $rgname" >> $delete_script
    echo "logmsg HAWS_INFO $MSG_RESOURCE_GROUP_DELETED \"Resource Group %s deleted..\\\\n\" \"$rgname\"" >> $delete_script
    echo " " >> $delete_script

    echo "logmsg HAWS_INFO $MSG_IMPORT_DELETE_COMPLETE \"%s import delete complete.\\\\n\" WAS" >> $delete_script
    echo " " >> $delete_script

    # Create the configuration script where application specific settings are stored
    logmsg HAWS_TRACE  "$MSG_CREATE_CONFIG" "Generating config file...\n"

    echo "#!/bin/sh" > $cfg_script
    echo "# Script Automatically created on `date`" >> $cfg_script
    echo "# Any changes made to this file will be lost next time" >> $cfg_script
    echo "# the webshphere configuration information is imported" >> $cfg_script
    echo " " >> $cfg_script
    echo "# WebSphere Application Server Configuration Information " >> $cfg_script
    echo " " >> $cfg_script
    echo "WAS_SERVER_NAME=\"$WAS_SERVER_NAME\"" >> $cfg_script
    echo "WAS_INSTALL_DIR=\"$WAS_INSTALL_DIR\"" >> $cfg_script
    echo "WAS_VG=\"$WAS_VG\"" >> $cfg_script
    echo "IHS_INSTALL_DIR=\"$IHS_INSTALL_DIR\"" >> $cfg_script
    echo "IHS_VG=\"$IHS_VG\"" >> $cfg_script
    echo "WAS_SERVICE_LABEL=\"$WAS_SERVICE_LABEL"\" >> $cfg_script

    echo "#!/bin/sh" > $app_monitor_script
    echo "# Script Automatically created on `date`" >> $app_monitor_script
    echo "# Any changes made to this file will be lost next time" >> $app_monitor_script
    echo "# the webshphere configuration information is imported" >> $app_monitor_script
    echo " " >> $app_monitor_script
    echo "# WebSphere Application Server monitor script for server: $WAS_SERVER_NAME" >> $app_monitor_script
    echo " " >> $app_monitor_script
    echo "$was_monitor_script \"$cfg_script\"" >> $app_monitor_script
    echo " " >> $app_monitor_script
    echo "exit \$?" >> $app_monitor_script

    complete_scripts $WAS_FALLOVER_NODE
}

###############################################################################
#
# Function: process_arguments
# This is a subsystem-specific version of the routine that processes
# command line arguments. 
#
###############################################################################
process_arguments() {
    logmsg HAWS_INFO "$MSG_WAS_PROCARG" "Processing 'was' arguments...\n";

    while getopts c:n:l:o: c 
      do 
        case $c in
         c) WAS_CELL=$OPTARG
            logmsg HAWS_INFO "$MSG_WAS_CELL_NAME_SET" "Cell name specified as %s\n" $WAS_CELL
	    ;;
	 n) WAS_FALLOVER_NODE=$OPTARG
            logmsg HAWS_INFO "$MSG_FALLOVER_NODE_NAME_SET" "Fallover Node specified as %s\n" $WAS_FALLOVER_NODE
	    ;;
	 l) WAS_SERVICE_LABEL=$OPTARG
	    logmsg HAWS_INFO "$MSG_SERVICE_LABEL_SET" "Service Label set to %s\n" $WAS_SERVICE_LABEL
	    ;;
	 o) WAS_NODE=$OPTARG
	    logmsg HAWS_INFO "$MSG_WAS_NODE_SET" "WAS Node set to %s\n" $WAS_NODE
	    ;;
	 \?)
	    logmsg HAWS_ERROR "$MSG_UNKNOWN_OPTION" "Unrecognized command line option specified\n"
            exit $HAWS_EXIT_FAIL;;   
         esac 
      done 
      logmsg HAWS_DEBUG "$MSG_WAS_PROCDONE" "Done processing 'was' arguments...\n";

    # Make sure that a fallover node was specified
    if [[ "$WAS_FALLOVER_NODE" = "" ]]; then
	logmsg HAWS_ERROR "$MSG_NO_FALLOVER" "You must specify a fallover node using the -n flag\n"
	exit $HAWS_EXIT_FAIL
    fi

    # Make sure that a service label was specified
    if [[ "$WAS_SERVICE_LABEL" = "" ]]; then
	logmsg HAWS_ERROR "$MSG_NO_SERVICE_LABEL" "You must specify a service label using the -l flag\n"
	exit $HAWS_EXIT_FAIL
    fi
}


###############################################################################
#
# SCRIPT EXECUTION SECTION
#
###############################################################################
# This section of the script is used to call into the various predefined
# to perform the actual script execution.
#
# The intent of this section is to provide a high-level view of how this
# script operates.
###############################################################################

process_arguments $*

verify_websphere_version
get_websphere_install_dir
get_websphere_vg
parse_websphere_config
init_java
get_server_names
get_ihs_install_dir
get_ihs_vg
create_script_names "was" $WAS_CELL"_"`hostname` $WAS_FALLOVER_NODE
validate_websphere_config

# If not Validating, generate the scripts
if [[ $VALIDATE_FLAG -ne 1 ]] ; then
    generate_import_script
fi

