Jenkins/Hudson CLI API使用Groovy修改节点标签 [英] Jenkins/Hudson CLI API to modify the node labels using Groovy
问题描述
有人知道如何以非手动方式修改Jenkins/Hudson节点标签吗?我的意思是,请透彻使用该工具提供的类似CLI API的API(当然,无需重启Jenkins/Hudson).
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).
我的猜测是最好的选择是使用Groovy脚本进入Jenkins/Hudson胆量.执行类似的内容:
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
java -jar -s HUDSON_URL:8080 groovy /path/to/groovy.groovy
该脚本的内容如下:
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???
}
提前谢谢!
维克多
推荐答案
注意:其他答案有些陈旧,因此可能是从那时起API出现了.
Note: the other answers are a bit old, so it could be that the API has appeared since then.
节点标签在API中作为单个字符串访问,就像在配置"屏幕中一样.
Node labels are accessed in the API as a single string, just like in the Configure screen.
要读取和写入标签,请执行以下操作: Node.getLabelString( )和 Node.setLabelString(字符串).
To read and write labels: Node.getLabelString() and Node.setLabelString(String).
请注意,您也可以通过以下方式获取有效标签: Node.getAssignedLabels(),它返回LabelAtom的集合,其中包括动态计算的标签,例如自我标签"(代表节点名称本身).
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).
最后,可以直接从以下位置访问 Node 类上的这些方法从属对象也可以,例如作为系统Groovy脚本:
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()
这篇关于Jenkins/Hudson CLI API使用Groovy修改节点标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!