0
votes

All!! Let's say I have Jenkins with few slave nodes (Slave1, Slave2 & Slave3). Each slave have few labels associated with it, i.e Slave1 (test build python), Slave2 (develop update clean)...

I'm looking for groovy script that I can run from Jenkins Script Console to modify labels on given slaves at once by adding/removing "-offline" suffix for each label on each slave, i.e Slave1 will have (test-offline build-offline python-offline) & Slave2 (develop-offline update-offline clean-offline).

I need this functionality in order to prevent next job start running on these slaves (let's say I want to set them offline for maintenance, once all jobs finished on these nodes). And once I done with maintenance on these slaves, I need to run another groovy script which set back all labels as they was configured before, i.e Slave1 (test build python), Slave2 (develop update clean).

So far I've found old topic about similar issue. The bellow code can return all labels for chosen slave, but its adding "offline" as additional label and not as a suffix for each existing label:

hudson = hudson.model.Hudson.instance
hudson.slaves.findAll { it.nodeName.equals("slave1") }.each { slave -> 
  print "Slave  $slave.nodeName : Labels: $slave.labelString"
  slave.labelString = slave.labelString + " " + "offline"
  println "   --> New labels: $slave.labelString"
}
hudson.save()

So I have few questions:

  1. How to use list of nodes (slave1,slave2, slave3...) and not single node slave1
  2. How to add/remove"-offline" suffix to/from labels? It can be two different script for adding and removing labels suffix.

Any help & suggestions are much appreciated.

Regrads, Igor.

2

2 Answers

0
votes

I think there is some issue in the script.

hudson = hudson.model.Hudson.instance 
hudson.slaves.findAll { it.nodeName.equals("slave1") }.each { slave ->    
    print "Slave  $slave.nodeName : Labels: $slave.labelString"
    labels = aSlave.getLabelString()
    labels = labels + "-offline"
    aSlave.setLabelString(labels)
    println "   --> New labels: $slave.labelString" 
} 
hudson.save()

Here too the assumption is that there is only one lable. Then only this will work.

if there are multiple labels

labels = aSlave.getLabelString()
labelList = labels.split(' ') //This is a list
labelList[0] = labelList[0]+"-offline"
labels = labelList.join(' ')

You will have to split the labelString by space and then work on them.

And to get all slaves you can create a loop as follows

for (aSlave in hudson.model.Hudson.instance.slaves) {
    if(aSlave.name == "")//here you can pass a list or a single node for which changes needs to be done
    println "${aSlave.name}"
}

I would do something like this for multiple nodes

hudson = hudson.model.Hudson.instance 
for (aSlave in hudson.slaves) {
   labels = aSlave.getLabelString()
   labelList = labels.split(' ') //This is a list
   i = 0
   for (individualLabel in labelList) {
       if(individualLabel == "python") {
            labelList[i] = labelList[i]+"-offline"
       }
       i++
   }
   labels = labelList.join(' ')
   aSlave.setLabelString(labels)
 }
 hudson.save()

You can also add a condition on name of the slave if this has to be done for a particular slave.

let me know if this works

0
votes

So far, thanks to @pratikvasa, for single node with single label working code is:

hudson = hudson.model.Hudson.instance
hudson.slaves.findAll { it.nodeName.equals("slave1") }.each { slave -> 
  println "   --> Slave  $slave.nodeName : Labels: $slave.labelString"
  slave.labelString = slave.labelString + "-offline"
  println "   --> New labels: $slave.labelString"
}
hudson.save()

And for single node & multiple labels working code is:

hudson = hudson.model.Hudson.instance
hudson.slaves.findAll { it.nodeName.equals("slave1") }.each { slave -> 
  println "   --> Slave  $slave.nodeName : Current Labels: $slave.labelString"
  labels = slave.labelString

def list = ["$labels"]
  labelList = labels.split(' ')
  i = 0
  for (item in labelList) {
  print "   --> $i Label In The List: "
  println item
  labelList[i] = labelList[i] + "-offline"
  i++
  }

  labels = labelList.join(' ')

  slave.labelString = labels

  print "   --> New Labels Are: $slave.labelString"
}

Using + "-offline" will append suffix to each label & using - "-offline" will remove suffix from each label.