Bring 'offline node' online script

Viewed 81

So I was wondering if it's possible to create a script that checks in a node is offline and if it is it should bring it back online. The login used should be by username and token.

I'm talking about a script that triggers this button on the right:

enter image description here

2 Answers

If you want to simply make temporarily disabled nodes online you can use the following script to do this.

def jenkinsNodes = Jenkins.instance.getNodes()
def nodeLabelToMatch = "label1"
  for(def node: jenkinsNodes) {
      if(node.labelString.contains(nodeLabelToMatch)) {
        if (node.getComputer().isOffline()){  
            node.getComputer().cliOnline()
        }
      }
  }

Update : Full Pipeline

The script is written in groovy

pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                script {
                    def jenkinsNodes = Jenkins.instance.getNodes()
                    def nodeLabelToMatch = "label1"
                      for(def node: jenkinsNodes) {
                          if(node.labelString.contains(nodeLabelToMatch)) {
                            if (node.getComputer().isOffline()){  
                                node.getComputer().cliOnline()
                            }
                          }
                      }
                }
            }
        }
   }
}

Non-Depricated Method.

If you look at this depricated method, it simply calls a non depricated method setTemporarilyOffline(boolean temporarilyOffline, OfflineCause cause). So instead of using cliOnline() you can use setTemporarilyOffline. Check the following.

node.getComputer().setTemporarilyOffline(false, null)

Some proper code with a proper cause. The cause is not really needed when setting the node online though.

import hudson.slaves.OfflineCause.UserCause

def jenkinsNodes = Jenkins.instance.getNodes()
  for(def node: jenkinsNodes) {
      if (node.getComputer().isTemporarilyOffline()){  
           node.getComputer().setTemporarilyOffline(false, null)
      }
  }

Setting to temporarily offline

UserCause cause =  new UserCause(User.current(), "This is a automated process!!")
node.getComputer().setTemporarilyOffline(true, cause)

TL;DR: the scripted action for that button is .doToggleOffline:
Jenkins.instance.getNode('Node-Name').getComputer().doToggleOffline(offlineMessage)

I knew I had dealt with this before but did not recall the cliOnline() command. In looking it up I noticed it was deprecated. Turns out I used a different approach.

Can't say I fully understand the possible states and their applicability as it's not well-documented. The table shown below is as reflected in the Build Executor Status side panel; the /computer Manage nodes and clouds table will only show the computer w/ or w/o X.

// Connect (Launch) / Disconnect Node
Jenkins.instance.getNode('Node-Name').getComputer().launch()
Jenkins.instance.getNode('Node-Name').getComputer().disconnect()

// Make this node temporarily offline (true) / Bring this node back online (false)
Jenkins.instance.getNode('Node-Name').getComputer().setTemporarilyOffline(false, OfflineCause cause)

// Availabiltiy: Accepting Tasks (true) / Not Accepting Tasks (false)
Jenkins.instance.getNode('Node-Name').getComputer().setAcceptingTasks(true)

Node States

The isAcceptingTasks() JavaDoc explains this as:

Needed to allow agents programmatic suspension of task scheduling that does not overlap with being offline.

The isTemporarilyOffline() JavaDoc elaborates:

Returns true if this node is marked temporarily offline by the user.
In contrast, isOffline() represents the actual online/offline state

JavaDoc for isOffline (both Temporarily and Disconnected), setTemporarilyOffline and setAcceptingTasks.


But, after all that, turns out there's one more option:

def offlineMessage = "I did it"
Jenkins.instance.getNode('Node-Name').getComputer().doToggleOffline(offlineMessage)

And if you run that from the groovy console, it toggles the state (so I guess you check state first): Toggle Off-line

And run it again:

Toggle Again


My experience relates to: JENKINS-59283 - Use distinct icon for disconnected and temporarily offline computers / PR-4195 and having brought agents on-line when they should have been unavailable per schedule (Node Availability: Bring this agent online according to a schedule) so nothing ran. The PR was to introduce a yellow X for the Not Accepting but On-line condition, but the icons have now changed.

Related