[jboss-dev-forums] [JBoss AS 7 Development] - Advanced CLI scripting with Groovy, Rhino, Jython, etc.

Stan Silvert do-not-reply at jboss.com
Thu Jul 19 17:52:32 EDT 2012


Stan Silvert [https://community.jboss.org/people/ssilvert] modified the document:

"Advanced CLI scripting with Groovy, Rhino, Jython, etc."

To view the document, visit: https://community.jboss.org/docs/DOC-18796

--------------------------------------------------------------
The JBoss AS7  https://community.jboss.org/docs/DOC-16581 Command Line Interface has support for https://community.jboss.org/docs/DOC-17041  writing CLI scripts that can be executed against a running AS7 instance.  You can also use the  https://community.jboss.org/docs/DOC-17597 CLI public API to write sophisticated server management programs in java.  

This article shows how to do something in between.  You can use the full power of any JVM-based scripting language using a simplified wrapper class for the CLI API.

AS7.2 introduces the scriptsupport.CLI class.  This class acts as a facade for the  https://community.jboss.org/docs/DOC-17597 CLI public API.  AS7.2 also introduces the  https://community.jboss.org/docs/DOC-18795 CLI remote client jar which provides everything you need to connect to AS7 and execute CLI commands.

h3. Example
As an example, we will implement a simple script in several different JVM-based scripting languages.  The script will do the following:
1. Connect to a local AS7 instance
2. Determine if the instance is standalone or domain
3. "cd" to the MBean containing runtime info
4. Read the attributes for "server start time" and "server up time".
5. Output those values.
6. Disconnect from the AS7 instance.
h3. 
h3. Groovy CLI scripts
To run the groovy script below, just add the  https://community.jboss.org/docs/DOC-18795 CLI remote client jar to the classpath when executing groovy:
groovy -cp jboss-cli-client.jar uptime.groovy


*uptime.groovy*
import org.jboss.as.cli.scriptsupport.*
 
cli = CLI.newInstance()
cli.connect()
 
if (cli.getCommandContext().isDomainMode()) {
  cli.cmd("cd /host=master/core-service=platform-mbean/type=runtime")
} else {
  cli.cmd("cd /core-service=platform-mbean/type=runtime")
}
 
result = cli.cmd(":read-attribute(name=start-time)")
response = result.getResponse()
startTime = response.get("result").asLong()
 
result = cli.cmd(":read-attribute(name=uptime)")
response = result.getResponse()
serveruptime = response.get("result").asString()
 
println()
println("The server was started on " + new Date(startTime))
println("It has been running for " + serveruptime + "ms")
 
cli.disconnect()



h3. Rhino (javascript) CLI scripts
To run the Rhino script below, add the Rhino jars and the jboss-cli-client.jar to the classpath.  You will also need to provide the Rhino main class.
java -cp js-14.jar;js.jar;jboss-cli-client.jar org.mozilla.javascript.tools.shell.Main -f uptime.js


*uptime.js*
importPackage(org.jboss.as.cli.scriptsupport)

cli = CLI.newInstance()
cli.connect()

if (cli.getCommandContext().isDomainMode()) {
  cli.cmd("cd /host=master/core-service=platform-mbean/type=runtime")
} else {
  cli.cmd("cd /core-service=platform-mbean/type=runtime")
}

result = cli.cmd(":read-attribute(name=start-time)")
response = result.getResponse()
startTime = response.get("result").asLong()

result = cli.cmd(":read-attribute(name=uptime)")
response = result.getResponse()
serveruptime = response.get("result").asString()

print()
print("The server was started on " + new Date(startTime))
print("It has been running for " + serveruptime + "ms")

cli.disconnect()


h3. Jython CLI scripts
To run the Jython script below, you will need to add jboss-cli-client.jar to your system CLASSPATH.  Just adding it to the python.path will not allow dynamic loading of some classes that CLI needs.
jython jython.py


*jython.py*
from java.util import Date
from org.jboss.as.cli.scriptsupport import CLI
 
cli = CLI.newInstance()
cli.connect()
 
if cli.getCommandContext().isDomainMode():
  cli.cmd("cd /host=master/core-service=platform-mbean/type=runtime")
else:
  cli.cmd("cd /core-service=platform-mbean/type=runtime")
 
result = cli.cmd(":read-attribute(name=start-time)")
response = result.getResponse()
startTime = response.get("result").asLong()
 
result = cli.cmd(":read-attribute(name=uptime)")
response = result.getResponse()
serveruptime = response.get("result").asString()
 
print
print 'The server was started on ' + Date(startTime).toString()
print 'It has been running for ' + serveruptime + 'ms'
cli.disconnect()

h3. 
h3. Using the scriptsupport.CLI class
As mentioned before, the *org.jboss.as.cli.scriptsupport.CLI* class acts as a facade for the  https://community.jboss.org/docs/DOC-17597 CLI public API.   For the most part, its methods are self-explanitory.

| CLI.Result | *cmd*(String (http://download.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true) cliCommand)Execute a CLI command. |
| void | *connect*()Connect to the server using the default host and port. |
| void | *connect*(String (http://download.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true) username, char[] password)Connect to the server using the default host and port. |
| void | *connect*(String (http://download.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true) controllerHost, int controllerPort, String (http://download.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true) username, char[] password)Connect to the server using a specified host and port. |
| void | *disconnect*()Disconnect from the server. |
| CommandContext | *getCommandContext*()Return the CLI CommandContext that was created when connected to the server. |
| static CLI | *newInstance*()Create a new CLI instance. |

h3. CLI.cmd() and CLI.Result
The *cmd*( http://download.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true String cliCommand) method takes a String that can be anything you would normally enter in CLI's regular interactive mode.  It returns CLI.Result, which has the following methods:
| String (http://download.oracle.com/javase/6/docs/api/java/lang/String.html?is-external=true) | *getCliCommand*()Return the original command as a String. |
| org.jboss.dmr.ModelNode | *getRequest*()If the command resulted in a server-side operation, return the ModelNode representation of the operation. |
| org.jboss.dmr.ModelNode | *getResponse*()If the command resulted in a server-side operation, return the ModelNode representation of the response. |
| boolean | *isLocalCommand*()Return true if the command was only executed locally and did not result in a server-side operation. |
| boolean | *isSuccess*()Return true if the command was successful. |

CLI.Result tells you everything that happened when you invoked CLI.cmd(cliCommand).  Most of the time, you just want to know your command was successful.  So you can just call the isSuccess() method.

But if you want more details on the request and response, that information is available.  Unless it is a local command, the command String is converted to a ModelNode object that the AS7 server understands.  See  https://community.jboss.org/docs/DOC-16336 Format of a Detyped Operation Request for details.  A response from the server will also be in the form of a ModelNode object.  See  https://community.jboss.org/docs/DOC-16354 Format of a Detyped Operation Response for details on the response.
--------------------------------------------------------------

Comment by going to Community
[https://community.jboss.org/docs/DOC-18796]

Create a new document in JBoss AS 7 Development at Community
[https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2225]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/jboss-dev-forums/attachments/20120719/5bff6f0c/attachment.html 


More information about the jboss-dev-forums mailing list