Wednesday, February 25, 2009

Variable Definitions In The Preamble [WLST scripting Best Practices (Part-1)]

With WLST becoming popular among WebLogic Administrator, there is an unsaid need to discuss the best practices with respect to WLST scripting. I am being asked to share information about WLST like an introductory/101 post. But I decided to start a series of post which might help WebLogic Administrators with some of the must follow best practices rather than writing some introductory post which might duplicate the documentation. For WLST documentation see here.

The common steps involved in a WLST script that is used to configure WebLogic Server are as follows:

1. Connect to server
2. Navigate to the respective location (MBean)
3. Modify the required properties
4. If more configuration needs to be modified continue Step-2
5. Else disconnect
6. Exit

The first of the series of best practices in authoring a WLST script is to define all the required variables at the beginning before connecting to the server. That way if any modifications must be done in the future it will be as easy as changing the variables defined at the beginning of the script. In the coming posts, I will talk about more of other best practices like writing a reusable WLST script, consuming information from the environment variables, exception handling, defensive scripting, avoiding to hard code critical information like username, password, url etc.

The usual variables are the username, password, url, resource names, attribute values etc. The following is an example of a script that defines all the required values as script variables which can avoid hardcoding and will also facilitate simple way to modify them in the future if required.


#CreateServer.py
#Define all the requried variable
username = 'system'
password = 'weblogic'
url = 't3://egAdm:7001'
serverName = 'mgdserver1'
listenAddress = 'egmgd1'
listenPort = '2001'

#Connect to the Administration Server
connect(username, password, url)

#Start a change session
edit()
startEdit()

#Create the Managed Server
create(serverName, 'Server')
cd('/Servers/' + serverName')
cmo.setListenPort(listenPort)
cmo.setListenAddress(listenAddress)

#Activate the changes
save()
activate()

#Disconnect from the Administration Server
disconnect()
exit()


The above script may not be authored using all the best practices now. But I will eventually build on the same example of creating a new Managed Server in my subsequent posts to address the other best practices as well. My next post will talk about how to check for existing change session before you start one with WLST.

WLST-BP-1 Define all the variables in the preamble

2 comments:

  1. Thanks Bala, this is helpful to WLST newbies.

    It is common practice to use all caps for names of constants in many programming languages including Python. In scripts like these, the variables are effectively constant. I would recommend using names like USERNAME and MGD_SERVER_NAME.

    These names also stand out more clearly in the main body of the script.

    BTW there is a bug in this line:
    cd('/Servers/' + mgdServerName')
    You have an extra quote.

    Cheers
    Michael

    ReplyDelete
  2. Yes if the truth be known, in some moments I can bruit about that I agree with you, but you may be making allowance for other options.
    to the article there is even now a suspect as you did in the decrease issue of this beg www.google.com/ie?as_q=lavasoft ad-aware 2007 professional 7.0.2.7 ?
    I noticed the utter you suffer with not used. Or you partake of the pitch-dark methods of development of the resource. I suffer with a week and do necheg

    ReplyDelete