Jenkins / Hudson CLI API for changing node labels using Groovy

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

I guess the best option is to use the Groovy script to enter Jenkins / Hudson's gut. Doing something like:

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

Being the contents of this script is 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

+6
source share
3 answers

Note: the other answers are a bit outdated, so it is possible that the API has appeared since then.

Node Shortcuts are accessed in the API as a single line, as on the Configuration screen.

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

Note that you can also get effective labels: Node.getAssignedLabels () , which returns a LabelAtom collection that includes dynamically calculated labels, such as "self-adhesive" (representing the name itself node).

Finally, these methods in the Node class are accessible directly from subordinate objects, for example. like a Groovy Script system:

 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() 
+4
source

I have not yet seen a way to change the driven label.

I started editing the main config.xml file and unloaded the reboot from the CLI.

This has its problems, though - any tasks currently performed are lost until the next jenkins restart - see https://issues.jenkins-ci.org/browse/JENKINS-3265

+1
source

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

I have a Jenkins job that takes several parameters (NodeToUpdate, LabelName, DesiredState) and executes this content in the Postwild Groovy 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) } } } } 
0
source

Source: https://habr.com/ru/post/905736/


All Articles