forked from adoptium/jenkins-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added CreateNewNode job - Updated readme file to include the new job - Moved string constants to the vars directory of the Node helper API - added a input parameter for SSH key id in CreateNewNode issue adoptium#10 Signed-off-by: Shubham Verma <[email protected]>
- Loading branch information
Showing
4 changed files
with
112 additions
and
7 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
@Library('NodeHelper') _ | ||
import hudson.slaves.CommandLauncher; | ||
import hudson.plugins.sshslaves.SSHLauncher | ||
import hudson.model.Node.Mode; | ||
import hudson.plugins.sshslaves.verifiers.NonVerifyingKeyVerificationStrategy; | ||
import jenkins.model.Jenkins; | ||
import hudson.model.Computer; | ||
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval; | ||
import hudson.model.Slave; | ||
import hudson.slaves.JNLPLauncher; | ||
|
||
node { | ||
stage('AddNewNode') { | ||
def nodeHelper = new NodeHelper(); | ||
|
||
String[] machines = params.machineNames.split(",") | ||
String[] machineIPs = params.machineIPs.split(",") | ||
String[] labels = params.labelStrings.split(",") | ||
|
||
def launcher | ||
String remoteFS | ||
String newMachineLabels | ||
String os | ||
String newMachineName | ||
|
||
for (int index = 0; index < machineIPs.length; index++) { | ||
|
||
if (nodeHelper.getComputer(machines[index]) != null) { | ||
println "Machine(${machines[index]}) already exists." | ||
} else { | ||
if (Integer.parseInt(machineIPs[index].split("\\.")[0]) == 10) { | ||
launcher = new CommandLauncher(Constants.SSH_COMMAND + "${machineIPs[index]} " + Constants.WGET_SLAVE_JAR); | ||
remoteFS = Constants.REMOTE_FS; | ||
} else if (machines[index].contains("win")) { | ||
launcher = new JNLPLauncher("", "", new jenkins.slaves.RemotingWorkDirSettings(false, "", "remoting", false)); | ||
remoteFS = Constants.WIN_REMOTE_FS; | ||
} else { | ||
launcher = new SSHLauncher( | ||
machines[index], | ||
22, | ||
params.SSHCredentialId.isEmpty() ? Constants.SSH_CREDENTIAL_ID : params.SSHCredentialId, | ||
null, null, null, null, null, null, null, | ||
new NonVerifyingKeyVerificationStrategy()); | ||
remoteFS = Constants.REMOTE_FS; | ||
} | ||
|
||
newMachineLabels = labels[index%labels.length] | ||
|
||
newMachineName = nodeHelper.addNewNode( | ||
machines[index], | ||
machineIPs[index], | ||
remoteFS, | ||
1, // Number of executers | ||
Mode.EXCLUSIVE, | ||
newMachineLabels.toLowerCase(), | ||
launcher | ||
); | ||
|
||
// This part is to approve the script used to add a 10. machine | ||
def scripts = ScriptApproval.get() | ||
def scriptSet = scripts.getPendingScripts() | ||
def iterator = scriptSet.iterator() | ||
if (launcher.getClass().toString().contains("slaves.CommandLauncher")) { | ||
for (ScriptApproval.PendingScript script : scriptSet) { | ||
if (script.script.contains(Constants.SSH_COMMAND + machineIPs[index])) { | ||
println "Script Approved" | ||
scripts.approveScript(script.getHash()); | ||
} | ||
} | ||
(Jenkins.getInstance().getComputer(newMachineName)).connect(false); | ||
} | ||
|
||
println "Machine ${newMachineName} was added with following labels ${newMachineLabels}"; | ||
} | ||
} | ||
|
||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Constants { | ||
static final String[] IGNORE_LABELS = ['.ignore']; | ||
static final String REMOTE_FS = "/home/jenkins"; | ||
static final String WIN_REMOTE_FS = "C:\\Users\\jenkins"; | ||
|
||
// This the key that'll be used for SSHLauncher in CreateNewNode | ||
static final String SSH_CREDENTIAL_ID = ""; | ||
|
||
static final String SLAVE_JAR_LOCATION = "<jenkins_URL/>/jnlpJars/slave.jar"; | ||
static final String WGET_SLAVE_JAR = "\"wget -q --no-check-certificate -O slave.jar ${SLAVE_JAR_LOCATION} ; java -jar slave.jar\""; | ||
static final String SSH_COMMAND = "ssh -C -i ${SSH_KEY_LOCATION} <userName/>@"; | ||
static final String SSH_KEY_LOCATION = ""; | ||
} | ||
|