Skip to content

Commit 71f2980

Browse files
Some scripts related with Agents and Shared Agents (#39)
Scripts for : create an script to create Agents from a Jenkins instance disable agents on a Jenkins instance delete agents on a Jenkins instance
1 parent 1f4e7e1 commit 71f2980

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed

createAgentsScript.groovy

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import jenkins.model.Jenkins
2+
import hudson.slaves.*
3+
import hudson.plugins.sshslaves.*
4+
5+
def createLauncher(agentLauncher){
6+
if(agentLauncher.class.name == 'hudson.plugins.sshslaves.SSHLauncher'){
7+
return "new hudson.plugins.sshslaves.SSHLauncher('${agentLauncher.host}',${agentLauncher.port},'${agentLauncher.credentialsId}','${agentLauncher?.jvmOptions}','${agentLauncher?.javaPath}','${agentLauncher?.prefixStartSlaveCmd}','${agentLauncher?.suffixStartSlaveCmd}',${agentLauncher?.launchTimeoutSeconds},${agentLauncher?.maxNumRetries},${agentLauncher?.retryWaitTime})"
8+
} else if(agentLauncher.class.name == 'com.cloudbees.jenkins.plugins.sshslaves.SSHLauncher'){
9+
return "new com.cloudbees.jenkins.plugins.sshslaves.SSHLauncher('${agentLauncher.host}',new com.cloudbees.jenkins.plugins.sshslaves.SSHConnectionDetails('${agentLauncher.connectionDetails.credentialsId}',${agentLauncher.connectionDetails.port},'${agentLauncher.connectionDetails?.javaPath}','${agentLauncher.connectionDetails?.jvmOptions}','${agentLauncher.connectionDetails?.prefixStartSlaveCmd}','${agentLauncher.connectionDetails?.suffixStartSlaveCmd}',${agentLauncher.connectionDetails?.displayEnvironment}))"
10+
} else if (agentLauncher.class.name == 'hudson.slaves.JNLPLauncher' ||agentLauncher.class.name == 'com.cloudbees.opscenter.server.jnlp.slave.JocJnlpSlaveLauncher'){
11+
def tunnel = agentLauncher?.tunnel == null ? "" : agentLauncher?.tunnel
12+
def vmargs = agentLauncher?.vmargs == null ? "" : agentLauncher?.vmargs
13+
return "new hudson.slaves.JNLPLauncher('${tunnel}','${vmargs}')"
14+
}
15+
16+
return "NOT SUPPORTED - " + agentLauncher.getClass().getCanonicalName() + " - NOT SUPPORTED"
17+
}
18+
19+
def createAgent(agentName, agentDescription, agentHome, agentExecutors, agentLabels, launcher) {
20+
println "//Create Agent ${agentName}"
21+
//DumbSlave(String name, String nodeDescription, String remoteFS, String numExecutors, Node.Mode mode, String labelString, ComputerLauncher launcher, RetentionStrategy retentionStrategy)
22+
println "Jenkins.instance.addNode(new DumbSlave('${agentName}', '${agentDescription}', '${agentHome}', '${agentExecutors}', Mode.NORMAL, '${agentLabels}', ${launcher}, new RetentionStrategy.Always()))"
23+
}
24+
25+
26+
println '''
27+
import jenkins.model.*
28+
import hudson.model.*
29+
import hudson.slaves.*
30+
import hudson.plugins.sshslaves.*
31+
import com.cloudbees.opscenter.server.model.*
32+
import hudson.model.Node.Mode
33+
'''
34+
35+
// Jenkins Master and slaves
36+
Jenkins.instance.computers.grep {
37+
it.class.superclass?.simpleName != 'AbstractCloudComputer' &&
38+
it.class.superclass?.simpleName != 'AbstractCloudSlave' &&
39+
it.class.simpleName != 'EC2AbstractSlave'
40+
}.each {
41+
if (!(it instanceof jenkins.model.Jenkins.MasterComputer)) {
42+
createAgent(it.name, it.displayName, it.getNode().getRemoteFS(), it.numExecutors, it.getNode().getLabelString(), createLauncher(it.getLauncher()))
43+
}
44+
}
45+
46+
// CJOC Shared Slaves
47+
Jenkins.instance.allItems.grep {
48+
it.class.name == 'com.cloudbees.opscenter.server.model.SharedSlave'
49+
}.each {
50+
createAgent(it.name, it.displayName, it.getRemoteFS(), it.numExecutors, it.getLabelString(), createLauncher(it.getLauncher()))
51+
}
52+
53+
println '''
54+
//END_OF_SCRIPT
55+
56+
'''

deleteAgents.groovy

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.*
2+
import jenkins.model.*
3+
import hudson.model.*
4+
import hudson.slaves.*
5+
import static com.cloudbees.opscenter.server.persistence.SlaveLeaseTable.getLeases;
6+
7+
// Set this to false once you run the script and checked the output
8+
dryRun = true
9+
10+
def goDelete(aSlave) {
11+
println "Agent: " + aSlave.name
12+
def online = false
13+
def busy = false
14+
if (!(aSlave instanceof com.cloudbees.opscenter.server.model.SharedSlave)) {
15+
online = aSlave.isOnline()
16+
busy = aSlave.countBusy() != 0
17+
} else {
18+
online = aSlave.getOfflineCause() == null
19+
def leases = getLeases(aSlave.getUid())
20+
busy = leases != null && !leases.isEmpty()
21+
}
22+
println('\tcomputer.isOnline: ' + online)
23+
println('\tcomputer.countBusy: ' + busy)
24+
if (!busy && !online && !dryRun) {
25+
if (aSlave instanceof com.cloudbees.opscenter.server.model.SharedSlave) {
26+
aSlave.doDoDelete()
27+
} else {
28+
aSlave.delete()
29+
}
30+
println('\tIs Deleted')
31+
} else if(dryRun) {
32+
println "SIMULATION MODE: Not Delete ${aSlave.name}, set dryRun variable to false if you wish to run it for real"
33+
} else {
34+
println('\tIs NOT Deleted')
35+
}
36+
37+
println('\tIs now offline :' + !online)
38+
39+
}
40+
41+
// Jenkins Master and slaves
42+
Jenkins.instance.computers.grep {
43+
it.class.superclass?.simpleName != 'AbstractCloudComputer' &&
44+
it.class.superclass?.simpleName != 'AbstractCloudSlave' &&
45+
it.class.simpleName != 'EC2AbstractSlave'
46+
}.each {
47+
if (!(it instanceof jenkins.model.Jenkins.MasterComputer)) {
48+
goDelete(it)
49+
}
50+
}
51+
52+
// CJOC Shared Slaves
53+
Jenkins.instance.allItems.grep {
54+
it.class.name == 'com.cloudbees.opscenter.server.model.SharedSlave'
55+
}.each {
56+
goDelete(it)
57+
}

disableAgents.groovy

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.*
2+
import jenkins.model.*
3+
import hudson.model.*
4+
import hudson.slaves.*
5+
import static com.cloudbees.opscenter.server.persistence.SlaveLeaseTable.getLeases;
6+
7+
// Set this to false once you run the script and checked the output
8+
dryRun = true
9+
10+
def goOffline(aSlave) {
11+
println "Agent: " + aSlave.name
12+
def online = false
13+
def busy = false
14+
if (!(aSlave instanceof com.cloudbees.opscenter.server.model.SharedSlave)) {
15+
online = aSlave.isOnline()
16+
busy = aSlave.countBusy() != 0
17+
} else {
18+
online = aSlave.getOfflineCause() == null
19+
def leases = getLeases(aSlave.getUid())
20+
busy = leases != null && !leases.isEmpty()
21+
}
22+
println('\tcomputer.isOnline: ' + online)
23+
println('\tcomputer.countBusy: ' + busy)
24+
if (!busy && online && !dryRun) {
25+
if (aSlave instanceof com.cloudbees.opscenter.server.model.SharedSlave) {
26+
aSlave.setDisabled(true)
27+
online = aSlave.getOfflineCause() == null
28+
} else {
29+
aSlave.doDoDisconnect("Setted offline by script")
30+
online = aSlave.isOnline()
31+
}
32+
} else if(dryRun) {
33+
println "SIMULATION MODE: Not disconnecting ${aSlave.name}, set dryRun variable to false if you wish to run it for real"
34+
}
35+
36+
println('\tIs now offline :' + !online)
37+
38+
}
39+
40+
// Jenkins Master and slaves
41+
Jenkins.instance.computers.grep {
42+
it.class.superclass?.simpleName != 'AbstractCloudComputer' &&
43+
it.class.superclass?.simpleName != 'AbstractCloudSlave' &&
44+
it.class.simpleName != 'EC2AbstractSlave'
45+
}.each {
46+
if (!(it instanceof jenkins.model.Jenkins.MasterComputer)) {
47+
goOffline(it)
48+
}
49+
}
50+
51+
// CJOC Shared Slaves
52+
Jenkins.instance.allItems.grep {
53+
it.class.name == 'com.cloudbees.opscenter.server.model.SharedSlave'
54+
}.each {
55+
goOffline(it)
56+
}

0 commit comments

Comments
 (0)