Search This Blog

Loading...

Sunday, March 20, 2011

WLST Tricks & Tips

Here I prepared few sorted out Tips and useful Tricks for developing the WLST scripts, which I learn with my experiments with Programming Python commands for WLST scripts. These WLST commands can be applicable in WebLogic 9.x to WebLogic 10.3.6 or WebLogic 12c.

Tip 1. Know about Where are you
On some situations you might need a clarity about which WebLogic version you are using. WebLogic 11g(10.3) has different sub-sequent versions 10.3.1,10.3.2,10.3.3 and 10.3.4, for all these the directory structure is ~/Oracle/Middleware/wlserver_10.3 which does not tells you about sub versions. Here WLST  is having a solution for this problem,

You can find the current WebLogic version to which WLST is connected.
wls:/offline> print version
WebLogic Server 10.3.3.0  Fri Apr 9 00:05:28 PDT 2010 1321401

How do you know that the WLST Shell running on which JVM version? The simple solution is you this following Python command that will gives you:

wls:/offline> sys.platform
'java1.6.0_21'

How to know which Python/Jython version is supported by WLST?
To get the Jython version you can use sys.version_info method that will returns. or you can use sys.version exact version.

wls:/offline> import sys
wls:/offline> sys.version_info
(2, 2, 1, 'final', 0)


Tip 2. Introspection in WLST
You might understand that using WLST provided help() sometimes you cannot reach your desired level of scripting. Some thing that won't be documented but you might need it to develop the prototype in your WLST script. Often I found most helpful way to reach depth of WLST classes, modules, functions. Best way is we need to use Python introspection methods. It is a wonderful way to resolve your coding clues to use best performing script as outcome.

In some of the situations, I felt using Jython (Java method) is lengthy than using Python. When Python giving you desired outcome in two lines of code why do you choose Java imports and classes and code become 6-10 lines.

You might be confused sometimes, brain storm about something why a MBean not working or giving the value of an attribute etc. In your mind there could be a question "what is this actually??" Best solution for this is using Python introspection built-in methods.

dir([WLST/Python Object])
If your WLST MBean or module or a function has documented then you use the following to get that:
_doc__

In Jython there could be classes or interfaces
__class__

Tip 3. Naming Rules for WLST
WLST script can have names are case sensitive and cannot start with a number.  They can contain letters, numbers, and underscore symbols. Let us see few examples here:
 state  State  _state  _2_state_  state_2  StatE

Most of us face an issue what name makes sense for a variable, module or classwhile writing the WLST scripts. Just like Java Language you can follow specific naming rules, we can also find keyword list available in WLST with the following commands
wls:/offline> import keyword
wls:/offline> keyword.kwlist
['and', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'yield']

Modules, packages: use lowercase
Classes: use Capitalized first letters in the Words also follow for exceptions that you create
Methods, attributes: use lowercase_words
Local variables: use alphanumeric i, j, sum, x0, etc.
Globals: use alphanumeric long_descriptive_names

wls:/offline> sys.modules
{'weblogic.store.admintool.CommandDefs.CommandType': , ...'sys': sys module, 'weblogic.management.scripting.utils': }

Introspection reveals useful information about your program's objects
Tip 4 Write a readable script
While writing a script think about the human readers, Question yourself can you still read your own code ???
next month?
next year?
Be consistent (but not too consistent!)
Use white-space judiciously
Write appropriate comments
Write helpful doc strings -- not stories or novels
Conform to the existing style
even if it’s not your favorite style!
local consistency overrides global

Update the comments!!
and the doc strings!!!

"""Brief one-line description.

Longer description, documenting
argument values, defaults,
return values, and exceptions.
"""

When to use classes in WLST? (...and when not!)
If your script objective is going to have multiple object creation then go for Object-Orientation.

You could choose class members or attribute  at the first time, the easy to make mistakes
  • name clashes between attribute


Tip 5 print with str()
Most of us from Java/JEE development background common attitude is that using the + operator overloading for strings. Sometimes your WLST script might need to have a print statement, that could include values from a variable or some mathematical expression that might be int type or some other object type. Then such cases, better to use that variable type must be converted to string type by using Python built-in function str() as shown :

print 'Heap Size' + str(totalHeap)

Tip 6: Create single properties file
Every Middleware admin who uses WLST in their environment builds, they must create/use a single properties file where you can have:

  1. Domain properties - username, password, admin url, cluster list
  2. Database configuration properties
  3. JMS configuration properties
  4. WorkManager properties
  5. Monitoring Threshold properties
Tip 7: Using time in WLST
In my daily news updates from social medias I found in reddit.com in the Python community doing excellent there is lots of news about the changes happening with Python programming. One of the blog found very nice examples with date and time functions in Python this can be applicable for our WLST too.

http://www.whitemiceconsulting.com/2012/10/setting-course-for-utc.html
WLST Tricks
We have come across many situations where it is simple trick works faster to work with WLST scripting. some of them collected here and future also we will update this WLST tricks

Trick 1. Multiple Assignment
In WLST you can assign multiple variables with corresponding multiple values at the same statement or command line.
wls:/offline> cl, ms = ['appclstr','webclstr'], ['app01','app02','web01','web02']
wls:/offline> cl
['appclstr', 'webclstr']
wls:/offline> ms
['app01', 'app02', 'web01', 'web02']

use these built-in functions, enjoy Python scripting in WLST... : -)

Trick 2. Connect admin without Credentials 
When you use storeUserConfig() command for your WebLogic domain environment, the trick here is when you don't specifying any arguments such as user security file, key file paths. WebLogic system automatically generates the following two files:
1. username-WebLogicConfig.properties
2. username-WebLogicKey.properties

For example, Let us assume your user name is 'padmin' then storeUserConfig() command will generates two files as:
* padmin-WebLogicConfig.properties
* padmin-WebLogicKey.properties

Once you got this autogenerated files in you home ($HOME in Unix) directory, WLST Shell automatically detects its existance from this location. You don't need to give the userConfig, Key file paths while connecting. Simply you can connect by giving the adminurl as url value.

wls:/offline> connect(url='t3://myapp.host.com:8756')
Connecting to t3://myapp.host.com:8756 with userid weblogic ...
Successfully connected to Admin Server 'wadmn' 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.


Trick 3: Command Trials
The interactive mode is great for getting instant feedback on the behavior of each WLST command. Having the whole startup, connection, session-init is time-consuming and the interactive mode of WLST avoids this.

First time if you are making a script better idea is have duplicate windows of the host. In one window you can edit and save according to your required changes, on the other hand you can execute the file with execfile('urScript.py').

Trick 4: Using loadProperties
One of my blog follower Raghu asked me how to use properties in the WLST script. Though I had used different types of properties from properties file, but his requirement is something different he want to reuse the WLST script only by changing the properties file.

mys.properties
SERVERS=myser1,myser2,myser3

Here is the python script snippet that uses above properties
loadProperties('path/mys.properties')
print SERVERS
serverlsit=SERVERS.split(',')
print serverlist[1]

for i in serverlist:
    print i

Remember that properties are just like Java language properties or C/C++ define constants, which holds the string values. So when you store portno in properties file better convert using int().
Hope this is useful for your reusable scripts.

Trick: How to debug WLST script?

The best way to identify the WLST script bugs using dumpStack(). This works only when you are running interactive mode. If you found a script (such as my blog) or from somewhere after you update all properties you wish to test and it doesn't works! then you wish to debug it.
1. Using simple print variable values
2. Using Python exception values
3. Double check indentations
4. Casting values (converting values str to int or str to bool)

Some of the cases you need to understand that casting fails the values are not allowed to change because WLST variables are created in Runtime. The datatypes are strong type like Java and C++ languages and dynamic type while running it will be assigned to a datatype.

You can enable debug flag at the time of invocation of WLST shell.

java -Dpython.verbose=debug weblogic.WLST

Also you have one more option using debug("true") or set('DebugEnabled', 'true') in the starting of WLST script that will force debug output

If you are using OEPE (Eclipse for developing WLST) then you have much scope to debug every variable content. Right click on the WLST script select 'Debug As', choose 'WLST run' from the list Menu. Please check the screenshot that give you more clear idea on this Oracle link.

Trick Cancel the Edit in WLST

Today while working on WLST in interactive mode to fix the JMS configuration issue, sometime you start editing the configuration you might need to undo the changes you did on the edit tree. This is possible using cancelEdit() give the answer y to confirm when it prompts you. Else you can use cancelEdit('y') as a trick.

Trick: Suppress Junk Output in WLST

There is junk when you capture the list from ls(returnMap='true') command execution.
If you are running WLST in *nix based environment you can use /dev/null path for supressing the junk outputs as shown below redirect command.
redirect('/dev/null','false')
ls()
redirect('/dev/null','true')


-->

Keep writing your valuable comments and great suggestions...

References:
  1. 1. Dive into Python
  2. Python Library link
  3. http://forums.oracle.com/forums/thread.jspa?messageID=9416182&tstart=0

No comments:

Facebook Blogger Plugin: By RNHckr.com

Post a Comment

Please write your comment here

Popular Posts

Followers