Sunday, November 13, 2011

Jython script to create Variable at Cluster Scope

import sys,java
from java.util import Properties
from org.python.modules import time
from java.io import FileInputStream

lineSep = java.lang.System.getProperty('line.separator')


def variable(cluster,varname1,varvalue1):

global AdminApp
global AdminConfig
global AdminControl

print " ----------------------------------------------------------------------------------------- "

######################################## Getting config ID of cell ################################################

cell = AdminControl.getCell()

cellid = AdminConfig.getid('/Cell:'+ cell)

Clusterid = AdminConfig.getid('/ServerCluster:'+ cluster +'/')

varMapserver = AdminConfig.getid('/ServerCluster:'+ cluster +'/VariableMap:/')

if len(varMapserver) > 0:

print " Variable Map exists on Cluster ........... "

variables = AdminConfig.list("VariableSubstitutionEntry", varMapserver).split(lineSeparator)
for variable in variables[:]:
vname = AdminConfig.showAttribute(variable, "symbolicName")
if (vname == varname1) :
print "Removing old variable " + vname + " from variables.xml..."
AdminConfig.remove(variable)

nameattr1 = ['symbolicName', varname1]
valattr1 = ['value', varvalue1]
desc1 = ["description", varname1]
attr1 = [nameattr1, valattr1, desc1]
attrs1 = [attr1]

entries1 = ['entries', attrs1 ]

print "creating new variable " + varname1

AdminConfig.modify(varMapserver, [entries1] )

print " Cluster Level Variable Configured ................ "

print " ------------------------------------------------------------------------------------------------------------------------ "


else:

print " Variable Map doesn't exists on Cluster ........... "

varMapserver = AdminConfig.create('VariableMap', Clusterid, [])

print " Variable Map created on Cluster ........... "

nameattr1 = ['symbolicName', varname1]
valattr1 = ['value', varvalue1]
desc1 = ["description", varname1]
attr1 = [nameattr1, valattr1, desc1]
attrs1 = [attr1]

entries1 = ['entries', attrs1 ]

print "creating new variable " + varname1

AdminConfig.modify(varMapserver, [entries1] )

print " Cluster Level Variable Configured ................ "

print " ------------------------------------------------------------------------------------------------------------------------ "


AdminConfig.save()

######################################################### Full Resyncronization #########################################################

print " Fully Resyncronizing nodes ........... "

nodelist = AdminTask.listManagedNodes().split(lineSep)

for nodename in nodelist :

####################Identifying the ConfigRepository MBean and assign it to variable######################

repo = AdminControl.completeObjectName('type=ConfigRepository,process=nodeagent,node='+ nodename +',*')

print AdminControl.invoke(repo, 'refreshRepositoryEpoch')

sync = AdminControl.completeObjectName('cell='+ cell +',node='+ nodename +',type=NodeSync,*')

print AdminControl.invoke(sync , 'sync')

print " ----------------------------------------------------------------------------------------- "

print " Full Resyncronization completed "

print " ----------------------------------------------------------------------------------------- "


########################################################## Main Program #############################################################

arglen=len(sys.argv)

num_exp_args=2

if (arglen != num_exp_args):

print "Two arguments are required."

print " ----------------------------------------------------------------------------------------- "

sys.exit(-1)

propFile=sys.argv[0]

properties=Properties();

try:

properties.load(FileInputStream(propFile))

print " ----------------------------------------------------------------------------------------- "

print "Succesfully read property file "+propFile

print " ----------------------------------------------------------------------------------------- "

except:

print "Cannot read property file "+propFile
sys.exit(-1)

print " ----------------------------------------------------------------------------------------- "


cluster = sys.argv[1]
varname1=str(properties.getProperty("VARIABLE_NAME"))
varvalue1=str(properties.getProperty("VARIABLE_VALUE"))

variable(cluster,varname1,varvalue1)

2 comments:

  1. Thankyou for the script, this is precisely what i was looking for.

    Question : Why are you passing the cluster name in the script, it can be given as a parameter in the property file as well, is this to make the script generic for multiple cluster scenario or anything else.

    Also can u please provide the sample properties files and what argument to pass for beginners.

    To what i understand its

    ./wsadmin.sh -f .py

    Kindly correct if i am wrong.

    Thanks again

    ReplyDelete
  2. Hi,
    The script looks really great, can you include a sample usage, and a sample property file with this. It would be of great help for beginners.

    Thank You

    ReplyDelete