Showing posts with label WLST. Show all posts
Showing posts with label WLST. Show all posts

Tuesday, August 23, 2011

Advanced Weblogic Scripting(WLST) with Record functionality


            Weblogic Console has been a really good tool for administrating all server related configurations. One of the innovative features I liked in weblogic console was the Record option. It’s an amazing utility that allows us to capture the steps involved in server configuration. The recording gets stored as a collection of WLST commands that got executed behind the scenes in a python script file.
The Recording option can be made automatic based on the Preferences. It starts recording when you obtain a lock on the domain configuration and stops recording when you activate your changes, undo your changes, release the lock, or lose the lock. This feature is not available when "Automatically Acquire Lock and Activate Changes" (under "User Preferences") is enabled. When this is disabled we can have the manual Record option.


Here I will show you the WLST script that gets generated when we create a datasource.
Recorded Script
cd('/')
cmo.createJDBCSystemResource('TestDS1')

cd('/JDBCSystemResources/TestDS1/JDBCResource/TestDS1')
cmo.setName('TestDS1')

cd('/JDBCSystemResources/TestDS1/JDBCResource/TestDS1/JDBCDataSourceParams/TestDS1')
set('JNDINames',jarray.array([String('jdbc/TestDS1')], String))

cd('/JDBCSystemResources/TestDS1/JDBCResource/TestDS1/JDBCDriverParams/TestDS1')
cmo.setUrl('jdbc:oracle:thin:@localhost:1521:XE')
cmo.setDriverName('oracle.jdbc.xa.client.OracleXADataSource')
setEncrypted('Password', 'Password_1314091630125', 'C:/ Oracle/Middleware/SOA11gPS3/user_projects/domains/soa_domain/Script1314091383906Config', 'C:/Oracle/Middleware/SOA11gPS3/user_projects/domains/soa_domain/Script1314091383906Secret')

cd('/JDBCSystemResources/TestDS1/JDBCResource/TestDS1/JDBCConnectionPoolParams/TestDS1')
cmo.setTestTableName('SQL SELECT 1 FROM DUAL\r\n\r\n')

cd('/JDBCSystemResources/TestDS1/JDBCResource/TestDS1/JDBCDriverParams/TestDS1/Properties/TestDS1')
cmo.createProperty('user')

cd('/JDBCSystemResources/TestDS1/JDBCResource/TestDS1/JDBCDriverParams/TestDS1/Properties/TestDS1/Properties/user')
cmo.setValue('DEV_TestDB')

cd('/JDBCSystemResources/TestDS1/JDBCResource/TestDS1/JDBCDataSourceParams/TestDS1')
cmo.setGlobalTransactionsProtocol('TwoPhaseCommit')

cd('/SystemResources/TestDS1')
set('Targets',jarray.array([], ObjectName))

activate()

Modified  Script - Parameterized

wlURL = raw_input('Enter server URL:')
wlUserName = raw_input('Enter weblogic username:')
wlPassword = raw_input('Enter weblogic password:')
dsName = raw_input('Enter JDBC resource name:')
jndiName = raw_input('Enter JDBC JNDI name:')
dbURL = raw_input('Enter JDBC DB url:')
dsUsername= raw_input('Enter DB username:')
dsPassword=raw_input('Enter DB password:')

connect(wlUserName, wlPassword, wlURL)
edit()
startEdit()

cd('/')
cmo.createJDBCSystemResource(dsName)

cd('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName)
cmo.setName(dsName)

cd('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName+'/JDBCDataSourceParams/'+dsName)
set('JNDINames',jarray.array([String(jndiName)], String))

cd('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName+'/JDBCDriverParams/'+dsName)
cmo.setUrl(dbURL)
cmo.setDriverName('oracle.jdbc.xa.client.OracleXADataSource')
cmo.setPassword(dsPassword)

cd('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName+'/JDBCConnectionPoolParams/'+dsName)
cmo.setTestTableName('SQL SELECT 1 FROM DUAL\r\n\r\n')

cd('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName+'/JDBCDriverParams/'+dsName+'/Properties/'+dsName)
cmo.createProperty('user')

cd('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName+'/JDBCDriverParams/'+dsName+'/Properties/'+dsName+'/Properties/user')
cmo.setValue(dsUsername)

cd('/JDBCSystemResources/'+dsName+'/JDBCResource/'+dsName+'/JDBCDataSourceParams/'+dsName)
cmo.setGlobalTransactionsProtocol('TwoPhaseCommit')

cd('/SystemResources/'+dsName)
set('Targets',jarray.array([], ObjectName))

activate()

How to run the script
  1. Go to the Domain bin folder and run setDomainEnv.sh script
  2. Run  >> java weblogic.WLST  /tmp/geo/CreateDataSourceScript.py

Execution StackTrace
>>>java weblogic.WLST D:\CreateDataSourceScript.py

Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Enter server URL:t3://localhost:7001
Enter weblogic username:weblogic
Enter weblogic password:welcome1
Enter JDBC resource name:TestDS4
Enter JDBC JNDI name:jdbc/TestDS4
Enter JDBC DB url:jdbc:oracle:thin:@localhost:1521:XE
Enter DB username:DEV_TestDB
Enter DB password:welcome1
Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'soa_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.

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)
You already have an edit session in progress and hence WLST will
continue with your edit session.

Starting an edit session ...
Started edit session, please be sure to save and activate your
changes once you are done.
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

That’s it for today. thanks to below references.
References:
  1. Advanced WebLogic Scripting with WLST: Recording, Editing and Reusing
  2. Using the Console to Create Repeatable Configuration Scripts

Wednesday, August 3, 2011

Executing WLST - offline and online



  I recently got a query on one of my blogs regarding the WLST commands related to SOA suite that can be run in different modes. –offline and online
1.    online (connected to a running Administration Server or Managed Server instance)
2.    offline (not connected to a running server).

 As per the WLST documentation for SOASuite customWLST commands , the  sca_deployComposite and sca_undeployComposite  can be executed offline. So what does this offline and online mean?

The offline and online are specific to WLST. When you use connect() command in WSLT you are connecting to the server and becoming online, but if you wish to remain offline you can use disconnect() and exit the online session. The online and offline should not be implied as the running status of the server. If the server is shutdown these commands will fail as it requires the hostname and port and connects to it. These commands will work even if the soa-infra is shutdown.

The commands work properly when WLST is online. For testing the WLST command offline, I brought down the server and then executing it offline.
 But since the commands require serverURL and it does validate the connection to host and port.

I will show the difference between offline and online mode with sca_undeploy command.

OFFLINE

When you try to do cd('Servers/ soa_server1’) it will give below error
Error: No domain or domain template has been read.

So you will have to give the entire path of the domain

wls:/offline>readDomain('/geo/oracle/fusion/middleware/user_projects/domains/soa_domain’)
wls:/offline>cd('Servers/ AdminServer’)
wls:/offline/soa_domain/Server/AdminServer’>sca_undeployComposite("http://localhost:8001","GeoComposite", "1.0","weblogic","weblogic123",partition=’default’)
serverURL = http://localhost:7001
user = weblogic
partition = default
compositeName = GeoComposite
revision = 1.0  
timeout= -1
set user and password...
compositeDN = default/GeoComposite!1.0
Creating HTTP connection to host:localhost, port:7001
Received HTTP response from the server, response code=200
---->Undeploying composite (default/ GeoComposite!1.0) success.


ONLINE


wls:/offline> connect()
Please enter your username [weblogic] :weblogic
Please enter your password [welcome1] :
Please enter your server URL [t3://localhost:7001] : t3://localhost:7001
Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server 'AdminServer' that belongs to domain 'soa_domain'.

wls:/soa_domain/serverConfig> sca_undeployComposite("http://localhost:8001","GeoComposite", "1.0","weblogic","weblogic123")
serverURL = http://localhost:8001
user = weblogic
partition = default
compositeName = GeoComposite
revision = 1.0
timeout= -1
set user and password...
compositeDN = default/GeoComposite!1.0
Creating HTTP connection to host:localhost, port:8001
Received HTTP response from the server, response code=200
---->Undeploying composite (default/GeoComposite!1.0) success.


Hope this clarifies.

Thursday, June 23, 2011

How to remove unused files/artifacts from MDS?

          In development phase of project, we will be adding different artifacts to the MDS so that can be accessed by different processes. In course of development /design changes, applying best practices, there is bound to be lot of unused folders /artifacts lying around in the MDS store. So it is always advised to remove the unwanted artifacts, test the processes before we export them to other environments. I always used a workaround to remove the files, even though it was not the best approach.

Workaround
 There is a table called MDS_PATHS where there would be entries corresponding to each artifact. I used to delete the entries from the table and the artifacts never used to show up, even though there will be entries in other related tables.

BestApproach
The best approach/solution is to use WLST command for cleaning up the MDS store. There is a function called deleteMetadata in WLST which will do the job for you.
This is how Oracle documentation describes the command. It deletes the selected documents from the application repository. When this command is run against repositories that support versioning, that is a database-based repository, delete is logical and marks the tip version (the latest version) of the selected documents as "deleted" in the MDS repository partition.
For more attributes and options available with the command refer this doc

The WLST script is located at:

(UNIX) MIDDLEWARE_HOME/ORACLE_SOA1/common/bin/wlst.sh
(Windows) MIDDLEWARE_HOME\Oracle_SOA1\common\bin\wlst.cmd

Once the scripting tool is initialized, Connect to the server

offline>connect(‘username,’pwd’, ‘hostname:7001’)

For running deleteMetada function you need a minimum of 3 inputs
-          application  - since we are deleting from shared artifacts of soa-infra, the value should be soa-infra
-          server -  value should be ‘soa_server1’ or the server u use for SOA other than admin server.
-          docs – the folder path or artifact which you want to delete.

wls:/GEO_domain/serverConfig> deleteMetadata(application='soa-infra',server='soa
_server1',docs='/apps/dvm/oracle/dvm/*')

Executing operation: deleteMetadata.

Operation "deleteMetadata" completed. Summary of "deleteMetadata" operation is:

List of documents successfully deleted:
/apps/dvm/oracle/dvm/GeoXRef.dvm

1 documents successfully deleted.


This will be really useful in all projects as cleanup in mandatory in the MDS. Thanks to jithendra and Ram for their inputs.