treatyoself
treatyoself

Reputation: 83

How to reboot Jenkins node using shell in Groovy

I am writing a Groovy script to perform an automatic reboot of Windows servers. In the script, I am first taking the nodes offline, then checking to see if there are any builds, if there aren't, then perform a restart.

I wanted to use the safeRestart() method but it doesn't support the import statement I am using when looping through the nodes. I have seen an execute() method which basically executes a shell line of code in groovy.

How would I execute a restart of the Windows computers using execute()?

Upvotes: 0

Views: 5671

Answers (1)

Ian W
Ian W

Reputation: 4767

Not sure if this will answer your question directly, but will point in the right direction ...

You can leverage this S/O question: Run a remote command on all Jenkins slaves via Masters's script console or this Gist: run_command_on_all_slaves.groovy

btw: Jenkins API does seem to support running a script directly on the server (Computer).

Your actual command should be shutdown /r`

I don't believe you can do this unless the Node is on-line. Disconnecting the node stops the Jenkins slave process, then there's nothing running on the node, so not sure what control you'd have. Instead you want to block the queue and let the existing jobs finish:

Jenkins.instance.getNode('Node-Name').toComputer().setAcceptingTasks(false)

and check:

Jenkins.instance.getNode('Node-Name').toComputer().countBusy() == 0

Then run your (work on server) restart command

When the server is available again, launch the node and open the queue.

Jenkins.instance.getNode('Node-Name').getComputer().launch()
Jenkins.instance.getNode('Node-Name').getComputer().setAcceptingTasks(true)

Hope that helps.

Upvotes: 1

Related Questions