Groovy Script: How to iterate through Jenkins Masters from JOC?
I am looking to generate a report based on jobs on all masters.
For example, if I execute script on the MASTER I can get all jobs I'm looking for with something like:
`jenkins.instance.getAllItems(Job.class)`
From the JOC, I can get high-level info on each master with something like:
`Jenkins.instance.getAllItems(ConnectedMaster.class)`
However...I can't seem to figure out how to iterate through items on each master when issuing scripts on the JOC. What I'm hoping for is something like an object hierarchy that I can drill down into such as:
`for Jenkins.instance.getAllItems(ConnectedMaster.class).each {
it.getAllItems(Job.class)
}`
At one point I managed to find a single example that showed a way to accomplish this, but I can't find it. The JOC script had what seemed to be a fairly complicated way of running script on each master from the JOC.
Is that my only option? Are there other ways to accomplish this?
-
Here's the related script that I found. Do I need to "execute remote script on master" from the JOC to accomplish this? I'm hoping there might be a more direct way.
https://github.com/cloudbees/jenkins-scripts/blob/master/print-masters-plugins.groovy#L28
0 -
You can create a Cluster Operation project which allows sending Groovy scripts to execute on connected Masters of your choice.
https://go.cloudbees.com/docs/cloudbees-documentation/cjoc-user-guide/index.html#_cluster_operations
0 -
That's a great idea Denys. Will be good for a few other things we are trying to accomplish as well.
I can see that Cluster Operations is able to run a single script in parallel across multiple masters, but is there a way to consolidate the output?My original thinking was that I could have a single map/array/etc. and I could loop each master and update the counts. Then, I would end up with one single output (which is a report from all of the masters) instead of duplicated info from each master that I then have to concatenate somehow.
Helpful either way, thanks!0 -
It doesn't look like a Cluster Operation will work for this.
It seems like the Cluster Operations script run as an isolate script on each master; I can't see any way to report back to the Operations Center, and even if I could (say as output) that doesn't allow me to write another script to concatenate/interpret all of the results.
Is there an equivalent to http://<JOC URL>/script that I can setup as a job that runs groovy script within the context of the JOC?0 -
Updating as an FYI for anyone else who comes across this.
So far I think the only possible solution is to navigate to JOC/script and run groovy which executes code on each master, like this:
https://github.com/cloudbees/jenkins-scripts/blob/master/print-masters-plugins.groovy#L30
From there I can create a return object that can handle the response within the JOC's script, where I can aggregate results from all of the masters.0 -
The Jenkins CLI has a script subcommand and there is an HTTP endpoint as well. Therefore, it should be possible to create a Freestyle job that executes the script whenever necessary.
https://wiki.jenkins.io/display/JENKINS/Jenkins+Script+Console
0
Please sign in to leave a comment.
Comments
6 comments