Skip to content

Commit

Permalink
Jenkins Create new node job
Browse files Browse the repository at this point in the history
- Added CreateNewNode job
- Updated readme file to include the new job
- Moved string constants to the vars directory of the Node helper API

Issue adoptium#10
  • Loading branch information
VermaSh committed Apr 9, 2018
1 parent 6e8c8f2 commit 65201ef
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 0 deletions.
124 changes: 124 additions & 0 deletions Jenkins_jobs/CreateNewNode.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
@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();

if (params.machineNames.length() > 0){
String[] machines = params.machineNames.split(",")
}
if (params.machineIPs.length() > 0){
String[] machines = params.machineNames.split(",")
}
if (params.labelStrings.length() > 0){
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 (true || 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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ Iterates over online nodes on Jenkins and prints the contents of the workspace d
* The computers it iterates over can be limited by input parameter, ```projectLabel```
* As of now, it only works for linux, aix, and mac

### Create New Node (CreateNewNode.groovy)
Used to create new nodes with any basic labels

* This job expects 3 parameters
* ```String machineNames```
* Host names of the machine(s), seperated by ```,```
* ```String machineIPs```
* ip address of the machine(s), seperated by ```,```
* ```String labelStrings```
* Labels you would like to be added to the machine. Seperate the labels for a single machine with spaces. Use ```,``` if doing batch update

## How-to

### Setup
Expand Down
13 changes: 13 additions & 0 deletions vars/Constants.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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 = "";
}

0 comments on commit 65201ef

Please sign in to comment.