This repository has been archived by the owner on Jan 19, 2022. It is now read-only.
/
__init__.py
381 lines (316 loc) · 12.1 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import absolute_import
import collections
import re
import time
import flask_login
import structlog
import taskcluster
from flask import Blueprint
from flask import current_app
from flask import g
from flask import request
from flask import url_for
from flask.ext.login import current_user
from sqlalchemy import and_
from sqlalchemy import desc
from sqlalchemy import func
from sqlalchemy import not_
from sqlalchemy import or_
from relengapi.blueprints.clobberer import rest
from relengapi.blueprints.clobberer.models import DB_DECLARATIVE_BASE
from relengapi.blueprints.clobberer.models import Build
from relengapi.blueprints.clobberer.models import ClobberTime
from relengapi.lib import angular
from relengapi.lib import api
from relengapi.lib.api import apimethod
from relengapi.lib.permissions import p
logger = structlog.get_logger()
bp = Blueprint(
'clobberer',
__name__,
template_folder='templates',
static_folder='static'
)
p.clobberer.post.clobber.doc('Submit clobber requests')
bp.root_widget_template('clobberer_root_widget.html', priority=100)
# prefix which denotes release builddirs
BUILDDIR_REL_PREFIX = 'rel-'
BUILDER_REL_PREFIX = 'release-'
@bp.route('/')
@bp.route('/<string:branch>')
@flask_login.login_required
def root(branch=None):
return angular.template(
'clobberer.html',
url_for('.static', filename='clobberer.js'),
url_for('.static', filename='clobberer.css'),
branches=api.get_data(branches),
selected_branch=branch
)
def _add_clobber(session, branch, builddir, slave=None):
"""
A common method for adding clobber times to a session. The session passed
in is returned; but is only committed if the commit option is True.
"""
if re.search('^' + BUILDDIR_REL_PREFIX + '.*', builddir) is None:
try:
who = current_user.authenticated_email
except AttributeError:
if current_user.anonymous:
who = 'anonymous'
else:
# TokenUser doesn't show up as anonymous; but also has no
# authenticated_email
who = 'automation'
clobber_time = ClobberTime.as_unique(
session,
branch=branch,
builddir=builddir,
slave=slave,
)
clobber_time.lastclobber = int(time.time())
clobber_time.who = who
session.add(clobber_time)
return None
logger.debug('Rejecting clobber of builddir with release prefix: {}'.format(
builddir))
return None
@bp.route('/clobber', methods=['POST'])
@apimethod(None, body=[rest.ClobberRequest])
@p.clobberer.post.clobber.require()
def clobber(body):
"Request clobbers for particular branches and builddirs."
session = g.db.session(DB_DECLARATIVE_BASE)
for clobber in body:
_add_clobber(
session,
branch=clobber.branch,
builddir=clobber.builddir,
slave=clobber.slave
)
session.commit()
return None
@bp.route('/clobber/by-builder', methods=['POST'])
@apimethod(None, body=[rest.ClobberRequestByBuilder])
@p.clobberer.post.clobber.require()
def clobber_by_builder(body):
"""
Request clobbers for app builddirs associated with a particular buildername.
"""
session = g.db.session(DB_DECLARATIVE_BASE)
for clobber in body:
builddirs_query = session.query(Build.builddir, Build.branch).filter(
Build.buildername == clobber.buildername
)
if clobber.branch is not None:
builddirs_query = builddirs_query.filter(Build.branch == clobber.branch)
for result in builddirs_query.distinct():
_add_clobber(
session,
builddir=result[0],
branch=result[1],
slave=clobber.slave
)
session.commit()
return None
@bp.route('/branches')
@apimethod([unicode])
def branches():
"Return a list of all the branches clobberer knows about."
session = g.db.session(DB_DECLARATIVE_BASE)
branches = session.query(Build.branch).distinct()
# Users shouldn't see any branch associated with a release builddir
branches = branches.filter(not_(Build.builddir.startswith(BUILDDIR_REL_PREFIX)))
branches = branches.order_by(Build.branch)
return [branch[0] for branch in branches]
@bp.route('/lastclobber/all', methods=['GET'])
@apimethod([rest.ClobberTime])
def lastclobber_all():
"Return a sorted list of all clobbers"
session = g.db.session(DB_DECLARATIVE_BASE)
return session.query(ClobberTime).order_by(ClobberTime.lastclobber)
@bp.route('/lastclobber/branch/by-builder/<string:branch>', methods=['GET'])
@apimethod({unicode: [rest.ClobberTime]}, unicode)
def lastclobber_by_builder(branch):
"Return a dictionary of most recent ClobberTimes grouped by buildername."
session = g.db.session(DB_DECLARATIVE_BASE)
# Isolates the maximum lastclobber for each builddir on a branch
max_ct_sub_query = session.query(
func.max(ClobberTime.lastclobber).label('lastclobber'),
ClobberTime.builddir,
ClobberTime.branch
).group_by(
ClobberTime.builddir,
ClobberTime.branch
).filter(ClobberTime.branch == branch).subquery()
# Finds the "greatest n per group" by joining with the max_ct_sub_query
# This is necessary to get the correct "who" values
sub_query = session.query(ClobberTime).join(max_ct_sub_query, and_(
ClobberTime.builddir == max_ct_sub_query.c.builddir,
ClobberTime.lastclobber == max_ct_sub_query.c.lastclobber,
ClobberTime.branch == max_ct_sub_query.c.branch)).subquery()
# Attaches builddirs, along with their max lastclobber to a buildername
full_query = session.query(
Build.buildername,
Build.builddir,
sub_query.c.lastclobber,
sub_query.c.who
).outerjoin(
sub_query,
Build.builddir == sub_query.c.builddir,
).filter(
Build.branch == branch,
not_(Build.buildername.startswith(BUILDER_REL_PREFIX))
).distinct().order_by(Build.buildername)
summary = collections.defaultdict(list)
for result in full_query:
buildername, builddir, lastclobber, who = result
summary[buildername].append(
rest.ClobberTime(
branch=branch,
builddir=builddir,
lastclobber=lastclobber,
who=who
)
)
return summary
@bp.route('/lastclobber', methods=['GET'])
def lastclobber():
"Get the max/last clobber time for a particular builddir and branch."
session = g.db.session(DB_DECLARATIVE_BASE)
now = int(time.time())
branch = request.args.get('branch')
slave = request.args.get('slave')
builddir = request.args.get('builddir')
buildername = request.args.get('buildername')
# TODO: Move the builds update to a separate endpoint (requires client changes)
build = Build.as_unique(
session,
branch=branch,
builddir=builddir,
buildername=buildername,
)
# Always force the time to update
build.last_build_time = now
session.add(build)
session.commit()
max_ct = session.query(ClobberTime).filter(
ClobberTime.builddir == builddir,
ClobberTime.branch == branch,
# a NULL slave value signifies all slaves
or_(ClobberTime.slave == slave, ClobberTime.slave == None) # noqa
).order_by(desc(ClobberTime.lastclobber)).first()
if max_ct:
# The client parses this result by colon as:
# builddir, lastclobber, who = urlib2.open.split(':')
# as such it's important for this to be plain text and have
# no extra colons within the field values themselves
return "{}:{}:{}\n".format(max_ct.builddir, max_ct.lastclobber, max_ct.who)
return ""
@bp.route('/forceclobber', methods=['GET'])
def forceclobber():
"""
Coerce the client to clobber by always returning a future clobber time.
This works because the client decides to clobber based on a timestamp
comparison.
"""
future_time = int(time.time()) + 3600
builddir = request.args.get('builddir')
return "{}:{}:forceclobber".format(builddir, future_time)
def tc_branches():
decision_namespace = 'gecko.v2.%s.latest.firefox.decision'
index = taskcluster.Index()
queue = taskcluster.Queue()
result = index.listNamespaces('gecko.v2', dict(limit=1000))
branches = {
i['name']: dict(name=i['name'], workerTypes=dict())
for i in result.get('namespaces', [])
}
for branchName, branch in branches.items():
# decision task might not exist
try:
decision_task = index.findTask(decision_namespace % branchName)
decision_graph = queue.getLatestArtifact(
decision_task['taskId'], 'public/graph.json')
except taskcluster.exceptions.TaskclusterRestFailure:
continue
for task in decision_graph.get('tasks', []):
task = task['task']
task_cache = task.get('payload', dict()).get('cache', dict())
provisionerId = task.get('provisionerId')
if provisionerId:
branch['provisionerId'] = provisionerId
workerType = task.get('workerType')
if workerType:
branch['workerTypes'].setdefault(
workerType, dict(name=workerType, caches=[]))
if len(task_cache) > 0:
branch['workerTypes'][workerType]['caches'] = list(set(
branch['workerTypes'][workerType]['caches'] +
task_cache.keys()
))
caches_to_skip = current_app.config.get('TASKCLUSTER_CACHES_TO_SKIP', [])
return [
rest.TCBranch(
name=branchName,
provisionerId=branch.get('provisionerId'),
workerTypes={
workerType: rest.TCWorkerType(
name=workerType,
caches=[
cache
for cache in branch['workerTypes'][workerType]['caches']
if cache not in caches_to_skip
],
)
for workerType in branch['workerTypes']
})
for branchName, branch in branches.items()]
@bp.route('/tc/branches', methods=['GET'])
@apimethod([rest.TCBranch])
def tc_branches_cached():
"""List of all the gecko branches with their worker types
"""
def cache_key():
return time.time() // current_app.config.get(
'TASKCLUSTER_CACHE_DURATION', 60 * 5)
def cache(fun):
def wrap(*args, **kwargs):
key = cache_key()
if getattr(fun, '__cache_key', None) != key:
fun.__cache_value = fun(*args, **kwargs)
fun.__cache_key = key
return getattr(fun, '__cache_value', None)
return wrap
return cache(tc_branches)()
@bp.route('/tc/purgecache', methods=['POST'])
@apimethod(None, body=[rest.TCPurgeCacheRequest])
@p.clobberer.post.clobber.require()
def tc_purgecache(body):
"""Purge cache on taskcluster
"""
credentials = []
client_id = current_app.config.get('TASKCLUSTER_CLIENT_ID')
access_token = current_app.config.get('TASKCLUSTER_ACCESS_TOKEN')
if client_id and access_token:
credentials = [dict(
credentials=dict(
clientId=client_id,
accessToken=access_token,
))]
purge_cache = taskcluster.PurgeCache(*credentials)
for item in body:
purge_cache.purgeCache(item.provisionerId,
item.workerType,
dict(cacheName=item.cacheName))
return None
_ROR = "https://github.com/mozilla/build-relengapi-clobberer"
_ISSUE_URL = "%s/issues" % _ROR
# See <relengapi>/docs/development/@relengapi/blueprints/#project-metadata
metadata = {
"repository_of_record": _ROR,
"bug_report_url": _ISSUE_URL,
}