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:WLSTDataSource“
Step2). Write a Properties file “DsDetails.properties“ inside “C:WLSTDataSource” 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.
Ravish Mody
October 8th, 2011 on 4:32 pm
Hi,
I do not know the exact place to this question!
is it possible to store Datasource(username,password,databaselink,…etc) in ldap?
we can define jndi in properties and define jdbc weblogic console,so if we want to change password or any properties, have to do lots of changes and to do redeploy all applications.
These properties have to take the ldap,
I will be only one place to take changes.
I think just enough to change the ldap.
is it possible?
thanks…
October 8th, 2011 on 9:30 pm
Hi Erdem_ustun,
I don’t see any such option to store the DataSource credentials inside an external LDAP. The datasouce details like URL,username & passwords are placed inside the datasource configuration and their authentication is done by the Database itself….It means weblogic itself does not authenticates these credentials, Because it’s the responsibility of the Database to authenticate the username/passwords.
But if you want to minimize the job of changing the username / password and URL of the database then you can take advantage of the WLST like following:
http://middlewaremagic.com/weblogic/?p=4956
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
October 9th, 2011 on 3:40 pm
thanks for reply.
I have not seen this link( http://middlewaremagic.com/weblogic/?p=4956).
I will try it.I guess this solves my issue.
can be done in the same way
create multiple datasource
deploy/redoploy applications
Is there another code I did not know these issues?
best regards..
October 11th, 2011 on 11:04 pm
Hi Erdem_Ustun,
There are various ways present to deploy/redeploy/undeploy application with less effort like:
Your script is very complicated…WebLogic provides various simple ways to do the deployment task:
1). http://middlewaremagic.com/weblogic/?p=303
WLST as you can perform recursive deployments in less number of lines.
2). http://middlewaremagic.com/weblogic/?p=373
ANT Scripts will be the second alternative..as for plain developers as well it is very easy to write and maintain.
3). http://middlewaremagic.com/weblogic/?p=394
weblogic.Deployer is a utility which usually an Administrator Likes more …as it is very easy for an administrator to maintain and managed. because it does not require any Scripting knowledge. and it can be easily associated with a Shell Script.
4). http://middlewaremagic.com/weblogic/?p=483
If you are Java friendly then you would like the JMX way to deploy your applications….
Similarly if you want to create various DataSources at on go then you can use WLST
http://middlewaremagic.com/weblogic/?p=6248 Creating Multiple DataSource Using WLST with Properties File
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
October 13th, 2011 on 9:23 pm
thanks for links…
April 19th, 2012 on 11:04 pm
When i am using this script to target multiple datasources to multiple servers its failing.
I have 6 datasources which i need to target and untarget to 4 individual servers.
Appreciate quick response.
April 25th, 2012 on 9:37 pm
I am able to do it now. Thanks
December 13th, 2017 on 1:37 am
Hi Jay,
Thanks for such wonderful scripts.
I have a small question regarding this targeting and untargeting script. I am trying to create a script where we should be able to target multiple managed servers to one datasource. Can you please help me on this ?
Thanks,
Sharath