Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins/Hudson CLI API to modify the node labels using Groovy

Does anyone know how to modify the Jenkins/Hudson node labels in a non-manually way? I mean, thorough an API like the CLI API that this tool offers (without restarting Jenkins/Hudson of course).

My guess is that the best option is using a Groovy script to enter into the Jenkins/Hudson guts. Executing something like:

java -jar -s HUDSON_URL:8080 groovy /path/to/groovy.groovy

Being the content of that script something like:

for (aSlave in hudson.model.Hudson.instance.slaves) {
   labels = aSlave.getAssignedLabels()
   println labels
   **aSlave.setLabel("blabla")** // this method doesn't exist, is there any other way???
}

Thanks in advance!

Victor

like image 262
victorgp Avatar asked Jan 11 '12 17:01

victorgp


People also ask

How do I add a Groovy script to Jenkins?

Usage. To create Groovy-based project, add new free-style project and select "Execute Groovy script" in the Build section, select previously configured Groovy installation and then type your command, or specify your script file name. In the second case path taken is relatively from the project workspace directory.

What is Jenkins node label?

When creating a new slave node, Jenkins allows us to tag a slave node with a label. Labels represent a way of naming one or more slaves. We leverage this labeling system to tie the execution of a job directly to one or more slave nodes.

What is Jenkins Groovy?

Groovy is a very powerful language which offers the ability to do practically anything Java can do including: Create sub-processes and execute arbitrary commands on the Jenkins controller and agents. It can even read files in which the Jenkins controller has access to on the host (like /etc/passwd )

How do I get a list of all jobs in Jenkins?

Get a list of jobs. This can be done requesting http://jenkins_url:port/api/json?tree=jobs[name,url] .


3 Answers

Note: the other answers are a bit old, so it could be that the API has appeared since then.

Node labels are accessed in the API as a single string, just like in the Configure screen.

To read and write labels: Node.getLabelString() and Node.setLabelString(String).

Note that you can get the effective labels as well via: Node.getAssignedLabels(), which returns a Collection of LabelAtom that includes dynamically computed labels such as the 'self-label' (representing the node name itself).

Last, these methods on the Node class are directly accessible from the slave objects also, e.g. as a System Groovy Script:

hudson = hudson.model.Hudson.instance
hudson.slaves.findAll { it.nodeName.equals("slave4") }.each { slave -> 
  print "Slave  $slave.nodeName : Labels: $slave.labelString"
  slave.labelString = slave.labelString + " " + "offline"
  println "   --> New labels: $slave.labelString"
}
hudson.save()
like image 114
Patrice M. Avatar answered Nov 06 '22 07:11

Patrice M.


I've found a way to do this using the Groovy Postbuild Plugin.

I have a Jenkins job that takes a few parameters (NodeToUpdate, LabelName, DesiredState) and executes this content in the Groovy Postbuild Plugin:

nodeName = manager.envVars['NodeToUpdate']
labelName = manager.envVars['LabelName']
set = manager.envVars['DesiredState']

for (node in jenkins.model.Jenkins.instance.nodes) {
    if (node.getNodeName().equals(nodeName)) {
        manager.listener.logger.println("Found node to update: " + nodeName)
        oldLabelString = node.getLabelString()
        if (set.equals('true')) {
            if (!oldLabelString.contains(labelName)) {
                manager.listener.logger.println("Adding label '" + labelName     + "' from node " + nodeName);
                newLabelString = oldLabelString + " " + labelName
                node.setLabelString(newLabelString)
                node.save()
            } else {
                manager.listener.logger.println("Label '" + labelName + "' already exists on node " + nodeName)
            }
        }
        else {
            if (oldLabelString.contains(labelName)) {
                manager.listener.logger.println("Removing label '" + labelName + "' from node " + nodeName)
                newLabelString = oldLabelString.replaceAll(labelName, "")
                node.setLabelString(newLabelString)
                node.save()
            } else {
                manager.listener.logger.println("Label '" + labelName + "' doesn't exist on node " + nodeName)
            }
        }
    }
}
like image 43
jpadams Avatar answered Nov 06 '22 07:11

jpadams


I've not seen a way yet to change the slave label either.

I've taken to editing the main config.xml file and issuing a reload from the CLI.

This has it's own problems though - any jobs currently running are lost until the next jenkins restart - see https://issues.jenkins-ci.org/browse/JENKINS-3265

like image 31
Captain Kerf Avatar answered Nov 06 '22 07:11

Captain Kerf