From b44d3678be97052f44c0d40b67b8cd54097ae667 Mon Sep 17 00:00:00 2001 From: Mahdi Ardekanian Date: Tue, 10 Oct 2023 17:21:14 -0400 Subject: [PATCH] add (set|get)Mode and enhance label functionality append label now stable and will not revert back and set|get_mode method were added to library Signed-off-by: Mahdi Ardekanian --- src/NodeHelper.groovy | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/NodeHelper.groovy b/src/NodeHelper.groovy index 234ae53..57e9bfc 100644 --- a/src/NodeHelper.groovy +++ b/src/NodeHelper.groovy @@ -93,6 +93,51 @@ class NodeHelper { return ret; } + /** + * Overwrites the existing mode with the one passed in. + * modes could be either normal or exclusive + * + * @param computerName the computer whose labels need to be updated + * @param mode the net mode for the computer + * + * @return Done if mode changed and setMode:SLAVE_NOT_FOUND if no node found + */ + public String setMode(String computerName, String mode) { + String ret = "setMode:SLAVE_NOT_FOUND"; + + def slave = Jenkins.getInstance().getSlave(computerName) + if (slave != null) { + if(mode == 'NORMAL') { + slave.setMode(hudson.model.Node.Mode.NORMAL); + } else { + slave.setMode(hudson.model.Node.Mode.EXCLUSIVE); + } + slave.save() + return 'Done' + } + + return ret; + } + + + /** + * Returns mode of the node. + * + * @param computerName the computer whose labels need to be updated + * + * @return either NORMAL or EXCLUSIVE + */ + public String getMode(String computerName) { + String ret = "setMode:SLAVE_NOT_FOUND"; + + def slave = Jenkins.getInstance().getSlave(computerName) + if (slave != null) { + return slave.getMode() == hudson.model.Node.Mode.NORMAL ? "NORMAL" : "EXCLUSIVE" + } + + return ret; + } + /** * Overwrites the existing labels with the ones passed * in. Labels should be seperated by spaces. @@ -108,6 +153,7 @@ class NodeHelper { Computer computer = getComputer(computerName); if (computer != null) { computer.getNode().setLabelString(label.toLowerCase()); + computer.getNode().save() ret = getLabels(computer.getName()); }