Last year I created a set of
start and stop scripts for Weblogic/Fusion MiddleWare. Although I was quite happy with them at the time, I found that there was quite a lot of duplicate code. And I think I could improve them by combining at least the wlst scripts into one, making it a lot better maintainable, but also opening up for restart functionality. And doing so make the scripts more generic. I took my
(re-)start and stop script for OHS components as an example and came up with an improved set of start/stop scripts.
For the property files (fmw12_env.sh, fmw.properties, user key files, etc. ) I refer to
start and stop scripts for Weblogic/Fusion MiddleWare.
WLST
As said I created just one wlst script,
startStopDomain.py:
#############################################################################
# Start, Stop, Restart FMW Domain
#
# @author Martien van den Akker, Darwin-IT Professionals
# @version 1.1, 2017-04-20
#
#############################################################################
# Modify these values as necessary
import sys, traceback
scriptName = sys.argv[0]
#
#
lineSeperator='__________________________________________________________________________________'
#
#
def usage():
print 'Call script as: '
print 'Windows: wlst.cmd '+scriptName+' -loadProperties localhost.properties'
print 'Linux: wlst.sh '+scriptName+' -loadProperties environment.properties'
print 'Property file should contain the following properties: '
print "adminUrl=localhost:7001"
print "adminUser=weblogic"
print "adminPwd=welcome1"
#
#
def connectToNM():
try:
wlsDomainHome = wlsDomainsHome+'/'+wlsDomainName
print(lineSeperator)
print('Try to connect to the Node Manager')
try:
nmConnect(userConfigFile=usrCfgFile, userKeyFile=usrKeyFile, host=nmHost, port=nmPort, domainName=wlsDomainName, domainDir=wlsDomainHome, nmType=nmType)
except NameError, e:
print('Apparently user config properties usrCfgFile and usrKeyFile not set.')
print('Try to connect to the NodeManager adminUser and adminPwd properties')
nmConnect(username=adminUser, password=adminPwd, host=nmHost, port=nmPort, domainName=wlsDomainName, domainDir=wlsDomainHome, nmType=nmType)
print('Connected to the Node Mananger')
except WLSTException:
message='Apparently NodeManager not Started!'
print (message)
raise Exception(message)
#
# Start Admin Server
def startAdminServer(adminServerName):
# Set wlsDomainHome
print ('Connect to the Node Manager')
connectToNM()
print('Start AdminServer')
nmStart(adminServerName)
#
# Connect To the AdminServer
def connectToAdminServer(adminUrl, adminServerName):
try:
print(lineSeperator)
print('Try to connect to the AdminServer')
try:
connect(userConfigFile=usrCfgFile, userKeyFile=usrKeyFile, url=adminUrl)
except NameError, e:
print('Apparently user config properties usrCfgFile and usrKeyFile not set.')
print('Try to connect to the AdminServer adminUser and adminPwd properties')
connect(adminUser, adminPwd, adminUrl)
except WLSTException:
print('Apparently AdminServer not Started!')
startAdminServer(adminServerName)
print('Retry to connect to the AdminServer')
try:
connect(userConfigFile=usrCfgFile, userKeyFile=usrKeyFile, url=adminUrl)
except NameError, e:
print('Apparently user config properties usrCfgFile and usrKeyFile not set.')
print('Try to connect to the AdminServer adminUser and adminPwd properties')
connect(adminUser, adminPwd, adminUrl)
#
# Get the Name of the AdminServer of Domain
def getAdminServerName():
serverConfig()
cd('/')
adminServerName=cmo.getAdminServerName()
return adminServerName
#
# Get the Servers of Domain
def getDomainServers():
print(lineSeperator)
print('\nGet Servers from domain')
serverConfig()
cd("/")
servers = cmo.getServers()
return servers
#
# Get the Servers of Cluster
def getClusterServers(clustername):
#Cluster config to be fetched from ServerConfig
print(lineSeperator)
print('\nGet Servers from cluster '+clustername)
serverConfig()
cluster = getMBean("/Clusters/" + clustername)
if cluster is None:
errorMsg= "Cluster " + clustername + " does not appear to exist!"
print errorMsg
raise(Exception(errorMsg))
print "Found cluster "+ clustername+ "."
servers = cluster.getServers()
return servers
#
# Get the Domain Clusters
def getClusters():
#Cluster config to be fetched from ServerConfig
print(lineSeperator)
print('\nGet Clusters')
cd('/')
clusters = cmo.getClusters()
return clusters
#
# Get status of a server
def serverStatus(serverName):
serverRuntime=getMBean('/ServerRuntimes/'+serverName)
if serverRuntime is None:
print("Server Runtime for " + serverName + " not found, server apparently SHUTDOWN")
serverState="SHUTDOWN"
else:
print "Found Server Runtime for "+ serverName+ "."
serverState = serverRuntime.getState()
return serverState
#
# Start Server
# Expected to be in domainRuntime to get to the serverRuntimes.
def startMServer(serverName):
print(lineSeperator)
print('ServerName: '+serverName)
serverState = serverStatus(serverName)
print('Server '+serverName+': '+serverState)
if serverState=="SHUTDOWN":
print ('Server '+serverName+' is not running so start it.')
start(serverName,'Server')
elif serverState=="RUNNING":
print ('Server '+serverName+' is already running')
else:
print ('Server '+serverName+' in state '+serverState+', not startable!')
#
# Start servers in a cluster one by one.
def startMServers(serverList):
print(lineSeperator)
print ('Start servers from list: '+serverList)
servers=serverList.split(',')
# Need to go to domainRuntime to get to the serverRuntimes.
domainRuntime()
#
for serverName in servers:
startMServer(serverName)
#
print ('\nFinished starting servers.')
#
# Start servers in a cluster one by one.
def startClusterServers(clusterName):
print(lineSeperator)
print ('Start servers for cluster: '+clusterName)
servers=getClusterServers(clusterName)
# Need to go to domainRuntime to get to the serverRuntimes.
domainRuntime()
#
for server in servers:
serverName = server.getName()
startMServer(serverName)
#
print ('\nFinished starting servers.')
#
# Start servers in domain one by one.
def startDomainServers():
print(lineSeperator)
print ('Start servers for domain')
servers=getDomainServers()
adminServerName=getAdminServerName()
# Need to go to domainRuntime to get to the serverRuntimes.
domainRuntime()
#
for server in servers:
serverName = server.getName()
if (serverName!=adminServerName):
startMServer(serverName)
else:
print('Skip '+adminServerName)
#
print ('\nFinished starting servers.')
#
# Stop Server
# Expected to be in domainRuntime to get to the serverRuntimes.
def stopMServer(serverName):
print(lineSeperator)
print('ServerName: '+serverName)
serverState = serverStatus(serverName)
print('Server '+serverName+': '+serverState)
if serverState=="RUNNING":
print ('Server '+serverName+' is running so shut it down.')
shutdown(name=serverName, entityType='Server', force='true')
elif serverState=="SHUTDOWN":
print ('Server '+serverName+' is already down.')
else:
print ('Server '+serverName+' in state '+serverState+', but try to stop it!')
shutdown(name=serverName, entityType='Server', force='true')
#
# Stop servers in a list
def stopMServers(serverList):
print(lineSeperator)
print ('Stop servers from list: '+serverList)
servers=serverList.split(',')
# Need to go to domainRuntime to get to the serverRuntimes.
domainRuntime()
#
for serverName in servers:
stopMServer(serverName)
#
print ('\nFinished stopping servers.')
#
# Stop servers in a cluster one by one.
def stopClusterServers(clusterName):
print(lineSeperator)
print ('Stop servers for cluster: '+clusterName)
servers=getClusterServers(clusterName)
# Need to go to domainRuntime to get to the serverRuntimes.
domainRuntime()
#
for server in servers:
serverName = server.getName()
stopMServer(serverName)
#
print ('\nFinished stopping servers.')
#
# Stop servers in a domain one by one.
def stopDomainServers():
print(lineSeperator)
print ('Stop servers for domain')
servers=getDomainServers()
adminServerName=getAdminServerName()
# Need to go to domainRuntime to get to the serverRuntimes.
domainRuntime()
#
for server in servers:
serverName = server.getName()
if (serverName!=adminServerName):
stopMServer(serverName)
else:
print('Skip '+adminServerName)
#
print ('\nFinished stopping servers.')
#
# Start cluster
def startCluster(clusterName):
print(lineSeperator)
print ('Start cluster: '+clusterName)
try:
start(clusterName,'Cluster')
except WLSTException:
print "Apparently Cluster in incompatible state!", sys.exc_info()[0], sys.exc_info()[1]
startClusterServers(clusterName)
state(clusterName,'Cluster')
#
print ('\nFinished starting cluster '+clusterName)
#
# Start clusters in a list
def startClusters(clusterList):
print(lineSeperator)
print ('Start clusters from list: '+clusterList)
clusters=clusterList.split(',')
#
for clusterName in clusters:
startCluster(clusterName)
#
print ('\nFinished starting clusters.')
#
# Start clusters
def startDomainClusters():
print(lineSeperator)
print ('Start clusters')
clusters=getClusters()
#
for cluster in clusters:
clusterName = cluster.getName()
startCluster(clusterName)
#
print ('\nFinished starting clusters.')
#
# Stop cluster
def stopCluster(clusterName):
print(lineSeperator)
print ('Stop cluster: '+clusterName)
try:
#shutdown(clusterName,'Cluster')
shutdown(name=serverName, entityType='Cluster', force='true')
state(clusterName,'Cluster')
except WLSTException:
print "Apparently Cluster in incompatible state!", sys.exc_info()[0], sys.exc_info()[1]
state(clusterName,'Cluster')
print ('Try to stop servers for cluster: '+clusterName+', one by one')
stopClusterServers(clusterName)
#
print ('\nFinished stopping cluster '+clusterName)
#
# Stop clusters in a list
def stopClusters(clusterList):
print(lineSeperator)
print ('Stop clusters from list: '+clusterList)
clusters=clusterList.split(',')
#
for clusterName in clusters:
stopCluster(clusterName)
#
print ('\nFinished stopping clusters.')
#
# Stop all clusters of the domain
def stopDomainClusters():
print(lineSeperator)
print ('Stop clusters')
clusters=getClusters()
#
for cluster in clusters:
clusterName = cluster.getName()
stopCluster(clusterName)
#
print ('\nFinished stopping clusters.')
#
# StopAdmin
def stopAdmin():
print(lineSeperator)
print('Stop '+adminServerName)
shutdown(force='true')
serverState = serverStatus(serverName)
print('State '+adminServerName+': '+ serverState)
print('\nFinished stopping AdminServer: '+adminServerName)
#
# Start admin Server
def startAdmin():
print(lineSeperator)
print('Start and connect to '+adminServerName)
connectToAdminServer(adminUrl, adminServerName)
print('\nFinished starting AdminServer: '+adminServerName)
#
# Stop a domain
def stopDomain():
print (lineSeperator)
stopDomainClusters()
#
stopAdmin()
print ('\nFinished stopping domain.')
#
#
# Start a Domain
def startDomain():
print (lineSeperator)
#
startAdmin()
#
startDomainClusters()
#
print ('\nFinished starting domain.')
#
#
# (Re-)Start or Stop Domain
def startStopDomain(startStopOption):
if startStopOption=="stop" or startStopOption=="restart":
stopDomain()
if startStopOption=="start" or startStopOption=="restart":
startDomain()
#
# (Re-)Start or Stop Admin
def startStopAdmin(startStopOption):
if startStopOption=="stop" or startStopOption=="restart":
stopAdmin()
if startStopOption=="start" or startStopOption=="restart":
startAdmin()
#
# (Re-)Start or Stop Clusters
def startStopClusters(startStopOption, clusterList):
if startStopOption=="stop" or startStopOption=="restart":
if clusterList is None:
stopDomainClusters()
else:
stopClusters(clusterList)
if startStopOption=="start" or startStopOption=="restart":
if clusterList is None:
startDomainClusters()
else:
startClusters(clusterList)
#
# (Re-)Start or Stop Servers
def startStopServers(startStopOption, serverList):
if startStopOption=="stop" or startStopOption=="restart":
if serverList is None:
stopDomainServers()
else:
stopMServers(serverList)
if startStopOption=="start" or startStopOption=="restart":
if serverList is None:
startDomainServers()
else:
startMServers(serverList)
#
# Main
def main():
try:
print ('Args passed: '+ str(len(sys.argv) ))
componentList = None
idx = 0
for arg in sys.argv:
if idx == 0:
print 'ScriptName: '+arg
elif idx == 1:
# startStopOption: start, stop, restart
startStopOption=arg
elif idx == 2:
# componentType: AdminSvr, Domain, Clusters, Servers
componentType=arg
elif idx == 3:
componentList=arg
else:
componentList = componentList+','+arg
idx=idx+1
#
#componentName=sys.argv[3]
# Set wlsDomainHome
wlsDomainHome = wlsDomainsHome+'/'+wlsDomainName
#
print(lineSeperator)
if componentList is None:
componentStr = componentType
else:
componentStr = componentType+' '+componentList
if startStopOption=="start" or startStopOption=="restart":
print('(Re)Start '+componentStr+' in '+wlsDomainHome+', with option '+startStopOption)
elif startStopOption=="stop":
print('Stop '+componentStr+' in '+wlsDomainHome+', with option '+startStopOption)
else:
raise Exception("Unkown startStopOption: "+ startStopOption)
#
print(lineSeperator)
print('\nConnect to the AdminServer: '+adminServerName)
connectToAdminServer(adminUrl, adminServerName)
#
print('Connected, so proceed with: '+startStopOption+' of '+componentType)
#
if componentType=="AdminSvr":
startStopAdmin(startStopOption)
elif componentType=="Domain":
startStopDomain(startStopOption)
elif componentType=="Clusters":
startStopClusters(startStopOption, componentList)
elif componentType=="Servers":
startStopServers(startStopOption, componentList)
else:
raise Exception('Undefined componentType: '+componentType);
#
print('\nExiting...')
exit()
except NameError, e:
print('Apparently properties not set.')
print "Please check the property: ", sys.exc_info()[0], sys.exc_info()[1]
usage()
except:
apply(traceback.print_exception, sys.exc_info())
exit(exitcode=1)
#call main()
main()
exit()
The script can be called with two parameters and a property file:
- startStopOption: start, stop, restart
- component: AdminSvr, Domain, Clusters, Servers
Based on the value of component the start, stop or restart option is issued on either the AdminServer, the Domain clusters, servers or the complete domain. The main function starts with connecting to the AdminServer. If the AdminServer is not started yet, it will connect to the NodeManager to start the AdminServer.
In case of a restart or a stop the particular components are stopped. And in case of a start or restart the particular components are started.
The advancement of this script, in contrast with other start/stop scripts I've seen is that it will give a start command to a cluster. Doing so, all the servers in a cluster will get a start command simultaneously. For stopping the server in a cluster, the WLSException that can be raised in the stop command is caught, to try to stop try to stop the servers one by one.
Another advancement of this script is that is determined which clusters there are in a domain, and these are traversed one by one.
Update 2017-04-20: Today I added also the possiblilty to start a list of clusters or servers. If you don't provide a list of clusters or servers, all the clusters or servers in the domain are (re-)started or stopped. Except for the AdminServer, by the way.
Bash
To call the wlst script I created a set of bash scripts.
To start the AdminServer, startAdmin.sh:
#!/bin/bash
#############################################################################
# Start AdminServer using wlst and connect to it.
#
# @author Martien van den Akker, Darwin-IT Professionals
# @version 2.2, 2016-04-18
#
#############################################################################
#
. fmw12c_env.sh
echo
echo Start AdminServer
wlst.sh ./startStopDomain.py start AdminSvr -loadProperties fmw.properties
To stop or restart the AdminServer, replace
start in 'wlst.sh ./startStopDomain.py start AdminSvr' by either
stop or
restart. And save the file to either
stopAdmin.sh or
restartAdmin.sh.
To create the corresponding files for
Domain or
Clusters replace
AdminSvr in the same line with either
Domains or
Clusters.
I think you'll get the drift. And of course you should adapt the comments and echo's.
But maybe a more generic (re-)start or stop script is startStopDmn.sh:
#!/bin/bash
#############################################################################
# Start Domain components using wlst
#
# @author Martien van den Akker, Darwin-IT Professionals
# @version 1.0, 2017-04-19
#
#############################################################################
#
. fmw12c_env.sh
export START_STOP_OPTION=$1
export COMPONENT_TYPE=$2
export ENV=$3
export COMPONENT_NAME=$4
echo
echo "(Re-)Start or stop Domain components"
wlst.sh ./startStopDomain.py ${START_STOP_OPTION} ${COMPONENT_TYPE} "${COMPONENT_NAME}" -loadProperties ${ENV}.properties
Then you could do a
startAdmin.sh like:
./startStopDmn.sh start AdminSvr fmw
And a
stopAdmin.sh like:
./startStopDmn.sh stop AdminSvr fmw
Or a
restartServers.sh like:
./startStopDmn.sh restart Servers fmw $1
Here you can provide a comma-separated list of servers, between double-quotes:
./restartServers "soa_server1,soa_server2"
etc. etc.
Advantage of this approach is that it allows you to use the same script set for multiple environemts, by duplicating and adapting the
fmw.properties file to other environments (like domains).