Thursday, December 9, 2010

How to start a Node Agent using scripting?

This is a tricky question.  The quick answer is that it can't be done using the typical wsadmin commands.  But it can be done by 'shelling out' from your python script to issue an operating system command.

My colleague and fellow wsadmin contributor Jose Luis provided this Q&A dialog...


1) Is it possible to start the Node agent using wsadmin scripting?

No.

You can not do any operations on a Node agent if it is down. A Node agent must be running in order to work with it.

2) Why don't wsadminlin.py methods such as 'startServer' and 'isServerRunning' work for Node agents?

The job of the node agent is to start and stop servers, and to monitor them.  Under the covers, the wsadmin startServer command asks a running node agent to start servers.  But the node agent can not start itself.  Therefore the 'startServer' command can not start a node agent.

However, method 'isServerRunning' does work on Node agents. It returns True if the node agent is running and False if it is down.

3) How do I start a Node agent that is not running?



Execute operating system commmand 'startNode.sh' or 'startNode.bat'. This command is under /profiles//bin.

For example:
/opt/WAS61/profiles/node1/bin/startNode.sh

4) Hold it there!!! Question 3 describes how to start a Node agent manually and not programmatically. How can I achieve that?

You could use python libraries such as OS or Subprocess to do it. These libraries allow you to execute "external" commands within a script.

For example, the following command will list the contents of the current working directory:

os.system('ls')


5) How can I use these libraries to start a Node agent?

Execute:

os.system(PATH_TO_STARTNODE_SCRIPT)

where PATH_TO_STARTNODE_SCRIPT is the location of your startNode.sh script.

In my case, is something like:

os.system('/opt/WAS61/profiles/node1/bin/startNode.sh')


Below is a script that exercises these concepts. It checks the status of a Node agent. If it is running, the script issues a command to stop it; else it issues a command to start it.

Script:

# Import python os library to execute external commands
import os


# Import wsadminlib.py (update this to your own wsadminlib.py path)
execfile('/tmp/wsadminlib.py')


# Node name
nodename = 'node1'


# Server name
servername = 'nodeagent'


# Counter used to keep track of while loop execution
counter = 0


while(counter < 2):
    # Check if Node agent is running
    print "\nIs Node agent running?"
    isItRunning = isServerRunning(nodename,servername)
    
    # If Node agent is running let's stop it
    # else let's start it
    if (isItRunning):
        print "YES\n"
        
        print "Request issued to stop Node agent\n"
        stopServer( nodename, servername, immediate=False )
        counter = counter + 1
    else:
        print "NO\n"
        print "Request issued to start Node agent\n"
        os.system('/opt/WAS61/profiles/node1/bin/startNode.sh')
        counter = counter + 1


Script Output:

Is Node agent running?
YES


Request issued to stop Node agent


WASX7337I: Invoked stop for server "nodeagent" Waiting for stop completion.


Is Node agent running?
NO


Request issued to start Node agent


ADMU0116I: Tool information is being logged in file
           /opt/WAS61/profiles/node1/logs/nodeagent/startServer.log
ADMU0128I: Starting tool with the node1 profile
ADMU3100I: Reading configuration for server: nodeagent
ADMU3200I: Server launched. Waiting for initialization status.
ADMU3000I: Server nodeagent open for e-business; process id is 24175