I recently had a problem where I need to automatically take nodes online and 
offline.  My solution was to install the Scriptler plugin and write a couple of 
Groovy scripts that talk directly to the server data structures.  The code 
below will take any "temporarily offline" hosts for label "labelName" (passed 
in as a script parameter) and make them online.

Besides the Scriptler plugin, you will need to know a little bit about Groovy 
(most valid Java is valid Groovy), and you'll need to understand the Jenkins, 
Node and Computer classes.  Download the sources so you can see how to work 
those classes.  One caveat: these scripts need to specifically be run on the 
master node-if run on a slave, it can't reach the Jenkins data objects.

And now for the script:

import hudson.model.Hudson
import jenkins.model.Jenkins
import hudson.model.Computer

println "Passed in label $labelName."

label = Jenkins.instance.getLabel(labelName)
if(!label){
  println("Could not find label $labelName")
}else{
  nodes = label.getNodes()
  for(node in nodes){
    nodeName = node.getNodeName()
    println("Node $nodeName is part of label $labelName")
  }
}

nodeToComputer = new HashMap<Node, Computer>()
for(node in nodes){
  computer = node.toComputer()
  if(null == computer){
    println("NO COMPUTER for node $nodeName")
  }else{
    nodeToComputer.put(node, computer)
  }
}

for(node in nodes){
  nodeName = node.getNodeName()
  computer = nodeToComputer.get(node)

  println("We have a computer for node $nodeName")
  if(computer.isTemporarilyOffline()){
    computer.setTemporarilyOffline(false)
    println("Setting Node $nodeName to online");
  }else{
    println("Node $nodeName is already online.")
  }
}

for(node in nodes){
  nodeName = node.getNodeName()
  computer = nodeToComputer.get(node)
  print("Waiting for node $nodeName to finish...")
  computer.waitUntilOnline()
  println("done");
}

From: [email protected] 
[mailto:[email protected]] On Behalf Of Stanley, Jason
Sent: Thursday, June 14, 2012 9:35 AM
To: [email protected]
Subject: How to update node description via a script?

Hello

I have automated a build process to update a database on my build nodes.

The one last step I am trying to do is update the node description in Jenkins.

There is no CLI operation to change the description of the Node.

Does anyone have a simple script or idea of how I can update the description in 
Jenkins outside of editing the config.xml file and running the "Reload 
Configuration from Disk" operation.

The reason for not using the "Reload Configuration from Disk" operation is I 
found if any existing jobs are running, they are stopped.

Thanks
Jason

The information in this message is for the intended recipient(s) only and may 
be the proprietary and/or confidential property of Litle & Co., LLC, and thus 
protected from disclosure. If you are not the intended recipient(s), or an 
employee or agent responsible for delivering this message to the intended 
recipient, you are hereby notified that any use, dissemination, distribution or 
copying of this communication is prohibited. If you have received this 
communication in error, please notify Litle & Co. immediately by replying to 
this message and then promptly deleting it and your reply permanently from your 
computer.

Reply via email to