Development Override Compose File
[laas.git] / src / jenkins / adapter.py
1 ##############################################################################
2 # Copyright (c) 2016 Max Breitenfeldt and others.
3 #
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 ##############################################################################
9
10
11 import logging
12 import re
13 from django.conf import settings
14
15 import requests
16 from django.core.cache import cache
17
18 logger = logging.getLogger(__name__)
19
20 # TODO: implement caching decorator, cache get_* functions
21 def get_json(url):
22     if cache.get(url) is None:
23         try:
24             response = requests.get(url)
25             json = response.json()
26             cache.set(url, json, 180)  # cache result for 180 seconds
27             return json
28         except requests.exceptions.RequestException as e:
29             logger.exception(e)
30         except ValueError as e:
31             logger.exception(e)
32     else:
33         return cache.get(url)
34
35
36 def get_all_slaves():
37     url = settings.ALL_SLAVES_URL
38     json = get_json(url)
39     if json is not None:
40         return json['computer']  # return list of dictionaries
41     return []
42
43
44
45
46 def get_slave(slavename):
47     slaves = get_all_slaves()
48     for slave in slaves:
49         if slave['displayName'] == slavename:
50             return slave
51     return {}
52
53
54 def get_ci_slaves():
55     url = settings.CI_SLAVES_URL
56     json = get_json(url)
57     if json is not None:
58         return json['nodes']
59     return []
60
61
62 def get_all_jobs():
63     url = settings.ALL_JOBS_URL
64     json = get_json(url)
65     if json is not None:
66         return json['jobs']  # return list of dictionaries
67     return []
68
69
70 def get_jenkins_job(slavename):
71     jobs = get_all_jobs()
72     max_time = 0
73     last_job = None
74     for job in jobs:
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:
80                     last_job = job
81                     max_time = job['lastBuild']['timestamp']
82     return last_job
83
84
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:
89             return True
90     return False
91
92
93 def is_dev_pod(slavename):
94     if is_ci_slave(slavename):
95         return False
96     if slavename.find('pod') != -1:
97         return True
98     return False
99
100
101 def parse_job(job):
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']
108     return result
109
110
111 def parse_job_string(full_displayname):
112     job = {}
113     job['scenario'] = ''
114     job['installer'] = ''
115     job['branch'] = ''
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]
126     return job
127
128 def get_slave_url(slave):
129     return settings.GET_SLAVE_URL + slave['displayName']
130
131
132 def get_slave_status(slave):
133     if not slave['offline'] and slave['idle']:
134         return 'online / idle'
135     if not slave['offline']:
136         return 'online'
137     return 'offline'