Help needed with a shell script for deploying ear file to a weblogic server using WLST

Hi,

Please help me through a shell script to run from command prompt using WLST for the below purpose:

Automation process:
>Check the availability of an application.
>Stops the EAR if it already exists in Weblogic Server
>Undeploys/Delete the project (EAR file)
>Deploys the new ear file into the Weblogic Server
>Restart the services of the project (EAR)
By using the below command we can execute the shell script from command prompt through WLST:
>>java weblogic.WLST autodeploy.sh -u username -p password -a adminUrl [<hostname>:<port>] -n deploymentName -f deploymentFile -t deploymentTarget
I am attaching a python script which does the same but could you please provide the shell script in bash?

#! /usr/bin/env python
import sys
import os
from java.lang import System
#Python Script to manage applications in weblogic server.
#This script takes input from command line and executes it.
#It can be used to check status,stop,start,deploy,undeploy of applications in weblogic server using weblogic wlst tool.
import getopt
#========================
#Usage Section
#========================
def usage():
    print 'Usage:'
    print 'java weblogic.WLST autodeploy.py -u username -p password -a adminUrl [<hostname>:<port>] -n deploymentName -f deploymentFile -t deploymentTarget\n';
sys.exit(2)
#========================
#Connect To Domain
#========================
def connectToDomain():
    try:
        connect(username, password, adminUrl)
        print 'Successfully connected to the domain\n'
    except:
        print 'The domain is unreacheable. Please try again\n'
exit()
#========================
#Checking Application Status Section
#========================
def appstatus(deploymentName, deploymentTarget):
    try:
        cd('domainRuntime:/AppRuntimeStateRuntime/AppRuntimeStateRuntime')
        currentState = cmo.getCurrentState(deploymentName, deploymentTarget)
        return currentState
    except:
        print 'Error in getting current status of ' +deploymentName+ '\n'
exit()
#========================
#Application undeployment Section
#========================
def undeployApplication():
    try:
        print 'stopping and undeploying ..' +deploymentName+ '\n'
        stopApplication(deploymentName, targets=deploymentTarget)
        undeploy(deploymentName, targets=deploymentTarget)
    except:
        print 'Error during the stop and undeployment of ' +deploymentName+ '\n'
#========================
#Applications deployment Section
#========================
def deployApplication():
    try:
        print 'Deploying the application ' +deploymentName+ '\n'
        deploy(deploymentName,deploymentFile,targets=deploymentTarget)
        startApplication(deploymentName)
    except:
        print 'Error during the deployment of ' +deploymentName+ '\n'
exit()
#========================
#Input Values Validation Section
#========================
if __name__=='__main__' or __name__== 'main':
    try:
        opts, args = getopt.getopt(sys.argv[1:], "u:p:a:n:f:t:", ["username=", "password=", "adminUrl=", "deploymentName=", "deploymentFile=", "deploymentTarget="])
    except getopt.GetoptError, err:
        print str(err)
  
usage()
username = ''
password = ''
adminUrl = ''
deploymentName = ''
deploymentFile = ''
deploymentTarget = ''
for opt, arg in opts:
    if opt == "-u":
      username = arg
    elif opt == "-p":
      password = arg
    elif opt == "-a":
      adminUrl = arg
    elif opt == "-n":
      deploymentName = arg
    elif opt == "-f":
      deploymentFile = arg
    elif opt == "-t":
      deploymentTarget = arg
if username == "":
   print "Missing \"-u username\" parameter.\n"
   usage()
elif password == "":
   print "Missing \"-p password\" parameter.\n"
   usage()
elif adminUrl == "":
   print "Missing \"-a adminUrl\" parameter.\n"
   usage()
elif deploymentName == "":
   print "Missing \"-n deploymentName\" parameter.\n"
   usage()
elif deploymentFile == "":
   print "Missing \"-c deploymentFile\" parameter.\n"
   usage()
elif deploymentTarget == "":
   print "Missing \"-c deploymentTarget\" parameter.\n"
   usage()
#========================
#Main Control Block For Operations
#========================
def deployUndeployMain():
    try:
        if appstatus(deploymentName, deploymentTarget)=='STATE_RETIRED' :
           appflag=1
        elif appstatus(deploymentName, deploymentTarget)=='STATE_ACTIVE':
           appflag=2
        else:
           print ' other Applications are Running '
           pass
           if appflag == 1 :
              print 'application having RETIERED STATE ..'
              undeployApplication()
              print appstatus(deploymentName, deploymentTarget)
              deployApplication()
              print appstatus(deploymentName, deploymentTarget)
           elif appflag== 2:
              print 'Application exists in ACTIVE state...'
              undeployApplication()
              deployApplication()
              print appstatus(deploymentName, deploymentTarget)
           else:
              print 'new application'
              deployApplication()
    except:
        print 'Error during the deployment of ' +deploymentName+ '\n'
exit()
#========================
#Execute Block
#========================
connectToDomain()
deployUndeployMain()
disconnect()
exit()

Thanks in Advance!

Hi Amulya,

Nice script to deploy applications. Can the script be modified to get the values/parameters from a file rather than providing in the command line.

It is too long to run giving all the values.
e.g: java weblogic.WLST autodeploy.py -u username -p password -a adminUrl [<hostname>:<port>] -n deploymentName -f deploymentFile -t deploymentTarget

Instead can the script get the values from a separate file or within the script.

Please let me know how it can be done.

Many thanks....

I tried to run the script but it is not working.

I have not modified your script and tried to run with the following command line:

java weblogic.WLST autodeploy.py -u weblogic -p password123 -a [localhost:7001] -n ShoppingCart -f ShoppingCart.war -t AdminServer

The WLST is started but the script is not executed. All I see is

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

and it exists.

Could you please advise if I have missed something

Thanks...