WLST – Modular Scripts to create/destroy WLS resources

This post shows how to create modular WLST scripts to create weblogic resources.

The principal followed was to keep the actual resource names and attribute values for the resources separated from the jython script used to create those resources. Each resource type had an associated properties file where the names and attribute values are defined and these are loaded by the script.

Also the script checks for the presence of an existing resource type of the same name before creating a new resource. So this prevents the master build script which invokes these scripts to not to fail if some resources are already present. Also this allows of incrementally creating new resources e.g. to create a new data source , just add the new data source details to the properties file and rerun the create DataSource script. This leaves the other existing datasources as is and just creates the new datasource.

Create DataSource

# ##########################################################################
# Creates Data Sources
# Usage : CreateDataSources.py <Name of Properties file >
# #########################################################################
import sys
#read properties file

if len(sys.argv) != 2:
	print "Invalid Arguements :: Usage CreateDataSources.py <DataSource Properties file>"
	exit()
try:
	print "Load properties file"
	properties=sys.argv[1]
	file=open(properties,'r')
	print "Read properties file"
	exec file
	print "Execute properties file"
	file.close
except:
	exit()
#Connect

connect(USER,PASSWORD,ADMIN_URL)
edit()
dsCount=0
successCount=0
for dataSource in DATASOURCE_ARRAY:
	startEdit()
	jdbcSystemResource=getMBean("/JDBCSystemResources/"+dataSource['NAME'])
	if jdbcSystemResource is None:
		jdbcSystemResource=create(dataSource['NAME'],"JDBCSystemResource")
	
		if (dataSource['TARGET_TYPE']=="SERVER"):
			targetMB=getMBean("/Servers/"+dataSource['TARGET'])
		if (dataSource['TARGET_TYPE']=="CLUSTER"):
			targetMB=getMBean("/Clusters/"+dataSource['TARGET'])

		if targetMB is None:
			print "@@@ Invalid DataSource Target '"+dataSource['TARGET']+"'"
			exit()
	
		jdbcSystemResource.addTarget(targetMB)
		jdbcResource=jdbcSystemResource.getJDBCResource()
		jdbcResource.setName(dataSource['NAME'])
		jdbcResource.JDBCDataSourceParams.setJNDINames(jarray.array([String(dataSource['JNDI_NAME'])], String))
		jdbcResource.JDBCDriverParams.setUrl(dataSource['URL'])
		jdbcResource.JDBCDriverParams.setDriverName(dataSource['DRIVER_NAME'])
		property=jdbcResource.JDBCDriverParams.getProperties().createProperty("USER")
		property.setValue(dataSource['USER'])
		jdbcResource.JDBCDriverParams.setPassword(dataSource['PASSWORD'])
		jdbcResource.JDBCConnectionPoolParams.setTestConnectionsOnReserve(true)
		jdbcResource.JDBCConnectionPoolParams.setTestTableName("SQL SELECT 1 FROM DUAL\r\n\r\n")
		
	else:
		if (dataSource['TARGET_TYPE']=="SERVER"):
			targetMB=getMBean("/Servers/"+dataSource['TARGET'])
		if (dataSource['TARGET_TYPE']=="CLUSTER"):
			targetMB=getMBean("/Clusters/"+dataSource['TARGET'])
		if targetMB is None:
			print "@@@ Invalid DataSource Target '"+dataSource['TARGET']+"'"
			exit()	
		if getMBean("/JDBCSystemResources/"+dataSource['NAME']+"/Targets/"+dataSource['TARGET']) is None:	
			jdbcSystemResource.addTarget(targetMB)
	try:
		activate()
		successCount=successCount+1
	except:			
		undo(defaultAnswer='y')
	dsCount=dsCount+1		
		
print str(successCount)+" DataSources out of "+str(dsCount)+"had successful connection during testing"
		
exit(defaultAnswer='y')

Create Foreign JNDI Providers

# ##########################################################################
# Creates Foreign JNDI Links
# Usage : CreateForeignJNDIProviders.py <Name of Properties file >
# #########################################################################
import sys
#read properties file

if len(sys.argv) != 2:
	print "Invalid Arguements :: Usage ForeignJNDIProviders.py <ForeignJNDI Properties file>"
	exit()
try:
	print "Load properties file"
	properties=sys.argv[1]
	file=open(properties,'r')
	print "Read properties file"
	exec file
	print "Execute properties file"
	file.close
except:
	exit()
#Connect

connect(USER,PASSWORD,ADMIN_URL)
edit()
startEdit()

for foreignJNDI in FOREIGN_JNDI_ARRAY:
	if (foreignJNDI['TARGET_TYPE']=="SERVER"):
		targetMB=getMBean("/Servers/"+foreignJNDI['TARGET'])
	if (foreignJNDI['TARGET_TYPE']=="CLUSTER"):
		targetMB=getMBean("/Clusters/"+foreignJNDI['TARGET'])
	if targetMB is None:
		print "@@@ Invalid Foreign JNDI Provider Target '"+foreignJNDI['TARGET']+"'"
		exit()
	if getMBean("/ForeignJNDIProviders/"+foreignJNDI['NAME']) is None:		
		foreignJNDIInstance=create(foreignJNDI['NAME'],"ForeignJNDIProvider")
		foreignJNDIInstance.addTarget(targetMB)
		foreignJNDIInstance.setInitialContextFactory(foreignJNDI['INITIAL_CONTEXT_FACTORY'])
		foreignJNDIInstance.setProviderURL(foreignJNDI['PROVIDER_URL'])
		foreignJNDIInstance.setUser(foreignJNDI['USER'])
		foreignJNDIInstance.setPassword(foreignJNDI['PASSWORD'])
	
		for foreignLink in foreignJNDI['LINKS_ARRAY']:
			foreignLinkInstance=foreignJNDIInstance.createForeignJNDILink(foreignLink['NAME'])
			foreignLinkInstance.setLocalJNDIName(foreignLink['LOCAL_JNDI'])
			foreignLinkInstance.setRemoteJNDIName(foreignLink['REMOTE_JNDI'])
	
		
activate()
exit()

Create JMS Modules including foreign JMS Servers

# ##########################################################################
# Creates a JMS Module and associated objects
# Usage : CreateJMSModule.py <Name of Properties file >
# #########################################################################
import sys
#read properties file

if len(sys.argv) != 2:
	print "Invalid Arguements :: Usage CreateJMSModule.py <JMS Module Properties file>"
	exit()
try:
	print "Load properties file"
	jms_properties=sys.argv[1]
	file=open(jms_properties,'r')
	print "Read properties file"
	exec file
	print "Execute properties file"
	file.close
except:
	exit()
#Connect

connect(USER,PASSWORD,ADMIN_URL)
edit()
startEdit()

if (JMS_MODULE_TARGET_TYPE=="SERVER"):
	defTargetMB=getMBean("/Servers/"+JMS_MODULE_TARGET)
if (JMS_MODULE_TARGET_TYPE=="CLUSTER"):
	defTargetMB=getMBean("/Clusters/"+JMS_MODULE_TARGET)
if (JMS_MODULE_TARGET_TYPE=="JMS"):
	defTargetMB=getMBean("/JMSServers/"+JMS_MODULE_TARGET)

if defTargetMB is None:
	print "@@@ Invalid JMS Module Target '"+JMS_MODULE_TARGET+"'"
	exit()

# Create Module
if getMBean("/JMSSystemResources/"+JMS_MODULE_NAME) is None:
	jmsModule = create(JMS_MODULE_NAME, "JMSSystemResource")
	jmsModule.addTarget(defTargetMB)
else:
	jmsModule=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME)
jmsResource = jmsModule.getJMSResource()

# Create JMS sub deployment 

for subDeployment in JMS_SUB_DEPLOYMENT_ARRAY :
	jmsSubDeployment=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/SubDeployments/"+subDeployment["JMS_SUB_DEPLOYMENT_NAME"])
	if jmsSubDeployment is None:
		jmsSubDeployment=jmsModule.createSubDeployment(subDeployment["JMS_SUB_DEPLOYMENT_NAME"])
	
		if (subDeployment['JMS_SUB_DEPLOYMENT_TARGET_TYPE']=="SERVER"):
			sdTargetMB=getMBean("/Servers/"+subDeployment['JMS_SUB_DEPLOYMENT_TARGET'])
		if (subDeployment['JMS_SUB_DEPLOYMENT_TARGET_TYPE']=="CLUSTER"):
			sdTargetMB=getMBean("/Clusters/"+subDeployment['JMS_SUB_DEPLOYMENT_TARGET'])
		if (subDeployment['JMS_SUB_DEPLOYMENT_TARGET_TYPE']=="JMS"):
			sdTargetMB=getMBean("/JMSServers/"+subDeployment['JMS_SUB_DEPLOYMENT_TARGET'])	 
		if sdTargetMB is None:
			print "@@@ Invalid JMS Sub Deployment Target '"+subDeployment['JMS_SUB_DEPLOYMENT_TARGET']+"'"
			exit()
		jmsSubDeployment.addTarget(sdTargetMB)
	
# Create & Target CF
	for name,jndiName in subDeployment['XA_CONNECTION_FACTORY_ARRAY'].items():
		newConnectionFactory=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ConnectionFactories/"+name)
		if newConnectionFactory is None:
			newConnectionFactory = jmsResource.createConnectionFactory(name)
		newConnectionFactory.setJNDIName(jndiName)
		newConnectionFactory.setSubDeploymentName(subDeployment['JMS_SUB_DEPLOYMENT_NAME'])
		newConnectionFactory.transactionParams.setXAConnectionFactoryEnabled(true)

	for name,jndiName in subDeployment['CONNECTION_FACTORY_ARRAY'].items():
		newConnectionFactory=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ConnectionFactories/"+name)
		if newConnectionFactory  is None:
			newConnectionFactory = jmsResource.createConnectionFactory(name)
		newConnectionFactory.setJNDIName(jndiName)
		newConnectionFactory.setSubDeploymentName(subDeployment['JMS_SUB_DEPLOYMENT_NAME'])
		newConnectionFactory.transactionParams.setXAConnectionFactoryEnabled(false)
		
# Create & Target Queues
	for name,jndiName in subDeployment['QUEUE_ARRAY'].items():
		newQueue=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/Queues/"+name)
		if newQueue is None:
			newQueue = jmsResource.createQueue(name)
		newQueue.setJNDIName(jndiName)
		newQueue.setSubDeploymentName(subDeployment['JMS_SUB_DEPLOYMENT_NAME'])

# Create & Target Topics
	for name,jndiName in subDeployment['TOPIC_ARRAY'].items():
		newTopic=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/Topics/"+name)
		if newTopic is None:
			newTopic = jmsResource.createTopic(name)
		newTopic.setJNDIName(jndiName)
		newTopic.setSubDeploymentName(subDeployment['JMS_SUB_DEPLOYMENT_NAME'])

# Create & Target ForeignJMS
	for foreignServer in subDeployment['FOREIGN_JMS_SERVER_ARRAY']:
		foreignJMS=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ForeignServers/"+foreignServer['FOREIGN_JMS_SERVER_NAME'])
		if foreignJMS is None:
			foreignJMS =jmsResource.createForeignServer(foreignServer['FOREIGN_JMS_SERVER_NAME'])
		foreignJMS.setSubDeploymentName(subDeployment['JMS_SUB_DEPLOYMENT_NAME'])
		foreignJMS.setInitialContextFactory(foreignServer['FOREIGN_JMS_SERVER_INITIAL_CONTEXT_FACTORY'])
		foreignJMS.setConnectionURL(foreignServer['FOREIGN_JMS_SERVER_PROVIDER_URL'])  
		foreignJMS.unSet('JNDIPropertiesCredentialEncrypted')
		jndiProperty=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ForeignServers/"+foreignServer['FOREIGN_JMS_SERVER_NAME']+"/JNDIProperties/"+foreignServer['FOREIGN_JMS_SERVER_JNDI_PROPERTY_KEY'])
		if jndiProperty is None:
			jndiProperty=foreignJMS.createJNDIProperty(foreignServer['FOREIGN_JMS_SERVER_JNDI_PROPERTY_KEY'])
		jndiProperty.setValue(foreignServer['FOREIGN_JMS_SERVER_JNDI_PROPERTY_VALUE'])		
	# Create ForeignJMS Connection Factory
		for foreignCF in foreignServer['FOREIGN_JMS_CF_ARRAY']:
			foreignJMSConnectionFactory=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ForeignServers/"+foreignServer['FOREIGN_JMS_SERVER_NAME']+"/ForeignConnectionFactories/"+foreignCF['NAME'])
			if foreignJMSConnectionFactory is None:
				foreignJMSConnectionFactory = foreignJMS.createForeignConnectionFactory(foreignCF['NAME']);
			foreignJMSConnectionFactory.setLocalJNDIName(foreignCF['LOCAL_JNDI'])
			foreignJMSConnectionFactory.setRemoteJNDIName(foreignCF['REMOTE_JNDI'])
	
	# Create ForeignJMS Destinations
		for foreignDest in foreignServer['FOREIGN_JMS_DEST_ARRAY']:
			foreignJMSDest=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ForeignServers/"+foreignServer['FOREIGN_JMS_SERVER_NAME']+"/ForeignDestinations/"+foreignDest['NAME'])
			if foreignJMSDest is None :
				foreignJMSDest = foreignJMS.createForeignDestination(foreignDest['NAME']);
			foreignJMSDest.setLocalJNDIName(foreignDest['LOCAL_JNDI'])
			foreignJMSDest.setRemoteJNDIName(foreignDest['REMOTE_JNDI'])
	
activate()
exit()

Create MessageBridges

# ##########################################################################
# Creates Message Bridges
# Usage : CreateMessageBridges.py <Name of Properties file >
# #########################################################################
import sys
#read properties file

if len(sys.argv) != 2:
	print "Invalid Arguements :: Usage CreateMessageBridges.py <Message Bridge Properties file>"
	exit()
try:
	print "Load properties file"
	properties=sys.argv[1]
	file=open(properties,'r')
	print "Read properties file"
	exec file
	print "Execute properties file"
	file.close
except:
	exit()
#Connect

connect(USER,PASSWORD,ADMIN_URL)
edit()
startEdit()
for msgBridge in MSGBRIDGE_ARRAY:
	 
	destinations = (msgBridge['SOURCE_DEST'],msgBridge['TARGET_DEST'])
	
	for dest in destinations :
		if getMBean("/JMSBridgeDestinations/"+dest['NAME']) is None:
			jmsBridgeDestination=create(dest['NAME'],"JMSBridgeDestination")
			jmsBridgeDestination.setAdapterJNDIName(dest['ADAPTER_JNDI'])
			jmsBridgeDestination.setInitialContextFactory(dest['INITIAL_CONTEXT_FACTORY'])
			jmsBridgeDestination.setConnectionFactoryJNDIName(dest['CONNECTION_FACTORY'])
			jmsBridgeDestination.setDestinationJNDIName(dest['DESTINATION'])
			jmsBridgeDestination.setDestinationType(dest['DESTINATION_TYPE'])
	if getMBean("/MessagingBridges/"+msgBridge['NAME']) is None:
		messagingBridgeInstance=create(msgBridge['NAME'],"MessagingBridge")
	
		if (msgBridge['TARGET_TYPE']=="SERVER"):
			targetMB=getMBean("/Servers/"+msgBridge['TARGET'])
		if (msgBridge['TARGET_TYPE']=="CLUSTER"):
			targetMB=getMBean("/Clusters/"+msgBridge['TARGET'])

		if targetMB is None:
			print "@@@ Invalid Message Bridge Target '"+msgBridge['TARGET']+"'"
			exit()
	
		messagingBridgeInstance.addTarget(targetMB)
		messagingBridgeInstance.setSourceDestination(getMBean("/JMSBridgeDestinations/"+msgBridge['SOURCE_DEST']['NAME']))
		messagingBridgeInstance.setTargetDestination(getMBean("/JMSBridgeDestinations/"+msgBridge['TARGET_DEST']['NAME']))
		messagingBridgeInstance.setStarted(true)
		messagingBridgeInstance.setQualityOfService(msgBridge['QOS'])
		messagingBridgeInstance.setAsyncEnabled(false)
		messagingBridgeInstance.setPreserveMsgProperty(true)
		messagingBridgeInstance.setReconnectDelayMaximum(5)
		messagingBridgeInstance.setTransactionTimeout(10)
	
	
activate()

exit()

Create Work Managers

# ##########################################################################
# Creates Work Managers & Thread Constraints
# Usage : CreateWorkManagers.py <Name of Properties file >
# #########################################################################
import sys
#read properties file

if len(sys.argv) != 2:
	print "Invalid Arguements :: Usage CreateWorkManagers.py <WorkManagers Properties file>"
	exit()
try:
	print "Load properties file"
	properties=sys.argv[1]
	file=open(properties,'r')
	print "Read properties file"
	exec file
	print "Execute properties file"
	file.close
except:
	exit()
#Connect

connect(USER,PASSWORD,ADMIN_URL)
edit()
startEdit()
wmMBean=getMBean("/SelfTuning/"+DOMAIN)
if wmMBean is None:
		print "@@@ Invalid DOMAIN Name '"+DOMAIN+"'"
		exit()

for maxThreadsConst in MAX_THREADS_CONSTRAINT_ARRAY :
	if (maxThreadsConst['TARGET_TYPE']=="SERVER"):
		targetMB=getMBean("/Servers/"+maxThreadsConst['TARGET'])
	if (maxThreadsConst['TARGET_TYPE']=="CLUSTER"):
		targetMB=getMBean("/Clusters/"+maxThreadsConst['TARGET'])
	if targetMB is None:
		print "@@@ Invalid MAX THREADS CONSTRAINT Target '"+maxThreadsConst['TARGET']+"'"
		exit()
	if getMBean("/SelfTuning/"+DOMAIN+"/MaxThreadsConstraints/"+maxThreadsConst['NAME']) is None:		
		maxThreadConstInstance=wmMBean.createMaxThreadsConstraint(maxThreadsConst['NAME'])
		maxThreadConstInstance.addTarget(targetMB)
		maxThreadConstInstance.setCount(maxThreadsConst['COUNT'])

for workManager in WORK_MANAGER_ARRAY :
	if (workManager['TARGET_TYPE']=="SERVER"):
		targetMB=getMBean("/Servers/"+workManager['TARGET'])
	if (workManager['TARGET_TYPE']=="CLUSTER"):
		targetMB=getMBean("/Clusters/"+workManager['TARGET'])
	if targetMB is None:
		print "@@@ Invalid Work Manager Target '"+workManager['TARGET']+"'"
		exit()
	if getMBean("/SelfTuning/"+DOMAIN+"/WorkManagers/"+workManager['NAME']) is None:		
		workManagerInstance=wmMBean.createWorkManager(workManager['NAME'])
		workManagerInstance.addTarget(targetMB)
		workManagerInstance.setMaxThreadsConstraint(wmMBean.lookupMaxThreadsConstraint(workManager['MAX_THREADS_CONSTRAINT']))
	
activate()
exit()

Delete Datasources

# ##########################################################################
# Deletes Data Sources
# Usage : DeleteDataSources.py <Name of Properties file >
# #########################################################################
import sys
#read properties file

if len(sys.argv) != 2:
	print "Invalid Arguements :: Usage DeleteDataSources.py <DataSource Properties file>"
	exit()
try:
	print "Load properties file"
	properties=sys.argv[1]
	file=open(properties,'r')
	print "Read properties file"
	exec file
	print "Execute properties file"
	file.close
except:
	exit()
#Connect
connect(USER,PASSWORD,ADMIN_URL)
edit()
startEdit()

jdbcSysResourceMBean=getMBean("/JDBCSystemResources/") 
for dataSource in DATASOURCE_ARRAY:
	dataSourceMBean=getMBean("/JDBCSystemResources/"+dataSource['NAME'])
	if dataSourceMBean != None:
		if (dataSource['TARGET_TYPE']=="SERVER"):
			targetMB=getMBean("/Servers/"+dataSource['TARGET'])
		if (dataSource['TARGET_TYPE']=="CLUSTER"):
			targetMB=getMBean("/Clusters/"+dataSource['TARGET'])
		if targetMB != None:	
			dataSourceMBean.removeTarget(targetMB)
		if len(dataSourceMBean.getTargets()) ==0: #No Targets 
			jdbcSysResourceMBean.destroyJDBCSystemResource(dataSourceMBean)

activate()		
exit()

Delete ForeignJNDIProviders

# ##########################################################################
# Delete Foreign JNDI Links
# Usage : DeleteForeignJNDIProviders.py <Name of Properties file >
# #########################################################################
import sys
#read properties file

if len(sys.argv) != 2:
	print "Invalid Arguements :: Usage ForeignJNDIProviders.py <ForeignJNDI Properties file>"
	exit()
try:
	print "Load properties file"
	properties=sys.argv[1]
	file=open(properties,'r')
	print "Read properties file"
	exec file
	print "Execute properties file"
	file.close
except:
	exit()
#Connect

connect(USER,PASSWORD,ADMIN_URL)
edit()
startEdit()
forJNDIProviderMBean=getMBean("/ForeignJNDIProviders/")
for foreignJNDI in FOREIGN_JNDI_ARRAY:
	jndiProviderInstance=getMBean("/ForeignJNDIProviders/"+foreignJNDI['NAME'])
	if jndiProviderInstance != None: 
		forJNDIProviderMBean.destroyForeignJNDIProvider(jndiProviderInstance)
		
activate()
exit()

Delete JMS Module

# ##########################################################################
# Deletes a JMS Module and associated objects
# Usage : DeleteJMSModule.py <Name of Properties file >
# #########################################################################
import sys
#read properties file

if len(sys.argv) != 2:
	print "Invalid Arguements :: Usage DeleteJMSModule.py <JMS Module Properties file>"
	exit()
try:
	print "Load properties file"
	jms_properties=sys.argv[1]
	file=open(jms_properties,'r')
	print "Read properties file"
	exec file
	print "Execute properties file"
	file.close
except:
	exit()
#Connect

connect(USER,PASSWORD,ADMIN_URL)
edit()
startEdit()
moduleMBean=getMBean("/JMSSystemResources/")
sdMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/SubDeployments/")
cfMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ConnectionFactories/")
queueMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/Queues/")
topicMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/Topics/")
foreignServerMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ForeignServers/")

for subDeployment in JMS_SUB_DEPLOYMENT_ARRAY :
	
	for name,jndiName in subDeployment['XA_CONNECTION_FACTORY_ARRAY'].items():
		cfInstanceMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ConnectionFactories/"+name)
		if cfInstanceMBean != None:
			cfMBean.destroyConnectionFactory(cfInstanceMBean)	
	
	for name,jndiName in subDeployment['CONNECTION_FACTORY_ARRAY'].items():
		cfInstanceMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ConnectionFactories/"+name)
		if cfInstanceMBean != None:
			cfMBean.destroyConnectionFactory(cfInstanceMBean)	
		
	
	for name,jndiName in subDeployment['QUEUE_ARRAY'].items():
		qInstanceMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/Queues/"+name)
		if qInstanceMBean != None:
			queueMBean.destroyQueue(qInstanceMBean)
	
	for name,jndiName in subDeployment['TOPIC_ARRAY'].items():
		tInstanceMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/Topics/"+name)
		if tInstanceMBean != None:
			topicMBean.destroyTopic(tInstanceMBean)
	
	for foreignServer in subDeployment['FOREIGN_JMS_SERVER_ARRAY']:
		foreignCFMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ForeignServers/"+foreignServer['FOREIGN_JMS_SERVER_NAME']+"/ForeignConnectionFactories/")
		foreignJMSDestMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ForeignServers/"+foreignServer['FOREIGN_JMS_SERVER_NAME']+"/ForeignDestinations/")
		foreignPropertiesMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ForeignServers/"+foreignServer['FOREIGN_JMS_SERVER_NAME']+"/JNDIProperties/")
		foreignPropertiesInstanceMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ForeignServers/"+foreignServer['FOREIGN_JMS_SERVER_NAME']+"/JNDIProperties/"+foreignServer['FOREIGN_JMS_SERVER_JNDI_PROPERTY_KEY'])
		
		if foreignPropertiesInstanceMBean != None :
			foreignPropertiesMBean.destroyJNDIProperty(foreignPropertiesInstanceMBean)
		
		for  foreignCF in foreignServer['FOREIGN_JMS_CF_ARRAY']:
			foreignCFInstanceMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ForeignServers/"+foreignServer['FOREIGN_JMS_SERVER_NAME']+"/ForeignConnectionFactories/"+foreignCF['NAME'])	
			if foreignCFInstanceMBean != None :
				foreignCFMBean.destroyForeignConnectionFactory(foreignCFInstanceMBean)
		for foreignDest in foreignServer['FOREIGN_JMS_DEST_ARRAY']:
			foreignJMSDestInstanceMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ForeignServers/"+foreignServer['FOREIGN_JMS_SERVER_NAME']+"/ForeignDestinations/"+foreignDest['NAME'])
			if foreignJMSDestInstanceMBean != None :
				foreignJMSDestMBean.destroyForeignDestination(foreignJMSDestInstanceMBean)
		
		foreignServerInstanceMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/JMSResource/"+JMS_MODULE_NAME+"/ForeignServers/"+foreignServer['FOREIGN_JMS_SERVER_NAME'])
		if foreignServerInstanceMBean != None:
			foreignServerMBean.destroyForeignServer(foreignServerInstanceMBean)
	
	sdInstanceMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME+"/SubDeployments/"+subDeployment['JMS_SUB_DEPLOYMENT_NAME'])
	if sdInstanceMBean != None:
		sdMBean.destroySubDeployment(sdInstanceMBean)
	
moduleInstanceMBean=getMBean("/JMSSystemResources/"+JMS_MODULE_NAME)
if moduleInstanceMBean != None:
	moduleMBean.destroyJMSSystemResource(moduleInstanceMBean)
	
activate()
exit()

Delete MessageBridges

# ##########################################################################
# Creates Message Bridges
# Usage : DeleteMessageBridges.py <Name of Properties file >
# #########################################################################
import sys
#read properties file

if len(sys.argv) != 2:
	print "Invalid Arguements :: Usage DeleteMessageBridges.py <Message Bridge Properties file>"
	exit()
try:
	print "Load properties file"
	properties=sys.argv[1]
	file=open(properties,'r')
	print "Read properties file"
	exec file
	print "Execute properties file"
	file.close
except:
	exit()
#Connect

connect(USER,PASSWORD,ADMIN_URL)
edit()
startEdit()
bdgDestMBean=getMBean("/JMSBridgeDestinations/")
bdgMBean=getMBean("/MessagingBridges/")
for msgBridge in MSGBRIDGE_ARRAY:
	 
	destinations = (msgBridge['SOURCE_DEST'],msgBridge['TARGET_DEST'])
	
	bdgInstanceMBean=getMBean("/MessagingBridges/"+msgBridge['NAME'])
	if bdgInstanceMBean != None:
		bdgMBean.destroyMessagingBridge(bdgInstanceMBean)
		
	for dest in destinations :
		destMBean=getMBean("/JMSBridgeDestinations/"+dest['NAME'])
		if destMBean != None: 
			bdgDestMBean.destroyJMSBridgeDestination(destMBean)
		
activate()
exit()

Delete Work Managers

# ###################################################################################################################
# Deletes Work Managers & Thread Constraints
# Usage :	DeleteWorkManagers.py  	WorkManagers Properties file
#			
# ####################################################################################################################

import sys
#read properties file

if len(sys.argv) != 2:
	print "Invalid Arguements :: Usage DeleteWorkManagers.py <WorkManagers Properties file>"
	exit()
try:
	print "Load properties file"
	properties=sys.argv[1]
	file=open(properties,'r')
	print "Read properties file"
	exec file
	print "Execute properties file"
	file.close
except:
	exit()
	
#Connect

connect(USER,PASSWORD,ADMIN_URL)
edit()
startEdit()
stMBean=getMBean("/SelfTuning/"+DOMAIN)
for workManager in WORK_MANAGER_ARRAY:
	wmMBean=getMBean("/SelfTuning/"+DOMAIN+"/WorkManagers/"+workManager['NAME'])
	if wmMBean != None: 
		stMBean.destroyWorkManager(wmMBean)
for mtConstraint in MAX_THREADS_CONSTRAINT_ARRAY:
	
	mtcMBean=getMBean("/SelfTuning/"+DOMAIN+"/MaxThreadsConstraints/"+mtConstraint['NAME'])
	if mtcMBean != None:
		print "destroying max threads constraints...."
		stMBean.destroyMaxThreadsConstraint(mtcMBean)
		print "destroyed max threads constraint"

activate()
exit()

Sample Resource Type Properties

DataSource

USER="weblogic"
PASSWORD="weblogic1"
ADMIN_URL="t3://devserver:7001"

#SOA DataSource				   
SOADataSource={
					"NAME":"soa_ds",
					"JNDI_NAME":"soa_ds_jndi",
					"TARGET":"soa_server1",
					"TARGET_TYPE":"SERVER",
					"URL":"jdbc:oracle:thin:@oradevdbx01:1521:oradev",
					"DRIVER_NAME":"oracle.jdbc.xa.client.OracleXADataSource",
					"USER":"soa_user",
					"PASSWORD":"soa_pass"
				   }

#OSB DataSource				   
OSBDataSource={
					"NAME":"osb_ds",
					"JNDI_NAME":"osb_ds_jndi",
					"TARGET":"osb_server1",
					"TARGET_TYPE":"SERVER",
					"URL":"jdbc:oracle:thin:@oradevdbx01:1521:oradev",
					"DRIVER_NAME":"oracle.jdbc.xa.client.OracleXADataSource",
					"USER":"osb_user",
					"PASSWORD":"osb_pass"
				   }	


  
				   
#################  E  N D 	of 	D A T A  S O U R C E	C O N F I G U R A T I O N ################################ 
DATASOURCE_ARRAY=(SOADataSource,OSBDataSource)

JMSModule

USER="weblogic"
PASSWORD="weblogic1"
ADMIN_URL="t3://devserver:7001"

JMS_MODULE_NAME="IJmsModule"
JMS_MODULE_TARGET="osb_server1"
JMS_MODULE_TARGET_TYPE="SERVER"

# REPEAT BELOW SECTION FOR EACH SUBDEPLOYMENT 

###################################
# Sub Deployment : ISubDeployment
###################################

IXAConnFactoryArray ={"IWlsConnectionFactory":"jndi/cf/IWlsConnectionFactory"}

IQueueArray={
			   "LOG_ALERT":"jndi/queues/LOG_ALERT",
			   "POST_UINSTATUS":"jndi/queues/POST_UINSTATUS"			   
			  }
			  
IConnFactory1 ={
					"NAME":"BdgIConnectionFactory",
					"LOCAL_JNDI":"jndi/cf/BdgIConnectionFactory",
					"REMOTE_JNDI":"XAQueueConnectionFactory"
				   }
IConnFactoryArray=(IConnFactory1,)

IDest1 = {
				"NAME":"BdgIUINStatusForeignDestination",
				"LOCAL_JNDI":"jndi/queues/BdgIUINStatusForeignDestination",
				"REMOTE_JNDI":"Queues/POST_UIN_STATUS_UPDATEQ"
			 }
IDestArray=(IDest1,)
			  
IForeignServer={
					"FOREIGN_JMS_SERVER_NAME":"BdgIForeignServer",
                    "FOREIGN_JMS_SERVER_INITIAL_CONTEXT_FACTORY":"oracle.jms.AQjmsInitialContextFactory",
					"FOREIGN_JMS_SERVER_PROVIDER_URL":"",
					"FOREIGN_JMS_SERVER_JNDI_PROPERTY_KEY":"datasource",
					"FOREIGN_JMS_SERVER_JNDI_PROPERTY_VALUE":"bdg_ds_Iaq_jndi",
					"FOREIGN_JMS_CF_ARRAY":IConnFactoryArray,
					"FOREIGN_JMS_DEST_ARRAY":IDestArray					  
					 }			 


osbSubDeploymentForeignServerArray=(IForeignServer,)

ISubDeployment={
					  "JMS_SUB_DEPLOYMENT_NAME":"ISubDeployment",
					  "JMS_SUB_DEPLOYMENT_TARGET":"I_jmss_Server",
					  "JMS_SUB_DEPLOYMENT_TARGET_TYPE":"JMS",
                      "XA_CONNECTION_FACTORY_ARRAY":IXAConnFactoryArray,
					  "CONNECTION_FACTORY_ARRAY":{},
					  "QUEUE_ARRAY":IQueueArray,
					  "TOPIC_ARRAY":{},
					  "FOREIGN_JMS_SERVER_ARRAY":IForeignServer
					 }

#################  E  N D 	of 	S U B	D E P L O Y M E N T		C O N F I G U R A T I O N ################################ 

JMS_SUB_DEPLOYMENT_ARRAY=(ISubDeployment,)

ForeignJNDIProviders

USER="weblogic"
PASSWORD="weblogic1"
ADMIN_URL="t3://devserver:7001"

#SoaForeignJNDIProvider

SoaLocalConnectionFactoryLink={"NAME":"SoaLocalConnectionFactoryLink",
						       "LOCAL_JNDI":"jndi/local/cf/IWlsConnectionFactory",
							   "REMOTE_JNDI":"jndi/cf/IWlsConnectionFactory"
							   }


							   

SoaForeignJNDIProviderLinks =(SoaLocalConnectionFactoryLink,)

							  	
SoaForeignJNDIProvider={
	          "NAME":"SoaForeignJNDIProvider",
			  "TARGET":"soa_server1",
			  "TARGET_TYPE":"SERVER",
			  "INITIAL_CONTEXT_FACTORY":"weblogic.jndi.WLInitialContextFactory",
			  "PROVIDER_URL":"t3://osbhost:7101",	
			  "USER":"osb_wls_user",
			  "PASSWORD":"osb_wls_pass",
			  "LINKS_ARRAY":SoaForeignJNDIProviderLinks
			 } 



#################  E  N D 	of 	F O R E I G N  J N D I 	C O N F I G U R A T I O N ################################ 
FOREIGN_JNDI_ARRAY=(SoaForeignJNDIProvider,)

MessageBridge

USER="weblogic"
PASSWORD="weblogic1"
ADMIN_URL="t3://devserver:7001"



BdgPostAStatusBridgeSrc={
					"NAME":"BdgPostAStatusPosAQDestination",
					"ADAPTER_JNDI":"eis.jms.WLSConnectionFactoryJNDIXA",
					"INITIAL_CONTEXT_FACTORY":"weblogic.jndi.WLInitialContextFactory",
					"CONNECTION_FACTORY":"jndi/cf/BdgPosAUConnectionFactory",
					"DESTINATION":"jndi/queues/BdgPostAStatusForeignDestination",				
					"DESTINATION_TYPE":"Queue"					
				   }
				   
BdgPostAStatusBridgeTrg={
					"NAME":"BdgPostAStatusWLSDestination",
					"ADAPTER_JNDI":"eis.jms.WLSConnectionFactoryJNDIXA",
					"INITIAL_CONTEXT_FACTORY":"weblogic.jndi.WLInitialContextFactory",
					"CONNECTION_FACTORY":"jndi/cf/BdgCIWlsConnectionFactory",
					"DESTINATION":"jndi/queues/Bdg_POS_UINSTATUS",				
					"DESTINATION_TYPE":"Queue"					
				   }
				   

BdgPostAStatusBridge={
					"NAME":"BdgPostAStatusBridge",
					"TARGET":"bdg_server1",
					"TARGET_TYPE":"SERVER",
					"SOURCE_DEST":BdgPostAStatusBridgeSrc,
					"TARGET_DEST":BdgPostAStatusBridgeTrg,
					"QOS":"Exactly-once"					
				   }
				   
BdgPostNStatusBridgeSrc={
					"NAME":"BdgPostNStatusPosAQDestination",
					"ADAPTER_JNDI":"eis.jms.WLSConnectionFactoryJNDIXA",
					"INITIAL_CONTEXT_FACTORY":"weblogic.jndi.WLInitialContextFactory",
					"CONNECTION_FACTORY":"jndi/cf/BdgPosNZConnectionFactory",
					"DESTINATION":"jndi/queues/BdgPostNStatusForeignDestination",				
					"DESTINATION_TYPE":"Queue"					
				   }
				   
BdgPostNStatusBridgeTrg={
					"NAME":"BdgPostNStatusWLSDestination",
					"ADAPTER_JNDI":"eis.jms.WLSConnectionFactoryJNDIXA",
					"INITIAL_CONTEXT_FACTORY":"weblogic.jndi.WLInitialContextFactory",
					"CONNECTION_FACTORY":"jndi/cf/BdgCIWlsConnectionFactory",
					"DESTINATION":"jndi/queues/Bdg_POS_UINSTATUS",				
					"DESTINATION_TYPE":"Queue"					
				   }

BdgPostNStatusBridge={
					"NAME":"BdgPostNStatusBridge",
					"TARGET":"bdg_server1",
					"TARGET_TYPE":"SERVER",
					"SOURCE_DEST":BdgPostNStatusBridgeSrc,
					"TARGET_DEST":BdgPostNStatusBridgeTrg,
					"QOS":"Exactly-once"					
				   }  
#################  E  N D 	of 	M E S S A G E   B R I D G E 	C O N F I G U R A T I O N ################################ 
MSGBRIDGE_ARRAY=(BdgPostAStatusBridge,BdgPostNStatusBridge)


WorkManagers

USER="weblogic"
PASSWORD="weblogic1"
ADMIN_URL="t3://devserver:7001"


#ISingletonMaxThreadConstraint
ISingleton={
	          "NAME":"ISingleton",
			  "TARGET":"osb_server1",
			  "TARGET_TYPE":"SERVER",
			  "COUNT":1
			 } 

#ISStatusJmsLocalProducerWorkManager
ISStatusJmsLocalProducer={
									"NAME":"ISStatusJmsLocalProducer",
									"TARGET":"osb_server1",
									"TARGET_TYPE":"SERVER",
									"MAX_THREADS_CONSTRAINT":"ISingleton"
								}
#ISStatusJmsRemoteConsumerWorkManager
ISStatusJmsRemoteConsumer={
									"NAME":"ISStatusJmsRemoteConsumer",
									"TARGET":"osb_server1",
									"TARGET_TYPE":"SERVER",
									"MAX_THREADS_CONSTRAINT":"ISingleton"
								}
#R_W_JMS_Local_Producer_WorkManager
R_W_JMS_Local_Producer_WorkManager={
									"NAME":"R_W_JMS_Local_Producer_WorkManager",
									"TARGET":"osb_server1",
									"TARGET_TYPE":"SERVER",
									"MAX_THREADS_CONSTRAINT":"ISingleton"
								}
				
#################  E  N D 	of 	W O R K   M A N A G E R 	C O N F I G U R A T I O N ################################ 
MAX_THREADS_CONSTRAINT_ARRAY=(ISingleton,)
WORK_MANAGER_ARRAY=(ISStatusJmsLocalProducer,ISStatusJmsRemoteConsumer,R_W_JMS_Local_Producer_WorkManager)


Advertisements

About atheek

I am a Weblogic consultant working in Middleware/Integration area.
This entry was posted in Weblogic, WLST and tagged , . Bookmark the permalink.

12 Responses to WLST – Modular Scripts to create/destroy WLS resources

  1. RR says:

    hello, am getting the following error while trying to create data sources using the scirpt.

    Starting an edit session …
    Started edit session, please be sure to save and activate your
    changes once you are done.
    Problem invoking WLST – Traceback (innermost last):
    File “/tmp/CreateDataSources.py”, line 31, in ?
    TypeError: sequence subscript must be integer or slice

    Line31:

    jdbcSystemResource=getMBean(“/JDBCSystemResources/”+dataSource[‘NAME’])

    -RR

  2. Gendre says:

    Hi,

    I am new to jython and I was wondering if you could show a sample of the properties file that is loaded?

  3. Gendre says:

    Thanks I did not realize the property files were listed. Thanks so much this is such a useful blog.

  4. Gendre says:

    Atheek,

    I created script for application deployment and I was wondering if you had any insight as to why it is not working. So I have a build machine running Linux. On this build machine is where the source code is compiled and an EAR file is created. Once the EAR file is created, I have a SCP script that transfers the built EAR to a DEV machine running Weblogic 10GR3 version 10.3.0.

    The WLST scripts works as expected if it is run on our DEV machine. However, the script fails when it runs from our build machine. When the WLST script runs on build machine, it looks for the EAR file locally. I expected the WLST script to look for the EAR file on the DEV machine.

    Here are the scripts:

    # ##########################################################################
    # Creates Data Sources
    # Usage : Createapplicationlications.py
    # #########################################################################
    import sys
    #read properties file

    try:
    print “Load properties file”
    file=open(‘appProps.py’,’r’)
    print “Read properties file”
    exec file
    print “Execute properties file”
    file.close
    except:
    exit()
    #Connect

    connect(USER,PASSWORD,ADMIN_URL)
    edit()
    for application in APPLICATION_DEPLOYMENT_ARRAY:
    startEdit()

    if (application[‘TARGET_TYPE’]==”SERVER”):
    targetMB=getMBean(“/Servers/”+application[‘TARGET’])
    if (application[‘TARGET_TYPE’]==”CLUSTER”):
    targetMB=getMBean(“/Clusters/”+application[‘TARGET’])

    if targetMB is None:
    print “@@@ Invalid application Target ‘”+application[‘TARGET’]+”‘”
    exit()

    if(application[‘TYPE’]==”APPLICATION”):
    deploy(application[‘NAME’],application[‘SOURCE_DIR’] + application[‘FILE_NAME’], application[‘TARGET’])
    if(application[‘TYPE’]==”LIBRARY”):
    deploy(application[‘NAME’],application[‘SOURCE_DIR’] + application[‘FILE_NAME’], targetMB, libraryModule=’true’)
    try:
    save()
    activate(block=”true”)
    exit()
    except:
    undo(defaultAnswer=’y’)

    exit(defaultAnswer=’y’)

    Here is the properties file:
    USER=”weblogic”
    PASSWORD=”weblogic”
    ADMIN_URL=”DEV_Server_URL:8001″

    ###################################
    # Sub Deployment : ISubDeployment
    ###################################

    Application1 ={
    “NAME”:”GasPilotv1″,
    “TYPE”:”APPLICATION”,
    “TARGET”:”EIWLSD01C1″,
    “TARGET_TYPE”:”CLUSTER”,
    “SOURCE_DIR”:”/home/gxab/”,
    “FILE_NAME”:”GasPilotv1.12.0.ear”
    }

    ################# E N D of S U B D E P L O Y M E N T C O N F I G U R A T I O N ################################

    APPLICATION_DEPLOYMENT_ARRAY=(Application1,)

  5. Gendre says:

    Here is the error from the WLST when it is run on the build machine. The actual EAR file is in /home/gxab/ directory of my DEV machine.

    Problem invoking WLST – Traceback (innermost last):
    File “/tmp/jenkins/workspace/GASTR/_projects/vobs/EI/WFM_ws/GasPilot/deploy/deployApplications.py”, line 35, in ?
    File “”, line 234, in deploy
    WLSTException: Error occured while performing deploy : Target exception thrown while deploying application: No file or directory found at the specified application path: /home/gxab/GasPilotv1.12.0.earNo file or directory found at the specified application path: /home/gxab/GasPilotv1.12.0.ear Use dumpStack() to view the full stacktrace

  6. atheek says:

    Hi Gendre
    Firstly, Nice to see that you extended this for your requirement of application deployment.
    Looks like deploy task performs the file transfer from local to server via t3, so you wont need to use scp. Shouldn’t this be the simplest approach then ?
    -Atheek

  7. Gendre says:

    Hi Atheek,

    Thanks for the response. I believe the file transfer via t3 does cut out the scp step. But how do we specify where to store the EAR on the DEV machine. Right now, if I put the EAR in the /home/gxab/ directory on the build machine, it copies the EAR on the DEV server to the /home/gxab directory. I want to specify which directory the EAR goes on the DEV server. Right now it looks like the only way to accomplish this is to create a mirror directory structure. For example, on the build machine, I would have to put the directory in /deployments/ear. I would then need to create the same directory structure on our DEV machine. I am just hoping there is another way to accomplish this without coupling the two machines. Thanks again for responding.

    – Gendre

  8. Gendre says:

    Hi Atheek,

    I figured out the problem. I did not use the remote option in my deployment command. If this is not specified it assumes the deployment is local.

    Here is the original command:
    deploy(application[‘NAME’],application[‘SOURCE_DIR’] + application[‘FILE_NAME’], application[‘TARGET’])

    Here is what I added:
    deploy(application[‘NAME’],application[‘SOURCE_DIR’] + application[‘FILE_NAME’], application[‘TARGET’],remote=’true’)

    Thanks again for these great samples.

  9. ramy says:

    Is there any WLST script to update DVM from backend in a particular cluster node?
    OR
    Is there a method to delete DVM cache?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s