Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
- Improved the Jenkins job based on the deskside review
- Replaced initial_run with overWriteLabels

Signed-off-by: Shubham Verma <[email protected]>
  • Loading branch information
VermaSh committed Apr 10, 2018
1 parent 1a74c64 commit c75359f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 35 deletions.
69 changes: 41 additions & 28 deletions Jenkins_jobs/UpdateMachineIdentifiers.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,62 @@

node {
String[] machineNames = params.machineNames.trim().split(",")
def nodeHelper = new NodeHelper();

/* if the flag updateDescription is not set we only update the labels
* The user can chose to either overwrite, add preconfigured labels or append labels
*
* To overwrite labels, the overwrite flag must be set
* To add preconfigured labels, only the manchine names need to passed in
* To append labels, a set of label(s) must be passed in
*/
stage('Update_Labels') {
if (!params.updateDescription) {
String[] labels;
if (!params.labels.isEmpty()) {
labels = params.labels.trim().split(",")
}

for (int index = 0; index < machineNames.length; index++) {
println "Machine ${machineNames[index]} old labels: ${nodeHelper.getLabels(machineNames[index])}"

if (!(params.initialRun || params.projectLabel.length() > 0)) {
if (!params.overWriteLabels && params.projectLabel.isEmpty()) {
// We shoulidn't be touching any machine if a project label isn't is passed
error("Please specify a project label");
error("Neither project label was provied nor overWriteLabels flag was set")
}

def nodeHelper = new NodeHelper();
String[] labels = params.labels.trim().split(",")
if (params.overWriteLabels && labels == null) {
error("No labels supplied for overWriteLabels option")
}

if (params.overWriteLabels) { // Overwrite labels
println "Machine ${machineNames[index]} updated labels: ${nodeHelper.addLabel(machineNames[index], labels[index%labels.length])}"
} else if (params.projectLabel.equals("all")
|| nodeHelper.getLabels(machineNames[index]).contains(params.projectLabel)) {
if (labels == null) { // Add preconfigured labels

for (int index = 0; index < machineNames.length; index++) {
if (params.initialRun) {

println "Machine ${machineNames[index]} labels: " + nodeHelper.addLabel(machineNames[index], labels[index%labels.length])

} else if (nodeHelper.getLabels(machineNames[index]).contains(params.projectLabel)) {
if (params.labels.length() == 0) {

String constructedLabels = "${params.projectLabel} " + nodeHelper.constructLabels(machineNames[index]);
println "Machine ${machineNames[index]} labels: " + nodeHelper.addLabel(machineNames[index],constructedLabels);

} else {
println "Machine ${machineNames[index]} labels: " + nodeHelper.appendLabel(machineNames[index], labels[index%labels.length])
}
String constructedLabels = "${params.projectLabel} ${nodeHelper.constructLabels(machineNames[index])}"
println "Machine ${machineNames[index]} updated labels: ${nodeHelper.addLabel(machineNames[index],constructedLabels)}"

} else { // Append labels
println "Machine ${machineNames[index]} updated labels: ${nodeHelper.appendLabel(machineNames[index], labels[index%labels.length])}"
}
}
}
}

stage('Update_Description') {
if (params.updateDescription && params.projectLabel.length() > 0) {
def nodeHelper = new NodeHelper();
if (params.updateDescription) {
/* Update the description if the updateDescription flag is set
* This stage assumes that the description doesn't already have RAM, CPU and disk info
*/
stage('Update_Description') {
// def nodeHelper = new NodeHelper();

for (int index = 0; index < machineNames.length; index++) {
if (nodeHelper.getLabels(machineNames[index]).contains(params.projectLabel)) {
println "Pre-update description of ${machineNames[index]}: ${nodeHelper.getDescription(machineNames[index])}"
if (!params.projectLabel.equals("all")
&& nodeHelper.getLabels(machineNames[index]).contains(params.projectLabel)) {

String description = nodeHelper.getDescription(machineNames[index]);
description += " - ${nodeHelper.getCpuCount(machineNames[index])}CPU"; // TODO: Implement VCPU
description += " - ${nodeHelper.getCpuCount(machineNames[index])}CPU";
description += " ${nodeHelper.getMemory(machineNames[index]).get(0)} RAM";
description += " ${nodeHelper.getTotalSpace(machineNames[index])} Disk";

Expand All @@ -52,9 +68,6 @@ node {
println "Machine specified ${machineNames[index]} does not belong to the project ${params.projectLabel}";
}
}
} else {
// We shoulidn't be touching any machine if a project label isn't is passed
error("Please specify a project label");
}
}
}
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,35 @@ Used to update machine labels and description

* The computers it iterates over can be limited by input parameter, ```projectLabel```
* The job expects 5 input parameters
* ```boolean initialRun```
* This is used to add the project or any other required labels on the machine
* ```boolean overWriteLabels```
* Does excatly as the name suggests, completely wipes previous labels
* If set true, you do not need to pass a ```projectLabel```
* ```String labels```
* Labels you would like to be added to the machine. Seperate the labels for a single machine with spaces. Use "," if doing batch update. Or if they will be same across the machines supply only one set
* 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
* Use Cases:
* Multiple machines, unique labels: `machine1Label1 machine1Label2, machine2Label1 machine2Label2`
* Single or multiple machines, identical labels: `Label1 Label2`
* ```String machineNames```
* Can either enter a list of names or a single name. For list seperate them with ","
* ```boolean updateDescription```
* If this is set true, the job will update labels and update description
* If this is set true, the job will update description
* This has higher precedence than overWriteLabels
* ```String projectlabel```
* This limits which machines will be touched
* Use Cases:
* Update labels:
* Objective: add default, os, arch, and kernel lables
* Procedure: initial_run is checked off and only the machine name(s) is supplied
* Objective: add default labels(os, arch, and kernel)
* Procedure: overWriteLabels is not set and only the machine name(s) is supplied
* Overwrite Labels:
* Objective: overwrite previous labels with new ones
* Procedure: overWriteLabels is set and machine name(s) + labels are supplied
* Append labels:
* Objective: want to add a custom label.
* Procedure: supply labels and machine names
* Update description:
* It adds CPU count, Disk space and installed RAM to the description
* Side note: this is implemented here instead of in CreateNewNode job because sometimes the machine may not have the needed information available in time for the script to udpate the description
* Procedure: have ```updateDescription``` parameter checked

## How-to
Expand Down

0 comments on commit c75359f

Please sign in to comment.