Thursday, July 4, 2013

JBoss Operations Network CLI (JON CLI)

I recenlty faced a task where i need to change all the userNames and Password of the JBoss EWP Instances.Even though i was aware of how to do this , there is another step that needs to be done after this.I need to update the JON ( JBoss Operations Network) too with the Updated Credentials.But since there are more than 50 Ewp Instances , i cant login to JON for every Instances and Change.

JBoss Operations Network provides a solution by using JON CLI.

The JON CLI is a stand alone java application which uses the Java Scripting API to interact with JON programmatically.Since it use Java Scripting API , we require Java 6 and later versions.The CLI allows developers and administrators to connect to JON servers and perform various actions like retriving metrics, changing credendital, enabling and disabling metrics e.t.c.Java 6 continas Rhino Java Script Engine and hence Java Script is supported Scripting Language in the CLI.

here is how it goes

1.Download the JON CLI package ( Login to JON server -> Administration -> Downloads -> select the rhq-remoting zip to download)

2.Change the RHQ_CLI_JAVA_HOME in the file pointing to JAVA_HOME location like
RHQ_CLI_JAVA_HOME ="/software/java32/jdk1.6.0_29"

3.Connect to the server using 
 ./ -u rhqadmin -p <Password>  --host <Host> --port <Port>

4.Now run the below command -u rhqadmin -p <Password>  --host <Host> --port <Port> -c "pretty.print(ResourceTypeManager.findResourceTypesByCriteria(new ResourceTypeCriteria()))" > resource.txt

This connects to the CLI ,logs into your RHQ server running on host specified and executes the command in quotoes.It finally redirects the output to the file resource.txt.

The command says

ResourceTypeManager.findResourceTypesByCriteria(new ResourceTypeCriteria()). This invokes the findResourceTypesByCriteria operation on ResourceTypeManager. A new ResourceTypeCriteria object is passed as the argument. Because nothing has been specified on the criteria object, all resource types will be returned. 

now the pretty.print(...) portion. pretty is an implicit object made available to commands and scripts by the CLI. It is very useful for outputting objects in a readable, tabular format, designed with enhanced capabilities for domain objects. In review, this single command gives us a nicely formatted,text-based report of the resource types in our inventory.

Now to my task in order to get all the list of Ewp Instances available run the Below command one by one

var critria = ResourceCriteria();
criteria.addFilterResourceTypeName("JBossAS Server");
var resources = ResourceManager.findResourcesByCriteria(criteria);

We can see the Response as

id    name             version      currentAvailabil resourceType
47305 ABC-1   EWP 5.0.0.GA     DOWN             JBossAS Server
26923 DEC-2            EWP 5.0.0.GA UP               JBossAS Server
75270 ZXC-1   EWP 5.0.0.GA UNKNOWN          JBossAS Server
3 rows

Now we can get what are the available Plugin Options for the resource like


Configuration [178486] - null
  homeDir = /software/jboss/ewp32/5.0
  shutdownMBeanName = jboss.system:type=Server
  scriptPrefix = null
  shutdownScript = xxxxxxxxxx
  serverHomeDir = xxxxxxxxxx
  serverName = ABC-1
  shutdownMethod = SCRIPT
  startScript = xxxxxxxxx
  javaHome = /software/java32/jdk1.6.0_16
  namingURL = xxxxxxxxxxx
  principal = admin
  childJmxServerName = JVM
  shutdownMBeanOperation = shutdown
  availabilityCheckPeriod = null
  bindAddress = xxxxxxxx
  credentials = _._._[MaSKeD]_._._
  logEventSources [0] {

Now to change the credentials we can use

//For each resource, get it's Plugin Configuration, make change for Credentials and Password and update the Plugin Configuration

for (var i=0; i < resources.size(); i++) {
    var myConfig = ConfigurationManager.getPluginConfiguration(resources.get(i).id);
    var property = new PropertySimple("principal","myAdmin");
    property = new PropertySimple("credentials","myPassword");
    ConfigurationManager.updatePluginConfiguration(resources.get(i).id, myConfig);

The whole thing can be copied to a file and the entire file can be executed using -u rhqadmin -p <Password>  --host <Host> --port <Port>  -f /full/path/changePassword.js

Happy learning :-) , More To Come