From 178e35995c29855ed199e68b6d46b28b240cac0d Mon Sep 17 00:00:00 2001 From: Shubham Verma Date: Tue, 10 Apr 2018 12:01:36 -0400 Subject: [PATCH] Review changes - Improved the Jenkins job based on the deskside review - Replaced initial_run with overWriteLabels Signed-off-by: Shubham Verma --- Jenkins_jobs/UpdateMachineIdentifiers.groovy | 72 ++++++++++++-------- README.md | 23 +++++-- 2 files changed, 61 insertions(+), 34 deletions(-) diff --git a/Jenkins_jobs/UpdateMachineIdentifiers.groovy b/Jenkins_jobs/UpdateMachineIdentifiers.groovy index 3ce3f52..898b9e6 100644 --- a/Jenkins_jobs/UpdateMachineIdentifiers.groovy +++ b/Jenkins_jobs/UpdateMachineIdentifiers.groovy @@ -2,46 +2,67 @@ node { String[] machineNames = params.machineNames.trim().split(",") + def nodeHelper = new NodeHelper(); - stage('Update_Labels') { - if (!params.updateDescription) { + if (params.updateDescription && overWriteLabels) { + println "Between the two flags, updateDescription has precedence" + } - if (!(params.initialRun || params.projectLabel.length() > 0)) { - // We shoulidn't be touching any machine if a project label isn't is passed - error("Please specify a project label"); + if (!params.updateDescription) { + /* 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') { + String[] labels; + if (!params.labels.isEmpty()) { + labels = params.labels.trim().split(",") } - def nodeHelper = new NodeHelper(); - String[] labels = params.labels.trim().split(",") - for (int index = 0; index < machineNames.length; index++) { - if (params.initialRun) { - - println "Machine ${machineNames[index]} labels: " + nodeHelper.addLabel(machineNames[index], labels[index%labels.length]) + println "Machine ${machineNames[index]} old labels: ${nodeHelper.getLabels(machineNames[index])}" + + if (!params.overWriteLabels && params.projectLabel.isEmpty()) { + // We shoulidn't be touching any machine if a project label isn't is passed + error("Neither project label was provied nor overWriteLabels flag was set") + } + + if (params.overWriteLabels && labels == null) { + error("No labels supplied for overWriteLabels option") + } - } else if (nodeHelper.getLabels(machineNames[index]).contains(params.projectLabel)) { - if (params.labels.length() == 0) { + 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 - String constructedLabels = "${params.projectLabel} " + nodeHelper.constructLabels(machineNames[index]); - println "Machine ${machineNames[index]} labels: " + nodeHelper.addLabel(machineNames[index],constructedLabels); + String constructedLabels = "${params.projectLabel} ${nodeHelper.constructLabels(machineNames[index])}" + println "Machine ${machineNames[index]} updated labels: ${nodeHelper.addLabel(machineNames[index],constructedLabels)}" - } else { - println "Machine ${machineNames[index]} labels: " + nodeHelper.appendLabel(machineNames[index], labels[index%labels.length]) + } 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(); + } else { + /* 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"; @@ -52,9 +73,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"); } } } diff --git a/README.md b/README.md index 0ef1b89..1ffbf1f 100644 --- a/README.md +++ b/README.md @@ -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