Friday, July 4, 2014

WebLogic Scripting Tool (WLST) made easy

The WebLogic Scripting Tool (WLST) is a command-line scripting environment that you can use to create, manage, and monitor WebLogic Server domains.
It is based on the Java scripting interpreter, Jython. It's implemented in Jython i.e., Python running in JVM.
In addition to supporting standard Jython features such as local variables, conditional variables, and flow control statements, WLST provides a set of scripting functions (commands) that are specific to WebLogic Server.

WLST script is running JMX against Weblogic Server.

WLST is efficient and versatile, it can,
I. replace Configuration wizard,
ii. replace template builder,
iii. if you are doing command line deployment using weblogic.Deployer then it can also be done with wlst, and
iv. wlst can also replace the whole Admin console.

WLST is running in offline mode i.e. when you use wlst it is not connected to Admin server. To be online mode we should connect to the admin server using the wlst 'connect()' command.

start WLST
Run the script $MW_HOME/wlserver_12.1/common/bin/wlst.sh
or
export WL_HOME=/u01/mw/wlserver_12.1
export CLASSPATH=weblogic.jar
java weblogic.WLST <script.py>

If you have script.py then use it other wise you can just connect to the wlst and use it interactively.

offline mode
By default the wlst takes you to offline mode prompt,

wls:/offline>

online mode
To connect to the Admin server use the below connect command with username/password,
wls:/offline> connect('weblogic','weblogic1')

The above connect will connect to the default t3://localhost:7001 and takes you to the below online prompt,
wls:/dev12/serverConfig>
 
If you don't want the username password credentials in connect() then use 'storeUserConfig()' which will create a key file with encrypted username/password in it, and it will be used to connect to the admin server next time when you just provide connect().
wls:/dev12/serverConfig> storeUserConfig()
 
Below are few commands that you can use,
ls() - To retrieve all the attributes, all the operations and all the child mbeans.
ls('c') - To retrieve all the child mbeans.
ls('a') - To retrieve all the attributes.
ls('o') - To retrieve all the operations.
 
wls:/dev12/serverConfig> cd('Servers')
wls:/dev12/serverConfig/Servers> ls()
dr--  AdminServer
dr--  oam_server1

Above commands are using Python syntax so if you want to make it more easier just use 'easeSyntax()'
wls:/dev12/serverConfig/Servers> easeSyntax()

easeSyntax() - Allows you to just type commands without ( ), example: ls, 'ls c', 'ls a', 'ls o', cd Servers, etc
But it works for most of the commands with about one attribute/parameter.

lock & edit mode
For 'lock and edit' mode use edit() and startEdit() commands,
wls:/dev12/serverConfig/Servers> cd oam_server1
wls:/dev12/serverConfig/Servers/oam_server1> edit()
wls:/dev12/edit> cd /Servers/oam_server1
wls:/dev12/edit/Servers/oam_server1> ls('a')
-rw- AdministrationPort 7001
-rw- HttpEnabled   true

Note the -rw- read write mode for the attributes. Now to edit the attribute use the startEdit() command,
wls:/dev12/edit/Servers/oam_server1> startEdit()
wls:/dev12/edit/Servers/oam_server1 !> cd SSL
wls:/dev12/edit/Servers/oam_server1/SSL !> cd oam_server1
wls:/dev12/edit/Servers/oam_server1/SSL/oam_server1 !> ls a
-rw- ListenPort   7005
-rw- LoginTimeoutMillis  25000
-rw- Name oam_server1

Now let us change ListenPort from 7005 to 7777 using set() command, while to get a value of any attribute use the get() command, and save(), activate() to save and activate the changes.
wls:/dev12/edit/Servers/oam_server1/SSL/oam_server1 !> set('ListenPort','7777')
wls:/dev12/edit/Servers/oam_server1/SSL/oam_server1 !> get ListenPort
7777

save settings
wls:/dev12/edit/Servers/oam_server1/SSL/oam_server1 !> save()
wls:/dev12/edit/Servers/oam_server1/SSL/oam_server1 !> activate()wls:/dev12/edit/Servers/oam_server1/SSL/oam_server1>

To go back from edit to serverConfig mode use the serverConfig() command,
wls:/dev12/edit/Servers/oam_server1/SSL/oam_server1> serverConfig()

wls:/dev12/serverConfig>  

check current managed object - cmo
To check the 'current managed object - cmo' use the below command,
wls:/dev12/edit/Servers/oam_server1/SSL/oam_server1 !> print cmo
[MBeanServerInvocationHandler]com.bea:Name=oam_server1,Type=SSL,ManagedServer=oam_server1





find
The find command is useful, for us to know on which level is so an so MBean attribute,
wls:/dev12/serverConfig> find('ListenPort')


 
help
To display information about WLST commands and variables, enter the 'help' command.
If you specify the help command without arguments, WLST summarizes the command categories. To display information about a particular command, variable, or command category, specify its name as an argument to the help command.

To list a summary of all online or offline commands from the command line using the following commands, respectively:

help('online')  
help('offline')
help('all')
help('browse')
help('common')
help('contorl')
help('deployment')
help('diagnostics')
help('editing')
help('information')
help('lifecycle')
help('nodemanager')
help('storeadmin')
help('trees')
help('variables')
The help command will support a query; for example,
help('get*')
It displays the syntax and usage information for all commands that begin with get.

For example, to display information about the disconnect command, enter the following command:
wls:/mydomain/serverConfig> help('disconnect')
The command returns the following:
Description:
Disconnect from a weblogic server instance.
Syntax:
disconnect()
Example:
wls:/mydomain/serverConfig> disconnect()

wlst quick reference
ls() - ls('a') shows attributes, 'c' child MBeans, 'o' operations
cd() - navigation

serverConfig() - change to config MBeans
serverRuntime() - change to server runtime
domainRuntime() - change to domain runtime
custom() - change to custom tree
jndi() - change to JNDI tree

edit() - change to writable edit tree
startEdit() -  start an edit session, then use set('attribute','value')
save() - persist changes
activate() - activate changes
undo() - undo changes

configToScript() - export domain configuration to WLST script
find() - find attributes
easeSyntax() - swith off python syntax(do not use for scripting!)

Useful Pointer:
On weblogic console page, there is a 'Record' link, that can record all the changes that we are doing in 'Lock and Edit' mode and it saves the complete steps to a file in wlst commands format. This is a good way of getting the MBean hierarchy automatically by just recording it.

a. login to weblogic console http://servername.domainname:7001/console
b. Click 'Lock & Edit' button
c. Click on 'Record' link
d. Now do any changes on the console and it gets recorded into a .PY extention file in $DOMAIN_HOME/dev12 directory

From below link, you can download the 'Oracle® Fusion Middleware WebLogic Scripting Tool Command Reference 11g Release 1 (10.3.6)' 
http://docs.oracle.com/cd/E23943_01/web.1111/e13813.pdf

For any further queries or help please don't hesitate to contact me on samiora@gmail.com