This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
update_from_files.py
304 lines (254 loc) · 10.9 KB
/
update_from_files.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
import buildbotcustom.status.db.model as model
import cPickle, os, re, time, sys
from datetime import datetime
from buildbot.status.builder import BuilderStatus, BuildStepStatus
# Monkey patching!
# These are various replacement functions for __setstate__, which is
# called when unpickling files.
def monkeypatch(orig, new):
orig_setstate = orig.__setstate__
def wrapper(self, state):
return new(self, orig_setstate, state)
orig.__setstate__ = wrapper
def builder_setstate_noevents(self, orig, state):
slaves = state['slavenames']
state['events'] = []
orig(self, state)
self.slavenames = slaves
def builder_setstate_events(self, orig, state):
slaves = state['slavenames']
orig(self, state)
self.slavenames = slaves
def buildstep_setstate(self, orig, state):
state['logs'] = []
orig(self, state)
def getBuildNumbers(builder, last_time):
files = os.listdir(builder)
def _sortfunc(x):
try:
return int(x)
except:
return x
files.sort(key=_sortfunc)
retval = []
for f in files:
if re.match("^\d+$", f):
p = os.path.join(builder, f)
if os.path.getmtime(p) < last_time:
continue
retval.append(f)
return retval
def getBuild(builder, number):
try:
return cPickle.load(open(os.path.join(builder, number)))
except:
return None
def getBuilder(builder):
builder = cPickle.load(open(os.path.join(builder, 'builder')))
return builder
def updateBuilderSlaves(session, builder, db_builder):
bb_slaves = set(s for s in builder.slavenames)
db_slaves = set()
db_slaves_by_name = {}
for builder_slave in db_builder.slaves:
db_slaves.add(builder_slave.slave.name)
db_slaves_by_name[builder_slave.slave.name] = builder_slave
# Which slaves were added to this builder
new_slaves = bb_slaves - db_slaves
# Which slaves were removed from this builder
old_slaves = db_slaves - bb_slaves
for s in new_slaves:
bs = model.BuilderSlave(added=datetime.now(), slave=model.Slave.get(session, s))
db_builder.slaves.append(bs)
session.add(bs)
for s in db_builder.slaves[:]:
if s.slave.name in old_slaves:
# Mark it as removed
db_slaves_by_name[s.slave.name].removed = datetime.now()
session.commit()
def updateSlaveTimes(session, master, builder, db_builder, last_time):
db_slaves = {}
for builder_slave in db_builder.slaves:
db_slaves[builder_slave.slave.name] = builder_slave
# Fetch all the events from the database for these slaves
events = session.query(model.MasterSlave).\
filter(model.MasterSlave.slave_id.in_([slave.slave.id for slave in db_builder.slaves]))
if last_time:
events = events.filter(model.MasterSlave.connected > datetime.utcfromtimestamp(last_time))
events = events.order_by(model.MasterSlave.connected.asc()).all()
events_by_slave = {}
for e in events:
if e.slave.name not in events_by_slave:
events_by_slave[e.slave.name] = []
events_by_slave[e.slave.name].append(e)
for e in builder.events:
if len(e.text) == 2 and e.text[0] in ("connect", "disconnect"):
name = e.text[1]
if e.started < last_time:
continue
if name not in db_slaves:
# We don't know about this slave
continue
if name not in events_by_slave:
events_by_slave[name] = []
slave_events = events_by_slave[name]
t = datetime.utcfromtimestamp(int(e.started))
if e.text[0] == "connect":
# This slave just connected to this builder
# Check if we've got an entry earlier than this that hasn't been disconnected yet
found = False
for event in reversed(slave_events):
if event.connected < t and not event.disconnected:
found = True
break
if event.connected == t:
found = True
break
if not found:
# Didn't find an event, need to create one!
event = session.query(model.MasterSlave).filter_by(connected=t, slave=db_slaves[name].slave, master=master).first()
if event:
print t
for e in reversed(slave_events):
print e.connected, e.connected-t, e.connected == t
raise ValueError("Shouldn't be here!")
event = model.MasterSlave(connected=t, slave=db_slaves[name].slave, master=master)
session.add(event)
slave_events.append(event)
events.append(event)
slave_events.sort(key=lambda x:x.connected)
events.sort(key=lambda x:x.connected)
else:
# If this is a disconnect event, find the last connect event and mark it as disconnected
found = False
for event in reversed(slave_events):
if event.connected < t:
event.disconnected = t
found = True
break
# If we didn't find anything, ignore it for now
elif e.text == ['master', 'shutdown']:
# Set any slaves that were connected at the time to disconnected
t = datetime.utcfromtimestamp(e.started)
for event in reversed(events):
if event.connected < t and not event.disconnected:
event.disconnected = t
session.commit()
def updateFromFiles(session, master_url, master_name, builders, last_time, update_times):
master = model.Master.get(session, master_url)
master.name = unicode(master_name)
i = 0
n = 0
allBuilds = {}
for builder in builders:
buildNumbers = getBuildNumbers(builder, last_time)
allBuilds[builder] = buildNumbers
n += len(buildNumbers)
s = time.time()
for builder in builders:
builds = allBuilds[builder]
bn = len(builds)
if bn == 0:
continue
master = session.merge(master)
builder_name = os.path.basename(builder)
bb_builder = getBuilder(builder)
db_builder = model.Builder.get(session, builder_name, master.id)
db_builder.category = unicode(bb_builder.category)
updateBuilderSlaves(session, bb_builder, db_builder)
if update_times:
updateSlaveTimes(session, master, bb_builder, db_builder, last_time)
for j, buildNumber in enumerate(builds):
master = session.merge(master)
db_builder = session.merge(db_builder)
complete = i / float(n)
if complete == 0:
eta = 0
else:
eta = (time.time() - s) / (complete)
eta = (1-complete) * eta
print builder, buildNumber, "%i/%i" % (j+1, bn), "%.2f%% complete" % (100* complete), "ETA in %i seconds" % eta
i += 1
build = getBuild(builder, buildNumber)
if not build:
continue
starttime = None
if build.started:
starttime = datetime.utcfromtimestamp(build.started)
q = session.query(model.Build).filter_by(
master_id=master.id,
builder=db_builder,
buildnumber=build.number,
starttime=starttime,
)
db_build = q.first()
if not db_build:
db_build = model.Build.fromBBBuild(session, build, builder_name, master.id)
else:
db_build.updateFromBBBuild(session, build)
session.commit()
session.expunge_all()
return i
if __name__ == "__main__":
from optparse import OptionParser
parser = OptionParser("%prog [options] builders")
parser.add_option("-d", "--database", dest="database", help="database url")
parser.add_option("-m", "--master", dest="master", help="master url (buildbotURL in the master.cfg file)")
parser.add_option("-n", "--description", dest="name", help="human friendly name for master")
parser.add_option("", "--times", dest="times", help="update slave connect/disconnect times", action="store_true", default=False)
parser.add_option("-c", "--config", dest="config",
help="read configurations from a file")
options, args = parser.parse_args()
if options.config:
from ConfigParser import RawConfigParser
from ConfigParser import ParsingError, MissingSectionHeaderError
supported_params = ('database', 'master', 'name')
config = RawConfigParser()
try:
if not config.read(options.config):
print >> sys.stderr, "Error reading config file %s" % options.config
sys.exit(1)
for param in supported_params:
# Rewrite empty CLI params if we have them in config
if not getattr(options, param, None) and \
config.has_option('DEFAULT', param):
setattr(options, param, config.get('DEFAULT', param))
except (ParsingError, MissingSectionHeaderError):
print >> sys.stderr, "Error parsing config file %s" % options.config
sys.exit(2)
if not options.database:
parser.error("Must specify a database to connect to")
if not options.master:
parser.error("Must specify a master url")
if not args:
parser.error("Must specify at least one builder or directory")
# Do some monkey patching!
# This is required to prevent this script from trying to load all the logs
# for all the builds, which slows things down quite a bit, and increases
# memory load
if options.times:
# Preserve events if we're updating slave times
monkeypatch(BuilderStatus, builder_setstate_events)
else:
monkeypatch(BuilderStatus, builder_setstate_noevents)
monkeypatch(BuildStepStatus, buildstep_setstate)
builders = []
for a in args:
if os.path.exists(os.path.join(a, "builder")):
builders.append(a)
else:
for d in os.listdir(a):
p = os.path.join(a, d)
if os.path.exists(os.path.join(a, d, "builder")):
builders.append(p)
session = model.connect(options.database)()
started = time.time()
try:
last_time = float(open("last_time.txt").read())
except:
last_time = 0
print "\n" + "-"*75
print "Starting update at", time.ctime(started)
updated = updateFromFiles(session, options.master, options.name, builders, last_time, options.times)
print "Updated", updated, "builds in:"
open("last_time.txt", "w").write(str(started))