Upload
prathap-kumar
View
32
Download
0
Embed Size (px)
Citation preview
Weblogic- WLST-SCRIPTING
What is WLST ?
WebLogic Scripting tool. This was introduced in weblogic 9.2 . Prior to this weblogic.Admin java utility was used extensively for command line administration of
weblogic.
WLST uses Jython as scripting language. Jython is pure java based implementation of Python.
Invoking the WLST script:
Java weblogic.WLST Or
$WL_HOME/common/bin/wlst.sh
This script will internally invoke the previous command java weblogic.WLST. NOTE: make sure you are sourcing the environment before executing the WLST.
# . $DOMAIN_HOME/bin/setDomainEnv.sh # ( dot space script location)
How WLST works ?
It works through JMX ( Java Management Extension). The API to managed the Mbeans ( Management Beans)
Two modes of WLST
Online : this is when connected to a running admin/managed server
Used to browse through the runtime Mbeans of the weblogic Can change security configuration of the domain
Query the runtime mbeans
Offline : When not connected to running weblogic.
Creation of domain and changing the configuration
Weblogic- WLST-SCRIPTING
Can not change security configuration in this mode…
Template based domain creation.
WLST Basics:
Total Domain is divided into various Tree line verticals.
domainConfig() serverConfig() domainRuntime() serverRuntime() Edit()
Online Online Online Online Online
Similar to the configuration information, WebLogic Server runtime MBeans are arranged in a hierarchical data structure.
When connected to an Administration Server, you access the runtime MBean hierarchy by entering the serverRuntime or the domainRuntime command.
The serverRuntime command places WLST at the root of the server runtime
management objects, ServerRuntimeMBean; the domainRuntime command, at the root
of the domain-wide runtime management objects, DomainRuntimeMBean.
When connected to a Managed Server, the root of the runtime MBeans is ServerRuntimeMBean.
The domain runtime MBean hierarchy exists on the Administration Server only; you cannot use the domainRuntime command when connected to a Managed Server.
serverRuntime() & domainRuntime() are read-only trees. These two trees are run-time
related trees.
With in each tree you can browser through
Domain Mbean – (root)
| ---- Mbean Type ( ServerMBean)
|-- Mbean Instance ( wl_mng1)
|-- Mbean attributes & Operations
( port no, listen address, start/stop)
Weblogic- WLST-SCRIPTING |-- Mbean Instance
( wl_mng2)
|--Mbean attributes & Operations
Types of using WLST :
1) Interactive ( browsing through the Mbean structure)
cd()
ls() help(‘command-name’)
cd(configuration-tree-name)
ls()
directories are the Mbeans ( start with dr)
-r— files are attributes –r—x files are operations attributes can be changed using set & get commands.
2) Invoking a script from command line. The script should have an extension .py to be able to invoke from command line.
$ Java weblogic.WLST –skipWLSModuleScanning ( run
$DOMAIN_HOME/bin/setDomainEnv.sh before invoking WLST to setup the environment)
Invoking WLST can be invoked in two ways. 1) $WL_HOME/common/bin/Wlst.sh
connect()
java weblogic.WLST -skipWLSModuleScanning wlst-application-status.py
localhost:10001 weblogic weblogic
Weblogic- WLST-SCRIPTING
Redirection of output :
redirect(outputFile,[toStdOut])
stopRedirect()
CMO. – current management object
After cd(), u can get the properties by calling cmo.operation
creating a domain template
readDomain('/home/durgasoft/domains/DurgaSoft_Dmn')
writeTemplate('/home/durgasoft/mytemplate.jar') closeDomain()
Weblogic- WLST-SCRIPTING createDomain('/home/durgasoft/mytemplate.jar','/home/durgasoft/domains/new-
domain','wlsadmin','wlsadmin')
otherway is to use this template with config.sh script…2nd step
alternative way is
configToScript(‘domain-directory’,’location- for-scripts’)
dumpVariables()
dumpStack()
after ls() there will be both properties & Operations
Properties will not have x ( -r---). where as operations will have –r-x To acquire a lock and edit the configuration of the domain
edit() startEdit()
save() activate(block=”true”)
import X imports the module X, and creates a reference to that module in the current namespace. Or in other words, after you’ve run this statement, you can use X.name to refer to things defined in module X.
from X import * imports the module X, and creates references in the current namespace to all public objects defined by that module (that is, everything that doesn’t have a name starting with “_”). Or in other words, after you’ve run this statement, you can simply use a plain name to refer to things defined in module X. But X itself is not defined, so X.name doesn’t work. And if name was already defined, it is replaced by the new version. And if name in X is changed to point to some other object, your module won’t notice.
from X import a, b, c imports the module X, and creates references in the current namespace to the given objects. Or in other words, you can now usea and b and c in your program.
Weblogic- WLST-SCRIPTING
Finally, X = __import__(‘X’) works like import X, with the difference that you 1) pass the module name as a string, and 2) explicitly assign it to a variable in your current namespac
Prompt()
sys.ps1="myprompt>"
cmo- is current management object. You can use get or set methods to retrive/assign properties to the current management object using this.
scripts
1) Script to Check the Status of All the deployed Applications
Wlst-application-status.py
How to Invoke : java weblogic.WLST –skipWLSModuleScanning wlst-
application-status.py
#This wlst script give the status of the deployed applications import os
import sys
username='weblogic' password='weblogic'
url='t3://localhost:7001'
connect(username, password, url)
print ' Application Deployed '
cd("AppDeployments")
app = ls(returnMap='true')
domainRuntime()
cd("AppRuntimeStateRuntime/AppRuntimeStateRuntime")
i=1
for appName in app:
appState = cmo.getIntendedState(appName, "mycluster")
print appName + '' + '==>' + appState
i=i+1
Weblogic- WLST-SCRIPTING
2) Script for resetting administrator’s password for a weblogic domain
wlst-admin-password-change.py
How to Invoke : java weblogic.WLST –skipWLSModuleScanning wlst-admin-password-change.py
DomainName = "mydomain"
ADMINUrl = "t3://localhost:7001"
ADMINuser = "weblogic"
oldPassword = "weblogic"
newPassword = "weblogicadmin"
print ' ---- Changing the Administration Password ------- '
print ' '
connect(ADMINuser,oldPassword,ADMINUrl)
cd('/SecurityConfiguration/'+DomainName+'/Realms/myrealm/AuthenticationProviders
/DefaultAuthenticator')
cmo.resetUserPassword(ADMINuser,newPassword)
print ' ---- Weblogic Admin Password Reset Completed Successfully --- '
print ' '
disconnect()
print ' '
print ' --- Testing with the new Credentials --- '
print ' '
connect(ADMINuser,newPassword,ADMINUrl)
print '--- Testing with the new Credentials Completed Successfully ---'
Weblogic- WLST-SCRIPTING
print ' '
disconnect()
3) Cluster Creation ( cluster_creation.py)
How to Invoke : java weblogic.WLST –skipWLSModuleScanning
cluster_creation.py
DurgaSoft# cat cluster_creation.py
# This example script:
# * connects WLST to the Admin Examples Server
# * starts an edit session
# * creates 10 managed servers.
# * It creates 2 clusters and assigns the servers to the respective clusters.
from java.util import *
from javax.management import *
import javax.management.Attribute
print 'starting the script ....'
username = 'weblogic'
password = 'weblogic'
clusters = 'cluster1','cluster2'
ms1 = {'managed1':7701,'managed2':7702,'managed3':7703, 'managed4':7704,
'managed5':7705}
ms2 = {'managed6':7706,'managed7':7707,'managed8':7708, 'managed9':7709, 'managed10':7710}
Weblogic- WLST-SCRIPTING
connect(username,password,'t3://localhost:7001')
clustHM = HashMap()
edit()
startEdit()
for c in clusters:
print 'creating cluster '+c
clu = create(c,'Cluster')
clustHM.put(c,clu)
cd('..\..')
clus1 = clustHM.get(clusters[0])
clus2 = clustHM.get(clusters[1])
for m, lp in ms1.items():
managedServer = create(m,'Server')
print 'creating managed server '+m
managedServer.setListenPort(lp)
managedServer.setCluster(clus1)
for m1, lp1 in ms2.items():
managedServer = create(m1,'Server')
Weblogic- WLST-SCRIPTING print 'creating managed server '+m1
managedServer.setListenPort(lp1)
managedServer.setCluster(clus2)
save()
activate(block="true")
disconnect()
4) Cluster Deletion Script ( cluster_deletion.py)
How to invoke it : java weblogic.WLST –skipWLSModuleScanning cluster_deletion.py
DurgaSoft# cat cluster_deletion.py
# This example script corresponds to the cluster_creation.py script:
# * connects WLST to the Admin Examples Server
# * starts an edit session
# * For each cluster created in cluster_creation.py, it deletes the
# * managed servers and the migratabletargets.
# * Lastly, it deletes the clusters.
print 'starting the script ....'
username = 'weblogic'
password = 'weblogic'
clusters = 'cluster1','cluster2'
ms1 = {'managed1':7701,'managed2':7702,'managed3':7703, 'managed4':7704,
'managed5':7705}
ms2 = {'managed6':7706,'managed7':7707,'managed8':7708, 'managed9':7709,
Weblogic- WLST-SCRIPTING 'managed10':7710}
connect(username,password,'t3://localhost:7001')
edit()
startEdit()
s = " (migratable)"
for m, lp in ms1.items():
print 'deleting managed server '+m
serverMBean = getMBean("Servers/"+m)
serverMBean.setCluster(None)
managedServer = delete(m,'Server')
for m1, lp1 in ms2.items():
print 'deleting managed server '+m1
serverMBean = getMBean("Servers/"+m1)
serverMBean.setCluster(None)
managedServer = delete(m1,'Server')
for c in clusters:
print 'deleting cluster '+c
clu = delete(c,'Cluster')
Weblogic- WLST-SCRIPTING save()
activate(block="true")
print 'Saved Changes ...'
print 'End of script ...'
disconnect()
5) Script to create JMS Resources ( configJMSSystemResource.py )
How to Invoke the script :
# java weblogic.WLST –skipWLSModuleScanning configJMSSystemResource.py
DurgaSoft# cat configJMSSystemResource.py
"""
This script starts an edit session, creates two different JMS Servers,
targets the jms servers to the server WLST is connected to and creates
jms topics, jms queues and jms templates in a JMS System module. The
jms queues and topics are targeted using sub-deployments.
"""
import sys
from java.lang import System
print "Starting the script ..."
Weblogic- WLST-SCRIPTING connect('weblogic','weblogic','t3://localhost:7001')
edit()
startEdit()
servermb=getMBean("Servers/examplesServer")
if servermb is None:
print 'Value is Null'
else:
jmsserver1mb = create('MyJMSServer1','JMSServer')
jmsserver1mb.addTarget(servermb)
jmsserver2mb = create('MyJMSServer2','JMSServer')
jmsserver2mb.addTarget(servermb)
jmsMySystemResource = create("myJmsSystemResource","JMSSystemResource")
jmsMySystemResource.addTarget(servermb)
subDep1mb = jmsMySystemResource.createSubDeployment('DeployToJMSServer1')
subDep1mb.addTarget(jmsserver1mb)
subDep2mb = jmsMySystemResource.createSubDeployment('DeployToJMSServer2')
subDep2mb.addTarget(jmsserver2mb)
theJMSResource = jmsMySystemResource.getJMSResource()
connfact1 = theJMSResource.createConnectionFactory('MyCF1')
Weblogic- WLST-SCRIPTING connfact1.setJNDIName('jms.MyCF1')
connfact1.setSubDeploymentName('DeployToJMSServer1')
connfact2 = theJMSResource.createConnectionFactory('MyCF2')
connfact2.setJNDIName('jms.MyCF2')
connfact2.setSubDeploymentName('DeployToJMSServer2')
print "Creating MyQueue1..."
jmsqueue1 = theJMSResource.createQueue('MyQueue1')
jmsqueue1.setJNDIName('jms.MyJMSQueue1')
jmsqueue1.setSubDeploymentName('DeployToJMSServer1')
print "Creating MyQueue2..."
jmsqueue2 = theJMSResource.createQueue('MyQueue2')
jmsqueue2.setJNDIName('jms.MyJMSQueue2')
jmsqueue2.setSubDeploymentName('DeployToJMSServer2')
print "Creating MyTopic1..."
jmstopic1 = theJMSResource.createTopic("MyTopic1")
jmstopic1.setJNDIName('jms.MyJMSTopic1')
jmstopic1.setSubDeploymentName('DeployToJMSServer1')
print "Creating MyTopic2..."
jmstopic2 = theJMSResource.createTopic("MyTopic2")
jmstopic2.setJNDIName('jms.MyJMSTopic2')
jmstopic2.setSubDeploymentName('DeployToJMSServer2')
Weblogic- WLST-SCRIPTING
print "Creating MyJMSTemplate..."
jmstemplate = theJMSResource.createTemplate('MyJMSTemplate')
jmstemplate.setMaximumMessageSize(20)
try:
save()
activate(block="true")
print "script returns SUCCESS"
except:
print "Error while trying to save and/or activate!!!"
dumpStack()
6) Deleting the JMS Resources (deleteJMSSystemResource.py)
How to Invoke the Script:
#java weblogic.WLST –skipWLSModuleScanning deleteJMSSystemResource.py
DurgaSoft# cat deleteJMSSystemResource.py
# This example script connects WLST to the Admin Examples Server
# * starts an edit session
# * removes a JMS system resource module.
import sys
from java.lang import System
Weblogic- WLST-SCRIPTING print 'starting the script ....'
connect('weblogic','weblogic','t3://localhost:7001')
edit()
startEdit()
jmsMySystemResource = delete("myJmsSystemResource","JMSSystemResource")
jmsMyServer1 = delete("MyJMSServer1","JMSServer")
jmsMyServer2 = delete("MyJMSServer2","JMSServer")
save()
activate(block="true")
print 'System Resource removed ...'
disconnect()
7) Script to create JDBC resources (jdbc_data_source_creation.py)
How to Invoke :
# java weblogic.WLST –skipWLSModuleScanning jdbc_data_source_creation.py
DurgaSoft# cat jdbc_data_source_creation.py
"""
This script configures a JDBC data source as a System Module and deploys it
to the server
"""
connect("weblogic","weblogic")
edit()
Weblogic- WLST-SCRIPTING # Change these names as necessary
dsname="myJDBCDataSource"
server="examplesServer"
cd("Servers/"+server)
target=cmo
cd("../..")
startEdit()
# start creation
print 'Creating JDBCSystemResource with name '+dsname
jdbcSR = create(dsname,"JDBCSystemResource")
theJDBCResource = jdbcSR.getJDBCResource()
theJDBCResource.setName("myJDBCDataSource")
connectionPoolParams = theJDBCResource.getJDBCConnectionPoolParams()
connectionPoolParams.setConnectionReserveTimeoutSeconds(25)
connectionPoolParams.setMaxCapacity(100)
connectionPoolParams.setTestTableName("SYSTABLES")
dsParams = theJDBCResource.getJDBCDataSourceParams()
dsParams.addJNDIName("ds.myJDBCDataSource")
driverParams = theJDBCResource.getJDBCDriverParams()
driverParams.setUrl("jdbc:pointbase:server://localhost/demo")
driverParams.setDriverName("com.pointbase.xa.xaDataSource")
# driverParams.setUrl("jdbc:oracle:thin:@my-oracle-server:my-oracle-server-port:my-
Weblogic- WLST-SCRIPTING oracle-sid")
# driverParams.setDriverName("oracle.jdbc.driver.OracleDriver")
driverParams.setPassword("examples")
# driverParams.setLoginDelaySeconds(60)
driverProperties = driverParams.getProperties()
proper = driverProperties.createProperty("user")
#proper.setName("user")
proper.setValue("examples")
proper1 = driverProperties.createProperty("DatabaseName")
#proper1.setName("DatabaseName")
proper1.setValue("jdbc:pointbase:server://localhost/demo")
jdbcSR.addTarget(target)
save()
activate(block="true")
print 'Done configuring the data source'
8) deletion of datasources (jdbc_data_source_deletion.py)
How to Invoke It:
# java weblogic.WLST –skipWLSModuleScanning
jdbc_data_source_deletion.py
Weblogic- WLST-SCRIPTING
DurgaSoft# cat jdbc_data_source_deletion.py print 'starting the script ....'
username = 'weblogic' password = 'weblogic'
connect(username,password,'t3://localhost:7001')
edit() startEdit()
delete('myJDBCDataSource','JDBCSystemResource')
save() activate(block="true")
Finding MBeans and Attributes
To locate a particular MBean and attribute, you use the find command. WLST returns
the pathname to the MBean that stores the attribute and its value. You can use the
getMBean command to return the MBean specified by the path. For more information,
see find and getMBean.
For example:
wls:/mydomain/edit !> find('logfilename')
searching ...
/ApplicationRuntimes/myserver_wlnav.war/WebAppComponentRuntime/myserver
_myserver_wlnav.war_wlnav_/wlnavLogFilename null
/Servers/myserver JDBCLogFileName jdbc.log
/Servers/myserver/WebServer/myserver LogFileName access.log
wls:/mydomain/edit !>
bean=getMBean('Servers/myserver/WebServer/myserver')
wls:/mydomain/edit !> print bean
[MBeanServerInvocationHandler]mydomain:Name=myserver,Type=WebServer,Ser
ver=myserver
wls:/mydomain/edit !>
Weblogic- WLST-SCRIPTING
Note: getMBean does not throw an exception when an instance is not found.
Alternatively, the getPath command returns the MBean path for a specified MBean
instance or ObjectName for the MBean in the current MBean hierarchy. See getPath.
wls:/mydomain/serverConfig>path=getPath('com.bea:Name=myserver,Type=Ser
ver')
wls:/mydomain/serverConfig> print path
Servers/myserver