-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Modified the API to use label strings from Constants.groovy - Added setDescription function Signed-off-by: Shubham Verma <shubhamv.sv@gmail.com>
- Loading branch information
Showing
4 changed files
with
267 additions
and
12 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,116 @@ | ||
@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; | ||
|
||
clones = [:] | ||
List<String> newMachines = new ArrayList<String>(); | ||
|
||
node { | ||
stage('jenkins made me do it') { | ||
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) { | ||
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, Constants.SSH_CREDENTIAL_ID, 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) { | ||
// TODO: Search for entire string instead of a part | ||
if (script.script.contains("ssh -C -i /data/jenkins/J9_Build/home/.ssh/j9build.key j9build@" + 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 will be used to get the names of machines that need to have description updated | ||
newMachines.add(newMachineName); | ||
} else { | ||
echo "Machine(${machines[index]}) already exists." | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
/* Iterates over the machine names and updated their descriptin with CPU count, | ||
* total memory and total space | ||
*/ | ||
for (String machine : newMachines) { | ||
String machineName = machine | ||
clones[machineName] = { | ||
node (machineName) { | ||
stage('Add_Labels') { | ||
echo "NODE_NAME = ${env.NODE_NAME}" | ||
|
||
Slave slave = (Slave)Jenkins.getInstance().getNode(env.NODE_NAME); | ||
NodeHelper nodeHelper = new NodeHelper(); | ||
|
||
String newMachineLabels = nodeHelper.constructLabels(env.NODE_NAME); | ||
for (String label : slave.getLabelString().split(" ")) { | ||
/* Here we check if the machine one of the above three labels | ||
* So that we can add them back to the machine. | ||
*/ | ||
if (label.contains(Constants.IGNORE_LABELS[0]) | ||
|| label.contains(Constants.IGNORE_LABELS[1]) | ||
|| label.contains(Constants.IGNORE_LABELS[2])) { | ||
newMachineLabels += " " + label; | ||
} | ||
} | ||
|
||
newMachineLabels = nodeHelper.addLabel(env.NODE_NAME, newMachineLabels); | ||
|
||
println "Machine ${env.NODE_NAME} was added with following labels ${newMachineLabels}"; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
parallel clones |
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,25 @@ | ||
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 = ""; | ||
|
||
static final String JENKINS_URL = "https://ci.adoptopenjdk.net/"; | ||
|
||
static final String OS_LABEL_FORMAT = "%s.%s.%s"; | ||
static final String GENERIC_LABEL_FORMAT = "%s.%s"; | ||
static final String OS_LABEL_PREFIX = "sw.os"; | ||
static final String ENDIAN_LABEL_PREFIX = "hw.endian"; | ||
static final String PLATFORM_LABEL_PREFIX = "hw.platform"; | ||
static final String ARCH_LABEL_PREFIX = "hw.arch"; | ||
static final String KERNEL_LABEL_PREFIX = "hw.kernel"; | ||
static final String HYPERVISOR_LABEL_PREFIX = "hw.hypervisor"; | ||
} | ||
|