forked from cloudbees/jenkins-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disableAgents.groovy
56 lines (50 loc) · 1.78 KB
/
disableAgents.groovy
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.util.*
import jenkins.model.*
import hudson.model.*
import hudson.slaves.*
import static com.cloudbees.opscenter.server.persistence.SlaveLeaseTable.getLeases;
// Set this to false once you run the script and checked the output
dryRun = true
def goOffline(aSlave) {
println "Agent: " + aSlave.name
def online = false
def busy = false
if (!(aSlave instanceof com.cloudbees.opscenter.server.model.SharedSlave)) {
online = aSlave.isOnline()
busy = aSlave.countBusy() != 0
} else {
online = aSlave.getOfflineCause() == null
def leases = getLeases(aSlave.getUid())
busy = leases != null && !leases.isEmpty()
}
println('\tcomputer.isOnline: ' + online)
println('\tcomputer.countBusy: ' + busy)
if (!busy && online && !dryRun) {
if (aSlave instanceof com.cloudbees.opscenter.server.model.SharedSlave) {
aSlave.setDisabled(true)
online = aSlave.getOfflineCause() == null
} else {
aSlave.doDoDisconnect("Setted offline by script")
online = aSlave.isOnline()
}
} else if(dryRun) {
println "SIMULATION MODE: Not disconnecting ${aSlave.name}, set dryRun variable to false if you wish to run it for real"
}
println('\tIs now offline :' + !online)
}
// Jenkins Master and slaves
Jenkins.instance.computers.grep {
it.class.superclass?.simpleName != 'AbstractCloudComputer' &&
it.class.superclass?.simpleName != 'AbstractCloudSlave' &&
it.class.simpleName != 'EC2AbstractSlave'
}.each {
if (!(it instanceof jenkins.model.Jenkins.MasterComputer)) {
goOffline(it)
}
}
// CJOC Shared Slaves
Jenkins.instance.allItems.grep {
it.class.name == 'com.cloudbees.opscenter.server.model.SharedSlave'
}.each {
goOffline(it)
}