start a server based on sys.arg in Jython

CharanR
CharanR used Ask the Experts™
on
Hi guys,

I'm trying to start or stop servers based on the command line arguments. Could some one check whether the below approach I'm following is correct or not?


from java.util import Properties
from java.io import FileInputStream
from java.io import File
import os.path

from string import split

def carryFileProperties(serverName) :
        print 'carrying properties of: ' + serverName
        #properties file to be loaded
        myProps = Properties()
        #load properties file
        myProps.load(FileInputStream(File(serverName)))
        return myProps

def serversList(fileProperties):
        print 'selecting domains from the properties'
        domains = split(fileProperties.get('domainList'), ',')
        print 'Domains: ' + str(domains)
        print 'selecting servers from the properties'
        servers = []
        properties = fileProperties.propertyNames()
        while properties.hasMoreElements():
                key = properties.nextElement()
                element = split(key, '.')
                if len(element) > 1 and element[1] == 'Targets':
                        domain = element[0]
                        dservers = split(fileProperties.get(key), ',')
                        servers.append([domain, dservers])
        print'LIST OF servers to start through nodemanager'
        for dsvrs in servers:
                print '(domain, Servers) ==>> ' + '(' + dsvrs[0] + ', ' + str(dsvrs[1]) + ')'
        return servers

def startInstance():
        nmIP = fileProperties.get('nmIP')
        nmPort = fileProperties.get('nmPort')
        domainRoot = fileProperties.get('domainRoot')
        for dsvrs in servers:
                domainDirectory = os.path.join(domainRoot, dsvrs[0])
                nmConnect(userConfigFile, userKeyFile, host, port, domainName, domainDir, nmType, verbose)
                if sys.argv[1] == 'start':
                        for server in dsvrs[1]:
                                print 'requesting the server startup ' + server
                                nmStart(server, domainDirectory)
                        nmDisconnect()
                elif sys.argv[1] == 'stop':
                        for server in dsvrs[1]:
                                print 'requesting the server stop' + server
                                nmKill(server)
                        nmDisconnect()
                else:
                        print 'Invalid command line arguments\t-->' + sys.argv[1]
fileProperties = carryFileProperties('servers.properties')
servers = serversList(fileProperties)
startInstance()

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Commented:
Well, I don't know about WebLogic, but I do have some suggestions for you.

I like to write scripts that check/verify that they are being used correctly, so instead of this kind of code:
--------------------------------------------------
fileProperties = carryFileProperties('servers.properties')
servers = serversList(fileProperties)
startInstance()
--------------------------------------------------

I much prefer this:

#---------------------------------------------------------------------
# Role: main entry point, used to verify that script was executed,
#       not imported
#---------------------------------------------------------------------
if ( __name__ == '__main__' ) :
  main();
else :
  Usage( __name__ );


I also suggest that you have a "Usage()" procedure to explain how the script is to be used, as well as defining any values (parameters) that are expected.  One common
technique, or idiom that occurs in Python & Jython scripts is to have the script docstring
be displayed as Usage() text.  To do this, we have a triple quoted string as the first part of the script file, perhaps something like this:

'''
 Name: %(cmdName)s\n
 Role: To start or stop a group of servers identified in a properties file
       (i.e., 'servers.properties').\n
Usage: java weblogic.WLST %(cmdName)s.py <option>\n
Where: option identifies what operation is to be performed "stop" or "start"
'''

The, the Usage() routine can be something simple like:

def Usage( cmdName ) :
  print __doc__ % locals();
  sys.exit();

How does this work?  Well, this is an example of a formatted message.
The % operator is used to have the places in the input string (the script
docstring) that start with a % replaced by values of local variables. Since
the only local variable that is referenced in the docstring is %(cmdName)s,
each of these occurrences is replaced by the string value of this local
variable.

If the script verifies that it was executed, and not imported, it calls the
main() routine. In this routine, we can check for proper number, type,
and values of the expected parameters.  For example, something like this:

#---------------------------------------------------------------------
# Name: main()
# Role: Routine called to perform the actual script function,
#       i.e., start or stop the servers defined in the property file.
#---------------------------------------------------------------------
def main() :
  argc = len( sys.argv );    # Number of user specified parameters
  if argc != 1 :
    Usage( 'startStop' );

def main() :
  argc = len( sys.argv );         # Number of user specified parameters
  if argc != 1 :                  # Check for the correct # of parms
    Usage( 'startStop' );         # Specify the actual script name

  action = sys.argv[ 0 ].lower(); # User specified option in lowercase
  if action in [ 'start', 'stop' ] : # check for valid values
    Usage( 'startStop' );         # Specify the actual script name

  #-------------------------------------------------------------------
  # Load the property file contents
  #-------------------------------------------------------------------
  fileProperties = carryFileProperties( 'servers.properties' );
 
  #-------------------------------------------------------------------
  # Build the list of servers to be controlled
  #-------------------------------------------------------------------
  servers = serversList( fileProperties );

  #-------------------------------------------------------------------
  # Control the specified servers
  #-------------------------------------------------------------------
  control( servers, action );

If you really want to have your function named startInstance(), you certainly can.
However, by renaming it to something like "control", we can convey more information.
Additionally, it is better to pass the information about the servers to be controlled, and the action to be performed as parameters.

Does this help?

Commented:
Hi,

i think it could be easier to connect to the admin server and then request managed servers to start (it will be safer too)

Emmanuel

Author

Commented:
Thank you HonorGod

These are some excellent tips to follow best programming
HonorGodSoftware Engineer

Commented:
You are very welcome.

Thanks for the grade and points.

Good luck & have a great day.

This article may have some ideas that could help you:
http://www.ibm.com/developerworks/websphere/library/techarticles/1004_gibson/1004_gibson.html
Hi
CharanR,
Can u please post the corrected script once..?..

Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial