This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
scheduler.js
393 lines (335 loc) · 11.1 KB
/
scheduler.js
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
382
383
384
385
386
387
388
389
390
391
392
393
'use strict';
var debug = require('debug')(__filename.slice(__dirname.length + 1));
var util = require('./crater-util');
var crateIndex = require('./crate-index');
var Promise = require('promise');
var async = require('async');
var slugid = require('slugid');
var tc = require('taskcluster-client');
var assert = require('assert');
var dist = require('./rust-dist');
var db = require('./crater-db');
var sleep = require('sleep');
var https = require('https');
var customBuildMaxRunTimeInSeconds = 240 * 60;
var crateBuildMaxRunTimeInSeconds = 10 * 60;
/**
* Create a schedule of tasks for execution by `scheduleBuilds`.
*/
function createSchedule(schedOpts, config, dbctx) {
return crateIndex.loadCrates(config).then(function(crates) {
if (schedOpts.crateName) {
return retainMatchingNames(crates, schedOpts.crateName);
} else {
return crates;
}
}).then(function(crates) {
if (schedOpts.top) {
return retainTop(crates, schedOpts.top);
} else {
return crates;
}
}).then(function(crates) {
if (schedOpts.mostRecentOnly) {
return retainMostRecent(crates);
} else {
return crates;
}
}).then(function(crates) {
if (schedOpts.skipExisting) {
return removeCratesWithCompleteResults(crates, dbctx, schedOpts.toolchain);
} else {
return crates;
}
}).then(function(crates) {
return createScheduleForCratesForToolchain(crates, schedOpts.toolchain);
});
}
function removeCratesWithCompleteResults(crates, dbctx, toolchain) {
// Look up every crate's results and throw out the build request
// if it exists, by first setting it to null then filtering it out.
// (async doesn't have 'filterLimit').
return Promise.denodeify(async.mapLimit)(crates, 1, function(crate, cb) {
var buildResultKey = {
toolchain: toolchain,
crateName: crate.name,
crateVers: crate.vers
};
db.getBuildResult(dbctx, buildResultKey).then(function(buildResult) {
if (buildResult) {
if (buildResult.status == "success" || buildResult.status == "failure") {
// Already have a result, map this crate to null
debug("existing result for " + crate.name + "-" + crate.vers);
cb(null, null);
} else {
// Have a result, but not a usable one. Possibly an exception
debug("bad existing result for " + crate.name + "-" + crate.vers);
cb(null, crate);
}
} else {
debug("no existing result for " + crate.name + "-" + crate.vers);
cb(null, crate);
}
}).catch(function(e) {
cb(e, null);
});
}).then(function(crates) {
return crates.filter(function(crate) {
if (crate) { return true; } else { return false; }
});
});
}
function retainTop(crates, count) {
var popMap = crateIndex.getPopularityMap(crates);
var sorted = crates.slice();
sorted.sort(function(a, b) {
var aPop = popMap[a.name];
var bPop = popMap[b.name];
if (aPop == bPop) { return 0; }
if (aPop < bPop) { return 1; }
if (aPop > bPop) { return -1; }
});
// We want the to *count* unique crate names, but to keep
// all revisions.
var finalSorted = [];
var seenCrateNames = {};
for (var i = 0; i < sorted.length; i++) {
var crate = sorted[i];
seenCrateNames[crate.name] = 0;
if (Object.keys(seenCrateNames).length > count) {
break;
}
finalSorted.push(crate);
}
return finalSorted;
}
function retainMostRecent(crates, count) {
var mostRecent = crateIndex.getMostRecentRevs(crates);
var result = [];
crates.forEach(function(crate) {
var recent = mostRecent[crate.name];
if (crate.vers == recent.vers) {
result.push(crate);
}
});
return result;
}
function retainMatchingNames(crates, name) {
var result = [];
crates.forEach(function(crate) {
if (crate.name == name) {
result.push(crate);
}
});
return result;
}
function createScheduleForCratesForToolchain(crates, toolchain) {
// Convert to scheduler commands
var tasks = [];
crates.forEach(function(crate) {
var task = {
toolchain: toolchain,
crateName: crate.name,
crateVers: crate.vers
}
tasks.push(task);
});
return tasks;
}
function scheduleBuilds(dbctx, schedule, config) {
var tcCredentials = config.tcCredentials;
assert(tcCredentials != null);
// FIXME: For testing, just schedule five builds instead of thousands
if (schedule.length > 5) {
//schedule = schedule.slice(0, 5)
}
var queue = new tc.Queue({
credentials: tcCredentials
});
var total = schedule.length;
var i = 1;
return Promise.denodeify(async.mapLimit)(schedule, 50, function(schedule, cb) {
createTaskDescriptorForCrateBuild(dbctx, schedule, config).then(function(taskDesc) {
debug("createTask payload: " + JSON.stringify(taskDesc));
var taskId = slugid.v4();
debug("creating task " + i + " of " + total + " for " + schedule.crateName + "-" + schedule.crateVers);
i = i + 1;
queue.createTask(taskId, taskDesc)
.catch(function(e) {
// TODO: How to handle a single failure here?
console.log("error creating task for " + JSON.stringify(schedule));
console.log("error is " + e);
cb(e, null);
}).then(function(result) {
console.log("created task for " + JSON.stringify(schedule));
console.log("inspector link: https://tools.taskcluster.net/task-inspector/#" + taskId);
cb(null, result);
});
}).catch(function(e) {
cb(e, null);
}).done();
})
return p;
}
function createTaskDescriptorForCrateBuild(dbctx, schedule, config) {
var dlRootAddr = config.dlRootAddr;
debug("creating task descriptor for " + JSON.stringify(schedule));
var crateName = schedule.crateName;
var crateVers = schedule.crateVers;
assert(crateName != null);
assert(crateVers != null);
var p = installerUrlsForToolchain(dbctx, schedule.toolchain, config)
return p.then(function(installerUrls) {
var crateUrl = dlRootAddr + "/" + crateName + "/" + crateVers + "/download";
var taskName = util.toolchainToString(schedule.toolchain) + "-vs-" + crateName + "-" + crateVers;
var env = {
"CRATER_RUST_INSTALLER": installerUrls.rustInstallerUrl,
"CRATER_CRATE_FILE": crateUrl
};
if (installerUrls.stdInstallerUrl) {
env["CRATER_STD_INSTALLER"] = installerUrls.stdInstallerUrl;
}
if (installerUrls.cargoInstallerUrl) {
env["CRATER_CARGO_INSTALLER"] = installerUrls.cargoInstallerUrl;
}
var extra = {
"toolchain": schedule.toolchain,
"crateName": crateName,
"crateVers": crateVers
};
return createTaskDescriptor(taskName, env, extra,
"crate-build", crateBuildMaxRunTimeInSeconds, "cratertest",
{ }, 960 /* deadline in minutes */);
});
}
// FIXME Too many arguments
function createTaskDescriptor(taskName, env, extra, taskType, maxRunTime, workerType, artifacts, deadlineInMinutes) {
var createTime = new Date(Date.now());
var deadlineTime = new Date(createTime.getTime() + deadlineInMinutes * 60000);
var cmd = "cd /home && curl -sfL https://raw.githubusercontent.com/brson/taskcluster-crater/master/run-crater-task.sh -o ./run.sh && sh ./run.sh";
env.CRATER_TASK_TYPE = taskType;
extra.taskType = taskType;
var task = {
"provisionerId": "aws-provisioner-v1",
"workerType": workerType,
"created": createTime.toISOString(),
"deadline": deadlineTime.toISOString(),
"retries": 5,
"routes": [
"crater.#"
],
"payload": {
"image": "brson/crater:3",
"command": [ "/bin/bash", "-c", cmd ],
"env": env,
"maxRunTime": maxRunTime,
"artifacts": artifacts
},
"metadata": {
"name": "Crater task " + taskName,
"description": "Testing Rust crates for Rust language regressions",
"owner": "[email protected]",
"source": "http://github.com/brson/taskcluster-crater"
},
"extra": {
"crater": extra
}
};
return task;
}
function installerUrlsForToolchain(dbctx, toolchain, config) {
if (toolchain.channel) {
return dist.installerUrlForToolchain(toolchain, "x86_64-unknown-linux-gnu", config)
.then(function(url) {
return {
rustInstallerUrl: url,
stdInstallerUrl: null,
cargoInstallerUrl: null
};
});
} else {
debug(toolchain);
assert(toolchain.customSha);
var cargoBaseUrl = "https://s3.amazonaws.com/rust-lang-ci/cargo-builds/";
return db.getCustomToolchain(dbctx, toolchain).then(function(custom) {
var stdUrl = custom.url.replace("rustc-", "rust-std-");
var cargoUrl = custom.url.replace("rustc-", "cargo-");
return {
rustInstallerUrl: custom.url,
stdInstallerUrl: stdUrl,
cargoInstallerUrl: cargoUrl,
};
});
}
}
var cargoNightlySha = null;
function getCargoNightlySha(dbctx) {
if (cargoNightlySha) {
return cargoNightlySha;
}
// curl -H "Accept: application/vnd.github.3.sha" -sSf https://api.github.com/repos/rust-lang/cargo/commits/master
var url = "https://api.github.com/repos/rust-lang/cargo/commits/master";
cargoNightlySha = util.downloadToMem(url).then(function (data) {
return JSON.parse(data).sha;
});
return cargoNightlySha;
}
/**
* Schedules a build and upload of a custom build. Fails if `uniqueName`
* has already been taken.
*/
function scheduleCustomBuild(options, config) {
var gitRepo = options.gitRepo;
var commitSha = options.commitSha;
if (commitSha.length != 40) {
return Promise.reject("bogus sha");
}
var tcCredentials = config.tcCredentials;
var queue = new tc.Queue({ credentials: tcCredentials });
var taskId = slugid.v4();
var taskDesc = createTaskDescriptorForCustomBuild(gitRepo, commitSha);
return queue.createTask(taskId, taskDesc).then(function(result) {
console.log("created task for " + gitRepo);
console.log("inspector link: https://tools.taskcluster.net/task-inspector/#" + taskId);
return result;
});
}
function createTaskDescriptorForCustomBuild(gitRepo, commitSha) {
var taskName = "build-" + commitSha;
var env = {
"CRATER_TOOLCHAIN_GIT_REPO": gitRepo,
"CRATER_TOOLCHAIN_GIT_SHA": commitSha,
};
var extra = {
toolchainGitRepo: gitRepo,
toolchainGitSha: commitSha
};
var twoMonths = 60 /*s*/ * (24 * 60) /*m*/ * (30 * 2) /*d*/;
var expiry = new Date(Date.now());
expiry.setDate(expiry.getDate() + 60);
// Upload the installer
var artifacts = {
"public/rustc-dev-x86_64-unknown-linux-gnu.tar.gz": {
type: "file",
path: "/home/rust/build/dist/rustc-dev-x86_64-unknown-linux-gnu.tar.gz",
expires: expiry
},
"public/rust-std-dev-x86_64-unknown-linux-gnu.tar.gz": {
type: "file",
path: "/home/rust/build/dist/rust-std-dev-x86_64-unknown-linux-gnu.tar.gz",
expires: expiry
},
"public/cargo-dev-x86_64-unknown-linux-gnu.tar.gz": {
type: "file",
path: "/home/rust/build/dist/cargo-dev-x86_64-unknown-linux-gnu.tar.gz",
expires: expiry
}
};
var deadlineInMinutes = 60 * 24; // Rust can take a long time to build successfully
return createTaskDescriptor(taskName, env, extra,
"custom-build", customBuildMaxRunTimeInSeconds, "rustbuild",
artifacts, deadlineInMinutes);
}
exports.createSchedule = createSchedule;
exports.scheduleBuilds = scheduleBuilds;
exports.scheduleCustomBuild = scheduleCustomBuild;