Ravish Mody

Yesterday one of our subscribers Ravi Shankar Kumar had asked us using comment how to target and untarget multiple datasources in the same domian using a single generic WLST script, as they have total 5 clusters and want to target each cluster to different datasources and as per his requirement we created this simple WLST script which can do the same things in this single script.

As usual we have used a properties file which has all the required details in it like the datasources names, targets name and number of datasoures which has to be updated. This way you would not have to modify anything in your actual WLST script.

Target and Untargeting of Multiple DataSources using WLST

Step1). Create a Directory somewhere in your file system like : “C:\WLST\DataSource

Step2). Write a Properties fileDsDetails.properties inside “C:\WLST\DataSource” like following:

userName=weblogic
password=weblogic
admin.Url=t3://localhost:7001

####### DataSources names ######
ds.Name.1 = MyJDBC_Data_Source-1
ds.Name.2 = MyJDBC_Data_Source-2

##### DataSources Target names #####
target.1= Cluster
target.2= Cluster-0

##### Total Numbers of DataSources has to be modified  #####
total.Ds.Count=2

Step2). Now in the same directory write the following WLST Script “Tragate_UnTragate_DS.py” like following:

#############################################################################
#
# @author Copyright (c) 2010 - 2011 by Middleware Magic, All Rights Reserved.
#
#############################################################################

from java.io import FileInputStream

propInputStream = FileInputStream("DsDetails.properties")
configProps = Properties()
configProps.load(propInputStream)

userName = configProps.get("userName")
password = configProps.get("password")
adminUrl = configProps.get("admin.Url")
totalDsCount = configProps.get("total.Ds.Count")

connect(userName,password,adminUrl)
edit()
startEdit()
print ''
print '======================================================================'
print 'UnTargeting and Targeting of the DataSources has started.....'
print '======================================================================'

dsCount=1
while (dsCount <= int(totalDsCount)) :
	dsName = configProps.get("ds.Name."+ str(dsCount))
	tgName = configProps.get("target."+ str(dsCount))
	cd ('/JDBCSystemResources/'+ dsName)
	set('Targets',jarray.array([], ObjectName))
	print ''
	print 'DataSource = ', dsName ,', has been UnTargeted'
	set('Targets',jarray.array([ObjectName('com.bea:Name='+tgName+',Type=Cluster')], ObjectName))
	print 'Congrats!!! DataSource = ', dsName ,', now has been Targeted to "',tgName,'"'
	print ''
	dsCount = dsCount + 1

print '======================================================================'
print 'UnTrageting and Targeting of the DataSources has been completed !!!'
print '======================================================================'
print ''

activate()
exit()

Step3). Now Open a Command/Shell Prompt and then run the “setWLSEnv.sh” script to set the CLASSPATH and PATH environment variables. Run the “. ./setWLSEnv.sh” by adding two DOTs separated by a single space …..before the actual script like following : (use ‘cd’ command to move inside the <BEA_HOME>/wlserver_10.3/server/bin) then run the following command….
. ./setWLSEnv.sh

Note: Here The first DOT represents that set the Environment in the current Shell, AND the second ./ represents execute the script from the current directory.

Step4). Run the Above WLST Script like following:

java weblogic.WLST Tragate_UnTragate_DS.py

Following would be the Output


java weblogic.WLST Tragate_UnTragate_DS.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'Domain_7001'.

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.

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.

======================================================================
UnTargeting and Targeting of the DataSources has started .....
======================================================================

DataSource =  MyJDBC_Data_Source-1 , has been UnTargeted
Congrats!!! DataSource =  MyJDBC_Data_Source-1 , now has been Targeted to " Cluster "

DataSource =  MyJDBC_Data_Source-2 , has been UnTargeted
Congrats!!! DataSource =  MyJDBC_Data_Source-2 , now has been Targeted to " Cluster-0 "

======================================================================
UnTargeting and Targeting of the DataSources has been completed !!!
======================================================================

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

Exiting WebLogic Scripting Tool.

Note:

Make sure that the Database is running while you are running the DataSource.

Regards,

Ravish Mody