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
- added a input parameter for SSH key id in CreateNewNode

issue adoptium#10

Signed-off-by: Shubham Verma <shubhamv.sv@gmail.com>
  • Loading branch information
VermaSh committed Apr 11, 2018
1 parent 6e8c8f2 commit db8e192
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 7 deletions.
78 changes: 78 additions & 0 deletions Jenkins_jobs/CreateNewNode.groovy
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}";
}
}

}
}
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ 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```
* Comma seperated host names of the machine(s)
* ```String machineIPs```
* Comma seperated IP address of the machine(s)
* ```String labelStrings```
* Labels you would like to be added to the machine.
* Each label must be separated by spaces and labels for different machines must be separated by `,`
* If identical labels need to be applied to all the machines, only one set of labels need to be supplied

## How-to

### Setup
Expand Down
14 changes: 7 additions & 7 deletions src/NodeHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import hudson.plugins.sshslaves.SSHLauncher;

class NodeHelper {

// TODO: Move strings out to a config file

/* Java Web Start (Windows)
- Internal data directory: remoting
SSH (most linux machines)
Expand Down Expand Up @@ -74,7 +72,7 @@ class NodeHelper {

String ret = "INVALID_NODE_NAME";

if (newNodeName.length() > 2) { // TODO: Some sort of validation for node names
if (newNodeName.length() > 2) {
DumbSlave newSlave = new DumbSlave(
newNodeName,
newNodeRemoteFS,
Expand Down Expand Up @@ -1030,7 +1028,7 @@ class NodeHelper {
return ret;
}

private String execGroovy(String cmd, Computer computer) { // TODO: Also check for invalid command (?)
private String execGroovy(String cmd, Computer computer) {
String ret = "execGroovy:INVALID_COMMAND";

if (cmd.length() > 1 && computer != null) {
Expand Down Expand Up @@ -1115,9 +1113,11 @@ class NodeHelper {
* the 2
*/
ret = Jenkins.getInstance().getComputer(computerName);
if (ret == null) {
// tries to search for computer without the domain
ret = Jenkins.getInstance().getComputer(computerName.substring(0,computerName.indexOf(".")));
if (ret == null) { // tries to search for computer without the domain
int endIndex = computerName.indexOf(".");
if ( endIndex > 0) {
ret = Jenkins.getInstance().getComputer(computerName.substring(0,endIndex));
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions vars/Constants.groovy
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 = "";
}

0 comments on commit db8e192

Please sign in to comment.