Skip to main content

Hudson CLI and Groovy shell

Posted by kohsuke on May 2, 2009 at 8:26 AM PDT

"You can do everything from GUI" has always been one of Hudson's strengths, and we also have the REST API, but at the same time, CLI is also very useful for improving automation around administration, builds, and so on. So starting Hudson 1.302, I added a CLI agent to Hudson. CLI can be downloaded from your Hudson from http://server/hudson/cli, which also shows you the basic usage.

Internally, the interesting implementation detail of this CLI is that CLI is "dumb" — it doesn't have any hard-coded knowlege of any command, and instead it downloads the necessary code on-demand from the master via the same remoting technology that Hudson uses between the master and slaves, which is rather transparent to the program running on it.

This enables a single CLI jar to morph into whatever command that plugins on the master has provided. And once the necessary code is obtained, the CLI command can choose what to execute locally in CLI agent and what to execute on the master, making the optimal use of network.

As one of the first commands, I implemented a Groovy shell, which can be started like this:

$ java -jar hudson-cli.jar -s http://yourserver/hudson groovysh
Groovy Shell (1.6.0, JVM: 1.6.0_02-fastdebug)
Type 'help' or '\h' for help.
-------------------------------------------------------------------------------
groovy:000>;

The underlying remoting technology enabled us to write this command in a single Java class of 45 lines (including empty lines and comments), so you can see how easy it is to write a new command in Hudson.

Since this shell is connected to the live Hudson instance, you can introspect the state of the running Hudson instance, or even changes them on the fly.

groovy:000> import hudson.model.*;
===> [import hudson.model.*;]
groovy:000> Hudson.instance
===> hudson.model.Hudson@4b09558d

For example, you can define a new builder class in Groovy, and add it to your project, all on a live system (not that it's recommended, but it's a very useful tool for inspecting problems and applying a temporary bandaid.) This is also very useful if you are writing plugins and try out Hudson's behaviors. The interactivity of Groovy shell allows you to quickly figure out what API has what effects, which you can in turn bake into your code.

All in all, I really like Groovy. And if you have ideas about the CLI commands you'd like to see, please let us know.

Related Topics >>

Comments

Having a terminal with hudson would be cool!

auth for hudson cli

i can find any information about authentication for the hudson cli. is there a way to run the current version of hudson cli under the same user and on the same server that runs the master hudson and being able to use it (with anonymous off ?)

Thanks. This is interesting. I actually recently ported Ajaxterm to Java with JNA. Come to think of it, I should integrate that into Hudson, too.

Hi Kohsuke, I really like the groovy shell also. I made a project to access the groovy shell as a ajax-web app http://code.google.com/p/ajaxgroovyshell for the times when you don't have easy non-HTTP access to a server. Best Anders