... keeping update ...
- via CLI
$ ssh domain.jenkins.com groovy =< /path/to/script.groovy
- via GUI: copy & paste script into https://domina.jenkins.com/script
... keeping update ...
$ ssh domain.jenkins.com groovy =< /path/to/script.groovy
#!/usr/bin/env groovy | |
/** | |
* @author marslo | |
* @since 02/16/2024 | |
* | |
* @result | |
* | AGENT NAME | NODE CREDENTIAL | COMPUTER CREDENTIIAL | | |
* | ------------------ | --------------- | -------------------- | | |
* | STAGING_TEST_01 | SSH_CREDENTIAL | SSH_CREDENTIAL | | |
* | DEVELOPMENT_ENV_03 | SSH_CREDENTIAL | SSH_CREDENTIAL | | |
**/ | |
List<String> title = [ 'AGENT NAME', 'NODE CREDENTIAL', 'COMPUTER CREDENTIIAL' ] | |
List<List<String>> agentCredentials = jenkins.model.Jenkins.instance.computers.findAll { computer -> | |
! jenkins.model.Jenkins.MasterComputer.isInstance(computer) && | |
computer?.launcher instanceof hudson.plugins.sshslaves.SSHLauncher | |
}.collect { computer -> | |
[ computer.name, computer.node.launcher?.credentialsId?.toString() ?: '', computer.launcher?.credentialsId?.toString() ?: '' ] | |
} | |
agentCredentials.add( 0, title ) | |
agentCredentials.add( 0, agentCredentials.transpose().collect { column -> column.collect{ it.size() }.max() } ) | |
agentCredentials = agentCredentials.withIndex().collect { raw, idx -> | |
if ( idx ) raw.withIndex().collect { x, y -> x.toString().padRight(agentCredentials[0][y]) } | |
}.findAll() | |
String showTable ( List l ) { | |
l.collect{ '| ' + it.join(' | ' ) + ' |' }.join('\n') | |
} | |
println showTable( [ agentCredentials.head(), agentCredentials.head().collect { '-'*it.size() } ] ) | |
println showTable( agentCredentials.tail() ) | |
// vim:tabstop=2:softtabstop=2:shiftwidth=2:expandtab:filetype=Groovy |
#!/usr/bin/env groovy | |
/** | |
* @author marslo | |
* @since 02/16/2024 | |
* | |
* @result | |
* STAGING_TEST_01 | SSH_CREDENTIAL | SSH_CREDENTIAL | |
* DEVELOPMENT_ENV_03 | SSH_CREDENTIAL | SSH_CREDENTIAL | |
**/ | |
List<List<String>> agentCredentials = jenkins.model.Jenkins.instance.computers.findAll { computer -> | |
! jenkins.model.Jenkins.MasterComputer.isInstance(computer) && | |
computer?.launcher instanceof hudson.plugins.sshslaves.SSHLauncher | |
}.collect { computer -> | |
[ computer.name, computer.node.launcher?.credentialsId?.toString() ?: '', computer.launcher?.credentialsId?.toString() ?: '' ] | |
} | |
agentCredentials.add( 0, agentCredentials.transpose().collect { column -> column.collect{ it.size() }.max() } ) | |
println agentCredentials.withIndex().collect { raw, idx -> | |
if ( idx ) { | |
raw.withIndex().collect { x, y -> "${x.padRight(agentCredentials[0][y])}" }.join(' | ') | |
} | |
}.findAll().join('\n') | |
// vim:tabstop=2:softtabstop=2:shiftwidth=2:expandtab:filetype=Groovy |
#!/usr/bin/env groovy | |
/** | |
* @author marslo | |
* @since 02/14/2024 | |
**/ | |
import hudson.slaves.* | |
import hudson.model.Node.Mode | |
import jenkins.model.Jenkins | |
import hudson.plugins.sshslaves.SSHLauncher | |
String newCredId = 'NEW_CREDENTIAL' | |
jenkins.model.Jenkins.instance.nodes.findAll { node -> | |
! jenkins.model.Jenkins.MasterComputer.isInstance(node) && | |
node?.launcher instanceof hudson.plugins.sshslaves.SSHLauncher | |
}.each { node -> | |
println ">> ${node.name} update <<" | |
ComputerLauncher launcher = node.launcher | |
SSHLauncher newLauncher = new SSHLauncher( launcher.host, | |
launcher.port, | |
newCredId, | |
launcher.jvmOptions, | |
launcher.javaPath, | |
launcher.prefixStartSlaveCmd, | |
launcher.suffixStartSlaveCmd, | |
launcher.launchTimeoutSeconds, | |
launcher.maxNumRetries, | |
launcher.retryWaitTime, | |
launcher.sshHostKeyVerificationStrategy | |
) | |
DumbSlave agent = new DumbSlave( node.name, node.remoteFS, newLauncher ) | |
agent.nodeDescription = node.nodeDescription | |
agent.numExecutors = node.numExecutors | |
agent.labelString = node.labelString | |
agent.mode = node.mode | |
agent.retentionStrategy = node.retentionStrategy | |
node.computer.doDoDelete() // delete if necessary | |
Thread.sleep( 5*1000 ) | |
jenkins.model.Jenkins.instance.addNode( agent ) | |
} | |
"DONE" |
#!/usr/bin/env groovy | |
/** | |
* @author marslo | |
* @since 02/14/2024 | |
**/ | |
import jenkins.model.Jenkins | |
import hudson.plugins.sshslaves.SSHLauncher | |
import hudson.slaves.ComputerLauncher | |
String newCredId = 'NEW_CREDENTIAL' | |
jenkins.model.Jenkins.instance.nodes.findAll { node -> | |
! jenkins.model.Jenkins.MasterComputer.isInstance(node) && | |
node?.launcher instanceof hudson.plugins.sshslaves.SSHLauncher | |
}.each { node -> | |
println ">> update ${node.name} <<" | |
ComputerLauncher launcher = node.launcher | |
SSHLauncher newLauncher = new SSHLauncher( launcher.host, launcher.port, newCredId ) | |
newLauncher.sshHostKeyVerificationStrategy = launcher.sshHostKeyVerificationStrategy | |
node.setLauncher( newLauncher ) | |
node.save() | |
node.computer.setNode( node ) | |
// disconnect agent | |
if ( node.computer.isOnline() && node.computer.countBusy() == 0 ) { | |
println ">> disconnet ${node.name} <<" | |
String message = 'disconnect due to credential update' | |
node.computer.setTemporarilyOffline( true, new hudson.slaves.OfflineCause.ByCLI(message) ) | |
node.computer.disconnect( new hudson.slaves.OfflineCause.UserCause(User.current(), message ) ) | |
node.computer.doChangeOfflineCause( message ) | |
println '\t.. computer.getOfflineCause: ' + node.computer.getOfflineCause(); | |
} | |
// connect agent | |
Thread.sleep( 5*1000 ) | |
if ( node.computer.isOffline() ) { | |
println ">> reconnect ${node.name} <<" | |
node.getComputer().connect( true ) | |
node.computer.setTemporarilyOffline( false, null ) | |
} | |
println ">> ${node.name} DONE <<" | |
} | |
"DONE" | |
// vim:tabstop=2:softtabstop=2:shiftwidth=2:expandtab:filetype=Groovy |
#!/usr/bin/env groovy | |
/** | |
* @author marslo | |
* @since 02/14/2024 | |
**/ | |
import hudson.slaves.* | |
import hudson.model.Node.Mode | |
import jenkins.model.Jenkins | |
import hudson.plugins.sshslaves.SSHLauncher | |
// add/modify in newInfo | |
Map<String, Object> newInfo = [ | |
credId : 'NEW_CREDENTIAL' , | |
name : '' , | |
label : '' , | |
description : '' , | |
hostname : '' , | |
numExecutors : 1 , | |
port : 22 | |
] | |
jenkins.model.Jenkins.instance.nodes.findAll { node -> | |
! jenkins.model.Jenkins.MasterComputer.isInstance(node) && | |
node?.launcher instanceof hudson.plugins.sshslaves.SSHLauncher | |
}.each { node -> | |
println ">> update ${node.name} <<" | |
ComputerLauncher launcher = node.launcher | |
SSHLauncher newLauncher = new SSHLauncher( newInfo.get('hostname') ?: launcher.host, | |
newInfo.get('port') ?: launcher.port, | |
newInfo.get('credId') ?: launcher.credentialsId, | |
launcher.jvmOptions, | |
launcher.javaPath, | |
launcher.prefixStartSlaveCmd, | |
launcher.suffixStartSlaveCmd, | |
launcher.launchTimeoutSeconds, | |
launcher.maxNumRetries, | |
launcher.retryWaitTime, | |
launcher.sshHostKeyVerificationStrategy | |
) | |
node.nodeDescription = newInfo.get('description') ?: node.nodeDescription | |
node.numExecutors = newInfo.get('numExecutors') ?: node.numExecutors | |
node.labelString = newInfo.get('label') ?: node.labelString | |
node.mode = node.mode | |
node.retentionStrategy = node.retentionStrategy | |
node.setLauncher( newLauncher ) | |
node.save() | |
node.computer.setNode( node ) | |
// disconnect agent | |
if ( node.computer.isOnline() && node.computer.countBusy() == 0 ) { | |
println ">> disconnect ${node.name} <<" | |
String message = 'disconnect due to credential update' | |
node.computer.setTemporarilyOffline( true, new hudson.slaves.OfflineCause.ByCLI(message) ) | |
node.computer.disconnect( new hudson.slaves.OfflineCause.UserCause(User.current(), message ) ) | |
node.computer.doChangeOfflineCause( message ) | |
println '\t.. computer.getOfflineCause: ' + node.computer.getOfflineCause(); | |
} | |
// connect agent | |
Thread.sleep( 5*1000 ) | |
if ( node.computer.isOffline() ) { | |
println ">> reconnect ${node.name} <<" | |
node.getComputer().connect( true ) | |
node.computer.setTemporarilyOffline( false, null ) | |
} | |
} | |
"DONE" | |
// vim:tabstop=2:softtabstop=2:shiftwidth=2:expandtab:filetype=Groovy |