Created
October 11, 2022 05:11
-
-
Save xiaojueguan/603545688d0fe31f83c6b9183a582c91 to your computer and use it in GitHub Desktop.
use jenkins script to create ssh slaves from json string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hudson.model.* | |
import jenkins.model.* | |
import hudson.slaves.* | |
import hudson.slaves.EnvironmentVariablesNodeProperty.Entry | |
import hudson.plugins.sshslaves.verifiers.* | |
import groovy.json.JsonSlurper | |
// https://docs.cloudbees.com/docs/cloudbees-ci-kb/latest/client-and-managed-masters/create-agent-node-from-groovy | |
def createSlave(name, homeDir, host, port, credential, nodeDescription, numExecutors, labelString, mode, retentionStrategy) { | |
// Pick one of the strategies from the comments below this line | |
SshHostKeyVerificationStrategy hostKeyVerificationStrategy = new NonVerifyingKeyVerificationStrategy() | |
//= new KnownHostsFileKeyVerificationStrategy() // Known hosts file Verification Strategy | |
//= new ManuallyProvidedKeyVerificationStrategy("<your-key-here>") // Manually provided key Verification Strategy | |
//= new ManuallyTrustedKeyVerificationStrategy(false /*requires initial manual trust*/) // Manually trusted key Verification Strategy | |
//= new NonVerifyingKeyVerificationStrategy() // Non verifying Verification Strategy | |
// Define a "Launch method": "Launch agents via SSH" | |
ComputerLauncher launcher = new hudson.plugins.sshslaves.SSHLauncher( | |
host, // Host | |
port, // Port | |
credential, // Credentials | |
(String)null, // JVM Options | |
(String)null, // JavaPath | |
(String)null, // Prefix Start Agent Command | |
(String)null, // Suffix Start Agent Command | |
(Integer)null, // Connection Timeout in Seconds | |
(Integer)null, // Maximum Number of Retries | |
(Integer)null, // The number of seconds to wait between retries | |
hostKeyVerificationStrategy // Host Key Verification Strategy | |
) | |
Slave agent = new DumbSlave( | |
name, | |
homeDir, | |
launcher) | |
agent.nodeDescription = nodeDescription | |
agent.numExecutors = numExecutors | |
agent.mode = mode | |
agent.retentionStrategy = retentionStrategy | |
agent.setLabelString(labelString) | |
Jenkins.instance.addNode(agent) | |
} | |
def createSlavesByJsonString(jsonStr) { | |
jsonSlurper = new JsonSlurper() | |
slaves = jsonSlurper.parseText(jsonStr) | |
slaves.each{slave-> | |
createSlave(slave['name'], '/root/jenkins_test', slave['host'], 22, slave['credential'], slave['nodeDescription'], slave['numExecutors'], slave['labelString'], Node.Mode.NORMAL, new RetentionStrategy.Always()) | |
} | |
} | |
// example | |
createSlavesByJsonString('[{"name":"build-node-01","home_dir":"/root/","host":"10.10.1.1","credential":"linux-root","port":22,"nodeDescription":"","numExecutors":1,"labelString":"linux build","mode":"NORMAL"}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment