Friday, April 24, 2015

Use WLST scripts just like SHELL scripts for automation

How to use wlst scripts for automation, with ease of shell script

 

Since I come from shell scripting background, the two wlst invocations methods i.e. interactive and script mode, caused discomfort to me due to my lack of expertise in java/jython programing. I will not even discuss about the third mode i.e. embedded mode, which requires much more java flare.

I often used to wonder about the way I can write and use wlst scripts just like I make use of shell scripts.

So sharing my finding which have made my life comfortable. I personally feel that this approach is better (at least for me), than most of the scripting approaches I found on net.

To make a point, let me give short example on how the other approaches lack the ease and flexibility that is so easily available from shell scripts.

Ø  Conventional Approach 1: Scripting Mode

java weblogic.WLST <<your wlst script.py>>

 

As per oracle documentation, this method helps you in automation since you can make use of loops, constructs based on jython.

BUT, why I do not like this method is, because using this approach, all your script files will have to have authentication and connection code. For e.g., script to stop the server and the other one to retrieve the status of the servers, both need to have code for authentication.

Another way to sooth this pain a bit, is to make use of functions in separate files and import those functions in the final code you write. Go ahead this method if you have time to practice scripting and gain expertise. Continue to read if you’d like to explore an easier option. J

Ø  Conventional Approach 2: Interactive Mode

Another usual way is to launch WLST shell and type your commands in the shell

WL_HOME/server/bin/setWLSEnv.sh

 

This is certainly is a good way of testing the wlst commands and constructs before you put them into a script, but it’s not an ultimate tool that will let you automate your tasks.

 

Ø  Easy Way: The Shell Script Way

An easier way, as per me, is to divide wlst code for basic tasks (like authentication, connection, executions, etc. tasks) in different files and call those snippet files from a single shell script file.

But to achieve the above we need to tweek the SHELL interpreter or SHE bang line, which is the very first line in any shell script, so as to use WLST as interpreter rather bash, ksh, or other shell.

 

The below example will make the things pretty clear.

Here, I have two wlst snippet files:

-          ConnectMyPOC.py: For authenticating and connecting to my wlst domain.

-          StatusAllServer.py: To check the status of the server in mydomain.

I’ll create a 3rd file to wrap first two files within the shell script like file and use wlst engine to interpret the later script rather than any other unix shell to do so.

 

Below are the wlst files with self-explanatory code.

# ConnectMyPOC.py wlst snippet file, to connect to domain

[oracle@POCSERVER WLSTScripts]$ vi ConnectMyPOC.py

#!/opt/app/oracle/middleware/oracle_common/common/bin/wlst.sh

connect(url='t3://10.xx.xx.xx:7001', userConfigFile='/home/oracle/.scripts/.wlstscriptkey/myconfigfile.secure', userKeyFile='/home/oracle/.scripts/.wlstscriptkey/mykeyfile.secure');

 

# StatusAllServer.py wlst snippet file, to check status of all servers in the domain

sList=ls('Servers', returnMap='true')

for i in sList:

state(i,'Server',returnMap='true')

 

# Wrapper script, calling the two scripts shown above. Notice the very first line in the script.

vi ServerStatus.wlst

#!/opt/app/oracle/middleware/oracle_common/common/bin/wlst.sh

execfile('/home/oracle/.scripts/WLSTScripts/ConnectMazdaPOC.py')

execfile('/home/oracle/.scripts/WLSTScripts/StatusAllServer.py')

 

# Now assign execute permission to the wrapper file.

chmod u+x ServerStatus.wlst

 

Now you are free to use it like a shell script and do your cron based scheduling or any automation activity you usually perform with shell scripts.

 

[oracle@POCSERVER WLSTScripts]$ ./ServerStatus.wlst

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0

 

Initializing WebLogic Scripting Tool (WLST) ...

 

Welcome to WebLogic Server Administration Scripting Shell

 

Type help() for help on available commands

 

Connecting to t3://10.xx.xx.xx:7001 with userid weblogic ...

Successfully connected to Admin Server "AdminServer" that belongs to domain "mazdapoc_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.

 

dr-- AdminServer

dr-- ess_server1

dr-- odi_server1

dr-- osb_server1

dr-- soa_server1

 

Current state of "AdminServer" : RUNNING

Current state of "ess_server1" : SHUTDOWN

Current state of "odi_server1" : RUNNING

Current state of "osb_server1" : RUNNING

Current state of "soa_server1" : RUNNING

[oracle@POCSERVER WLSTScripts]$

 

Voila... For more, continue to SeeItWithSanjay!

 

No comments:

Post a Comment