X
  • October 25, 2007

Automate WLS Console Tasks with WLST

Guest Author

This was originally posted on my dev2dev blog October 25th, 2007.

I do a fair amount of shoulder-surfing at some of my customer sites.  One of the fun parts of my job is to see how different organizations (and teams within an organization) go about their jobs and what types of processes they follow.  I've noticed frequently that it is common for developers that are doing code promotions, particularly from personal workstations to shared development environments, to use the WLS console to update their applications by using the point and click approach that takes several minutes and has multiple user interactions.  Once I see this happen twice in a span of 10 minutes because of a minor bug, I quickly realize that there is an optimization opportunity to use a script for this mundane deployment task that is usually done over and over again.

Script it

I'm not going to provide an all-encompassing WLST (WebLogic Scripting Tool) tutorial.  There are lots of other places to get more in-depth information for that including the documentation, numerous entries from Satya Ghattu's blog and code samples.  However, I will show an quick example of how to redeploy an application with the python based scripting language that ships with WLS.  WLST can do a whole lot more than this (automate domain creation, configuration, etc), but this should open the door on the possibilities, and you can be Alice in Wonderland yourself and see how deep the rabbit hold goes.  Let's start with the individual building blocks, then put them all together.

Create Building Blocks

  1. Environment Setup - Just open a command prompt and run the <DOMAIN_HOME>/bin/setDomainEnv.cmd (sh) script.  This sets up all the environment variables for you.

  2. Start WLST - Just type java weblogic.WLST

  3. Connect to the Admin Server - Just use the connect command.  To figure how to do this, just type help().  Then you see that there is help on the common options by typing help('common').  The connect operation is one of the common operations, so for help on that specific operation type help('connect').  Of course there is a full command reference published online as well.  In the most basic OOTB situation, I was able to use this syntax:  connect( 'weblogic', 'weblogic', 't3://localhost:7001')

  4. Let's assume the application hasn't been deployed yet.  So the first step is to deploy the application - deploy('PortalEAR', 'D:/TEMP/wlst/PortalEAR.ear', targets='AdminServer')

  5. Start the application - startApplication('PortalEAR')

  6. Now let's assume you're going to deploy a new version of the application.  You could use the side-by-side deployment feature, but we'll save that for another time and just get the basics down first.  First, stop the existing application - stopApplication('PortalEAR')

  7. Now undeploy it - undeploy('PortalEAR')

  8. Repeat steps 4 and 5, this time pointing to the updated .ear file.

Assemble the building blocks

Once that all works, chain it all together, so you can do it all from one simple script:

updatePortalEAR.cmd

call D:\bea921\user_projects\domains\basic_portal_domain\bin\setDomainEnv.cmd

cd d:\temp\wlst

java weblogic.WLST d:\temp\wlst\updatePortalEAR.py

pause

updatePortalEAR.py

execfile('connect.py')

execfile('stop_and_undeploy.py')

execfile('deploy_and_start.py')

execfile('disconnect.py')

connect.py

print 'connecting to admin server....'

connect( 'weblogic', 'weblogic', 't3://localhost:7001', adminServerName='AdminServer' )

stop_and_undeploy.py

print 'stopping and undeploying ....'

stopApplication('PortalEAR')

undeploy('PortalEAR')

deploy_and_start.py

print 'deploying....'

deploy('PortalEAR', 'D:/TEMP/wlst/PortalEAR.ear', targets='AdminServer')

startApplication('PortalEAR')

disconnect.py

print 'disconnecting from admin server....'

disconnect()

exit()

Hopefully this gives you a flavor of how WLST can automate tasks.  Not only can this save time, but it also can help ensure that consistent processes are applied across environments which can help prevent human error (such as forgetting to check a checkbox in a wizard).  Happy scripting!

Here's the full output from executing my updatePortalEAR.cmd

D:\TEMP\wlst>call D:\bea921\user_projects\domains\basic_portal_domain\bin\setDomainEnv.cmd

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

connecting to admin server....

Connecting to t3://localhost:7001 with userid weblogic ...

Successfully connected to Admin Server 'AdminServer' that belongs to domain 'basic_portal_domain'.

Warning: An insecure protocol was used to connect to the

server. To ensure on-the-wire security, the SSL port or

Admin port should be used instead.

stopping and undeploying ....

Stopping application PortalEAR.

<Oct 25, 2007 4:29:33 PM CDT> <Info> <J2EE Deployment SPI> <BEA-260121> <Initiating stop operation for application, PortalEAR [archive: null], to AdminServer .>

.Completed the stop of Application with status completed

Current Status of your Deployment:

Deployment command type: stop

Deployment State : completed

Deployment Message : no message

Undeploying application PortalEAR ...

<Oct 25, 2007 4:29:38 PM CDT> <Info> <J2EE Deployment SPI> <BEA-260121> <Initiating undeploy operation for application, PortalEAR [archive: null], to AdminServer .>

....Completed the undeployment of Application with status completed

Current Status of your Deployment:

Deployment command type: undeploy

Deployment State : completed

Deployment Message : no message

deploying ....

Deploying application from D:\TEMP\wlst\PortalEAR.ear to targets AdminServer (upload=false) ...

<Oct 25, 2007 4:29:51 PM CDT> <Info> <J2EE Deployment SPI> <BEA-260121> <Initiating deploy operation for application, PortalEAR [archive: D:\TEMP\wlst\PortalEAR.ear], to AdminServer .>

...................Completed the deployment of Application with status completed

Current Status of your Deployment:

Deployment command type: deploy

Deployment State : completed

Deployment Message : no message

Starting application PortalEAR.

<Oct 25, 2007 4:30:50 PM CDT> <Info> <J2EE Deployment SPI> <BEA-260121> <Initiating start operation for application, PortalEAR [archive: null], to AdminServer .>

.Completed the start of Application with status completed

Current Status of your Deployment:

Deployment command type: start

Deployment State : completed

Deployment Message : no message

disconnecting from admin server....

Disconnected from weblogic server: AdminServer


Exiting WebLogic Scripting Tool.

<Oct 25, 2007 4:30:53 PM CDT> <Warning> <JNDI> <BEA-050001> <WLContext.close() was called in a different thread than the one in which it was created.>Press any key to continue . . .


Join the discussion

Comments ( 18 )
  • raju Friday, January 23, 2009
    deploy('PortalEAR', 'D:/TEMP/wlst/PortalEAR.ear', targets='AdminServer')
    according to my envirolment i want deploy PortalEar.ear to specific managed servers not only admin server and also i want to deploy remote manage servers using wlst,so plz can u help me out.........
  • James Bayer Friday, January 23, 2009
    raju,
    you can specify the names of multiple servers or clusters in the targets parameter. i recommend that check out the following links:
    http://www.oracle.com/technology/obe/fusion_middleware/wls103/SystemMgmt/Command_Line/Command_Line.html
    http://edocs.bea.com/wls/docs100/config_scripting/index.html
    http://edocs.bea.com/wls/docs100/config_scripting/reference.html#wp1060229
    Also, post in the WLS forums if you get stuck.
    James
  • sarangapani Wednesday, September 23, 2009
    Done the deployment by using wlst as above successfully
    but when use the application ,Getting the following errors and
    these errors are not coming after i restart the server.
    <[weblogic.servlet.internal.WebAppServletContext@13db3fd - appName: 'xxx.war', name: 'xxx.war', context-path: '/zzz/test'] Servlet failed with Exception java.lang.NullPointerException
    com.gecs.apollo.common.exception.ApolloUnexpectedException: An unexpected remote exception occured
    --> java.rmi.NoSuchObjectException: Bean is already undeployed.
    at weblogic.ejb.container.manager.BaseEJBManager.preInvoke(BaseEJBManager.java)
  • Andreas Koop Thursday, December 10, 2009
    Hi,
    how to avoid the warning message "WLContext.close() was called in a different thread than the one in which it was created"? What are we doing wrong?
    Regards,
    Andreas.
  • james.bayer Friday, December 11, 2009
    I'm not sure, I recommend posting this question to the WLS OTN forum.
    http://forums.oracle.com/forums/forum.jspa?forumID=570
    Good luck, James
  • Kristoffer Laiz Tuesday, May 4, 2010
    Hi James,
    We are currently working on an automation project using WLST in ODSI and similarly in ALSB, which is hosted in a remote server. The process is called migration (deployment of the ODSI/ALSB configuration from one environment to another (development -> QA -> production). In detail:
    1. From source: export config as a JAR file using WLST scripts
    2. In the source remote server, JAR file is saved.
    3. From source remote server to target remote server, cut and paste the JAR file using DOS commands (batch file commands)
    4. In the target server, the JAR file is imported using WLST scripts
    For ODSI stuffs, we found this reference:
    The contents of DspCommonCommands follows :
    import wlstModule as wlst
    def moveToDspDir():
    wlst.runtime()
    wlst.custom()
    wlst.cd('com.bea.dsp')
    def moveToDomainServiceMBean():
    moveToDspDir()
    wlst.cd('com.bea.dsp:Name=DomainServiceMBean,DspType=com.bea.dsp.management.DomainServiceMBean')
    def moveToDeploymentSessionMBean(sessionName):
    moveToDspDir()
    if sessionName == None:
    wlst.cd('com.bea.dsp:Name=SessionMBean,DspType=com.bea.dsp.management.session.SessionMBean.null')
    else:
    wlst.cd('com.bea.dsp:Name=SessionMBean,DspType=com.bea.dsp.management.session.SessionMBean.'+sessionName)
    def moveToAdministrationServiceMBean(dataspaceName,sessionName):
    moveToDeploymentSessionMBean(sessionName)
    wlst.invoke('getAdministrationService',[dataspaceName],["java.lang.String"])
    moveToDspDir()
    wlst.cd('com.bea.dsp:Name=AdministrationServiceMBean,DspType=com.bea.dsp.management.admin.AdministrationServiceMBean.'+sessionName+'.'+dataspaceName)
    def moveToDataSpaceConfigMBean(dataspaceName,sessionName):
    moveToAdministrationServiceMBean(dataspaceName,sessionName)
    wlst.get('DataSpaceMBean')
    moveToDspDir()
    wlst.cd('com.bea.dsp:Name=DataSpaceConfig,DspType=com.bea.dsp.management.configmbeans.DataSpaceConfigMBean.'+sessionName+'.'+dataspaceName)
    def moveToDataServicesConfigMBean(dataspaceName,sessionName):
    moveToAdministrationServiceMBean(dataspaceName,sessionName)
    wlst.get('DataServicesMBean')
    moveToDspDir()
    wlst.cd('com.bea.dsp:Name=DataServicesConfig,DspType=com.bea.dsp.management.configmbeans.DataServicesConfigMBean.'+sessionName+'.'+dataspaceName)
    def moveToDataSourcesConfigMBean(dataspaceName,sessionName):
    moveToAdministrationServiceMBean(dataspaceName,sessionName)
    wlst.get('DataSourcesMBean')
    moveToDspDir()
    wlst.cd('com.bea.dsp:Name=DataSourcesConfig,DspType=com.bea.dsp.management.configmbeans.DataSourcesConfigMBean.'+sessionName+'.'+dataspaceName)
    def createDataSpace(dataspaceName):
    moveToDomainServiceMBean()
    targets = wlst.get('Targets')
    wlst.invoke('createDataSpace',[dataspaceName,targets],['java.lang.String','java.util.Set'])
    def deleteDataSpace(dataspaceName):
    moveToDomainServiceMBean()
    wlst.invoke('deleteDataSpace',[dataspaceName],['java.lang.String'])
    def importDataSpace(dataspaceName,sessionName,importJarFilePath):
    moveToDeploymentSessionMBean(sessionName)
    from com.bea.dsp.management.deployment.session import DeploymentOptions
    deploymentOptions = DeploymentOptions()
    wlst.invoke('importJar',[dataspaceName,importJarFilePath,deploymentOptions],['java.lang.String','java.lang.String','com.bea.dsp.management.deployment.session.DeploymentOptions'])
    def exportDataSpace(dataspaceName,exportJarFilePath):
    moveToDeploymentSessionMBean(None)
    from com.bea.dsp.management.deployment.session import ExportOptions
    exportoptions = ExportOptions()
    exportoptions.setIncludeConfiguration(1)
    exportoptions.setOverwriteJar(1)
    wlst.invoke('export',[dataspaceName,exportJarFilePath,exportoptions],['java.lang.String','java.lang.String','com.bea.dsp.management.deployment.session.ExportOptions'])
    def createDeploymentSession(sessionName):
    moveToDomainServiceMBean()
    wlst.invoke("createDeploymentSession",[sessionName],["java.lang.String"])
    moveToDeploymentSessionMBean(sessionName)
    def activateDeploymentSession(sessionName):
    moveToDomainServiceMBean()
    wlst.invoke('activateDeploymentSession',[sessionName],["java.lang.String"])
    def discardDeploymentSession(sessionName):
    moveToDomainServiceMBean()
    wlst.invoke('discardDeploymentSession',[sessionName],["java.lang.String"])
    def createFunctionRef(dataServiceLocator,functionName,arity):
    from com.bea.ld.metadata import NodeRef
    from javax.xml.namespace import *
    qname = QName(dataServiceLocator,functionName)
    return NodeRef.Factory.newInstance().createFunctionRef(qname, arity)
    The problem:
    When executing the method within this python script, we encounter an error:
    Traceback (innermost last):
    File "(console)", line 1, in ?
    ImportError: No module named dsp
    ***see below indicated by the arrow the exact line having the error
    def importDataSpace(dataspaceName,sessionName,importJarFilePath):
    moveToDeploymentSessionMBean(sessionName)
    -> from com.bea.dsp.management.deployment.session import DeploymentOptions
    deploymentOptions = DeploymentOptions()
    wlst.invoke('importJar',[dataspaceName,importJarFilePath,deploymentOptions],['java.lang.String','java.lang.String','com.bea.dsp.management.deployment.session.DeploymentOptions'])
    We are new to this scripting methodology and cannot identify the real problem.. We need your help.
    Thanks a lot in advance!!!
  • james.bayer Wednesday, May 5, 2010
    Kristoffer,
    I'm not a WLST expert - I just know the basics - it looks like something from your ODSI libraries are not in the classpath of the process executing WLST?
    ImportError: No module named dsp
    Sorry I can't be of further help - I'd advise you to try the ODSI forum.
    http://forums.oracle.com/forums/forum.jspa?forumID=596&start=0
    Good luck,
    James
  • Adrian Tuesday, June 8, 2010
    Hi James,
    I have a quick question. I'm using WLS 9.2 MP3.
    I found that we can use the 'Record' link on admin console to create scripts, but I couldn't find it on WLS 9.2 console.
    Is the 'Record' option available for WLS 9.2 ? if so how can I enable it ?
    Regards,
    Adrian
  • james.bayer Tuesday, June 8, 2010
    Adrian,
    The console "record" functionality was introduced in WLS 10.0 and it is unavailable - at least in the GUI - in 9.2.x. WLST is mostly backward compatible though, so one option you could try is to record a session in any release after 10.0 and try to execute the recorded WLST scripts in the 9.2.x server.
    Good luck, James
  • prasanth Wednesday, July 28, 2010
    HI
    James,
    The script is nice what if i want to restart the target servers after undeploy the application and before deploying the new application?.
  • james.bayer Friday, July 30, 2010
    Prasanth,
    The best way to automate starting and stopping servers is to use WLST and NodeManager together. Node Manager does require some setup to be used with your domain. One of the readers commented on this approach in this post.
    http://blogs.oracle.com/jamesbayer/2010/01/weblogic_nodemanager_quick_sta.html#comments
    Cheers,
    James
  • Lionel Frachon Tuesday, August 10, 2010
    Hello,
    my client uses, amongst others, WebLogic 8.1 sp5 for a fair amount of servers, and looks like it is impossible to find the wlst archive (before its integration in WebLo suite in 9).
    The dev2dev site, originally the trusted source, cannot be reached anymore, and cannot find any replacement anywhere on net.
    Any clue ?
    Thanks a million times for your help
    LF
  • james.bayer Tuesday, August 10, 2010
    Lionel, I am not sanctioning this site, but I found it on google. Make sure you carefully examine the content of the downloads and use at your own risk. http://unni-at-work.blogspot.com/2009/07/wlst-on-weblogic-81_08.html
    Thanks, James
  • prasanth Wednesday, August 11, 2010
    Hi
    All,
    I am trying to execute the below command.But it is throwing a error.Can any please suggest to resolve the below error:
    command:
    :/opt/app/$ java -cp /opt/app/component/bea103/wlserver_10.3/server/lib/weblogic.jar weblogic.WLST /opt/app/sampleconnect.py
    OUTPUT BELOW:
    Initializing WebLogic Scripting Tool (WLST) ...
    Problem invoking WLST - java.lang.UnsupportedClassVersionError: Bad version number in .class file
    I am using JDK1.6 with weblogic10.3
    Thanks
  • james.bayer Wednesday, August 11, 2010
    Prasanth,
    Try running:
    java -version
    I'm guessing that you do not current have a 1.6 JVM in your path. I recommend always calling your domain's setDomainEnv (sh/cmd) file before running WLST.
    Thanks,
    James
  • prasanth Wednesday, August 11, 2010
    you are correct James...Thaks a lot .....its working now....
  • John Container Wednesday, February 23, 2011
    Damn James, really helped me out automating my WLS tasks.
    Only have one question left over, are the .py files python code files ? I guess so..
    Thanks and keep up the good work !
    John
  • james.bayer Wednesday, February 23, 2011
    Thanks for the feedback! Yes they are python files.
    Thanks,
    James
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.