/
buildsets.py
231 lines (188 loc) · 8.33 KB
/
buildsets.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
# 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 copy
from buildbot.data import base
from buildbot.data import sourcestamps as sourcestampsapi
from buildbot.data import types
from buildbot.process.buildrequest import BuildRequestCollapser
from buildbot.status.results import SUCCESS
from buildbot.status.results import worst_status
from buildbot.util import datetime2epoch
from buildbot.util import epoch2datetime
from twisted.internet import defer
from twisted.internet import reactor
from twisted.python import log
class Db2DataMixin(object):
@defer.inlineCallbacks
def db2data(self, bsdict):
if not bsdict:
defer.returnValue(None)
buildset = bsdict.copy()
# gather the actual sourcestamps, in parallel
sourcestamps = []
@defer.inlineCallbacks
def getSs(ssid):
ss = yield self.master.data.get(('sourcestamps', str(ssid)))
sourcestamps.append(ss)
yield defer.DeferredList([getSs(id)
for id in buildset['sourcestamps']],
fireOnOneErrback=True, consumeErrors=True)
buildset['sourcestamps'] = sourcestamps
# minor modifications
buildset['submitted_at'] = datetime2epoch(buildset['submitted_at'])
buildset['complete_at'] = datetime2epoch(buildset['complete_at'])
defer.returnValue(buildset)
class BuildsetEndpoint(Db2DataMixin, base.Endpoint):
isCollection = False
pathPatterns = """
/buildsets/n:bsid
"""
@defer.inlineCallbacks
def get(self, resultSpec, kwargs):
res = yield self.master.db.buildsets.getBuildset(kwargs['bsid'])
res = yield self.db2data(res)
defer.returnValue(res)
def startConsuming(self, callback, options, kwargs):
return self.master.mq.startConsuming(callback,
('buildsets', str(kwargs['bsid']), 'complete'))
class BuildsetsEndpoint(Db2DataMixin, base.Endpoint):
isCollection = True
pathPatterns = """
/buildsets
"""
rootLinkName = 'buildset'
def get(self, resultSpec, kwargs):
complete = resultSpec.popBooleanFilter('complete')
d = self.master.db.buildsets.getBuildsets(complete=complete)
@d.addCallback
def db2data(buildsets):
d = defer.DeferredList([self.db2data(bs) for bs in buildsets],
fireOnOneErrback=True, consumeErrors=True)
@d.addCallback
def getResults(res):
return [r[1] for r in res]
return d
return d
def startConsuming(self, callback, options, kwargs):
return self.master.mq.startConsuming(callback,
('buildsets', None, 'new'))
class Buildset(base.ResourceType):
name = "buildset"
plural = "buildsets"
endpoints = [BuildsetEndpoint, BuildsetsEndpoint]
keyFields = ['bsid']
eventPathPatterns = """
/buildsets/:bsid
"""
class EntityType(types.Entity):
bsid = types.Integer()
external_idstring = types.NoneOk(types.String())
reason = types.String()
submitted_at = types.Integer()
complete = types.Boolean()
complete_at = types.NoneOk(types.Integer())
results = types.NoneOk(types.Integer())
sourcestamps = types.List(
of=sourcestampsapi.SourceStamp.entityType)
parent_buildid = types.NoneOk(types.Integer())
parent_relationship = types.NoneOk(types.String())
entityType = EntityType(name)
@base.updateMethod
@defer.inlineCallbacks
def addBuildset(self, waited_for, scheduler=None, sourcestamps=[], reason=u'',
properties={}, builderids=[], external_idstring=None,
parent_buildid=None, parent_relationship=None,
_reactor=reactor):
submitted_at = int(_reactor.seconds())
bsid, brids = yield self.master.db.buildsets.addBuildset(
sourcestamps=sourcestamps, reason=reason,
properties=properties, builderids=builderids,
waited_for=waited_for, external_idstring=external_idstring,
submitted_at=epoch2datetime(submitted_at),
parent_buildid=parent_buildid, parent_relationship=parent_relationship)
yield BuildRequestCollapser(self.master, brids.values()).collapse()
# get each of the sourcestamps for this buildset (sequentially)
bsdict = yield self.master.db.buildsets.getBuildset(bsid)
sourcestamps = [
(yield self.master.data.get(('sourcestamps', str(ssid)))).copy()
for ssid in bsdict['sourcestamps']]
# notify about the component build requests
brResource = self.master.data.getResourceType("buildrequest")
brResource.generateEvent(brids.values(), 'new')
# and the buildset itself
msg = dict(
bsid=bsid,
external_idstring=external_idstring,
reason=reason,
submitted_at=submitted_at,
complete=False,
complete_at=None,
results=None,
scheduler=scheduler,
sourcestamps=sourcestamps)
# TODO: properties=properties)
self.produceEvent(msg, "new")
log.msg("added buildset %d to database" % bsid)
# if there are no builders, then this is done already, so send the
# appropriate messages for that
if not builderids:
yield self.maybeBuildsetComplete(bsid, _reactor=_reactor)
defer.returnValue((bsid, brids))
@base.updateMethod
@defer.inlineCallbacks
def maybeBuildsetComplete(self, bsid, _reactor=reactor):
brdicts = yield self.master.db.buildrequests.getBuildRequests(
bsid=bsid, complete=False)
# if there are incomplete buildrequests, bail out
if brdicts:
return
brdicts = yield self.master.db.buildrequests.getBuildRequests(bsid=bsid)
# figure out the overall results of the buildset:
cumulative_results = SUCCESS
for brdict in brdicts:
cumulative_results = worst_status(cumulative_results, brdict['results'])
# get a copy of the buildset
bsdict = yield self.master.db.buildsets.getBuildset(bsid)
# if it's already completed, we're late to the game, and there's
# nothing to do.
#
# NOTE: there's still a strong possibility of a race condition here,
# which would cause two buildset.$bsid.complete messages to be sent.
# That's an acceptable risk, and a necessary consequence of this
# denormalized representation of a buildset's state.
if bsdict['complete']:
return
# mark it as completed in the database
complete_at = epoch2datetime(int(_reactor.seconds()))
yield self.master.db.buildsets.completeBuildset(bsid,
cumulative_results, complete_at=complete_at)
# get the sourcestamps for the message
# get each of the sourcestamps for this buildset (sequentially)
bsdict = yield self.master.db.buildsets.getBuildset(bsid)
sourcestamps = [
copy.deepcopy((yield self.master.data.get(
('sourcestamps', str(ssid)))))
for ssid in bsdict['sourcestamps']]
msg = dict(
bsid=bsid,
external_idstring=bsdict['external_idstring'],
reason=bsdict['reason'],
sourcestamps=sourcestamps,
submitted_at=bsdict['submitted_at'],
complete=True,
complete_at=complete_at,
results=cumulative_results)
# TODO: properties=properties)
self.produceEvent(msg, "complete")