Automatic/Simple WebLogic Application Manipulation and Deployment Script Utilizing Bash

Today I come to you with a fun script I wrote to manipulate applications in WebLogic.

Using the weblogic.Deployer class in the WebLogic JAR we are able to Automate and Simplify some of the more mundane tasks that we all have to do in WebLogic sometimes.

For the savvy, please refer to the “prerequisites()” function and uncomment/comment whatever things you want the user to enter for proper security and portability.

Gotta love that dumb generic 1/2/3/4/5 menu interface I keep using.

Below is the code:

#!/bin/bash

#These Variables are here purely for initialization 
#and are overwritten when the prerequisites()
#function is called down at bottom of script

USERNAME=""
PASSWORD=""

ADMIN_URL="" #the url for deployment
JAVA_PATH="/usr/java/latest/bin/java" #path to java

WEBLOGIC_JAR="" #my weblogic server path
TARGET="" #values can be seperated
APPLICATION_NAME="" #the deployment name 
APPLICATION_PATH="" #the path to the ear file

WEBLOGIC_DEPLOYER_CMD="$JAVA_PATH -cp $WEBLOGIC_JAR weblogic.Deployer -verbose -debug -adminurl "$ADMIN_URL" -username $USERNAME -password $PASSWORD "

prerequisites()
{
    #In this command you can uncomment/comment variable declarations you want the user to enter every time

    echo "Type The Application Name: "
    read APPLICATION_NAME

    #echo "Type The Target JVM Servers (Space Between Each JVM Name): "
    #read TARGETS
    TARGETS="AdminServer"

    #echo "Type WebLogic Admin Username: "
    #read USERNAME
    USERNAME="weblogic"

    #echo "Type WebLogic Admin Password: "
    #read PASSWORD
    PASSWORD="password1"

    #echo "Type AdminServer Protocol (t3 - not secure) or (t3s - secure): "
    #read ADMIN_PROTOCOL
    ADMIN_PROTOCOL="t3"

    #echo "Type AdminServer Hostname: "
    #read ADMIN_HOSTNAME
    ADMIN_HOSTNAME="localhost"

    #echo "Type AdminServer Port: "
    #read ADMIN_PORT
    ADMIN_PORT="8080"

    #This Sets the ADMIN_URL for WebLogic Connection
    ADMIN_URL="$ADMIN_PROTOCOL://$ADMIN_HOSTNAME:$ADMIN_PORT"

    #echo "Type the path to the Java Binary: "
    #read JAVA_PATH
    JAVA_PATH="/usr/java/latest/bin/java"

    #echo "Type the path to the WebLogic JAR: "
    #read WEBLOGIC_JAR
    WEBLOGIC_JAR="/home/oracle/Oracle/Middleware/Oracle_Home/wlserver/server/lib/weblogic.jar"

    #This Sets the Command for Application Manipulation Later On
    WEBLOGIC_DEPLOYER_CMD="$JAVA_PATH -cp $WEBLOGIC_JAR weblogic.Deployer -verbose -debug -adminurl "$ADMIN_URL" -username $USERNAME -password $PASSWORD "
    
    choiceFunction
}

baseCommand()
{
    CHOSEN_OPTION="$1"
    EXTRA_ARGS="$2"

    $WEBLOGIC_DEPLOYER_CMD $CHOSEN_OPTION -name "$APPLICATION_NAME" -targets "$TARGETS" $CHOSEN_OPTION $EXTRA_ARGS
    
    echo "FINISHED EXECUTION - PRESS ENTER TO CONTINUE..."
    read NOTHING

    choiceFunction
}

application_start()
{
    PARAMETER_CHOSEN_OPTION="-start"
    PARAMETER_EXTRA_ARGS=""
    baseCommand "$PARAMETER_CHOSEN_OPTION" "$PARAMETER_EXTRA_ARGS"
}

application_stop()
{
    PARAMETER_CHOSEN_OPTION="-stop"
    PARAMETER_EXTRA_ARGS=""
    baseCommand "$PARAMETER_CHOSEN_OPTION" "$PARAMETER_EXTRA_ARGS"
}

application_undeploy()
{
    PARAMETER_CHOSEN_OPTION="-undeploy"
    PARAMETER_EXTRA_ARGS=""
    baseCommand "$PARAMETER_CHOSEN_OPTION" "$PARAMETER_EXTRA_ARGS"
}

application_deploy()
{
    PARAMETER_CHOSEN_OPTION="-deploy"

    echo "Type Full Java WAR/EAR Path: "
    read APPLICATION_PATH

    PARAMETER_EXTRA_ARGS="-upload $APPLICATION_PATH"

    baseCommand "$PARAMETER_CHOSEN_OPTION" "$PARAMETER_EXTRA_ARGS"
}

# Bash Menu Script Example
choiceFunction() {
    SUCCESS="false"
 
    while [ $SUCCESS == "false" ]
    do
        clear
        echo "Please enter your choice: "
        echo "1 - Start WebLogic Application"
        echo "2 - Stop WebLogic Application"
        echo "3 - Undeploy WebLogic Application (Stop Application First Please)"
        echo "4 - Deploy WebLogic Application"
        echo "5 - Quit"
        read CHOICE
 
        case $CHOICE in
           "1") SUCCESS="true";echo "Executing Application Start ..."; application_start;;
           "2") SUCCESS="true";echo "Executing Application Stop ..."; application_stop;;
           "3") SUCCESS="true";echo "Executing Application Un-Deployment (Removal) ..."; application_undeploy;;
           "4") SUCCESS="true";echo "Executing Application Deployment ..."; application_deploy;;
           "5") SUCCESS="true";echo "Exiting ..."; exit;;
           *) read -p "Invalid choice, Press any key to continue..." fakeVariableIgnoreMe;;
        esac
    done
}

prerequisites

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s