/
buildrequest.py
367 lines (299 loc) · 14 KB
/
buildrequest.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
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
import calendar
from buildbot import interfaces
from buildbot.data import resultspec
from buildbot.db import buildrequests
from buildbot.process import properties
from buildbot.status.results import FAILURE
from buildbot.status.results import SKIPPED
from twisted.internet import defer
from twisted.python import log
from zope.interface import implements
class BuildRequestCollapser(object):
# brids is a list of the new added buildrequests id
# This class is called before generated the 'new' event for the buildrequest
# Before adding new buildset/buildrequests, we must examine each unclaimed
# buildrequest.
# EG:
# 1. get the list of all unclaimed buildrequests:
# - We must exclude all buildsets which have at least 1 claimed buildrequest
# 2. For each unclaimed buildrequests, if compatible with the new request
# (sourcestamps match, except for revision) Then:
# 2.1. claim it
# 2.2. complete it with result SKIPPED
def __init__(self, master, brids):
self.master = master
self.brids = brids
@defer.inlineCallbacks
def _getUnclaimedBrs(self, builderid):
# Retrieve the list of Brs for all unclaimed builds
unclaim_brs = yield self.master.data.get(('builders',
builderid,
'buildrequests'),
[resultspec.Filter('claimed',
'eq',
[False])])
# sort by submitted_at, so the first is the oldest
unclaim_brs.sort(key=lambda brd: brd['submitted_at'])
defer.returnValue(unclaim_brs)
@defer.inlineCallbacks
def collapse(self):
collapseBRs = []
for brid in self.brids:
# Get the BuildRequest object
br = yield self.master.data.get(('buildrequests', brid))
# Retreive the buildername
builderid = br['builderid']
bldrdict = yield self.master.data.get(('builders', builderid))
# Get the builder object
bldr = self.master.botmaster.builders.get(bldrdict['name'])
# Get the Collapse BuildRequest function (from the configuration)
collapseRequestsFn = bldr.getCollapseRequestsFn() if bldr else None
unclaim_brs = yield self._getUnclaimedBrs(builderid)
# short circuit if there is no merging to do
if not collapseRequestsFn or not unclaim_brs:
continue
for unclaim_br in unclaim_brs:
if unclaim_br['buildrequestid'] == br['buildrequestid']:
continue
canCollapse = yield collapseRequestsFn(bldr, br, unclaim_br)
if canCollapse is True:
collapseBRs.append(unclaim_br)
brids = [b['buildrequestid'] for b in collapseBRs]
if collapseBRs:
# Claim the buildrequests
yield self.master.data.updates.claimBuildRequests(brids)
# complete the buildrequest with result SKIPPED.
yield self.master.data.updates.completeBuildRequests(brids,
SKIPPED)
defer.returnValue(brids)
class TempSourceStamp(object):
# temporary fake sourcestamp; attributes are added below
def asDict(self):
# This return value should match the kwargs to SourceStampsConnectorComponent.findSourceStampId
result = vars(self).copy()
del result['ssid']
del result['changes']
if 'patch' in result and result['patch'] is None:
result['patch'] = (None, None, None)
result['patch_level'], result['patch_body'], result['patch_subdir'] = result.pop('patch')
result['patch_author'], result['patch_comment'] = result.pop('patch_info')
assert all(
isinstance(val, (unicode, type(None), int))
for attr, val in result.items()
), result
return result
class TempChange(object):
# temporary fake change
def __init__(self, d):
for k, v in d.items():
setattr(self, k, v)
self.properties = properties.Properties()
for k, v in d['properties'].items():
self.properties.setProperty(k, v[0], v[1])
self.who = d['author']
class BuildRequest(object):
"""
A rolled-up encapsulation of all of the data relevant to a build request.
This class is used by the C{nextBuild} and C{collapseRequests} configuration
parameters, as well as in starting a build. Construction of a BuildRequest
object is a heavyweight process involving a lot of database queries, so
it should be avoided where possible. See bug #1894.
@type reason: string
@ivar reason: the reason this Build is being requested. Schedulers provide
this, but for forced builds the user requesting the build will provide a
string. It comes from the buildsets table.
@type properties: L{properties.Properties}
@ivar properties: properties that should be applied to this build, taken
from the buildset containing this build request
@ivar submittedAt: a timestamp (seconds since epoch) when this request was
submitted to the Builder. This is used by the CVS step to compute a
checkout timestamp, as well as by the master to prioritize build requests
from oldest to newest.
@ivar buildername: name of the requested builder
@ivar priority: request priority
@ivar id: build request ID
@ivar bsid: ID of the parent buildset
"""
submittedAt = None
sources = {}
@classmethod
def fromBrdict(cls, master, brdict):
"""
Construct a new L{BuildRequest} from a dictionary as returned by
L{BuildRequestsConnectorComponent.getBuildRequest}.
This method uses a cache, which may result in return of stale objects;
for the most up-to-date information, use the database connector
methods.
@param master: current build master
@param brdict: build request dictionary
@returns: L{BuildRequest}, via Deferred
"""
cache = master.caches.get_cache("BuildRequests", cls._make_br)
return cache.get(brdict['buildrequestid'], brdict=brdict, master=master)
@classmethod
@defer.inlineCallbacks
def _make_br(cls, brid, brdict, master):
buildrequest = cls()
buildrequest.id = brid
buildrequest.bsid = brdict['buildsetid']
builder = yield master.db.builders.getBuilder(brdict['builderid'])
buildrequest.buildername = builder['name']
buildrequest.builderid = brdict['builderid']
buildrequest.priority = brdict['priority']
dt = brdict['submitted_at']
buildrequest.submittedAt = dt and calendar.timegm(dt.utctimetuple())
buildrequest.master = master
buildrequest.waitedFor = brdict['waited_for']
# fetch the buildset to get the reason
buildset = yield master.db.buildsets.getBuildset(brdict['buildsetid'])
assert buildset # schema should guarantee this
buildrequest.reason = buildset['reason']
# fetch the buildset properties, and convert to Properties
buildset_properties = yield master.db.buildsets.getBuildsetProperties(brdict['buildsetid'])
buildrequest.properties = properties.Properties.fromDict(buildset_properties)
# make a fake sources dict (temporary)
bsdata = yield master.data.get(('buildsets', str(buildrequest.bsid)))
assert bsdata['sourcestamps'], "buildset must have at least one sourcestamp"
buildrequest.sources = {}
for ssdata in bsdata['sourcestamps']:
ss = buildrequest.sources[ssdata['codebase']] = TempSourceStamp()
ss.ssid = ssdata['ssid']
ss.branch = ssdata['branch']
ss.revision = ssdata['revision']
ss.repository = ssdata['repository']
ss.project = ssdata['project']
ss.codebase = ssdata['codebase']
if ssdata['patch']:
patch = ssdata['patch']
ss.patch = (patch['level'], patch['body'], patch['subdir'])
ss.patch_info = (patch['author'], patch['comment'])
else:
ss.patch = None
ss.patch_info = (None, None)
changes = yield master.data.get(("sourcestamps", ss.ssid, "changes"))
ss.changes = [TempChange(change) for change in changes]
defer.returnValue(buildrequest)
@staticmethod
@defer.inlineCallbacks
def canBeCollapsed(master, br1, br2):
"""
Returns true if both buildrequest can be merged, via Deferred.
This implements Buildbot's default collapse strategy.
"""
# short-circuit: if these are for the same buildset, collapse away
if br1['buildsetid'] == br2['buildsetid']:
defer.returnValue(True)
return
# get the buidlsets for each buildrequest
selfBuildsets = yield master.data.get(
('buildsets', str(br1['buildsetid'])))
otherBuildsets = yield master.data.get(
('buildsets', str(br2['buildsetid'])))
# extract sourcestamps, as dictionaries by codebase
selfSources = dict((ss['codebase'], ss)
for ss in selfBuildsets['sourcestamps'])
otherSources = dict((ss['codebase'], ss)
for ss in otherBuildsets['sourcestamps'])
# if the sets of codebases do not match, we can't collapse
if set(selfSources) != set(otherSources):
defer.returnValue(False)
return
for c, selfSS in selfSources.iteritems():
otherSS = otherSources[c]
if selfSS['revision'] != otherSS['revision']:
defer.returnValue(False)
return
if selfSS['repository'] != otherSS['repository']:
defer.returnValue(False)
return
if selfSS['branch'] != otherSS['branch']:
defer.returnValue(False)
return
if selfSS['project'] != otherSS['project']:
defer.returnValue(False)
return
# anything with a patch won't be collapsed
if selfSS['patch'] or otherSS['patch']:
defer.returnValue(False)
return
defer.returnValue(True)
def mergeSourceStampsWith(self, others):
""" Returns one merged sourcestamp for every codebase """
# get all codebases from all requests
all_codebases = set(self.sources.iterkeys())
for other in others:
all_codebases |= set(other.sources.iterkeys())
all_merged_sources = {}
# walk along the codebases
for codebase in all_codebases:
all_sources = []
if codebase in self.sources:
all_sources.append(self.sources[codebase])
for other in others:
if codebase in other.sources:
all_sources.append(other.sources[codebase])
assert len(all_sources) > 0, "each codebase should have atleast one sourcestamp"
# TODO: select the sourcestamp that best represents the merge,
# preferably the latest one. This used to be accomplished by
# looking at changeids and picking the highest-numbered.
all_merged_sources[codebase] = all_sources[-1]
return [source for source in all_merged_sources.itervalues()]
def mergeReasons(self, others):
"""Return a reason for the merged build request."""
reasons = []
for req in [self] + others:
if req.reason and req.reason not in reasons:
reasons.append(req.reason)
return ", ".join(reasons)
def getSubmitTime(self):
return self.submittedAt
@defer.inlineCallbacks
def cancelBuildRequest(self):
# first, try to claim the request; if this fails, then it's too late to
# cancel the build anyway
try:
yield self.master.data.updates.claimBuildRequests([self.id])
except buildrequests.AlreadyClaimedError:
log.msg("build request already claimed; cannot cancel")
return
# send a cancellation message
builderid = -1 # TODO
key = ('buildrequests', self.bsid, builderid, self.id, 'cancelled')
msg = dict(
brid=self.id,
bsid=self.bsid,
buildername=self.buildername,
builderid=builderid)
self.master.mq.produce(key, msg)
# then complete it with 'FAILURE'; this is the closest we can get to
# cancelling a request without running into trouble with dangling
# references.
yield self.master.data.updates.completeBuildRequests([self.id],
FAILURE)
class BuildRequestControl:
implements(interfaces.IBuildRequestControl)
def __init__(self, builder, request):
self.original_builder = builder
self.original_request = request
self.brid = request.id
def subscribe(self, observer):
raise NotImplementedError
def unsubscribe(self, observer):
raise NotImplementedError
def cancel(self):
d = self.original_request.cancelBuildRequest()
d.addErrback(log.err, 'while cancelling build request')