1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
13 from django.conf import settings
16 from django.core.cache import cache
18 logger = logging.getLogger(__name__)
20 # TODO: implement caching decorator, cache get_* functions
22 if cache.get(url) is None:
24 response = requests.get(url)
25 json = response.json()
26 cache.set(url, json, 180) # cache result for 180 seconds
28 except requests.exceptions.RequestException as e:
30 except ValueError as e:
37 url = settings.ALL_SLAVES_URL
40 return json['computer'] # return list of dictionaries
46 def get_slave(slavename):
47 slaves = get_all_slaves()
49 if slave['displayName'] == slavename:
55 url = settings.CI_SLAVES_URL
63 url = settings.ALL_JOBS_URL
66 return json['jobs'] # return list of dictionaries
70 def get_jenkins_job(slavename):
75 if job['lastBuild'] is not None:
76 if job['lastBuild']['builtOn'] == slavename:
77 if job['lastBuild']['building'] is True:
78 return job # return active build
79 if job['lastBuild']['timestamp'] > max_time:
81 max_time = job['lastBuild']['timestamp']
85 def is_ci_slave(slavename):
86 ci_slaves = get_ci_slaves()
87 for ci_slave in ci_slaves:
88 if ci_slave['nodeName'] == slavename:
93 def is_dev_pod(slavename):
94 if is_ci_slave(slavename):
96 if slavename.find('pod') != -1:
102 result = parse_job_string(job['lastBuild']['fullDisplayName'])
103 result['building'] = job['lastBuild']['building']
104 result['result'] = ''
105 if not job['lastBuild']['building']:
106 result['result'] = job['lastBuild']['result']
107 result['url'] = job['url']
111 def parse_job_string(full_displayname):
114 job['installer'] = ''
116 tokens = re.split(r'[ -]', full_displayname)
117 for i in range(len(tokens)):
118 if tokens[i] == 'os':
119 job['scenario'] = '-'.join(tokens[i: i + 4])
120 elif tokens[i] in ['fuel', 'joid', 'apex', 'compass']:
121 job['installer'] = tokens[i]
122 elif tokens[i] in ['master', 'arno', 'brahmaputra', 'colorado']:
123 job['branch'] = tokens[i]
124 tokens = full_displayname.split(' ')
125 job['name'] = tokens[0]
128 def get_slave_url(slave):
129 return settings.GET_SLAVE_URL + slave['displayName']
132 def get_slave_status(slave):
133 if not slave['offline'] and slave['idle']:
134 return 'online / idle'
135 if not slave['offline']: