Permalink
Browse files

- adding a few sample scripts

- updating some of the previous samples to make them less verbose
  • Loading branch information...
1 parent 837addf commit 05a84473e0a48dd597fc204ef88657bb63f41e66 @kellyrob99 committed Feb 25, 2012
View
@@ -14,7 +14,7 @@ HudsonControlApi controlApi = new HudsonControlApi()
/* Example usages */
// Create a new job based on a config.xml file. Useful for copying a build from one node to another.
-assert HTTP_OK == jobApi.createJob(rootUrl, new File('src/test/resources/config.xml').text, 'test2')
+assert HTTP_OK == jobApi.createJob(rootUrl, new File('../src/test/resources/config.xml').text, 'test2')
// And then delete that job
apiResult = api.inspectApi(rootUrl)
@@ -23,19 +23,19 @@ assert HTTP_MOVED_TEMP == jobApi.deleteJob(newJob.url)
// Execute scripts remotely. Output streams are printed to System.err and System.out,
// so println's on the server appear locally.
-def listPlugins = 'jenkins.model.Jenkins.instance.pluginManager.plugins.each { \
+def listPlugins = 'Hudson.instance.pluginManager.plugins.each { \
println("${it.longName} - ${it.version}") };'
-def allFailedBuilds = '''hudsonInstance = hudson.model.Hudson.instance
+def allFailedBuilds = '''hudsonInstance = Hudson.instance
allItems = hudsonInstance.items
activeJobs = allItems.findAll{job -> job.isBuildable()}
-failedRuns = activeJobs.findAll{job -> job.lastBuild && job.lastBuild.result == hudson.model.Result.FAILURE}
+failedRuns = activeJobs.findAll{job -> job.lastBuild && job.lastBuild.result == Result.FAILURE}
failedRuns.each{run -> println(run.name)}'''
-def parseableAllFailedBuilds = '''hudsonInstance = hudson.model.Hudson.instance
+def parseableAllFailedBuilds = '''hudsonInstance = Hudson.instance
allItems = hudsonInstance.items
activeJobs = allItems.findAll{job -> job.isBuildable()}
-failedRuns = activeJobs.findAll{job -> job.lastBuild && job.lastBuild.result == hudson.model.Result.FAILURE}
+failedRuns = activeJobs.findAll{job -> job.lastBuild && job.lastBuild.result == Result.FAILURE}
[activeJobs:activeJobs?.collect{it.name}, failedRuns:failedRuns?.collect{it.name}].inspect()'''
[listPlugins, allFailedBuilds, parseableAllFailedBuilds].each{ script ->
@@ -45,7 +45,7 @@ failedRuns = activeJobs.findAll{job -> job.lastBuild && job.lastBuild.result ==
// Execute a script remotely and capture the output for further study.
OutputStream output = new ByteArrayOutputStream()
cliApi.runCliCommand(rootUrl, ['groovysh', parseableAllFailedBuilds],System.in, output, System.err)
-def mapOfBuildsString = output.toString().substring(11) //remove some of the output characters that are not part of the returned value
+def mapOfBuildsString = cliApi.parseResponse(output.toString())
Map mapOfBuilds = Eval.me(mapOfBuildsString) // convert to a map using the easiest available method
assert mapOfBuilds.activeJobs
assert mapOfBuilds.failedRuns
@@ -58,4 +58,5 @@ cliApi.runCliCommand(rootUrl, ['install-plugin', 'jira'])
println controlApi.sendQuiet(rootUrl)
// Restart a node, required for newly installed plugins to be made available.
-cliApi.runCliCommand(rootUrl, 'safe-restart')
+cliApi.runCliCommand(rootUrl, 'safe-restart')
+
@@ -0,0 +1,38 @@
+@GrabResolver(name = 'glassfish', root = 'http://maven.glassfish.org/content/groups/public/')
+@GrabResolver(name = "github", root = "http://kellyrob99.github.com/Jenkins-api-tour/repository")
+@Grab('org.kar:hudson-api:0.2-SNAPSHOT')
+@GrabExclude('org.codehaus.groovy:groovy')
+import static java.net.HttpURLConnection.*
+import org.kar.hudson.api.*
+import org.kar.hudson.api.cli.HudsonCliApi
+
+String rootUrl = 'http://localhost:8080'
+HudsonCliApi cliApi = new HudsonCliApi()
+
+['build':'Builds a job, and optionally waits until its completion.', 'cancel-quiet-down':'Cancel the effect of the "quiet-down" command.',
+ 'clear-queue':'Clears the build queue', 'connect-node':'Reconnect to a node', 'copy-job':'Copies a job.',
+ 'create-job':'Creates a new job by reading stdin as a configuration XML file.', 'delete-builds':'Deletes build record(s).',
+ 'delete-job':'Deletes a job', 'delete-node':'Deletes a node', 'disable-job':'Disables a job',
+ 'disconnect-node':'Disconnects from a node', 'enable-job':'Enables a job', 'get-job':'Dumps the job definition XML to stdout',
+ 'groovy':'Executes the specified Groovy script.', 'groovysh':'Runs an interactive groovy shell.',
+ 'help':'Lists all the available commands.',
+ 'install-plugin':'Installs a plugin either from a file, an URL, or from update center.',
+ 'install-tool':'Performs automatic tool installation, and print its location to stdout. Can be only called from inside a build.',
+ 'keep-build':'Mark the build to keep the build forever.', 'list-changes':'Dumps the changelog for the specified build(s).',
+ 'login':'Saves the current credential to allow future commands to run without explicit credential information.',
+ 'logout':'Deletes the credential stored with the login command.', 'mail':'Reads stdin and sends that out as an e-mail.',
+ 'offline-node':'Stop using a node for performing builds temporarily, until the next "online-node" command.',
+ 'online-node':'Resume using a node for performing builds, to cancel out the earlier "offline-node" command.',
+ 'quiet-down':'Quiet down Jenkins, in preparation for a restart. Don\'t start any builds.',
+ 'reload-configuration':'Discard all the loaded data in memory and reload everything from file system. Useful when you modified config files directly on disk.',
+ 'restart':'Restart Jenkins', 'safe-restart':'Safely restart Jenkins',
+ 'safe-shutdown':'Puts Jenkins into the quiet mode, wait for existing builds to be completed, and then shut down Jenkins.',
+ 'set-build-description':'Sets the description of a build.', 'set-build-display-name':'Sets the displayName of a build',
+ 'set-build-result':'Sets the result of the current build. Works only if invoked from within a build.',
+ 'shutdown':'Immediately shuts down Jenkins server', 'update-job':'Updates the job definition XML from stdin. The opposite of the get-job command',
+ 'version':'Outputs the current version.', 'wait-node-offline':'Wait for a node to become offline',
+ 'wait-node-online':'Wait for a node to become online', 'who-am-i':'Reports your credential and permissions']
+ .each { command, description ->
+ println "examining command: $command"
+ cliApi.runCliCommand(rootUrl, [command])
+}
View
@@ -0,0 +1,14 @@
+@GrabResolver(name = 'glassfish', root = 'http://maven.glassfish.org/content/groups/public/')
+@GrabResolver(name = "github", root = "http://kellyrob99.github.com/Jenkins-api-tour/repository")
+@Grab('org.kar:hudson-api:0.2-SNAPSHOT')
+@GrabExclude('org.codehaus.groovy:groovy')
+import static java.net.HttpURLConnection.*
+import org.kar.hudson.api.*
+import org.kar.hudson.api.cli.HudsonCliApi
+
+String rootUrl = 'http://localhost:8080'
+HudsonCliApi cliApi = new HudsonCliApi()
+OutputStream out = new ByteArrayOutputStream()
+cliApi.runCliCommand(rootUrl, ['groovysh', 'hudson.jobNames.inspect()'], System.in, out, System.err)
+List allJobs = Eval.me(cliApi.parseResponse(out.toString()))
+
@@ -0,0 +1,20 @@
+@GrabResolver(name='glassfish', root='http://maven.glassfish.org/content/groups/public/')
+@GrabResolver(name="github", root="http://kellyrob99.github.com/Jenkins-api-tour/repository")
+@Grab('org.kar:hudson-api:0.2-SNAPSHOT')
+@GrabExclude('org.codehaus.groovy:groovy')
+import static java.net.HttpURLConnection.*
+import org.kar.hudson.api.*
+import org.kar.hudson.api.cli.HudsonCliApi
+
+String rootUrl = 'http://localhost:8080'
+HudsonCliApi cliApi = new HudsonCliApi()
+
+['groovy', 'gradle', 'chucknorris', 'greenballs', 'github', 'analysis-core', 'analysis-collector', 'cobertura',
+ 'project-stats-plugin','audit-trail', 'view-job-filters', 'disk-usage', 'global-build-stats',
+ 'radiatorviewplugin', 'violations', 'build-pipeline-plugin', 'monitoring', 'dashboard-view',
+ 'iphoneview', 'jenkinswalldisplay'].each{ plugin ->
+ cliApi.runCliCommand(rootUrl, ['install-plugin', plugin])
+}
+
+// Restart a node, required for newly installed plugins to be made available.
+cliApi.runCliCommand(rootUrl, 'safe-restart')
@@ -0,0 +1,30 @@
+@GrabResolver(name='glassfish', root='http://maven.glassfish.org/content/groups/public/')
+@GrabResolver(name="github", root="http://kellyrob99.github.com/Jenkins-api-tour/repository")
+@Grab('org.kar:hudson-api:0.2-SNAPSHOT')
+@GrabExclude('org.codehaus.groovy:groovy')
+import static java.net.HttpURLConnection.*
+import org.kar.hudson.api.*
+import org.kar.hudson.api.cli.HudsonCliApi
+
+String rootUrl = 'http://localhost:8080'
+HudsonCliApi cliApi = new HudsonCliApi()
+
+def findPluginsWithUpdates = '''
+hudson.pluginManager.plugins.inject([]) { List toUpdate, plugin ->
+ if(plugin.hasUpdate())
+ {
+ toUpdate << plugin.shortName
+ }
+ toUpdate
+}.inspect()
+'''
+OutputStream updateablePlugins = new ByteArrayOutputStream()
+cliApi.runCliCommand(rootUrl, ['groovysh', findPluginsWithUpdates], System.in, updateablePlugins, System.err)
+
+def listOfPlugins = Eval.me(cliApi.parseResponse(updateablePlugins.toString()))
+listOfPlugins.each{ plugin ->
+ cliApi.runCliCommand(rootUrl, ['install-plugin', plugin])
+}
+
+// Restart a node, required for newly installed plugins to be made available.
+cliApi.runCliCommand(rootUrl, 'safe-restart')
@@ -77,4 +77,9 @@ class HudsonCliApi
cli.execute(args, input, output, err)
cli.close()
}
+
+ String parseResponse(String response)
+ {
+ return response.substring(11)
+ }
}
@@ -41,10 +41,10 @@ git looks like a short plugin name. Did you mean 'null'?
{
if (i % 2 == 0)
{
- commands[lines[i]] = lines[i + 1]
+ commands[lines[i].trim()] = lines[i + 1].trim()
}
}
-// println commands.inspect()
+ println commands.inspect()
commands.size() > 0
}
@@ -88,7 +88,7 @@ git looks like a short plugin name. Did you mean 'null'?
final ByteArrayOutputStream output = new ByteArrayOutputStream()
when:
api.runCliCommand(rootUrl, ['groovysh', '''
-hudson.model.Hudson.instance.administrativeMonitors.inject([:]){ result, it ->
+hudson.administrativeMonitors.inject([:]){ result, it ->
result[it.id] = [enabled: it.enabled, activated: it.activated]
result
}.inspect()

0 comments on commit 05a8447

Please sign in to comment.