故障现象:
修改了jenkins从节点numExecutors字段(并发构建数)后
节点从列表中消失,无法分配任务,并且访问{JENKINS_URL}/computer/{NODE_NAME}/config.xml
也报404,无法再通过api对节点进行操作
以下是引发该问题的脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| def adjust_executors(target_num): """调整节点并发执行器数量""" config_url = f"{JENKINS_URL}/computer/{NODE_NAME}/config.xml" try: response = session.get(config_url, headers=crumb_header) root = ET.fromstring(response.content) num_exec = root.find(".//numExecutors") num_exec.text = str(target_num) response = session.post( config_url, headers={'Content-Type': 'text/xml'}, data=ET.tostring(root), auth=session.auth ) return response.status_code == 200 except Exception as e: print(f"× 调整并发数失败: {e}") return False
|
故障原因:
在jenkins中将从节点并发构建数改为0是非法操作:在web上根本不允许
但是如果使用修改config.xml的方式强行修改,就会出现这样的情况
解决方法:
进入{JENKINS_URL}/script
脚本控制台,使用groovy脚本运行下面这段脚本
其原理是通过主节点的jenkins节点目录,一般是/var/lib/jenkins/nodes/slave-5/
来找到config.xml,随后使用该config.xml重新创建节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import jenkins.model.Jenkins def nodeName = "slave-5" def instance = Jenkins.instance def node = instance.getNode(nodeName) if (node == null) { println "节点 ${nodeName} 不存在" return } try { // 1. 通过文件系统路径读取配置 def configFilePath = "${instance.rootDir}/nodes/${nodeName}/config.xml" def configFile = new File(configFilePath) if (!configFile.exists()) { throw new Exception("配置文件 ${configFilePath} 不存在") } def configXml = configFile.text println "备份配置成功:\n${configXml}" // 2. 删除原节点 instance.removeNode(node) println "节点已删除" // 3. 从 XML 重建节点 def newNode = Jenkins.XSTREAM.fromXML(configXml) instance.addNode(newNode) println "节点已成功重建" } catch (Exception e) { println "操作失败: ${e.message}" e.printStackTrace() }
|