This repository has been archived by the owner on Mar 19, 2020. It is now read-only.
forked from mozilla/elmo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
status.py
267 lines (224 loc) · 9.96 KB
/
status.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
# 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/.
'''Status plugin to be used in buildbot masters serving the l10n dashboard.
'''
import os
import os.path
from buildbot.status.base import StatusReceiverMultiService, StatusReceiver
from buildbot.scheduler import BaseScheduler
from twisted.python import log
from collections import defaultdict
# no imports of bb2mbdb code here, needs to be done after setting
# settings.py in os.environ in setupBridge
'''Buildbot StatusReceiver for bb2mbdb
The main entry point for buildbot status notifications are a
- fake scheduler to get all changes, and a
- StatusReceiver to get all builder.
Both are set up by calling into
setupBridge()
so that you can pass in a single settings.py, and a BuildMasterConfig.
'''
# Notes on transaction handling:
# There are two patterns for the transaction handling in the status
# plugin below. Firstly, for methods that simply update the db, the
# handlers are decorated with @transation.commit_on_success. For
# handlers that attach new handlers, the decoration is
# @transition.commit_manually, and transaction.commit() is called after
# the child db objects is got, and before the child listener is created.
def setupBridge(master, settings, config):
'''Setup the bridget between buildbot and the database.
This is also the closure in which all things happen that depend
on the given settings.
'''
# allow settings to be none for tests
if settings is not None:
os.environ['DJANGO_SETTINGS_MODULE'] = settings
import bb2mbdb.utils
reload(bb2mbdb.utils)
import mbdb.models
reload(mbdb.models)
from bb2mbdb.utils import modelForSource, modelForChange, modelForLog, \
timeHelper
from mbdb.models import Master, Slave, Builder, BuildRequest, Build
from django.db import transaction
try:
# hack around the lack of @transaction.commit_manually
transaction.enter_transaction_management()
transaction.managed(True)
dbm, new_master = Master.objects.get_or_create(name=master)
transaction.commit()
except:
transaction.rollback()
raise
finally:
transaction.leave_transaction_management()
class Scheduler(BaseScheduler):
@transaction.commit_on_success
def addChange(self, change):
dbchange = modelForChange(dbm, change)
log.msg('ADDED CHANGE to DB, %d' % dbchange.number)
def listBuilderNames(self):
# Sadly, we need this. Buildbot is going to complain that we
# don't build. What does he know.
return []
if 'schedulers' not in config:
config['schedulers'] = []
config['schedulers'].insert(0, Scheduler('bb2mbdb'))
class StepReceiver(StatusReceiver):
'''Build- and StatusReceiver helper objects to receive all
events for a particular step.
'''
def __init__(self, dbstep, basedir):
self.step = dbstep
self.basedir = basedir
@transaction.commit_on_success
def stepTextChanged(self, build, step, text):
self.step.text = text
self.step.save()
@transaction.commit_on_success
def stepText2Changed(self, build, step, text2):
self.step.text2 = text2
self.step.save()
@transaction.commit_on_success
def logStarted(self, build, step, log):
self.log = modelForLog(self.step, log, self.basedir)
def logChunk(self, build, step, log, channel, text):
pass
@transaction.commit_on_success
def logFinished(self, build, step, log):
self.log.isFinished = True
self.log.save()
pass
def stepETAUpdate(self, build, step, ETA, expectations):
'''TODO: ETA support.
'''
pass
class BuildReceiver(StatusReceiver):
'''StatusReceiver helper object to receive all events
for a particular build.
Caches the database model object.
'''
def __init__(self, dbbuild, basedir):
self.build = dbbuild
self.basedir = basedir
self.latestStep = self.latestDbStep = None
@transaction.commit_manually
def stepStarted(self, build, step):
self.latestStep = step
starttime = timeHelper(step.getTimes()[0])
self.latestDbStep = self.build.steps.create(name=step.getName(),
starttime=starttime,
text=step.getText(),
text2=step.text2)
transaction.commit()
return StepReceiver(self.latestDbStep, self.basedir)
@transaction.commit_on_success
def stepFinished(self, build, step, results):
assert step == self.latestStep, "We lost a step somewhere"
try:
self.latestStep = None
self.latestDbStep.endtime = timeHelper(step.getTimes()[1])
# only the first is the result, the second is text2,
# ignore that.
self.latestDbStep.result = results[0]
self.latestDbStep.text = step.getText()
self.latestDbStep.text2 = step.text2
self.latestDbStep.save()
self.latestDbStep = None
except Exception, e:
log.msg(str(e))
pass
def buildETAUpdate(self, build, ETA):
'''TODO: ETA support.
'''
pass
class MyStatusReceiver(StatusReceiverMultiService):
'''StatusReceiver for buildbot to db bridge.
'''
requestsForBuild = defaultdict(list)
def setServiceParent(self, parent):
StatusReceiverMultiService.setServiceParent(self, parent)
self.setup()
def setup(self):
log.msg("mbdb subscribing")
status = self.parent.getStatus()
status.subscribe(self)
@transaction.commit_on_success
def builderAdded(self, builderName, builder):
log.msg("adding %s to mbdb" % builderName)
try:
dbbuilder = Builder.objects.get(master=dbm, name=builderName)
except Builder.DoesNotExist:
dbbuilder = Builder.objects.create(master=dbm,
name=builderName)
dbbuilder.bigState = builder.getState()[0]
if builder.category:
dbbuilder.category = builder.category
dbbuilder.save()
log.msg("added %s to mbdb" % builderName)
return self
@transaction.commit_manually
def requestSubmitted(self, request):
b, _ = dbm.builders.get_or_create(name=request.getBuilderName())
ss = modelForSource(dbm, request.source)
submitTime = timeHelper(request.getSubmitTime())
req = BuildRequest.objects.create(builder=b,
submitTime=submitTime,
sourcestamp=ss)
transaction.commit()
def addBuild(build):
dbbuild = b.builds.get(buildnumber=build.getNumber())
dbbuild.requests.add(req)
dbbuild.save()
request.subscribe(addBuild)
@transaction.commit_on_success
def builderChangedState(self, builderName, state):
log.msg("%s changed state to %s" % (builderName, state))
dbbuilder = Builder.objects.get(master=dbm, name=builderName)
dbbuilder.bigState = state
dbbuilder.save()
@transaction.commit_manually
def buildStarted(self, builderName, build):
log.msg("build started on %s" % builderName)
builder = Builder.objects.get(master=dbm, name=builderName)
slave, _ = Slave.objects.get_or_create(name=build.getSlavename())
ss = modelForSource(dbm, build.getSourceStamp())
starttime = timeHelper(build.getTimes()[0])
dbbuild, created = \
builder.builds.get_or_create(buildnumber=build.getNumber(),
slave=slave,
starttime=starttime,
reason=build.getReason(),
sourcestamp=ss)
if not created:
log.msg("%s build %d not created, endtime is %s" %
(builderName, build.getNumber(), dbbuild.endtime))
log.msg("not watch this build, to make sure")
return
for key, value, source in build.getProperties().asList():
dbbuild.setProperty(key, value, source)
dbbuild.save()
transaction.commit()
basedir = os.path.join(build.getBuilder().basedir, '..')
return BuildReceiver(dbbuild, basedir)
@transaction.commit_on_success
def buildFinished(self, builderName, build, results):
log.msg("finished build on %s with %s" %
(builderName, str(results)))
dbbuild = Build.objects.get(builder__name=builderName,
buildnumber=build.getNumber())
dbbuild.endtime = timeHelper(build.getTimes()[1])
dbbuild.result = results
for key, value, source in build.getProperties().asList():
dbbuild.setProperty(key, value, source)
dbbuild.save()
pass
def builderRemoved(self, builderName):
log.msg("removing %s to mbdb" % builderName)
# nothing to do here, afaict.
pass
if 'status' not in config:
config['status'] = []
config['status'].insert(0, MyStatusReceiver())
log.msg("Done setting up Bridge")