删除所有脱机节点,调整下面的脚本并运行doDelete()只对奴隶其中IsOffline之前()是真还是isOnline()是假的。如果你想删除所有节点(小心),则不要使用以下如果声明:
if (aSlave.name.indexOf(slaveStartsWith) == 0) {
我也忽略一个奴隶(如果你想一直忽略的奴隶被删除)。它可以被增强以使用奴隶列表来忽略。
不管怎么说,下面的脚本将正常删除任何詹金斯节点奴隶与给定的名称开始(让你有更多的控制),它会下线标记(ASAP),但只有后的任何正在运行的作业删除( s)在给定的奴隶上是完整的。以为我应该在这里分享。
使用詹金斯Scriptler插件,可以导入/上传/运行此脚本:https://github.com/gigaaks/jenkins-scripts/blob/7eaf41348e886db108bad9a72f876c3827085418/scriptler/disableSlaveNodeStartsWith.groovy
/*** BEGIN META {
"name" : "Disable Jenkins Hudson slaves nodes gracefully for all slaves starting with a given value",
"comment" : "Disables Jenkins Hudson slave nodes gracefully - waits until running jobs are complete.",
"parameters" : [ 'slaveStartsWith'],
"core": "1.350",
"authors" : [
{ name : "GigaAKS" }, { name : "Arun Sangal" }
]
} END META**/
// This scriptler script will mark Jenkins slave nodes offline for all slaves which starts with a given value.
// It will wait for any slave nodes which are running any job(s) and then delete them.
// It requires only one parameter named: slaveStartsWith and value can be passed as: "swarm-".
import java.util.*
import jenkins.model.*
import hudson.model.*
import hudson.slaves.*
def atleastOneSlaveRunnning = true;
def time = new Date().format("HH:mm MM/dd/yy z",TimeZone.getTimeZone("EST"))
while (atleastOneSlaveRunnning) {
//First thing - set the flag to false.
atleastOneSlaveRunnning = false;
time = new Date().format("HH:mm MM/dd/yy z",TimeZone.getTimeZone("EST"))
for (aSlave in hudson.model.Hudson.instance.slaves) {
println "-- Time: " + time;
println ""
//Dont do anything if the slave name is "ansible01"
if (aSlave.name == "ansible01") {
continue;
}
if (aSlave.name.indexOf(slaveStartsWith) == 0) {
println "Active slave: " + aSlave.name;
println('\tcomputer.isOnline: ' + aSlave.getComputer().isOnline());
println('\tcomputer.countBusy: ' + aSlave.getComputer().countBusy());
println ""
if (aSlave.getComputer().isOnline()) {
aSlave.getComputer().setTemporarilyOffline(true,null);
println('\tcomputer.isOnline: ' + aSlave.getComputer().isOnline());
println ""
}
if (aSlave.getComputer().countBusy() == 0) {
time = new Date().format("HH:mm MM/dd/yy z",TimeZone.getTimeZone("EST"))
println("-- Shutting down node: " + aSlave.name + " at " + time);
aSlave.getComputer().doDoDelete();
} else {
atleastOneSlaveRunnning = true;
}
}
}
//Sleep 60 seconds
if(atleastOneSlaveRunnning) {
println ""
println "------------------ sleeping 60 seconds -----------------"
sleep(60*1000);
println ""
}
}
现在,我可以创造一个自由风格的詹金斯的工作,在生成操作使用Scriptler script,并使用上述脚本适当地删除从给定名称开始的奴隶(将作业参数传递给脚本程序脚本)。
如果你足够快,以获得以下错误消息,这意味着,你跑或者叫做Scriptler脚本在工作(如上图所示)和限制了工作到运行在非-master又名节点/从属机器。 Scriptler脚本是SYSTEM Groovy脚本即他们必须在詹金斯大师的JVM运行访问所有詹金斯资源/调整它们。要解决以下问题,您可以创建一个作业(限制它的主服务器即上运行詹金斯主JVM),这将只接受在scriptler脚本一个参数,并呼吁从第一份工作这项工作(如触发器,直到作业项目/任务和块完成):
21:42:43 Execution of script [disableSlaveNodesWithPattern.groovy] failed - java.lang.NullPointerException: Cannot get property 'slaves' on null objectorg.jenkinsci.plugins.scriptler.util.GroovyScript$ScriptlerExecutionException: java.lang.NullPointerException: Cannot get property 'slaves' on null object
21:42:43 at org.jenkinsci.plugins.scriptler.util.GroovyScript.call(GroovyScript.java:131)
21:42:43 at hudson.remoting.UserRequest.perform(UserRequest.java:118)
21:42:43 at hudson.remoting.UserRequest.perform(UserRequest.java:48)
21:42:43 at hudson.remoting.Request$2.run(Request.java:328)
21:42:43 at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:72)
21:42:43 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
21:42:43 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
21:42:43 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
21:42:43 at java.lang.Thread.run(Thread.java:745)
21:42:43 Caused by: java.lang.NullPointerException: Cannot get property 'slaves' on null object
即 如果你有在作业中运行的Scriptler脚本构建步骤(不在MASTER Jenkins机器/ JVM上运行),那么上述错误将会出现并解决,创建一个作业“disableSlaveNodesStartsWith”并将其限制为在master上运行(更安全侧)并调用Scriptler脚本并将参数传递给作业/脚本。现在
,从其他的工作,把这种工作:
![enter image description here](https://i.stack.imgur.com/80O5v.jpg)
也许你可以使用https://wiki.jenkins-ci.org/display/JENKINS/Throttle+Concurrent+Builds+插件为这些作业创建一个类别,并且一次只允许一个作业。 –
@Tizkiko感谢分享。我认为这是设置给定作业的最大#来运行任何作业的OR max#以在给定节点/从属节点上运行。在我的情况下,我希望这个工作能够阻止/等待,直到所有以给定名称开始的节点从属节点都完成了对它们正在运行的任何正在进行的工作。这在我的情况下是必需的,因为我在一台机器上创建这些奴隶,并自动配置/动态地将它们撕掉 - 但不希望在节点/奴隶闲置之前发生撕裂部分。 –