使用 Groovy 修改节点标签的 Jenkins/Hudson CLI API

发布于 2024-12-26 03:13:27 字数 499 浏览 3 评论 0原文

有谁知道如何以非手动方式修改 Jenkins/Hudson 节点标签?我的意思是,彻底的 API,如该工具提供的 CLI API(当然无需重新启动 Jenkins/Hudson)。

我的猜测是,最好的选择是使用 Groovy 脚本进入 Jenkins/Hudson 的内部。执行如下内容:

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???
}

提前致谢!

胜利者

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天涯沦落人 2025-01-02 03:13:27

注意:其他答案有点旧,所以 API 可能是从那时起就出现的。

节点标签在 API 中作为单个字符串进行访问,就像在“配置”屏幕中一样。

读取和写入标签: Node.getLabelString() 和 Node.setLabelString(String)

请注意,您也可以通过以下方式获取有效标签: Node.getAssignedLabels (),它返回一个 LabelAtom 集合,其中包括动态计算的标签,例如“自标签”(代表节点名称本身)。

最后,Node 类上的这些方法可以直接从从属对象访问另外,例如作为系统 Groovy 脚本:

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()

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()
穿越时光隧道 2025-01-02 03:13:27

我找到了一种使用 Groovy Postbuild 插件来做到这一点的方法。

我有一个 Jenkins 作业,它需要一些参数(NodeToUpdate、LabelName、DesiredState)并在 Groovy Postbuild 插件中执行此内容:

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)
            }
        }
    }
}

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)
            }
        }
    }
}
趁微风不噪 2025-01-02 03:13:27

我还没有找到改变奴隶标签的方法。

我已经开始编辑主 config.xml 文件并从 CLI 发出重新加载。

但这有它自己的问题 - 当前运行的任何作业都会丢失,直到下一次 jenkins 重新启动 - 请参阅 https ://issues.jenkins-ci.org/browse/JENKINS-3265

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文