Wednesday, August 7, 2013

Use WLST For WebLogic Startup and Shutdown Scripts

There are multiple ways to have an automated WebLogic Server startup process for your servers. You can have a process the run the startWebLogic.cmd/sh and startManagedWebLogic.cmd/sh scripts, create a semi-Windows Service to start WebLogic, and you could configure Node Manager to automatically startup servers when Node Manager is first being started. However, there are significant drawbacks to all of these methods. The most reliable method is using WLST to start WebLogic via Node Manager.

Firstly, why use it? You can start your WebLogic servers in a proper order, waiting for one to start after the next. Since WebLogic provides a great Pytyhon API, i find it is the most reliable method for starting the servers. However, the most important reason is when you use WLST to start your servers via Node Manager, you can utilize a great feature of Node Manager called "Crash Recovery". If your WebLogic Servers crash or if the physical Server loses power, Node Manager will automatically boot back up.

After all, if theres one more thing to make WebLogic more reliable, you have to do it!

I found that theres plenty of articles on using WLST to start servers, however most of them had invalid python syntax and WLST commands! This solution can apply to WebLogic Servers that utilize most Fusion Middleware Technologies as well: ADF Servers, 11g Forms/Reports, Oracle Access Manager, and the list goes on...

My example focuses on a UNIX based solution but you can adapt it to use batch pretty easily.  Enough blabbering, lets get started!

Part 1: WebLogic Preparations


WebLogic needs a few properties initialized before we can consider using WLST Scripting. First we initialize the weblogic username/password credentials required by Node Manager. Next, we can generate an encrypted username and password store-file to avoid hardcoding the weblogic username/password into the WLST scripts. Next, we enroll the Node Manager with the WebLogic Domain - to ensure proper Node Manager operations. Lastly, we update the Node Manager configuration file to enable Crash Recovery and to use the startWebLogic.sh/cmd script when starting servers.

  1. Make sure AdminServer is running.
  2. Login to the Admin Console.
  3. Enter Lock & Edit mode.
  4. Click on your Domain Name from the Domain Structure Panel. For example my domain name is ReportsDomain.
  5. Click on the Security tab.
  6. Expand the Advanced Settings panel and fill out your weblogic username/password credentials in the NodeManager Username and NodeManager Password fields
  7. Click Save and Activate Changes.
  8. Go to Environment > Servers
  9. Enter Lock & Edit mode.
  10. For each server: click on the server, then open the sub tab Server Start
  11. For User Name and Password fields, specify your weblogic username and password respectively.
  12. Once you've updated all your server's with this change, Activate Changes.
  13. Make sure your WebLogic Servers are configured with a Machine. Respectively, makes sure the machine element has the proper Node Manager port configurations.
  14. Open a command-line session to your server where WebLogic is installed.
  15. Run the $DOMAIN_HOME/bin/setDomainEnv.sh script to set your command line's shell/batch environment
  16. Open WLST via the command: java weblogic.WLST
  17. Run the command similar to: connect('weblogic_username','weblogic_password','t3://hostname:7001')
  18. Run the command to enroll the domain with Node Manager: nmEnroll(domainDir='<WebLogic_Domain_Home>',nmHome='<Node_Manager_Home>')
    nmHome should be <WebLogic_Server_Home>/common/nodemanager
  19. Run the command similar to the following to generate an encrypted username and password credential file:
    storeUserConfig('<WebLogic_Domain_Home>/userCred','<WebLogic_Domain_Home>/passCred')
  20. To exit, run the command: exit()
  21. Go to the Node Manager Home directory and open nodemanager.properties
  22. Update the values:
    StartScriptEnabled=true
    CrashRecoveryEnabled=true
  23. Shutdown Node Manager and all WebLogic Servers


Part 2: Set OS User Profile

The WLST Scripts depend on some environment variables - to make them more configurable for mass server deployment. Set the following environment variables in your OS user's environment profile:

  • DOMAIN_HOME => Set this to the WebLogic Domain directory. For example /opt/oracle/middleware/user_projects/domains/ReportsDomain
  • MW_HOME => Set this to the middleware home directory. For example, /opt/oracle/middleware
  • NM_PORT => Set this to the port your Node Manager runs on. By default it runs on 5556.
  • DOMAIN_NAME => Set this to the logical name of your WebLogic Domain. For example, ReportsDomain
  • HOSTNAME => If not already set, set this to the hostname of the physical machine.


Part 3: Create and Run Scripts

This is the easiest part! Simply call the WebLogic scripts from shell or batch environment.


1.) Create a start all WLST Script on the server: wlst_startall.py

print '==================================='
print 'Starting WebLogic Domain ...'
print '==================================='

#Pull All Script configuration values from OS user's environment variables
import os
wlsDomainDir = os.getenv('DOMAIN_HOME')
mwHomeDir = os.getenv('MW_HOME')
nmPort=os.getenv('NM_PORT')
wlsDomainName=os.getenv('DOMAIN_NAME')
hostName=os.getenv('HOSTNAME')

#More Script Configuration Values - However, these shouldnt have to be touched
nmHomeDir=mwHomeDir + '/wlserver_10.3/common/nodemanager'
nmPropFile=nmHomeDir + '/nodemanager.properties'
userCred = wlsDomainDir + '/userCred'
passCred = wlsDomainDir + '/passCred'

# Connect to Node Manager, if it is running. Otherwise Start it up. This will take a minute or so.
#... instead of using hard coded username/passwords, we'll use an encrypted file, hence the "configFile" parameters
print '==================================='
print 'Connecting to Node Manager ...'
print '==================================='
try:
nmConnect(userConfigFile=userCred,userKeyFile=passCred,host=hostName,port=nmPort,domainName=wlsDomainName,domainDir=wlsDomainDir)
except:
startNodeManager(NodeManagerHome=nmHomeDir,PropertiesFile=nmPropFile)
nmConnect(userConfigFile=userCred,userKeyFile=passCred,host=hostName,port=nmPort,domainName=wlsDomainName,domainDir=wlsDomainDir)
# Now that we're connected to Node Manager, we can startup the servers.
# Start Admin Server
print '==================================='
print 'Starting AdminServer ...'
print '==================================='
nmStart('AdminServer')

# Start Managed Server - ms_adf_1
print '==================================='
print 'Starting ms_adf_1 ...'
print '==================================='
nmStart('ms_adf_1')

#ADD MORE SERVERS HERE!!!

#Startup and operations are complete, disconnect from Node Manager
nmDisconnect()

print '==================================='
print 'End of Startup Script'
print '==================================='


2. ) Create a stop all WLST Script on the server: wlst_stopall.py

print '==================================='
print 'Stopping WebLogic Domain ...'
print '==================================='

#Pull All Script configuration values from OS user's environment variables
import os
wlsDomainDir = os.getenv('DOMAIN_HOME')
mwHomeDir = os.getenv('MW_HOME')
nmPort=os.getenv('NM_PORT')
wlsDomainName=os.getenv('DOMAIN_NAME')
hostName=os.getenv('HOSTNAME')

#More Script Configuration Values - However, these shouldnt have to be touched
nmHomeDir=mwHomeDir + '/wlserver_10.3/common/nodemanager'
nmPropFile=nmHomeDir + '/nodemanager.properties'
userCred = wlsDomainDir + '/userCred'
passCred = wlsDomainDir + '/passCred'

print '==================================='
print 'Connecting to Node Manager ...'
print '==================================='
try:
nmConnect(userConfigFile=userCred,userKeyFile=passCred,host=hostName,port=nmPort,domainName=wlsDomainName,domainDir=wlsDomainDir)
except:
startNodeManager(NodeManagerHome=nmHomeDir,PropertiesFile=nmPropFile)
nmConnect(userConfigFile=userCred,userKeyFile=passCred,host=hostName,port=nmPort,domainName=wlsDomainName,domainDir=wlsDomainDir)

print '==================================='
print 'Stopping ms_adf_1 ...'
print '==================================='
try:
nmKill('ms_adf_1')
except:
print 'Error stopping ms_adf_1, it may already be down'

#ADD MORE SERVERS HERE!!!
print '==================================='
print 'Stopping AdminServer ...'
print '==================================='
try:
nmKill('AdminServer')
except: 
print 'Error stopping AdminServer, it may already be down'

nmDisconnect()
exit()

print '==================================='
print 'End of Stop Script'
print '==================================='


3.) Configure a shell script (or a batch version of this) to run the startall script:

# ADF - swat_domain - environment variables - required for WLST
. $DOMAIN_HOME/bin/setDomainEnv.sh
# ADF - swat_domain - start all servers via WLST
java weblogic.WLST /location/of/wlst_startall.py


4.) Configure a shell script (or batch version of this) to run the stopall script:

# ADF - swat_domain - environment variables - required for WLST
. $DOMAIN_HOME/bin/setDomainEnv.sh
# ADF - swat_domain - start all servers via WLST
java weblogic.WLST /location/of/wlst_stopall.py


5.) Configure your server to run the shell/batch scripts at startup and shutdown. After that you are good to go!


I'll update this later with some more helpful methods, but for now, this should be helpful!