Monday, August 17, 2009

Stopping and Starting a Cluster using wsadmin and jython for 5.1

A simple jython script that accepts two incoming arguments to stop a cluster within a cell. This script is built with global use in mind, meaning you can create multiple shell scripts to pass the arguments to stopCluster.py. This can easily be manipulated to start the cluster too!

Let the script begin
##########################################################

# This program may be used, executed, copied, modified and distributed
# without royalty for the purpose of developing, using, marketing, or distribution
#
#-----------------------------------------------------------------
# Global stopCluster
#-----------------------------------------------------------------
#
# The purpose of this example is to demonstrate the invocation
# of stopping a cluster passing args through a shell script.
#
# This script can be included in the wsadmin command invocation like this:
#
# ./wsadmin -lang jython -f stopCluster.py clusterName cell
#
# Simply create a shell script with the following
# #!/bin/sh
# # Change the directory to your WAS_HOME/bin
# cd /opt/websphere/bin
# # issue the wsadmin command with the two arguments
# # make sure your path to stopCluster.py is correct
# # in the example below it is located in WAS_HOME/bin
# # clusterName below = your cluster name found via the admin console
# # cell below = your cell name found via the admin console
# ./wsadmin.sh -lang jython stopCluster.py clusterName cell
#
# The script expects one parameter:
# arg1 - cluster Name
# arg2 - cell Name
#
#-----------------------------------------------------------------
#
import sys

def x(arg1, arg2):

#--------------------------------------------------------------
# set up globals
#--------------------------------------------------------------
global AdminApp
global AdminControl
global AdminConfig
global Help

#---------------------------------------------------------
# First, list the existing cluster(s)
#---------------------------------------------------------
cluster = AdminConfig.list('ServerCluster')
print "----------------------------------------------------------"
print "Clusters found: "
print cluster
print "----------------------------------------------------------"


#---------------------------------------------------------
# Second, get the cell information
#---------------------------------------------------------
printcellname = AdminConfig.list('Cell')
print "----------------------------------------------------------"
print "Cell(s): "
print printcellname
print "----------------------------------------------------------"


#---------------------------------------------------------
# Here is the cluster we'll be dealing with...
#---------------------------------------------------------
clusterwork = arg1
print "----------------------------------------------------------"
print "The cluster we are going to perform action STOP on:"
print clusterwork
print "----------------------------------------------------------"

#---------------------------------------------------------
# Here is the cell we'll be dealing with...
#---------------------------------------------------------
cellwork = arg2
print "----------------------------------------------------------"
print "The cell we are working with:"
print cellwork
print "----------------------------------------------------------"

#---------------------------------------------------------
# Creating the stopcluster variable
#---------------------------------------------------------
stopcluster = AdminControl.completeObjectName('cell='+arg2+',type=Cluster,name='+arg1+',*')
#---------------------------------------------------------
print "----------------------------------------------------------"
print "Successfully completed the object name and the cluster that will stop is:"
print stopcluster
print "----------------------------------------------------------"

#---------------------------------------------------------
# A very simple stop command
# to run start cluster simply change below command to
# AdminControl.invoke(stopcluster, 'start')
#---------------------------------------------------------
print "----------------------------------------------------------"
print "A simple stop command....."
print AdminControl.invoke(stopcluster, 'stop')
message = "Tail the app server logs to confirm they have stopped"
print message
print "----------------------------------------------------------"


#-----------------------------------------------------------------
# Main
#-----------------------------------------------------------------
if len(sys.argv) != 2:
print "x: this script requires 2 parameters: "
print " cluster name, and cell name"
print ""
print "e.g.: wsadmin -lang jython -f stopCluster.py REMCluster u060rem11Network"
else:
x(sys.argv[0], sys.argv[1])