+

Search Tips   |   Advanced Search


startCluster.py

For sample Liberty admin scripts, see:

#
# startCluster.py
#
# Starts a static cluster with the given cluster name.  A connection is
# made to the collective controller located at the given host and port,
# and the request to start the cluster is passed to the ClusterManagerMBean.
#
# Required parameters:
#
# --clusterName = The name of the static cluster to operate on.
# --truststore  = The path to the trust store to be used when establishing
#                 a connection to the collective controller.
# --truststorePassword = The password for the truststore specified by --truststore.
# --host        = The host name where the collective controller is running.
# --port        = The https port where the collective controller is listening.
# --user        = The user name to use when connecting to the collective controller.
# --password    = The password to use when connecting to the collective controller.
# 
# Optional parameters:
#
# --help= Displays help text.
# --debug= Displays additional details when an error occurs.
#
# ex. jython startCluster.py --clusterName=clusterName \
#                            --truststore=/wlp/usr/servers/servername/resources/security/trust.jks \
#                            --truststorePassword=secret \
#                            --host=localhost \
#                            --port=9443 \
#                            --user=Administrator \
#                            --password=secret2
#

import sys

from restConnector import JMXRESTConnector
import wlp_arguments as arguments
from wlp_arguments import MBeanArgs
import wlp_cluster as cluster

import java.lang.Throwable

CLUSTER_NAME = '--clusterName'

# Subclass of command line arguments for startCluster
class StartClusterArgs(MBeanArgs):

# StartClusterArgs constructor 
def __init__(self):

# Make the list of value arguments that we accept
valueParms = [CLUSTER_NAME]
valueParms += arguments.MBEAN_VALUE_PARMS
MBeanArgs.__init__(self, 0, arguments.STANDARD_KEYWORD_PARMS, valueParms)
    
# Obtain our usage string
def getUsage(self):
    usageString = MBeanArgs.getUsage(self) + \
                  CLUSTER_NAME + "=clusterName "
    return usageString
    		
# Print usage of this command
def printUsage(self):
    print "Usage: jython startCluster.py " + self.getUsage()
  
# Print required options
def printRequiredHelp(self):
    print CLUSTER_NAME + "= The name of the cluster to start"
    print
    MBeanArgs.printRequiredHelp(self)
  

# Print help for this command
def printHelp(self):
    self.printUsage()
    print ""
    print "Used to start all cluster members in the cluster specified by the clusterName"
    print "parameter."
    print
    MBeanArgs.printHelp(self)
    print "Example: jython startCluster.py --clusterName=clusterName --truststore=/wlp/usr/servers/servername/resources/security/trust.jks --truststorePassword=secret --host=localhost --port=9443 --user=Administrator --password=secret2"
    
# Validate that the arguments are specified correctly.
def validate(self):
    # First validate the MBean arguments
    if MBeanArgs.validate(self): 
      # Then validate update server config arguments
      missingArg = None
      if (CLUSTER_NAME not in self):
        missingArg = CLUSTER_NAME
  
      if (missingArg != None):
        print "The following required argument is missing: " + missingArg
        self.printUsage()
      else :
        # Make sure the positional parameter was specified correctly.
        positionalParm = self.getPositional(0)
      return (missingArg == None)
    else:
      return False

    
# Starts a cluster
def startCluster(trustStore, trustStorePassword, hostname, port, username, password, clusterName):
    JMXRESTConnector.trustStore = trustStore
    JMXRESTConnector.trustStorePassword = trustStorePassword
    
    connector = JMXRESTConnector()
    connector.connect(hostname, port, username, password)
    mconnection = connector.getMBeanServerConnection()

    clusterManager = cluster.manager(mconnection)
    startClusterResults = clusterManager.start(argParser[CLUSTER_NAME], "")
    
    return startClusterResults  

if __name__ == '__main__':
  argParser = StartClusterArgs()
  if (argParser.parse(sys.argv) == True):
    try:
      results = startCluster(argParser[arguments.TRUST_STORE], 
                             argParser[arguments.TRUST_STORE_PASSWORD],
                             argParser[arguments.HOSTNAME], 
                             int(argParser[arguments.PORT]), 
                             argParser[arguments.USERNAME], 
                             argParser[arguments.PASSWORD], 
                             argParser[CLUSTER_NAME]) # Cluster name

# See what the results of the start were.  The returned map will have
# an entry for each member server that we tried to start.  The key
# to the map is the results returned by the server command MBean, 
# which contains the stderr, stdout, and return code of the command.

if ((results != None) and (len(results) > 0)):
  for member in results:
    memberResults = results[member]
    if ((memberResults != None) and ("returnCode" in memberResults)):
      returnCode = memberResults["returnCode"]
      if (returnCode == 0):
        print str(member) + " started, RC=0"
      else:
        print str(member) + " not started, RC=" + str(returnCode)
    elif ((memberResults != None) and ("ExceptionMessage" in memberResults)):
      print str(member) + " start operation resulted in an Exception: " + memberResults["ExceptionMessage"]
    else:
      print str(member) + " did not have any results reported or returned unexpected data!" + str(memberResults)
else:
  print "No results were received from the start cluster command"
      
except java.lang.Throwable, t:
   print "An exception was caught while processing the startCluster command"
   if (arguments.DEBUG not in argParser):
     print t.toString()
   else:
     t.printStackTrace()