4

I want to accomplish, through a script using the Jenkins API, the equivalent of manually clicking on the "Disconnect" link on the node management page in Jenkins. I want to add this code to a script I made using the Pipeline scripting plugin.

In other words, as a user with Jenkins admin privileges, do

Manage Jenkins -> Manage Nodes -> Click on a node -> Click on "Disconnect"

As in:

Mange Node Page

I do not want to do the equivalent of "temporarily mark this node as offline".
That is not useful to me.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Mark
  • 61
  • 2
  • 4

2 Answers2

7

The hudson.model.Computer class has a disconnect(DisconnectReason) method

Here are the docs for this

In my case, I was able to acquire the Computer instance and disconnect by using Jenkins.instance.getNode(...).getComputer().disconnect(...)

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • That seems more accurate than my answer. +1 – VonC Nov 21 '18 at 19:14
  • For some reason, the command succeeded but still the agent did not disconnect. What could be the reason ? Jenkins.instance.getNode("agent_name").getComputer().disconnect(hudson.slaves.OfflineCause.create(hudson.model.Messages._Hudson_NodeBeingRemoved())) – Muhammad Faizan-Ul-Haq Aug 01 '22 at 15:15
6

I don't see anything in the pipeline-model-definition-plugin regarding node management.

So an alternative approach would be to call the disconnect action through the Jenkins API from the pipeline.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hi Von, Thanks for your help. I tried the httpRequest and it gave me: [Pipeline] httpRequest HttpMode: GET URL: http://myjenkins.mydomain.local:8080/computer/ci-xx-2/doDisconnect?offlineMessage=bye Sending request to url: http://myjenkins.mydomain.local:8080/computer/ci-da-2/doDisconnect?offlineMessage=bye Response Code: HTTP/1.1 405 Method Not Allowed [Pipeline] End of Pipeline ERROR: Fail: the returned code 405 is not in the accepted range: [[100‥399]] Finished: FAILURE – Mark May 23 '17 at 22:58