Using the RunDeck API from Groovy scripts

Here are some examples of what you can do with this lib and a few lines of Groovy.

We can use Grape to download the lib (and its dependencies) from the Maven Central Repository, so you don't have to install anything manually (except Groovy, of course).

Note that the lib is NOT in the Maven Central Repository for the moment... but hopefully it should be pretty soon ! You can still download it manually.

Basic usage

Save the following script in a file named "rundeck.groovy", and execute it with "groovy rundeck.groovy". Feeling Groovy ? ;-)

// we use Grape (Ivy) to download the lib (and its dependencies) from Maven Central Repository
@Grab(group='org.rundeck', module='rundeck-api-java-client', version='1.0-SNAPSHOT')
import org.rundeck.api.RundeckClient

rundeck = new RundeckClient("http://localhost:4440", "admin", "admin")

println "All RunDeck projects : ${rundeck.projects}"
println "All RunDeck nodes : ${rundeck.nodes}"
println "All RunDeck jobs : ${rundeck.jobs}"
println "All RunDeck running executions : ${rundeck.runningExecutions}"

You can also download the lib and all its dependencies in 1 big jar file, and add it to your classpath before running your script : save the following script in a file named "rundeck.groovy", and execute it with "groovy -cp /path/to/rundeck-api-java-client-VERSION-jar-with-dependencies.jar rundeck.groovy".

import org.rundeck.api.RundeckClient

rundeck = new RundeckClient("http://localhost:4440", "admin", "admin")

println "All RunDeck projects : ${rundeck.projects}"
println "All RunDeck nodes : ${rundeck.nodes}"
println "All RunDeck jobs : ${rundeck.jobs}"
println "All RunDeck running executions : ${rundeck.runningExecutions}"

Running a job

import org.rundeck.api.RundeckClient
rundeck = new RundeckClient("http://localhost:4440", "admin", "admin")

// find a job from its name, group and project
job = rundeck.findJob("my-project", "main-group/sub-group", "job-name")
println "Found job with ID : ${job.id}"

// trigger a simple job with no options
execution = rundeck.triggerJob(job.id)
println "Execution started, you can follow it at the URL : ${execution.url}"

// or with options...
execution = rundeck.triggerJob("job-id", new Properties(option1: "value one", option2: "value two"))

// you can also override the nodes on which the job should execute
execution = rundeck.triggerJob("job-id", new Properties(opt: "value"), new Properties(tags: "prod+appserv"))

// last one : you can run a job and wait until its execution is finished :
execution = rundeck.runJob("job-id")
println "Execution finished ! Status : ${execution.status}, duration (in seconds) : ${execution.durationInSeconds}"

Running an ad-hoc command

import org.rundeck.api.RundeckClient
rundeck = new RundeckClient("http://localhost:4440", "admin", "admin")

// trigger the execution of the "uptime" command on the RunDeck server
execution = rundeck.triggerAdhocCommand("my-project", "uptime")

// run the "uptime" command on all unix nodes
execution = rundeck.runAdhocCommand("my-project", "uptime", new Properties(os-family: "unix"))

And more...

See the API documentation of the RundeckClient class for more interactions with your RunDeck instance...