nmConnect(userConfigFile, userKeyFile, host, port, domainName, domainDir, nmType, verbose)
- userConfigFile - The path to the user config file. Refer to the storeUserConfig() WLST function for further information.
- userKeyFile - The path to the user key file. Refer to storeUserConfig() WLST function for further information.
- host - The listen address for the NodeManager. This is available in the domain configuration. We simply need to know which Machine definition we should reference. If we know which server we are interested in controlling then we can look up the associated Machine definition.
- port - the listen port for the NodeManager. Again this is available in the domain configuration and again we can use a reference to a Machine definition.
- domainName - The name for the domain. Again this is available in the domain configuration.
- domainDir - The directory for the domain. Ok we do need this one.
- nmType - The "type" of the NodeManager connection. Typically either "plain" or "ssl". This can be derived from the domain configuration. If a value has not been specified then the default of "ssl" should be assumed. Again we can use the reference to a Machine definition.
- verbose - Whether or not the NodeManager should use verbose messages. We will ignore this for our purposes and always assume a value of "false".
Therefore, assuming that we have access to the domain configuration in a given domain directory we should be able to derive many of the required parameters and simplify the nmConnect to be something a bit more user-friendly. We introduce a function nmConnectForServer which
nmConnectForServer(domainDir, serverName)
- domainDir - The directory containing the domain configuration.
- serverName - The name of the Managed Server whose Machine definition we want reference to determine our NodeManager connection parameters.
Once we have the WLST function we can flesh out a bit of a script that uses this function and takes appropriate arguments to allow us to control the server and view its status with the nmStart(), nmKill() and nmStatus() built-in WLST functions.
The script can then be invoked
wlst.sh nmServerControl.py start | stop | status
The advantages of this approach is that the domain configuration is used to determine the connection parameters rather then coding these parameters into a script or other configuration file. Should they change the start script will not have to be altered. Another advantage is the script becomes environment agnostic.
The following code defines such a WLST script:
# -----------------------------------------------------------------------------
# Control a WebLogic server with NodeManager
#
# @arg 1 action One of "start", "stop" or "status"
# @arg 2 domainDir Path to the domain directory
# @arg 3 serverName The name of the server to start
#
# -----------------------------------------------------------------------------
# Author: weblogically.blogspot.com
# Version: 1.0a
# Date: 18/07/2012
# Tested on: 11g PS5 11.1.1.5
# -----------------------------------------------------------------------------
# This WLST script controls servers by connecting to and issuing commands
# directly to the NodeManager. As such the NodeManager must be running but
# there is no requirement for the AdminServer to be running.
#
# The builtin WLST function 'nmConnect' requires parameters that are readily
# available in the domain configuration. This script provides an alternative
# function 'nmConnectForServer' which reads the domain to locate the specific
# parameters values for the given domain/server and passes these to nmConnect
# thereby significantly reducing the number of parameters that must be
# manually passed in to start a server.
#
# This does function requires that the domain directory to readable.
#
# This script does not try to set the server start properties so the server
# should have been started via the AdminServer at least once beforehand. This
# should have created a server start properties file that will be then used.
#
# This script does require previously stored the security credentials in the
# appropriate config and key files (see storeUserConfig)
# -----------------------------------------------------------------------------
import sys
# @function: nmConnectForServer
#
# Connects to the NodeManager associated with the specified NodeManager
#
# @param domainDir The path to the domain directory
# @param serverName The name of the server
#
def nmConnectForServer(domainDir, serverName):
readDomain(domainDir)
domainName=get("Name")
cd("/Server/" + serverName)
machineName = get("Machine").getName()
cd ("/Machine/" + machineName + "/NodeManager/" + machineName)
nmListenAddress = get("ListenAddress")
nmListenPort = get("ListenPort")
nmType = get("NMType")
if nmType == None:
nmType = "SSL"
closeDomain()
nmConnect(
userConfigFile=domainDir + "/nm-userconfig.properties",
userKeyFile=domainDir + "/nm-userKey.properties",
host=nmListenAddress,
port=nmListenPort,
domainName=domainName,
domainDir=domainDir,
nmType=nmType,
verbose="false")
#
# @function: nmStartServer
#
# Convenience function that connects to NM and asks it to start the server
#
# @param domainDir The path to the domain directory
# @param serverName The name of the server
#
def nmStartServer(domainDir, serverName):
nmConnectForServer(domainDir, serverName)
nmStart(
serverName=serverName,
domainDir=domainDir)
nmDisconnect()
#
# @function: nmStopServer
#
# Convenience function that connects to NM and asks it to stop the server
#
# @param domainDir The path to the domain directory
# @param serverName The name of the server
#
def nmStopServer(domainDir, serverName):
nmConnectForServer(domainDir, serverName)
nmKill(
serverName=serverName)
nmDisconnect()
#
# @function: nmStatusServer
#
# Convenience function that connects to NM and asks it for the status of the
# server
#
# @param domainDir The path to the domain directory
# @param serverName The name of the server
#
def nmStatusServer(domainDir, serverName):
nmConnectForServer(domainDir, serverName)
nmServerStatus(
serverName=serverName)
nmDisconnect()
#
# @function: printUsage
#
# Print the usage pattern for this script
#
# @param domainDir The path to the domain directory
# @param serverName The name of the server
#
def printUsage():
print "Usage: weblogic.WLST " + sys.argv[0] + " start|stop|status <domainDir> <serverName>"
# hostname = java.net.InetAddress.getLocalHost().getHostName()
# -----------------------------------------------------------------------------
# MAIN
# -----------------------------------------------------------------------------
try:
if len(sys.argv) < 4:
printUsage()
exit("y", 1)
else:
if "start" == sys.argv[1]:
nmStartServer(sys.argv[2], sys.argv[3])
elif "stop" == sys.argv[1]:
nmStopServer(sys.argv[2], sys.argv[3])
elif "status" == sys.argv[1]:
nmStatusServer(sys.argv[2], sys.argv[3])
else:
print "Error: Unknown instruction"
printUsage()
exit("y", 1)
except:
print "An error occurred."
dumpStack()
raise