WebLogic Scripting Tool scripts
Useful WLST resources and Gist examples for scripting WebLogic domain creation and server administration.
I mentioned my use of the WebLogic Scripting Tool a little while back. I have noticed since then that a number of folks visiting this site are looking for example scripts. I have obviously written a number myself and I promise I’ll try to get around to posting them here. However until I get myself in gear, I thought I would point you at some useful examples that are already out there. I’ll expand this post as I find more…
First, make sure you sign up on http://dev2dev.bea.com/. That’s the BEA site supporting developers where you will find provides news, tutorials and samples to help you get going with WebLogic.
They have a number of projects and code samples that you will be able to get at. My recommendation for getting started is to go to the CodeShare section and have a look around at all the good stuff that has been provided by generous developers around the world.
There’s a WLST project maintained by the guys who developed it in the first place. This contains a whole load of scripts.
And to give you more of a head start, you can go to the Code Samples area of dev2dev and search for WLST, which will give you a list of samples worth looking at. Currently these include a Server Health Monitor (artifact S198), which will periodically check your runtime heap and execute queues and log the state to file.
Also, if you are looking at WLST for its server monitoring capabilities, you should know that BEA Guardian is now free!
Update A few folks have asked about getting hold of some example scripts. Unfortunately most of what I have written is for work, so it is tied to work environments and not mine to share. However, I have created some simple scripts for sharing, that cover scripting the creation of a domain. These are available as GitHub gists for creating WebLogic domains:
bea_home=/path/to/bea
java_home=/path/to/bea/jdk160_18
domain_template=/path/to/bea/weblogic11/common/templates/domains/wls.jar
admin_server_name=my_admin_server
admin_url=t3\://myserver\:7001
admin_user=weblogic
admin_password=w3blogicpwd
admin_port=7001
admin_port_secure=7002
domain_name=my_domain
my_server_1_port=8001
my_server_2_port=8011
my_domain_log4j_file=/path/to/bea/user_projects/domains/my_domain/log4j.xml
# ----------------------------------------------------------------------
# Datasources
# ----------------------------------------------------------------------
my_ds_name=my_datasource
my_ds_targets=my_server_1,my_server_2
my_ds_jndi=my.datasource
my_ds_url=jdbc\:oracle\:thin\:@dbserver\:dbport\:dbname
my_ds_user=dbusername
my_ds_password=dbuserpassword
# ----------------------------------------------------------------------
# Applications
# ----------------------------------------------------------------------
my_app_name=my_app
my_app_targets=my_server_1
my_app_path=/path/to/my_app_1.0.ear
"""Stage 1 domain creation script.
Read a template domain and make some offline modifications to it to define admin server
details and create some managed servers. A follow-up online phase is needed once the
servers have been started up.
"""
execfile("wlst_util_offline.py")
#=======================================================================================
# Open a domain template.
#=======================================================================================
print "Reading: " + domain_template
readTemplate(domain_template)
#=======================================================================================
# Configure the Administration Server.
#=======================================================================================
print "Moving to Server/AdminServer"
cd('Server/AdminServer')
print "Setting Name to: " + admin_server_name
set('Name', admin_server_name)
print "Setting ListenAddress to nothing."
set('ListenAddress', '')
print "Setting ListenPort to " + admin_port
set('ListenPort', int(admin_port))
create(admin_server_name, 'SSL')
cd('SSL/' + admin_server_name)
set('Enabled', 'True')
set('ListenPort', int(admin_port_secure))
#=======================================================================================
# Define the password for user weblogic. You must define the password before you
# can write the domain.
#=======================================================================================
print "Admin password: " + admin_password
cd('/')
cd('Security/base_domain/User/weblogic')
cmo.setPassword(admin_password)
#=======================================================================================
# Set Options:
# - CreateStartMenu: Enable creation of Start Menu shortcut.
# - ServerStartMode: Set mode to development.
# - JavaHome: Sets home directory for the JVM used when starting the server.
# - OverwriteDomain: Overwrites domain, when saving, if one exists.
#=======================================================================================
setOption('CreateStartMenu', 'true')
setOption('ServerStartMode', 'prod')
setOption('JavaHome', bea_home + '/jdk160_18')
setOption('OverwriteDomain', 'true')
#=======================================================================================
# Write the domain and close the domain template.
#=======================================================================================
writeDomain(bea_home + '/user_projects/domains/' + domain_name)
closeTemplate()
#=======================================================================================
# Reopen the domain.
#=======================================================================================
readDomain(bea_home + '/user_projects/domains/' + domain_name)
print 'Creating managed servers...'
create_managed_server('my_server_1', my_server_1_port, '', my_domain_log4j_file)
create_managed_server('my_server_2', my_server_2_port, '', my_domain_log4j_file)
#=======================================================================================
# We're done with configuring the domain. Close the domain template.
#=======================================================================================
updateDomain()
closeDomain()
exit()
"""A collection of utility methods used in creating a domain.
"""
import os
def create_managed_server(server_name, port, listen_address, log4jfile):
"""Creates a managed server on the domain."""
print 'Creating managed server: server_name=' + server_name \
+ ', port=' + str(port) \
+ ', listen_address=' + listen_address \
+ ', log4jfile=' + log4jfile
cd('/')
create(server_name, 'Server')
cd('/Servers/' + server_name)
set('ListenPort', int(port))
set('ListenAddress', listen_address)
set('Machine', get_machine_name())
create(server_name, 'ServerStart')
def get_machine_name():
"""Determines the physical machine name."""
# HOSTNAME is usual on UNIX, but COMPUTERNAME is the Windows location.
if os.environ.has_key('HOSTNAME'):
return os.getenv('HOSTNAME')
elif os.environ.has_key('COMPUTERNAME'):
return os.getenv('COMPUTERNAME')
else:
return 'UNKNOWN'
def create_machine(machineName):
"""Creates a 'machine' on the domain against which servers can be allocated.
This is only used in clustered environments.
"""
cd('/')
create(machineName, 'Machine')