ff0508d969120eb329a5e4af0407a713aa338f4c
[pharos.git] / tools / pharos-dashboard / 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
13 import re
14 import requests
15 from django.core.cache import cache
16
17 logger = logging.getLogger(__name__)
18
19 # TODO: implement caching decorator, cache get_* functions
20 def get_json(url):
21     if cache.get(url) is None:
22         try:
23             response = requests.get(url)
24             json = response.json()
25             cache.set(url, json, 180)  # cache result for 180 seconds
26             return json
27         except requests.exceptions.RequestException as e:
28             logger.exception(e)
29         except ValueError as e:
30             logger.exception(e)
31     else:
32         return cache.get(url)
33
34
35 def get_all_slaves():
36     url = "https://build.opnfv.org/ci/computer/api/json?tree=computer[displayName,offline,idle]"
37     json = get_json(url)
38     if json is not None:
39         return json['computer']  # return list of dictionaries
40     return []
41
42
43 def get_slave(slavename):
44     slaves = get_all_slaves()
45     for slave in slaves:
46         if slave['displayName'] == slavename:
47             return slave
48     return {}
49
50
51 def get_ci_slaves():
52     url = "https://build.opnfv.org/ci/label/ci-pod/api/json?tree=nodes[nodeName,offline,idle]"
53     json = get_json(url)
54     if json is not None:
55         return json['nodes']
56     return []
57
58
59 def get_all_jobs():
60     url = "https://build.opnfv.org/ci/api/json?tree=jobs[displayName,url,lastBuild[fullDisplayName,building,builtOn,timestamp,result]]"
61     json = get_json(url)
62     if json is not None:
63         return json['jobs']  # return list of dictionaries
64     return []
65
66
67 def get_jenkins_job(slavename):
68     jobs = get_all_jobs()
69     max_time = 0
70     last_job = None
71     for job in jobs:
72         if job['lastBuild'] is not None:
73             if job['lastBuild']['builtOn'] == slavename:
74                 if job['lastBuild']['building'] is True:
75                     return job  # return active build
76                 if job['lastBuild']['timestamp'] > max_time:
77                     last_job = job
78                     max_time = job['lastBuild']['timestamp']
79     return last_job
80
81
82 def is_ci_slave(slavename):
83     ci_slaves = get_ci_slaves()
84     for ci_slave in ci_slaves:
85         if ci_slave['nodeName'] == slavename:
86             return True
87     return False
88
89
90 def is_dev_pod(slavename):
91     if is_ci_slave(slavename):
92         return False
93     if slavename.find('pod') != -1:
94         return True
95     return False
96
97
98 def parse_job(job):
99     result = parse_job_string(job['lastBuild']['fullDisplayName'])
100     result['building'] = job['lastBuild']['building']
101     result['result'] = ''
102     if not job['lastBuild']['building']:
103         result['result'] = job['lastBuild']['result']
104     result['url'] = job['url']
105     return result
106
107
108 def parse_job_string(full_displayname):
109     job = {}
110     job['scenario'] = ''
111     job['installer'] = ''
112     job['branch'] = ''
113     tokens = re.split(r'[ -]', full_displayname)
114     for i in range(len(tokens)):
115         if tokens[i] == 'os':
116             job['scenario'] = '-'.join(tokens[i: i + 4])
117         elif tokens[i] in ['fuel', 'joid', 'apex', 'compass']:
118             job['installer'] = tokens[i]
119         elif tokens[i] in ['master', 'arno', 'brahmaputra', 'colorado']:
120             job['branch'] = tokens[i]
121     tokens = full_displayname.split(' ')
122     job['name'] = tokens[0]
123     return job
124
125 def get_slave_url(slave):
126     return 'https://build.opnfv.org/ci/computer/' + slave['displayName']
127
128
129 def get_slave_status(slave):
130     if not slave['offline'] and slave['idle']:
131         return 'online / idle'
132     if not slave['offline']:
133         return 'online'
134     return 'offline'