Archive for the ‘wlst’ Category

WLST startServer re-visited

Monday, January 1st, 2007

From the past few months I have been working on a project to create a framework for our WebLogic application developers that can be used to easily build, configure and manage their WebLogic Servers. Needless to say the framework is built extensively with WLST.
After the WebLogic domain is configured we also provide infrastructure to manage their domain, mainly to start, stop, get the status of their domain and also to move their configuration from dev to uat and to production.
We use WLST’s startServer to start the Administration and Manager Server’s, although startServer does what we need but falls short in many ways. For example it does not provide a way to add PRE_CLASSPATH or POST_CLASSPATH to the Server’s classpath. Also it inherits the classpath from the JVM where WLST is invoked from which maynot be desirable in many situations.
Although startServer provides a way to redirect the server’s stdOut to a file, as soon as you exit the WLST client the redirection stops and there is no way to get WLST to redirect the server’s stdout/stderr to a file. With all these shortcomings I almost wish we could have an option in startServer command to use the startWebLogic.sh or startManagedWebLogic.sh script to start the Server. Since that option is not available I would like to share the workaround I used to get around the problem.
This workaround will use the startWebLogic.sh that is under the domain directory. The biggest advantage of making WLST to use a script is any custom extensions you have added to the script like the PRE_CLASSPATH/POST_CLASSPATH settings are preserved when you start the server, and also you can redirect the stdout/stderr to a file.

Copy the definitions below to your script and invoke the command’s by passing in the required arguments.

def startAdminServerViaScript(domainDirectory, adminServerName):
··· try:
······svrOutLoc = domainDirectory+”/servers/”+adminServerName+”/logs/”+adminServerName+”.out”
······os._shellEnv.system(domainDirectory+”/bin/startWebLogic.sh > “+svrOutLoc+” 2>&1 &\n”)
···except Exception,e:
······e.printStackTrace()
······raise “Error starting the admin server”

def startManagedServerViaScript(domainDirectory, managedServerName, adminUrl):
···try:
······svrOutLoc = domainDirectory+”/servers/”+managedServerName+”/logs/”+managedServerName+”_”+”.out”
······os._shellEnv.system(domainDirectory+”/bin/startManagedWebLogic.sh “+managedServerName+” “+adminUrl+” > “+svrOutLoc+” 2>&1 &\n”)
···except Exception,e:
······e.printStackTrace()
······raise “Error starting the weblogic server “+managedServerName

Both the functions will basically invoke the start scripts that are part of your domain from WLST. Note that both these functions work on Unix/Linux and if you are using windows make appropriate changes to call the right scripts.

_uacct = “UA-2684269-2″;
urchinTracker();

WLST as a Jython Module

Friday, January 6th, 2006

Few questions that I answered about WLST as a module via various email queries recently.

What is a Jython Module?
In simple words, a jython Module is more or less like a Java class/object that you can import into other Modules. Lets say, you wrote a script/module ‘A.py’ and you defined a function ‘myFunction’. Now you can import this A.py into a different script, say B.py’ and call the function ‘myFunction’. Here’s how you do it.

A.py
——-
def myFunction():
··print “Hello There!”

def printFirstName(myName):
··print “Hello “+myName

B.py
——
import A
A.myFunction()
A.printFirstName(“satya”)

WLST provides a convienient way for you to import it as a module into your scripts and use all the WLST commands that are supported. The advantage of doing this is you can write Jython Modules without any WebLogic dependency and import WLST module into these modules. For example, now you can have another module C.py where you will import wlstModule.py and A.py.

C.py
——
import A
import wlstModule.py as myWLST
# connect to the server
myWLST.connect(‘weblogic’,'weblogic’,'t3://sghattu:7001′)
# call a function from module A
A.printFirstName(“satya”)
#start and edit session and create a server
myWLST.edit()
myWLST.startEdit()
newSvr = myWLST.cmo.createServer(‘ms1′)
newSvr.setListenPort(8001)
myWLST.activate()
myWLST.disconnect()


What is the difference between WLST as a Module and invoking WLST?
If you invoke WLST via ‘java weblogic.WLST‘, WLST instantiates a Jython Interpreter, creates a WLST Namespace and installs all the WLST functions in it. You can import other modules, but you will not have access to WLST functions from these external modules because when you import other modules they will be imported into the Jython’s Default NameSpace not the WLST NameSpace. To use your modules in WLST namespace you will have to execute the module in wlst NameSpace using execfile(externalModule.py).

Where can I find WLST Module and are there any recommendations
WLST Module is under {WL_HOME}/common/wlst/modules/wlstModule.py. You can also generate one using the writeIniFile command.
If you are using WLST as a module, you should use java org.python.util.jython to invoke your scripts. Do not use java weblogic.WLST and import WLST as a module. This will import WLST Module into WLST NameSpace and if you mix the module calls with straight WLST calls, you will see some weird interactions. This is not recommended. Please use straight Jython if you are using WLST as a Module.

_uacct = “UA-2684269-2″;
urchinTracker();

Having a profile for your WLST sessions

Friday, November 25th, 2005

Did you know that you can have a profile setup for all your WLST sessions? What I mean by that is, you can have a profile script that gets executed whenever you invoke WLST. I usually have my few WLST extensions (some custom commands that are not part of WLST) and a connect command in this profile script such that I dont have to execute each of these extensions and type in my connect command for every WLST invocation.
···To have a profile, all you will need to do is create a file with name ‘wlstProfile.py’ and place it in your ‘user.home’ directory. You can also have the wlstProfile.py in your current directory from where you invoked WLST. An example wlstProfile.py is shown below.

# wlstProfile that gets executed for every WLST invocation

print ‘Setting up WLST JDBC extensions’
execfile(‘c:/wls/satya/scripts/jdbc/jdbcExtensions.py’)
print ‘Setting up WLST WLDF extensions’
execfile(‘c:/wls/satya/scripts/wldf/wldfExtensions.py’)

print ‘Now connecting… ‘

connect(‘weblogic’,'weblogic’,'t3://sghattu:7001′)

If you are not sure what your ‘user.home’ directory is, you can find out by doing this.

wls:/(offline)> java.lang.System.getProperty(“user.home”)
‘C:\\Documents and Settings\\SGhattu’
wls:/(offline)>

_uacct = “UA-2684269-2″;
urchinTracker();

WLST – BEAWorld session – part 3

Monday, October 17th, 2005

Continuing with the demo, this will be the final part of the demo.
Now we have a running domain that has an Administration server, two managed servers that belong to a Cluster. For the final part of my demo I used a script that deployed a simple web application to the cluster using the ‘deploy’ command. WLS 9.0 implements the JSR88 API’s for all deployments and WLST surfaces all the deployment functionality using these API’s. The script is shown below.

from java.lang import Runtime
print “Deploying demo application”
deploy(“demo”, “d:/bea/apps”, “mycluster”)
print “Done deploying to cluster”
Runtime.getRuntime().exec(“rundll32 url.dll,FileProtocolHandler http://localhost:8001/beaworld”)

In the script I used the class java.lang.Runtime to start a browser that points to the web app that I just deployed. This demonstrates one of the most useful features of Jython that is able to seemlessly invoke on any Java object. You can also create/modify deployment plans via WLST. If you are not familiar with what a deployment plan is, it is an XML document that resides outside of an application archive and configures an application for deployment. A deployment plan works by setting deployment property values that would normally be defined in an application’s WebLogic Server deployment descriptors, or by overriding property values that are already defined in a WebLogic Server deployment descriptor. For more information on deployment plan’s check out BEA edocs. For the demo I re-deployed the webapp with a deployment plan that overrides the web app’s context root from ‘beaworld’ to ‘wlst’. Before redeploying the application here’s the script that you can use to create a plan and override the context root.

# This will load the application and return a plan object
myplan=loadApplication(“d:/bea/apps”)
# create a Variable
myplan.createVariable(“croot”,”wlst”)
# assign the variable to the module descriptor
cc=myplan.createVA(“croot”,”apps”,”WEB-INF/weblogic.xml”)
# set the xpath
cc.setXpath(“/weblogic-web-app/context-root”)
# save the plan
myplan.save()

and the redeploy script is shown below.

print “Re-Deploying demo application with a Plan”
redeploy(“demo”, planPath=”d:/bea/plan.xml”)
print “Done re-deploying to cluster with plan”
Runtime.getRuntime().exec(“rundll32 url.dll,FileProtocolHandler http://localhost:8001/beaworld”)

The plan.xml is shown below.

<deployment-plan xmlns=”http://www.bea.com/ns/weblogic/90″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”>
···<application-name>apps</application-name>
······<variable-definition>
·········<variable>
············<name>croot</name>
············<value>wlst</value>
·········</variable>
······</variable-definition>
······<module-override>
·········<module-name>apps</module-name>
·········<module-type>war</module-type>
·········<module-descriptor external=”false”>
············<root-element>weblogic-web-app</root-element>
············<uri>WEB-INF/weblogic.xml</uri>
············<variable-assignment>
···············<name>croot</name>
···············<xpath>/weblogic-web-app/context-root</xpath>
············</variable-assignment>
·········</module-descriptor>
·········<module-descriptor external=”false”>
············<root-element>web-app</root-element>
············<uri>WEB-INF/web.xml</uri>
·········</module-descriptor>
······</module-override>
···<config-root xsi:nil=”true”></config-root>
</deployment-plan></font>

This is the end of the demo. You can also get more information on all the sessions that are presented at BEAWorld here.

_uacct = “UA-2684269-2″;
urchinTracker();

WLST – BEAWorld session – part 2

Monday, October 10th, 2005

Continuing with the demo …
After connecting to the Administration server, I ran a script that navigated me to the edit tree. Started an edit session with the server, configured servers, a Cluster and started the cluster. Lets take a look at the script.

# start the edit session, configure servers, cluster and start the cluster
print “configuring servers and cluster”
edit()
startEdit()
print cmo
svr1 = cmo.createServer(“ms1″)
svr2 = cmo.createServer(“ms2″)
clus = cmo.createCluster(“mycluster”)
svr1.setListenPort(8001)
svr2.setListenPort(9001)
svr1.setCluster(clus)
svr2.setCluster(clus)
save()
activate()
print “Done configuring servers and cluster”
print “start the cluster”
start(“mycluster”,”Cluster”)
print “Done starting the Cluster”

There is a global variable in WLST called ‘cmo‘ that stands for ‘Current Managed Object‘. This variable is always set to the MBean proxy object that the user navigated to. Since the root of edit tree/drive is DomainMBean, the cmo is set to the DomainMBean proxy. One of the best features of Jython or in fact any Java scripting tool I like the most is to be able to invoke any of the public methods on Java objects in a script. You can declare variables on the fly and assign java objects to these variables. In the script shown above I invoked the method ‘createServer‘ on DomainMBean’s proxy (cmo) and assigned the return value (which is the ServerMBean) to the variable ‘svr1‘. Later I invoked the method ‘setListenPort‘ on the ServerMBean Proxy stored in the variable ‘svr1‘. You can also verify all you changes with the ‘showChanges‘ command. Once you do a save(), you will notice that the changes are written to the DOMAIN_DIR/pending directory and once you do an activate the changes are applied to the running server and the files in the pending directory are removed. After activating my changes with the command ‘activate‘, I started the Cluster. Under the covers WLST invokes the Administration Server which inturn sends the command to start the Servers to Node Manager. Node Manager starts the servers and monitors the state of these servers.
···For the demo I chose to start a Cluster but I was little paranoid because to start a Cluster you will need a network connection to your machine. If in case I did not have a network connection I thought I would just start the servers without a cluster with the backup script as shown below.

# start the edit session, configure serversand start the servers
print “configuring servers “
edit()
startEdit()
print cmo
svr1 = cmo.createServer(“ms1″)
svr2 = cmo.createServer(“ms2″)
svr1.setListenPort(8001)
svr2.setListenPort(9001)
save()
activate()
print “Done configuring servers”
print “start the servers”
start(“ms1″, block=”false”)
start(“ms2″)
print “Done starting the servers”

In my script I had start(‘ms1′, block=’false’) which will let the script to execute the next command after that without blocking. Here’s the WLST console output after running the script.

wls:/mydomain/serverConfig> execfile(“d:/bea/cs.py”)
Already Connected!
configuring servers and cluster

Location changed to edit tree. This is a writable tree
with DomainMBean as the root. To make changes you will
need to start an edit session via startEdit().
For more help, use help(‘edit’)

Starting an edit session …
Started edit session, please be sure to save and activate your
changes once you are done.
[MBeanServerInvocationHandler]com.bea:Name=mydomain,Type=Domain
Saving all your changes …
Saved all your changes successfully.
Activating all your changes, this may take a while …
The edit lock associated with this edit session is released
once the activation is completed.
Activation completed
Done configuring servers and cluster
start the cluster

Starting the following servers in Cluster, mycluster: ms2,ms1
……………………………………………………………………………………
All servers in the cluster mycluster are started successfully.
Done starting the Cluster
wls:/mydomain/edit>

Also, if you are in the interactive mode when you start an edit session, you will notice that the prompt contains a ‘!’ (wls:/mydomain/edit !>) indicating an edit session is in progress. As soon as you activate or stop your edit session the ‘!’ is gone from the prompt.

I will continue the demo in my next blog entry.

_uacct = “UA-2684269-2″;
urchinTracker();

WLST – BEAWorld session – part 1

Monday, October 3rd, 2005

In BEAWorld this year I had a session on New and Enhanced WebLogic Scripting Tool with the tag line ‘Automate the mundane and slay the difficult’. Inspite of being the last session for the whole event I was impressed with the turn out. Before diving deep into my presentation I did a WLST familiarity check and was pleasantly surprised by the number of users in the audience who raised their hands. The familiarity is majorly due to the fact that we released WLST on dev2dev that works for WLS 7.0 and 8.1 (all service packs). BEA very well understands that many of its major customers do not prefer using tools that are not supported. Hence BEA recently announced support for WLST 7.0 and 8.1. So now users can grab the tool from dev2dev and start using it. If you find any problems you can even file a support case and we will resolve the issue.
···As part of my presentation I have demo’ed some new features in WLST that are available OOTB in 9.0 and promised the audience that I will make the demo scripts available via my Blog. The demo included how one could start from scratch and create a full blown domain consisting of servers in a cluster all via few or one single simple WLST script(s). The demo started of with a simple script that loaded a default template that ships with a typical WLS 9.0 installation. The script sets the administration username and password, sets the administration server name to ‘myserver’ and writes the domain to a directory. Here’s the script that is used to setup the default domain.

# Read the default template that comes with WLS installation
print “Read the default domain”
readTemplate(“d:/bea/load47/weblogic90/common/templates/domains/wls.jar”)
# set the password for default administration user
cd(“Security/base_domain/User/weblogic”)
cmo.setUserPassword(“weblogic”)
setOption(‘OverwriteDomain’, “true”)
cd(“/”)
cd(“Servers/AdminServer”)
cmo.setName(“myserver”)
writeDomain(“d:/bea/mydomain”)
print “Done creating and writing the domain”
closeTemplate()

Lets explore how you can enhance this script to do more. We will configure couple of managed servers, create a cluster and add these managed servers to this cluster. In my demo this is exactly what I did after starting the server and while connected to the server. Here we will do all this even before starting the administration server. The script will look as shown below.

# Read the default template that comes with WLS installation
print “Read the default domain”
readTemplate(“d:/bea/load47/weblogic90/common/templates/domains/wls.jar”)
# set the password for default administration user
cd(“Security/base_domain/User/weblogic”)
cmo.setUserPassword(“weblogic”)
setOption(‘OverwriteDomain’, “true”)
cd(“/”)
cd(“Servers/AdminServer”)
cmo.setName(“myserver”)
cd(“/”)
create(“ms1″,”Server”)
create(“ms2″,”Server”)
create(“mycluster”,”Cluster”)
cd(“Servers/ms1″)
cmo.setListenPort(8001)
cd(“/”)
cd(“Servers/ms2″)
cmo.setListenPort(9001)
assign(“Server”, “ms1,ms2″, “Cluster”, “mycluster”)
writeDomain(“d:/bea/mydomain”)
print “Done creating and writing the domain”
closeTemplate()

After we had our minimal domain setup, I started a Node Manager using all the defaults. By default the Node Manager Server is started at port number 5556 and all the logs will be written to the directory where WLST is running. For more options check out the help for startNodeManager via help(‘startNodeManager’). Now once I have the Node Manager up and running, I used the command nmConnect to connect to the Node Manager and started the administration Server. After the server is started I connected to the server. Here’s the script that I used.

# start the Node Manager, this starts at
# default port 5556
startNodeManager()
# connect to the NodeManager, the default admin
# username and password can be used
nmConnect(“weblogic”,”weblogic”)
# start the server via the NM
nmStart(“myserver”)
# now disconnect from NM
nmDisconnect()
# connect to the administration server that we just started
connect(“weblogic”,”weblogic”)

If you notice I used the administration username and password to connect to the NodeManager server. This is because by default the username and password are set to the administration server’s username and password in development mode. If you set up your domain to run in production mode you will have to explicitly set this username and password via the SecurityConfigurationMBean’s NodeManagerUsername and NodeManagerPassword attributes. Here’s the WLST console output after running both the scripts.

D:\bea\mydomain>java weblogic.WLST

Initializing WebLogic Scripting Tool (WLST) …

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

wls:/offline> execfile(“d:/bea/domain.py”)
Read the default domain
Done creating and writing the domain
wls:/offline>execfile(“d:/bea/start.py”)
Launching Node Manager …
Successfully launched the Node Manager.
The Node Manager process is running independent of the WLST process
Exiting WLST will not stop the Node Manager process. Please refer
to the Node Manager logs for more information.
The Node Manager logs will be under D:\bea\mydomain\.

Connecting to Node Manager …

Successfully connected.

Starting server myserver

Server myserver started successfully

Successfully disconnected from Node Manager

Connecting to weblogic server instance running at t3://localhost:7001 as username weblogic …

Successfully connected to Admin Server ‘myserver’ that belongs to domain ‘mydomain’.

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.

wls:/mydomain/serverConfig>

To recap what we did so far, I created a minimal domain from a template (that ships with BEA WebLogic Server), started a Node Manager Server, connected to it and started the administration server in the domain we created, disconnected from Node Manager Server and connected to the administration server that we just started.

I will continue the demo in my next blog entry.

_uacct = “UA-2684269-2″;
urchinTracker();

BEAWorld session on WLST

Tuesday, September 6th, 2005

BEAWorld session on WLST
We all know that BEAWorld 2005, Santa Clara is right around the corner (the last week of September). There are many interesting sessions this year, but unfortunately there isn’t a session that talk’s about the new WLS 9.0 Management Framework/API that we(i.e the WLS OAM Team) worked on for the past 2 years. Since I am a member of WLS OAM Team and doing a session on WLST this year, I thought I would take this oppurtunity to introduce the new Management API on which WLST is built on. In my coming blogs I will talk about the new WLS 9.0 Management API.

_uacct = “UA-2684269-2″;
urchinTracker();

Generate configuration scripts from console with WLSTScriptGenerator

Wednesday, August 17th, 2005

* Have you ever wondered if you could record all your configuration steps that you went through via the console to setup your domain and play the recorded steps to configure the same resources in different domains?

* Have you ever wondered what JMX MBean you are changing when you are clicking a checkbox in the console?

* Have you ever wondered if you could audit all the changes to your configuration (the user who started the edit session, at what time and the user who closed the edit session and at what time) ?

If your answer is Yes to any of the questions above, WLSTScriptGenerator is for you. If you never used WLST and would like to automate some WLS Administration tasks, you should start off with WLSTScriptGenerator.

Many 9.0 WebLogic Administrators are hooked to the console (well, with the new look and feel, who wouldn’t be ;) ), but there are times where you would make a bunch of changes, go through different wizards, click different buttons and setup your domain. Now if you have to go through the same process every time you setup a domain, it is time consuming and error prone. WLSTScriptGenerator lets you create a script per each edit session, the generated script will contain all the WLST equivalent commands that you can use to make same changes. The script will also contain the username who started the edit sessiion and at what time and the time and username who activated the changes.
When you start your Administration Server with WLSTScriptGenerator in front of your classpath, it will intercept each JMX call that is made to the MBeanServer and converts any change to the MBeans to a WLST command and writes it to the script, its that simple.. :-) . So, any changes via straight JMX are also converted to a WLST Script, not just the changes from the console. All the generated scripts will be saved to (DOMAIN_DIR)/wlstScripts directory.
For example, I have started an Administration server, logged into the console, created a Managed server and set the listen port. Created a Cluster and assigned the Managed Server to the Cluster. Saved my changes and activated them. If I look under my domain_dir/wlstScripts I see a jmxToWlst.py and the script generated looks something like this.
“”"
@author Copyright (c) 2004 by BEA WebLogic. All Rights Reserved.
This script is being created as part of your console/wlst/jmx interactions.
You might have to tweak this script a little bit if you may find any syntax errors.
If you find any problems, please email Satya Ghattu at sghattu@bea.com
Script Generation Begin Time: Tue Aug 16 18:37:00 EDT 2005
“”"
edit()
startEdit(0,-1,’false’)

#Script Generation Started By: weblogic

cd(‘/’)
cmo.createServer(‘ms1′)
cd(‘/Servers/ms1′)
set(‘ListenPort’,’8001′)
cd(‘/’)
cmo.createCluster(‘mycluster’)
cd(‘/Servers/ms1′)
cmo.setCluster(getMBean(‘/Clusters/mycluster’))
activate()

“”"
Script Generation Ended By: weblogic
Script Generation End Time: Tue Aug 16 18:37:52 EDT 2005
“”"

By default the scripts are always generated for every edit session when you have the wlstScriptGenerator.jar in your classpath. To disable the script generation, fire up a WLST session and connect to the Administration Server and,
edit()
startEdit()
mbs.setAttribute(ObjectName(‘com.bea:Name=WLST,Type=WLST’), Attribute(‘Recording’,'false’))
activate()

To re-enable the script generation just set the ‘Recording’ attribute on the WLST MBean to ‘true’.

I hope WLSTScriptGenerator lets you get a jump start on using WLST and help you automate your daily WebLogic Administration tasks.

_uacct = “UA-2684269-2″;
urchinTracker();