#!/bin/sh 
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/haws/sbin/subsys/dm/import.sh 1.16 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2004,2005 
# 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 
# @(#)31	1.16 src/43haes/usr/sbin/cluster/haws/sbin/subsys/dm/import.sh, hacmp.assist, 61haes_r714 7/22/05 08:00:39
#
###############################################################################
#
# This script performed the low-level task of importing the Deployment
# Manager configuration into HACMP.

# DM Package name
DM_PACKAGE_NAME="WSNAA"

# Version variables
DM_VERSION_5="5.0"
DM_VERSION_6="6.0"
DM_VERSION=""

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

# The directory where Deployment Manager is installed
WAS_INSTALL_DIR=""

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

# Specifies the name of the node where the DM will fallover to.
DM_FALLOVER_NODE=""

DM_SERVICE_LABEL=""

# The serverindex.xml file contains some information we need to extract.
SERVERINDEXFILE=""

#
###############################################################################
#
# SCRIPT DEFINITION SECTION
#
###############################################################################
# Define Functions
#
# This section should be used to define functions used by this script.
# The following functions are defined:
#
# verify_dm_version        - Verifies that the Deployment Manager version is
#                            at the correct level.
#
# get_dm_install_dir        - Find the Deployment Manager installation directory. 
#                             This function will also determine the vg
#                             that dm is installed under.
#
# parse_dm_config           - Parses the dm configuration files and
#                             loads internal variables
#
# validate_dm_config        - This name is a little misleading. What it
#                             does is to make sure the Deployment Manager config
#                             files contain nodes that are defined in
#                             hacmp. It will peform other checks too, but
#                             the intent is to make sure the import process
#                             will work.
# generate_import_script    - Generate the actual script that will be used
#                             to import dm configuration information
#                             into hacmp
###############################################################################


###############################################################################
# Function: verify_dm_version
###############################################################################
#
# Make sure that DM is at version 5.0.0
###############################################################################
verify_dm_version() {
    logmsg HAWS_DEBUG "$MSG_DEBUG_MSG" "Checking DM version...\n"
    DM_VERSION=`lslpp -Lc "$DM_PACKAGE_NAME" 2>/dev/null | sed -e '/^#/d' | cut -f3 -d':' | cut -f 1,2 -d '.'`
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "DM Version = $DM_VERSION\n"
    
    # We need to be prepared to support version 6 of Deployment Manager
    #  
    case $DM_VERSION in
	$DM_VERSION_5)
	    logmsg HAWS_INFO "$MSG_DM_VER5_DETECTED" "DM Version 5.0 detected\n"
	    ;;
	$DM_VERSION_6)
	    logmsg HAWS_INFO "$MSG_DM_VER6_DETECTED" "DM Version 6.0 detected\n"
	    logmsg HAWS_ERROR "$MSG_DM_VER6_NOTSUPPORTED" "Version 6.0 is not supported\n"
	    exit $HAWS_EXIT_FAIL
	    ;;
	"")
	    logmsg HAWS_ERROR "$MSG_DM_NOT_INSTALLED" "DM could not be found.  Please install DM v5.0\n"
	    exit $HAWS_EXIT_FAIL
	    ;;
	*)
	    logmsg HAWS_ERROR "$MSG_DM_UNKNOWN_VERSION" "Unsupported version of DM: %s.  Please install DM v5.0\n" $DM_VERSION
	    exit $HAWS_EXIT_FAIL
    esac
}

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

###############################################################################
# Function: get_dm_vg
###############################################################################
#
# Gets the volume group where the DM configuration files exist.
###############################################################################
get_dm_vg() {
    logmsg HAWS_DEBUG  "$MSG_DEBUG_MSG" "Getting the vg for DM..."
    VG=""
    if [[ -d "$WAS_INSTALL_DIR" ]]; then
	findvg $WAS_INSTALL_DIR"/bin"
	if [[ $? -eq HAWS_EXIT_FAIL ]]; then
	    exit $HAWS_EXIT_FAIL
	fi
	DM_VG=$VG
    fi
    if [[ "$DM_VG" = "" ]]; then
	logmsg HAWS_ERROR "$MSG_DM_UNKNOWN_VG" "Can't get the volume group for DM directory: %s\n" $WAS_INSTALL_DIR
	exit $HAWS_EXIT_FAIL
    fi
    logmsg HAWS_INFO  "$MSG_DM_VG_FOUND" "DM Volume group = %s\n" $DM_VG
}

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

    # 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 DM_VG $DM_VG"
    if [[ -z $(grep -w "DM_VG=\"$DM_VG\"" $cfg_script) ]] ; then
	logmsg HAWS_ERROR $MSG_DM_NO_MATCH_VG "The Deployment Manager Volume Group %s does not match value in %s\n" $DM_VG $cfg_script
    fi

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

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

    # Setup the variable names here
    dm_start_script="$HAWS_HOME/sbin/subsys/dm/startDM"
    dm_stop_script="$HAWS_HOME/sbin/subsys/dm/stopDM"
    dm_monitor_script="$HAWS_HOME/sbin/subsys/dm/monitor_dm"

    # 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 Deployment Manager 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'$dm_start_script $cfg_script' -e'$dm_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` $DM_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='300' RESTART_COUNT='3' RESTART_INTERVAL='1440' FAILURE_ACTION='fallover' CLEANUP_METHOD='$dm_stop_script $cfg_script' RESTART_METHOD='$dm_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='$DM_VG' SERVICE_LABEL='$DM_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\" DM" >> $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 Deployment Manager 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\" DM" >> $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 Deployment Manager configuration information is imported" >> $cfg_script
    echo " " >> $cfg_script
    echo "# Deployment Manager 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 "DM_VG=\"$DM_VG\"" >> $cfg_script
    echo "DM_SERVICE_LABEL=\"$DM_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 Deployment Manager configuration information is imported" >> $app_monitor_script
    echo " " >> $app_monitor_script
    echo "# Deployment Manager monitor script for server: $WAS_SERVER_NAME" >> $app_monitor_script
    echo " " >> $app_monitor_script
    echo "$dm_monitor_script \"$cfg_script\"" >> $app_monitor_script
    echo " " >> $app_monitor_script
    echo "exit \$?" >> $app_monitor_script

    complete_scripts $DM_FALLOVER_NODE
}

###############################################################################
#
# Function: process_arguments
# This is a subsystem-specific version of the routine that processes
# command line arguments. 
#
#   -c Specifies the cell name to use. This is necessary if multiple cells
#      are defined the app server.
###############################################################################
process_arguments() {
    logmsg HAWS_DEBUG "$MSG_DM_PROCARG" "Processing 'dm' arguments...\n";

    while getopts n:l: c 
      do 
        case $c in
         n) DM_FALLOVER_NODE=$OPTARG
            logmsg HAWS_INFO "$MSG_FALLOVER_NODE_NAME_SET" "Fallover Node specified as %s\n" $DM_FALLOVER_NODE
	    ;;
	 l) DM_SERVICE_LABEL=$OPTARG
	    logmsg HAWS_INFO "$MSG_SERVICE_LABEL_SET" "Service Label set to %s\n" $DM_SERVICE_LABEL
	    ;;
        \?)
	    logmsg HAWS_ERROR "$MSG_UNKNOWN_OPTION" "Unrecognized command line option specified\n"
            exit $HAWS_EXIT_FAIL;;   
         esac 
      done 
      logmsg HAWS_DEBUG "$MSG_DM_PROCDONE" "Done processing 'dm' arguments...\n";

    # Make sure that a fallover node dm specified
    if [[ "$DM_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 dm specified
    if [[ "$DM_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_dm_version
get_dm_install_dir
get_dm_vg
create_script_names "dm" `hostname` $DM_FALLOVER_NODE
validate_dm_config

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

