Skip to content

Instantly share code, notes, and snippets.

@seandst
Created Sep 2, 2016
Embed
What would you like to do?
$ cat /usr/bin/jenkies
#!/usr/bin/python
import warnings
from collections import defaultdict
from xml.etree.ElementTree import fromstring
import IPython
import jenkins
warnings.simplefilter("ignore")
j = jenkins.Jenkins('redacted', username='redacted', password='redacted')
user = j.get_whoami()
version = j.get_version()
print('Hello %s from Jenkins %s' % (user['fullName'], version))
def label_demand():
demand = dict()
for queued_job in j.get_queue_info():
task_name = queued_job['task']['name']
print('Checking queued task {}'.format(queued_job['id']))
try:
job_config = fromstring(j.get_job_config(task_name))
job_label = job_config.find('assignedNode').text
except jenkins.NotFoundException:
# job with task name not found, so assuming it's a "node-type=<label>" expression
job_label = task_name.split('=', 1)[1]
try:
demand[job_label] += 1
except KeyError:
demand[job_label] = 1
return demand
IPython.embed()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment