Mark Jenkins node temporarily disabled using JENKSES REST API
we have a Jenkins Matrix job that tests several variants of our software in parallel on many slave nodes. It sometimes happens that one of these alarms obeys and needs to be reloaded. I don't want to miss the run in this case. I don't want to mark a particular slave node as temporarily unavailable by an underloaded script that detects a failure and then reloads the node. I found out that this should be possible with the JENKSES REST API. And I found two python libraries that should do the job; https://python-jenkins.readthedocs.org/en/latest/index.html and http://pythonhosted.org/jenkinsapi/index.html... But both libraries have problems to change something in my Jenkins 1.580.2 system (getting information is not a problem) with python 3.4.3.
JenkinsAPI:
from jenkinsapi.jenkins import Jenkins
from jenkinsapi.utils.requester import Requester
class SSLRequester(Requester):
def __init__(self, username=None, password=None):
super(SSLRequester, self).__init__(username, password)
def get_request_dict(self, *largs, **kwargs):
requestKWargs = super(SSLRequester, self).get_request_dict(*largs, **kwargs)
requestKWargs['verify'] = False
return requestKWargs
jenkins = Jenkins(jenkinsurl, username, password, requester=SSLRequester())
I need to use the SSLRequester setup because I am using an https: // connection for my Jenkins server and I am getting the following error:
SSLError: SSL certificate confirmation: CERTIFICATE_VERIFY_FAILED] (_ssl.c: 600)
Ok if I try to get some information using jenkins object everything is fine.
node.is_temporarily_offline()
False
But if I try to switch node I get this:
node.toggle_temporarily_offline()
JenkinsAPIException: The operation failed. url = https: /// computer // toggleOffline? offlineMessage = requested% 20from% 20jenkinsapi, data = {}, headers = {'Content-Type': 'application / x-www-form-urlencoded'}, status = 403, text = b "% 2FtoggleOffline% 3FofflineMessage% 3Drequested% 2520from% 2520jenkinsapi '/ "> window.location.replace (' / login? From =% 2Fcomputer% 2F% 2FtoggleOffline% 3FofflineMessage% 3Drequested% 2520from% 2520jenkinsapi '); \ n \ n \ n Authentication required \ n \ n \ n
My login details are completely ignored.
Python-Jenkins:
import jenkins j = jenkins.Jenkins(jenkinsurl, username, password) j.disable_node(slavenode)
TypeError: JSON object must be str, not 'bytes'
After doing a little googling, I found out that I need to fix the library because JSON doesn't like the bytearrays that Jenkins provides the JSON API. After inserting several decrypted ('utf-8') statements, I was able to call the following statement:
j.get_node_info(slavenode)
But I still can't mark it as offline:
j.disable_node(slavenode)
TypeError: POST data must be bytes or iterable bytes. It cannot be of type str.
So translate this into a simple question. Do you know of any other handy, scripted way to mark a node as temporarily offline (and of course online again if the reboot is successful)? I would prefer a python solution because I am starting a reload from my python script. But the groovy script will be good enough too.
Thanks in advance for your help
source to share
You can look at the script console where you can test the scripts. You can also call these scripts with curl or CLI and I imagine python library
This is a good example groovy script that looks at nodes and removes a node
for (aSlave in hudson.model.Hudson.instance.slaves) {
println('====================');
println('Name: ' + aSlave.name);
println('getLabelString: ' + aSlave.getLabelString());
println('getNumExectutors: ' + aSlave.getNumExecutors());
println('getRemoteFS: ' + aSlave.getRemoteFS());
println('getMode: ' + aSlave.getMode());
println('getRootPath: ' + aSlave.getRootPath());
println('getDescriptor: ' + aSlave.getDescriptor());
println('getComputer: ' + aSlave.getComputer());
println('\tcomputer.isAcceptingTasks: ' + aSlave.getComputer().isAcceptingTasks());
println('\tcomputer.isLaunchSupported: ' + aSlave.getComputer().isLaunchSupported());
println('\tcomputer.getConnectTime: ' + aSlave.getComputer().getConnectTime());
println('\tcomputer.getDemandStartMilliseconds: ' + aSlave.getComputer().getDemandStartMilliseconds());
println('\tcomputer.isOffline: ' + aSlave.getComputer().isOffline());
println('\tcomputer.countBusy: ' + aSlave.getComputer().countBusy());
//if (aSlave.name == 'NAME OF NODE TO DELETE') {
// println('Shutting down node!!!!');
// aSlave.getComputer().setTemporarilyOffline(true,null);
// aSlave.getComputer().doDoDelete();
//}
println('\tcomputer.getLog: ' + aSlave.getComputer().getLog());
println('\tcomputer.getBuilds: ' + aSlave.getComputer().getBuilds());
}
source to share