May 3, 2017

Starting and Stopping Domains Using WLST - The Easy Way

Yes - another post 'how to start/stop a domain using WLST' ... But, with this scripts You only have to provide information about the nodemanager and the AdminServer. All other information used to start the domain is collected dynamically during runtime.

The tool will start or stop the nodemanager (domain- or host-based), the AdminServer and each ManagedServer. Before starting or stopping a component a check is done if the operation in question is possible or not. Important steps will display its result. Green if it's an ok message, red if not. A simple 'tee' command can be used to create a log file with all necessary information about the start / stop process.

The tool consists of three files
  • a parameter file which sets / defines the environment for a specific domain
  • a shell script which initiates the start / stop process
  • a python script which starts / stops the domain



Assumptions:
- The domain, in our example, is named 'DemoDomain'
- The domain's boot.properties exists
- The OS is a Linux based System (with an appropriate env- and a cmd file instead of a shell script, the tool can be used in Windows Server environments as well)

The parameter file:

Naming convention in my environments is 'set_<DomainName>_Env.sh'  - for example 'set_DemoDomain_Env.sh'. That makes it easier to set a specific domain environment in multi domain systems.

# ScriptHome
export SCRIPT_HOME=/home/ofmw/scripts/startDomain

# Paths
export ORACLE_BASE=/appl/oracle
export ORACLE_HOME=$ORACLE_BASE/product/fmwi12212
export WLST_HOME=$ORACLE_HOME/oracle_common/common/bin
export DOMAIN_BASE=$ORACLE_BASE/user_projects/domains

# Domain specific
export DOMAIN_NAME=DemoDomain

# AdminServer
export AS_NAME=AS_$DOMAIN_NAME
export AS_ADM_USER=weblogic
export AS_ADM_PWD=welcome1
export AS_PROTOCOL=t3
export AS_HOSTNAME=`hostname -f`
export AS_ADMINPORT=7001 
export AS_URL=$AS_PROTOCOL'://'$AS_HOSTNAME':'$AS_ADMINPORT

# NodeManager
export NM_LISTENADDRESS=localhost
export NM_DIR=$DOMAIN_BASE/$DOMAIN_NAME/nodemanager
export NM_PROPFILE=$NM_DIR/nodemanager.properties
export NM_TYPE=ssl
export NM_PORT=5556
export NM_USERNAME=nodemanager
export NM_USERPWD=welcome1

Hint: instead of writing username and password into a file you could (should) use the storeUserConfig feature

The Shell Script

Naming convention in my environments is simply 'startDomain.sh'  

#!/bin/bash
#=====================================================================
# Name: startDomain.sh
#
# PURPOSE: Script to start Nodemanager and the whole domain.
#          Be aware that comonents are started, in case they are down. 
#
# PARAMETERS: none, all required parameters are set in set_<DomainName>_Env.sh
#
# AUTHOR:  Robert Crames, May 2017
#
#
#=====================================================================
# set -x

source $PWD/set_DEV_CI_Env.sh

echo "======================================================================================"
echo " Program  : startDomain.sh                                                    ........"
echo "======================================================================================"

if [ -z "${WLST_HOME}" ]; then
    echo "Environment not correctly set - please verify"
    exit 1
fi

# In case we are facing problems with /dev/random
export CONFIG_JVM_ARGS=-Djava.security.egd=file:/dev/./urandom:$CONFIG_JVM_ARGS

${WLST_HOME}/wlst.sh ${SCRIPT_HOME}/start_stop_domains.py $1

Hint: this example is not using the domain's name as an argument to find the appropriate environment file (set_<DomainName>_Env.sh). Feel free to add this feature

The Python Script

Naming convention in my environments is 'start_stop_domains.py'.

# imports
import os 
import sys
import getopt

# Read environment variables
# Common variables
v_domainName=os.environ['DOMAIN_NAME']

# AdminServer related Variables
v_asName=os.environ['AS_NAME']
v_asUrl=os.environ['AS_URL']
v_asAdmUser=os.environ['AS_ADM_USER']
v_asAdmPwd=os.environ['AS_ADM_PWD']

# Nodemanager related Variables
v_nmListenAddress=os.environ['NM_LISTENADDRESS']
v_nmDir=os.environ['NM_DIR']
v_nmPropFile=os.environ['NM_PROPFILE']
v_nmUserName=os.environ['NM_USERNAME']
v_nmUserPwd=os.environ['NM_USERPWD']
v_nmType=os.environ['NM_TYPE']
v_nmPort=os.environ['NM_PORT']

# Functions
def printHeader(headerText):
    print "\n======================================================================================"
    print "--> "+headerText
    print "======================================================================================\n"

def printInfo(infoText):
    print "-->: "+infoText

def printInfoRed(infoText):
    print "-->: \033[1;31m"+infoText+"\033[0m"

def printInfoGreen(infoText):
    print "-->: \033[1;32m"+infoText+"\033[0m"

def checkSyntax():
  if startStopFlag != 'start' and startStopFlag != 'stop':
    printInfo('usage: ' + sys.argv[0] + ' [start|stop]')
    exit()

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

def start_stop_NodeManager():
  try:
    connectNodeManager()
    printInfo("NodeManager has already been started")
    if startStopFlag == 'stop':
      try:
        stopNM()
        printInfoGreen("NodeManager has been stopped")
      except:
        printInfoRed("NodeManager could not be stopped")
  except:
    printInfo("Node Manager is not running")
    if startStopFlag == 'start':
      try:
        startNM()
        connectNodeManager()
        printInfoGreen("Start NodeManager successful")
      except:
        printInfoRed("NodeManager could not be started ... check NodeManager's Logfiles")
        exit()

def start_stop_AdminServer():
  try:
    connectAdminServer()
    printInfo("AdminServer " + v_asName + " has already been started")
    if startStopFlag == 'stop':
      try:
        connectNodeManager()
        stopAdminserver()
        printInfoGreen("Stop AdminServer " + v_asName + " successful")
      except:
        printInfoRed("AdminServer " + v_asName + " could not be stopped")
  except:
    printInfo("AdminServer " + v_asName + " is not running")
    if startStopFlag == 'start':
      try:
        startAdminserver()
        printInfoGreen("Start AdminServer " + v_asName + " successful")
        nmDisconnect()
        try:
          connectAdminServer()
        except:
          printInfoRed("AdminServer " + v_asName + " could not be connected")
          exit()
      except:
        printInfoRed("AdminServer " + v_asName + " could not be started")
        exit()


def connectNodeManager():
  nmConnect(v_nmUserName,v_nmUserPwd,v_nmListenAddress,v_nmPort,v_domainName)

def connectAdminServer():
  connect(v_asAdmUser,v_asAdmPwd,v_asUrl)

def startAdminserver():
  nmStart(v_asName)

def startNM():
  startNodeManager(NodeManagerHome=v_nmDir, PropertiesFile=v_nmPropFile, verbose=false)

def stopAdminserver():
  nmKill(v_asName)

def stopNM():
  stopNodeManager()

def start_stop_ManagedServers():
  serverList = cmo.getServers()
  domainRuntime()
  for server in serverList:
    if server.getName() != v_asName:
      serverState = getServerStatus(server)
      serverConfig()
      machine = server.getMachine();
      printInfo(server.getName() + " on " + machine.getName() + " is currently in state: " + serverState)
      domainRuntime()
      if serverState == "RUNNING" and startStopFlag == 'stop':
        try:
          shutdown(server.getName(),'Server')
          serverState = getServerStatus(server)
          printInfoGreen("Stop successful: " + server.getName() + " on " + machine.getName() + " is in state: " + serverState)
        except:
          printInfoRed("Unable to start server " + server.getName() + ". Check server log files")
      if serverState != "RUNNING" and startStopFlag == 'start':
        try:
          start(server.getName(),'Server')
          serverState = getServerStatus(server)
          printInfoGreen("Start successful: " + server.getName() + " on " + machine.getName() + " is in state: " + serverState)
        except:
          printInfoRed("Unable to start server " + server.getName() + ". Check server log files")

# MAIN
if len(sys.argv) > 1:
  startStopFlag=sys.argv[1]
else:
  startStopFlag='Error'

checkSyntax() 

if startStopFlag == 'start':
  printInfoGreen('Currently in start tree')
  start_stop_NodeManager()
  start_stop_AdminServer()
  start_stop_ManagedServers()

if startStopFlag == 'stop':
  printInfoRed('Currently in stop tree')
  try:
    connectAdminServer()
    start_stop_ManagedServers()
    disconnect()
  except:
    printInfoRed("AdminServer " + v_asName + " could not be connected")
    exit()
  start_stop_AdminServer()
  start_stop_NodeManager()

disconnect()
exit()

Share:

1 comment:

Copyright © Robert Crames' Oracle Database and Middleware Blog | Powered by Blogger
Design by SimpleWpThemes | Blogger Theme by NewBloggerThemes.com