Monday, March 21, 2016

Oracle WebLogic Start/Stop scripts by WLST

Any comments on this are more that welcome.
Please give me any hints for improve them.

The scripts have been tested on WebLogic 12.2.1 and WebLogic 10.3.6. That's why the way of execute them are different from version to version.

The script file are made of:
- config.py - this is the config file;
- server.py - this is the main Pyton file.

1. The content of "config.py" file is:

#we intend to redirect the exception on this file
logfile='server.log'
#the username for conecting on WebLogic
username='weblogic'
#password for connecting on WebLogic
password='Weblogic123'
#the WebLogic domain home
domainHome='C:\\oracle\MW12c\\user_projects\\domains\\12c_domain'
#the WebLogic domain name
domainName='wcp_domain'
#the WebLogic Node Manager port
nmPort='5556'
#the nodemanager connection type
nmType='SSL'
#the name of the WebLogic AdminServer from your domain
adminName='AdminServer'
#the host for WebLogic AdminServer
hostName='192.168.1.100'
#the admin t3 url
admurl="t3://"+hostName+":7001"

2. The content of "server.py" file is:

################################################## 
# Catalin Prusu 17.03.2016
##################################################
#set PATH=D:\Java\jdk1.7.0_55\bin;%PATH%
#set CLASSPATH=D:\DEVELOPMENT\MW12c\wlserver\server\bin;%PATH%
#D:\DEVELOPMENT\MW12c\wlserver\server\bin\setWLSEnv.cmd
#java weblogic.WLST server.py start|stop|status


def conn():
 import config
 try:
  connect(config.username,config.password,config.admurl)
 except:
  import config
  StartAdminServer()
  connect(config.username,config.password,config.admurl)

def nmconn():
 import config
 try:
  nmConnect(config.username, config.password, config.hostName, config.nmPort, config.domainName, config.domainHome, config.nmType)
  print 'Connected to node manager !'
 except ConnectionException,e:
  print 'Unable to connect to nodemanager ...'
  exit()

def StartAdminServer():
 import config
 nmconn()
 print 'Starting server ' + config.adminName + ' ...'
 nmStart(config.adminName)
 nmDisconnect()
 print 'Server ' + config.adminName + ' is : RUNNING'

def StopAdminServer():
 import config
 nmconn()
 nmKill(config.adminName)
 nmDisconnect()
 print 'Server ' + config.adminName + ' is : SHUTDOWN'

def getServerStatus(server):
 cd('/ServerLifeCycleRuntimes/' + server.getName() )
 return cmo.getState()

def StartServers():
 import config
 print 'Starting servers from domain: ' + config.domainName
 for server in servers:
  if server.getName() != config.adminName:
   serverState = getServerStatus(server)
   if serverState == "RUNNING":
    print 'Server ' + server.getName() + ' is : ' + serverState
   elif serverState == "STARTING":
    print 'Server ' + server.getName() + ' is : ' + serverState
   elif serverState == "SHUTDOWN":
    print 'Starting server ' + server.getName()
    start(server.getName(),'Server')
    print 'Server ' + server.getName() + ' is : ' + getServerStatus(server)
   else:
    print 'Server ' + server.getName() + ' is : ' + serverState
  else:
   print 'Server ' + server.getName() + ' is : ' + getServerStatus(server)

def StopServers():
 import config
 print 'Shutting down servers from domain: ' + config.domainName
 for server in servers:
  if server.getName() != config.adminName:
   serverState = getServerStatus(server)
   if serverState == "SHUTDOWN":
    print 'Server ' + server.getName() + ' is already : ' + serverState
   else:
    print 'Shutting down server ' + server.getName() + '...'
    shutdown(server.getName(),'Server')
    print 'Server ' + server.getName() + ' is : ' + getServerStatus(server)
 StopAdminServer()
    
def StatusServers():
 import config
 print 'Fetching state of every server from domain: ' + config.domainName
 for server in servers:
  serverState = getServerStatus(server)
  if serverState == "RUNNING":
   print 'Server ' + server.getName() + ' is : ' + serverState
  elif serverState == "STARTING":
   print 'Server ' + server.getName() + ' is : ' + serverState
  elif serverState == "UNKNOWN":
   print 'Server ' + server.getName() + ' is : ' + serverState
  else:
   print 'Server ' + server.getName() + ' is : ' + serverState   

def quit():
    disconnect()
    stopRedirect()
    exit()
 
if __name__ == "main":
 import config
 if len(sys.argv[1:]) == 0:
  print '>> Please send the verb like start|stop|status'
  exit()
 if len(sys.argv[1:]) > 1:
  print '>> The permited arguments are only start|stop|status'
  exit()
 redirect(config.logfile, 'false')
 if sys.argv[1:][0] != 'startadmin':
  conn()
  servers = cmo.getServers()
  domainRuntime()
 if sys.argv[1:][0] == 'status':
  StatusServers()
 elif sys.argv[1:][0] == 'start':
  StartServers()
 elif sys.argv[1:][0] == 'stop':
  StopServers()
 elif sys.argv[1:][0] == 'startadmin':
  StartAdminServer()
 elif sys.argv[1:][0] == 'stopadmin':
  StopAdminServer()
 quit()

3. Execute the "server" pyton file.
3.1. On WebLogic 12.2.1

- We have to set the following environment variables:
set PATH=C:\oracle\Java\jdk1.8.0_65\bin;%PATH%
set CLASSPATH=C:\oracle\MW12c\wlserver\server\bin;%PATH%
C:\oracle\MW12c\wlserver\server\bin\setWLSEnv.cmd

- Execute the "server.py" file by:

#for getting status
java weblogic.WLST server.py status

#for starting only the AdminServer
java weblogic.WLST server.py startadmin

#for stoping only the AdminServer
java weblogic.WLST server.py startadmin

#for starting all managed servers including the AdminServer
java weblogic.WLST server.py start

#for stoping all managed servers including the AdminServer
java weblogic.WLST server.py stop


3.2. On WebLogic 10.3.6
The problem on WebLogic 10.3.6 is that the "import config" from "server.py" file does not working. That's why the config file has to be loaded as property file.
- Execute the "server.py" file by:

#for getting status
%MW_HOME%\common\bin\wlst.cmd -loadProperties config.py server.py status

#for starting only the AdminServer
%MW_HOME%\common\bin\wlst.cmd -loadProperties config.py server.py startadmin

#for stoping only the AdminServer
%MW_HOME%\common\bin\wlst.cmd -loadProperties config.py server.py startadmin

#for starting all managed servers including the AdminServer
%MW_HOME%\common\bin\wlst.cmd -loadProperties config.py server.py start

#for stoping all managed servers including the AdminServer
%MW_HOME%\common\bin\wlst.cmd -loadProperties config.py server.py stop

DOWNLOAD SCRIPTS

12c_startadmin.cmd
12c_stopadmin.cmd
12c_start.cmd
12c_stop.cmd
12c_status.cmd
config.py
server.py
env.cmd