Saturday, 13 August 2016

Automating weblogic server instance startup and shutdown


Overview

WebLogic server administration involves starting and stopping of WebLogic instances several times. This is repetitive task and involves manually running the individual scripts or going through the GUI (Admin Console) starting up each server. Automating this task will save lot or administrator work and time.

WLST

WLST stands for weblogic scripting tool.  The WebLogic Scripting Tool (WLST) is a command-line scripting interface that system administrators and operators use to monitor and manage WebLogic Server instances and domains.
WLST is a command-line interpreter that interprets commands either interactively, supplied one-at-a-time from a command prompt, or in batches, supplied in a file (script), or embedded in your Java code. The modes of operation represent methods for issuing WLST commands:
  • Interactively, on the command line—Interactive Mode
  • In a text file—Script Mode
  • Embedded in Java code—Embedded Mode
The Script Mode is based on the Java scripting interpreter, Jython and allows common features of interpreted languages, including local variables, conditional variables, and flow control statements in addition to WLST commands.
We will be using Script Mode to automate the WebLogic server startup and shutdown


Assumption

For the following script to work the following assumptions are made.
1.       We assume that a domain is created on weblogic server with following servers
1.       AdminServer
2.       UCM_server1
3.       IPM_server1
4.       osb_server1
5.       soa_server1
6.       iui_server1
2.       As the script uses node manager to either start or stop domain of servers, please ensure the domain is Node manager enabled.

3.       If the domain of servers has been created and managed servers are not started with WLST(after connecting to running Admin server) at least once, node manager properties(that are used by node manager to either start or stop servers) will not be created at ../servers/<ServerName>/data/nodemanager. So Node manager utility will not able to start managed servers.

4.       As the script is properties file driven, ensure properties are updated according to respective environment.


StartServers.py

<SNIP>
from java.io import File
from java.io import FileInputStream
from java.util import Properties
import time

# Reading properties file

inStream = FileInputStream('/home/oracle/Prajyot/serversProps_poc.properties');
propFil = Properties();
propFil.load(inStream);

serverStatus=' ';
serverName=' ';

# Retrieving values from properties file

count=propFil.getProperty('count');
nmHome=propFil.getProperty('nmHome');
nmPort=propFil.getProperty('nmPort');
nmListenAddress=propFil.getProperty('nmListenAddress');
nmVerboseMode=propFil.getProperty('nmVerboseMode');
uname=propFil.getProperty('Username');
pwd=propFil.getProperty('Password');
hostValue=propFil.getProperty('hostValue');
portValue=propFil.getProperty('portValue');
domainNameValue=propFil.getProperty('domainNameValue');
domainDirValue=propFil.getProperty('domainDirValue');
nmTypeValue=propFil.getProperty('nmTypeValue');
verboseModeValue=propFil.getProperty('verboseModeValue');

print 'Invoking startServers.py script';

try:
                #Attempt to connect to Node Manager. If fail to connect, start Node Manager.

try:        
nmConnect(uname,pwd,hostValue,portValue,domainNameValue,domainDirValue,nmTypeValue,verboseModeValue);

except:
startNodeManager(verbose=nmVerboseMode, NodeManagerHome=nmHome, ListenPort=nmPort, ListenAddress=nmListenAddress);
print 'Attempting to connect to Node Manager.';

nmConnect(uname,pwd,hostValue,portValue,domainNameValue,domainDirValue,nmTypeValue,verboseModeValue);

i=1;
isConnectedTonm=nm()
while isConnectedTonm and i<=count:
serverName=propFil.getProperty('server'+str(i));
print serverName +'\'s status';
serverStatus=nmServerStatus(serverName);                    
if not serverStatus == 'RUNNING':
nmStart(serverName);
time.sleep(60);                
i=i+1;

finally:
#Disconnect from Node Manager

nmDisconnect();
exit();
print 'End startServers.py script';
</SNIP>


Explanation

This Script is used to start the Node Manager, AdminServer and Managed Servers in the following order
5.       AdminServer
6.       UCM_server1
7.       IPM_server1
8.       osb_server1
9.       soa_server1
10.   iui_server1
The values required by the script are provided through properties file.
The script goes through in the following manner
1.       It first tries to connect to the Node Manager. It uses the following command
nmConnect([username, password], [host], [port], [domainName], [domainDir] [nmType], [verbose])
2.       If Node Manager is not started then first start the Node Manager using following command.
startNodeManager([verbose], [NodeManagerHome], [ListenPort], [ListenAddress])
3.       Check if Node Manager is connected using the following command.
nm()
4.       If connected to Node Manager , Check the status of each Server using the following command.
nmServerStatus([serverName])
If the status of server is not RUNNING then start the server using the following command.
nmStart([serverName])
This step goes on till the count = number of servers.

5.       Disconnect WLST from the Node Manager  session using the following command.
nmDisconnect()
6.       Exit WLST from the user session and close the scripting shell.
exit()
For more reference on WLST commands refer the link below


StopServers.py

<SNIP>
from java.io import File
from java.io import FileInputStream
from java.util import Properties
import time

# Reading properties file

inStream = FileInputStream('/home/oracle/Prajyot/serversProps_poc.properties');
propFil = Properties();
propFil.load(inStream);

serverStatus=' ';
serverName=' ';

# Retrieving values from properties file

count=propFil.getProperty('count');
nmHome=propFil.getProperty('nmHome');
nmPort=propFil.getProperty('nmPort');
nmListenAddress=propFil.getProperty('nmListenAddress');
nmVerboseMode=propFil.getProperty('nmVerboseMode');
uname=propFil.getProperty('Username');
pwd=propFil.getProperty('Password');
hostValue=propFil.getProperty('hostValue');
portValue=propFil.getProperty('portValue');
domainNameValue=propFil.getProperty('domainNameValue');
domainDirValue=propFil.getProperty('domainDirValue');
nmTypeValue=propFil.getProperty('nmTypeValue');
verboseModeValue=propFil.getProperty('verboseModeValue');

print 'Invoking startServers.py script';

try:
                #Attempt to connect to Node Manager. If fail to connect, start Node Manager.

try:        
nmConnect(uname,pwd,hostValue,portValue,domainNameValue,domainDirValue,nmTypeValue,verboseModeValue);

except:
startNodeManager(verbose=nmVerboseMode, NodeManagerHome=nmHome, ListenPort=nmPort, ListenAddress=nmListenAddress);
print 'Attempting to connect to Node Manager.';

nmConnect(uname,pwd,hostValue,portValue,domainNameValue,domainDirValue,nmTypeValue,verboseModeValue);

isConnected=nm();
i=6;
while isConnected and i>=1:
serverName=propFil.getProperty('server'+str(i));
print serverName+'\'s status:';
serverStatus=nmServerStatus(serverName);
if not serverStatus=='SHUTDOWN':
nmKill(serverName);
time.sleep(60);
i=i-1;

finally:
#Disconnect from Node Manager

nmDisconnect();
exit();
print 'End startServers.py script';
<SNIP>


Explanation

This script is used to stop weblogic server instances in the following order.
1.       iui_server1
2.       soa_server1
3.       osb_server1
4.       IPM_server1
5.       UCM_server1
6.       AdminServer

The values required by the script are provided through properties file.
The script goes through in the following manner

1.       It first tries to connect to the Node Manager. It uses the following command
nmConnect([username, password], [host], [port], [domainName], [domainDir] [nmType], [verbose])
2.       If Node Manager is not started then first start the Node Manager using following command.
startNodeManager([verbose], [NodeManagerHome], [ListenPort], [ListenAddress])
3.       Check if Node Manager is connected using the following command.
nm()
4.       If connected to Node Manager, Check the status of each Server using the following command.
nmServerStatus([serverName])
If the status of server is not SHUTDOWN then shut down the server using the following command.
nmKill([serverName])
This step goes on till the count = number of servers.

5.       Disconnect WLST from the Node Manager session using the following command.
nmDisconnect()
6.       Exit WLST from the user session and close the scripting shell.
exit()


Executing Script

1.       Change to bin directory under domain directory
Ex :$cd  /u07/oracle/middleware/user_projects/domains/wci_domain/bin

2.       Set Domain Environment.
Ex : $. ./setDomainEnv.sh
Note : Please ensure a single space between two dots(fist dot represents current session of putty and second dot represents current directory)

3.       Launch WLST by passing script file name as an argument

Ex: $java weblogic.WLST stopServers.py

No comments:

Post a Comment