Saturday, November 23, 2013

Automating WebLogic Server Instances Startup and Shutdown in Linux

Description: This post contains scripts to automate start up and shutdown Oracle Identity Manager managed server and WebLogic Admin server in a Linux environment. Before using these scripts, you need to configure Node Manager.
Start up Order:
  1. Start Node Manager.
  2. Connect to Node Manager using WebLogic Scripting Tool (WLST).
  3. Have Node Manager start up your WebLogic server instances.
 Shutdown Order:
  1. Connect to Node Manager using WebLogic Scripting Tool (WLST).
  2. Shutdown WebLogic Server instances.
  3. Terminate Node Manager.
WLST Scripts
Give the scripts mentioned below 750 permissions.

1. "startWebLogicServers.py" script connects to Node Manager and starts the WebLogic server instances. Node Manager must be running.

# A Jython script to start the WebLogic server instances.
# This script is for WebLogic Scripting Tool (WLST).
# Node Manager on current machine must be started before running this script.
# The configuration file and the key file can be generated using storeUserConfig() in WLST
# Go into WebLogic Administrator console to find more information about the Node Manager for a specific machine
# Environemnt -> Machines -> <Machine_Name> ->Configuration -> Node Manager 

from java.io import FileInputStream;
import java.lang;
import os;
import string;

# Load properties file
propInputStream = FileInputStream("/home/oracle/domain.properties");
configProps = Properties();
configProps.load(propInputStream);

# Get values from properties file
startServerValues = configProps.get("startServerValues");
userConfigFileValue = configProps.get("userConfigFileValue");
userKeyFileValue = configProps.get("userKeyFileValue");
oimHostValue = configProps.get("oimHostValue");
portValue = configProps.get("portValue");
domainNameValue = configProps.get("domainNameValue");
domainDirValue = configProps.get("domainDirValue");
nmTypeValue = configProps.get("nmTypeValue");
verboseModeValue = configProps.get("verboseModeValue");

print 'Invoking WLST';

try:
     # Connect to Node Manager on the current machine
     nmConnect(userConfigFile=userConfigFileValue, userKeyFile=userKeyFileValue, host=oimHostValue, port=portValue, domainName=domainNameValue, domainDir=domainDirValue, mType=nmTypeValue, verbose=verboseModeValue);
   
     # Determine if WLST is currently connected to Node Manager on the OIM machine
     if nm():
          #Loop through all the WebLogic Server Instances specified
          for serverValue in startServerValues.split("|"):
               print 'Current ', serverValue ,' Server Status:';
               serverStatus = nmServerStatus(serverValue);  
   
               if not serverStatus == 'RUNNING':
                    #Start WebLogic Server Instance
                    nmStart(serverValue);
                    print 'New ', serverValue ,' Server Status:';
                    nmServerStatus(serverValue);
  
     else:
          print 'You are not connected to Node Manager.';

finally:
     # Disconnect from Node Manager
     nmDisconnect();
     exit();

print 'End WLST';


2. "stopWebLogicServers.py" script stops the WebLogic server instances and Node Manager.

# A Jython script to stop the WebLogic server instances.
# Node Manager must be running before executing this script.
# Execute this script using WebLogic Scripting Tool (WLST).
# The configuration file and the key file can be generated using storeUserConfig() in WLST
# Go into WebLogic Administrator console to find more information about the Node Manager
# Environemnt -> Machines -> <Machine_Name> ->Configuration -> Node Manager 

from java.io import FileInputStream;
import java.lang;
import os;
import string;

# Load properties file
propInputStream = FileInputStream("/home/oracle/domain.properties");
configProps = Properties();
configProps.load(propInputStream);

# Get values from properties file
stopServerValues = configProps.get("stopServerValues");
userConfigFileValue = configProps.get("userConfigFileValue");
userKeyFileValue = configProps.get("userKeyFileValue");
oimHostValue = configProps.get("oimHostValue");
portValue = configProps.get("portValue");
domainNameValue = configProps.get("domainNameValue");
domainDirValue = configProps.get("domainDirValue");
nmTypeValue = configProps.get("nmTypeValue");
verboseModeValue = configProps.get("verboseModeValue");

print 'Invoking WLST';
try:
     #Connect to Node Manager on the current machine
     nmConnect(userConfigFile=userConfigFileValue, userKeyFile=userKeyFileValue, host=oimHostValue, port=portValue, domainName=domainNameValue, domainDir=domainDirValue, mType=nmTypeValue, verbose=verboseModeValue);
     
     #Determine if WLST is currently connected to Node Manager on the OIM machine
     if nm():
          for serverValue in stopServerValues.split("|"):
               print 'Current ', serverValue ,' Server Status:';
               serverStatus = nmServerStatus(serverValue);

               if serverStatus == 'RUNNING':
                    #Stop WebLogic Server Instance
                    nmKill(serverValue);
                    print 'New ', serverValue ,' Server Status:';
                    nmServerStatus(serverValue);
               
          print 'Stopping Node Manager:';
          stopNodeManager();
                  
     else:
          print 'You are not connected to Node Manager on OIM Machine.';

finally:
     #Disconnects WLST from session
     exit();
     
print 'End WLST'; 
Properties File
1. "domain.properties" contains environment information on Node Manager and WebLogic server instances. Adjust the variable accordingly. The WLST scripts mentioned above read from this file. You may need to change the path location of the domain.properties in the WLST scripts.

# Name of WebLogic servers instances; Each server name is delimited by a pipe; Order matters
startServerValues=AdminServer|oim_server1
stopServerValues=oim_server1|AdminServer

# Path to configuration file which contains an encrypted username and password
userConfigFileValue=/home/oracle/nodeManagerConfig/oracle-WebLogicConfig.properties

# Path to key file which contains the secret key used for encrypting and decrpyting the credential in the configuration file
userKeyFileValue=/home/oracle/nodeManagerConfig/oracle-WebLogicKey.properties

# Host name (Listen Address) of the OIM Node Manager
oimHostValue=localhost

# Port number of Node Manager
portValue=5556

# Name of the WebLogic domain that you want to manage.
domainNameValue=oim_domain

# Path of the domain directory to which you want to save the Node Manager secret file (nm_password.properties) and SerializedSystemIni.dat file.
domainDirValue=/home/oracle/Oracle/Middleware/user_projects/domains/oim_domain

# The Node Manager type
nmTypeValue=ssl

# Verbose mode
verboseModeValue=false 


Bash Script
The "oimNodeManager" script calls the WLST scripts. It also starts Node Manager on start up. Adjust the variables in the script accordingly. Place this file in the in the "/etc/init.d" directory. Give this script 750 permissions.

Execute the chkconfig command:
sudo /sbin/chkconfig --add oimNodeManager

Base on the chkconfig header defined in the "oimNodeManager", the script will execute on run levels 3 (Multi-user Mode with Networking) and 5 (Multi-user Mode with display manager). Also, the chkconfig add command will create symbolic links of the "oimNodeManager" scripts in the "/etc/rc.d/rc3.d" and  "/etc/rc.d/rc5.d" directory.


#!/bin/sh -x
# chkconfig: 35 99 10
# description: Service to start/stop Node Manager and WebLogic server instances

# Oracle Home Directory
ORACLE_HOME=/home/oracle

# Oracle Middleware Directory
MW_HOME=$ORACLE_HOME/Oracle/Middleware

# Linux user to start servers
ORACLE=oracle

# Path to stopWebLogicServers script
WLST_HELPER_STOP_SCRIPT=$ORACLE_HOME/stopWebLogicServers.py

# Path to startWebLogicServers script
WLST_HELPER_START_SCRIPT=$ORACLE_HOME/startWebLogicServers.py

# Lock file
lockfile=/var/lock/subsys/oimNodeManager

# Log File
logFile=/home/oracle/oimNodeManager.log

# Node Manager Port Number
nmPort=5556

case "$1" in
start) 
   # Check if the startNodeManager, the WLST script, and the WLST helper script exist
   if [ ! -f $MW_HOME/wlserver_10.3/server/bin/startNodeManager.sh -o ! -f $MW_HOME/wlserver_10.3/common/bin/wlst.sh -o ! -f $WLST_HELPER_START_SCRIPT ]
   then
      echo `date +%H:%M:%S` : "Startup scripts do not exist" >> $logFile
   exit
   fi

   # Start Node Manager if it is not running
   if ! netstat -atwn | grep "^.*:${nmPort}.*:\*\s*LISTEN\s*$"
   then
      echo `date +%H:%M:%S` : "Starting NodeManager" >> $logFile
      nohup su - $ORACLE -c $MW_HOME/wlserver_10.3/server/bin/startNodeManager.sh >> $logFile &
   fi

   echo `date +%H:%M:%S` : "Starting Servers" >> $logFile
   nohup su - $ORACLE -c "sh $MW_HOME/wlserver_10.3/common/bin/wlst.sh $WLST_HELPER_START_SCRIPT" >> $logFile &
   touch $lockfile
   ;;

stop) 
   echo `date +%H:%M:%S` : 'Stopping Servers' >> $logFile
   netstat -atwn | grep "^.*:${nmPort}.*:\*\s*LISTEN\s*$" >> $logFile
   
   #Check if the WLST script and the WLST helper script exist
   if [ ! -f $MW_HOME/wlserver_10.3/common/bin/wlst.sh -o ! -f $WLST_HELPER_STOP_SCRIPT ]
   then
      echo `date +%H:%M:%S` : "WLST script or WLST helper script does not exist" >> $logFile
      exit
   fi

   echo `date +%H:%M:%S` : "Stopping Servers and Node Manager" >> $logFile
   nohup su - $ORACLE -c "sh $MW_HOME/wlserver_10.3/common/bin/wlst.sh $WLST_HELPER_STOP_SCRIPT" >> $logFile &
   wait
   
   echo `date +%H:%M:%S` : 'After shutdown script executed' >> $logFile
   netstat -atwn | grep "^.*:${nmPort}.*:\*\s*LISTEN\s*$" >> $logFile
   rm -f $lockfile
   ;;

restart)
   /bin/sh $0 stop
   wait
   /bin/sh $0 start
   ;;

*)
   echo "usage: $0 (start|stop|restart)";;

esac

exit



1 comment:

  1. Hi This is a great post. I have tried this in my environment but have issues while invoking startWebLogicServers.py. When I include 'wait' in the service file (oimNodeManager) to complete the start operation, the wlst would not exit at all. But the stop operation is working perfectly fine with the same 'wait' command inside the service file (oimNodeManager). Any idea what could be the reason?

    Any help is much appreciated.

    Thanks

    ReplyDelete