From 65201ef6fdb961a01a5b4003e0b94a40c563125c Mon Sep 17 00:00:00 2001 From: Shubham Verma Date: Wed, 28 Mar 2018 17:26:37 -0400 Subject: [PATCH] Jenkins Create new node job - Added CreateNewNode job - Updated readme file to include the new job - Moved string constants to the vars directory of the Node helper API Issue #10 --- Jenkins_jobs/CreateNewNode.groovy | 124 ++++++++++++++++++++++++++++++ README.md | 11 +++ vars/Constants.groovy | 13 ++++ 3 files changed, 148 insertions(+) create mode 100644 Jenkins_jobs/CreateNewNode.groovy create mode 100644 vars/Constants.groovy diff --git a/Jenkins_jobs/CreateNewNode.groovy b/Jenkins_jobs/CreateNewNode.groovy new file mode 100644 index 0000000..ef8f6ff --- /dev/null +++ b/Jenkins_jobs/CreateNewNode.groovy @@ -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 newMachines = new ArrayList(); + +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 diff --git a/README.md b/README.md index a1b5dd2..678d6c8 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/vars/Constants.groovy b/vars/Constants.groovy new file mode 100644 index 0000000..5866c1f --- /dev/null +++ b/vars/Constants.groovy @@ -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 = "/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} @"; + static final String SSH_KEY_LOCATION = ""; +} \ No newline at end of file