Access list of slave nodes

6,673 views
Skip to first unread message

Eddie Sholl

unread,
Sep 30, 2014, 3:26:24 AM9/30/14
to job-dsl...@googlegroups.com
Hi all,

I'm looking for a way to access the list of current salve nodes on the jenkins instance. I'd like to use this list to perform some maintenance of nodes, probably with a job to implement the cleanup. For example:

  • Check if they are healthy
  • Check their disk status
  • Clean up leftover build files etc if disk is full
This may not sound very useful, but we have a very large build tree and right now standard artifact copying doesn't seem like it will cut it.

Is there are 'DSL centric' way to get the list of slave nodes? Or is it a matter of falling back to a jenkins REST API call, implemented in Groovy? If this is the case, I'm guessing it would be similar to the examples floating around for a branch query against an SVN URL.

Any examples that people might know of would be much appreciated.

Cheers,

Eddie

Daniel Spilker

unread,
Sep 30, 2014, 4:12:21 AM9/30/14
to job-dsl...@googlegroups.com
Currently there is no DSL support for Jenkins nodes.

I use the following script to access all nodes and perform some cleanup. While you can run that in a Job DSL script, I would recommend to run that as a system Groovy script with the Groovy Plugin (https://wiki.jenkins-ci.org/display/JENKINS/Groovy+plugin).

import hudson.FilePath
import hudson.model.Node
import hudson.model.Slave
import jenkins.model.Jenkins

Jenkins jenkins = Jenkins.instance
for (Node node in jenkins.nodes) {
  // Make sure slave is online
  if (!node.toComputer().online) {
    println "Node '$node.nodeName' is currently offline - skipping workspace cleanup"
    continue
  }

  println "Node '$node.nodeName' is online - performing cleanup:"

  // Do some cleanup
}


Daniel


--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To post to this group, send email to job-dsl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/512fd5d6-fd71-4381-ad48-269baccbbc6d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eddie Sholl

unread,
Oct 1, 2014, 3:51:11 AM10/1/14
to job-dsl...@googlegroups.com
Thanks for the ideas, that's perfect!

Eddie Sholl

unread,
Oct 31, 2014, 3:19:48 AM10/31/14
to job-dsl...@googlegroups.com
Hi Daniel,

I'm finally trying to get this implemented. I'm using some of the example at http://stackoverflow.com/a/23226035/2658793 to execute a script on each node, and getting a NullPointerException. I was curious if your experience with the 'do some cleanup' step you mentioned has ever included running a script on each node?

Any ideas would be much appreciated. I'd much prefer this model to having maintenance jobs for every single node, filling up the build queues.

I'm running this via the script console on master:

import hudson.util.*

Jenkins jenkins = Jenkins.instance
for (Node slave in jenkins.nodes) {

    println('Processing Slave ' + slave.name); 
  
    def output = new java.io.ByteArrayOutputStream();
    def listener = new hudson.util.StreamTaskListener(output);
    def launcher = slave.createLauncher(listener);
        
    def command = new hudson.util.ArgumentListBuilder(); 
    command.addTokenized("ipconfig"); 
    
    def ps = new hudson.Launcher.ProcStarter(); 
    ps = ps.cmds(command); 
        
    def proc = launcher.launch(ps);
    def retcode = proc.join(); 
    println("ret code="+retcode) 
}

And the stack trace is here. I've checked out https://github.com/jenkinsci/jenkins/blob/47b8d961ae64e2589fdcad509a4d1b843ac20e62/core/src/main/java/hudson/model/Slave.java to try to understand how the NullPointerException comes about, but no luck:

Processing Slave vmbuild10
java.lang.NullPointerException
	at hudson.model.Slave.createLauncher(Slave.java:362)
	at hudson.model.Slave$createLauncher.call(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
	at Script1.run(Script1.groovy:14)
...

dragon788

unread,
Oct 13, 2015, 7:25:16 PM10/13/15
to job-dsl-plugin
Thanks Daniel for this inspiration. We needed to get the list of nodes (and whether they were online or offline) for a script we are calling via the Jenkins CLI to manage node states en-masse. This worked perfectly to throw into a Groovy script and pass via jenkins-cli.jar groovy <scriptname> -s http://jenkins-ci.local/ and then pipe that list over to another jenkins-cli call to 'offline-node $node' and online the nodes after an upgrade.
Reply all
Reply to author
Forward
0 new messages