Why isn't the restart command working for Jenkins node in Groovy?

Viewed 254

I am trying to reboot nodes that are under a master node in Jenkins using Groovy. However, the reboot isn't working and it's only taking the nodes offline and then bringing them back online. Why isn't the reboot command working? Am I missing something? This is what I currently have:

Jenkins jenkins = Jenkins.getInstance()
def jenkins_Nodes = jenkins.getNodes()

for (Node node in jenkins_Nodes) {
    if(node.name == "some computer name") {
       node.getComputer().setAcceptingTasks(false);
       while(true) {
         if(node.getComputer().countBusy() == 0) {
            def restart = "shutdown /r /f".execute();
            restart.waitFor();
            break;
         }
       }
       node.getComputer().launch();
       node.getComputer().setAcceptingTasks(true);
     }
    //other computers
}
1 Answers

Your shutdown command is not executed on the remote node. You need to delegate the execution of shutdown to the remote node.

It's relatively easy to do via RemotingDiagnostics -- with that, your sample looks like this:

import hudson.util.RemotingDiagnostics

restart_script = '''
"shutdown /r /f".execute()
'''

Jenkins jenkins = Jenkins.getInstance()
def jenkins_Nodes = jenkins.getNodes()

for (Node node in jenkins_Nodes) {
    if(node.name == "some computer name") {
       node.getComputer().setAcceptingTasks(false);
       while(true) {
         if(node.getComputer().countBusy() == 0) {
            result = RemotingDiagnostics.executeGroovy(restart_script, node.channel)
            break;
         }
       }
       node.getComputer().launch();
       node.getComputer().setAcceptingTasks(true);
     }
    //other computers
}
Related