-
Notifications
You must be signed in to change notification settings - Fork 4
/
run.php
376 lines (347 loc) · 14 KB
/
run.php
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
<?php
// run.php -- Peteramati runner page
// HotCRP and Peteramati are Copyright (c) 2006-2021 Eddie Kohler and others
// See LICENSE for open-source distribution terms
require_once("src/initweb.php");
if ($Me->is_empty()) {
$Me->escape();
}
class RunRequest {
/** @var Conf */
public $conf;
/** @var Contact */
public $user;
/** @var Contact */
public $viewer;
/** @var Qrequest */
public $qreq;
/** @var Pset */
public $pset;
/** @var RunnerConfig */
public $runner;
/** @var bool */
public $if_needed;
/** @var ?PsetView */
private $info;
/** @param string $err
* @return array */
static function error($err = null) {
return ["ok" => false, "error" => $err];
}
static function quit($err = null, $js = null) {
json_exit(["ok" => false, "error" => $err] + ($js ?? []));
}
function __construct(Contact $viewer, Qrequest $qreq) {
$this->conf = $viewer->conf;
$this->user = $this->viewer = $viewer;
$this->qreq = $qreq;
if ($qreq->u !== null
&& !($this->user = ContactView::prepare_user($qreq, $viewer))) {
json_exit(["ok" => false]);
}
assert($this->user === $this->viewer || $this->viewer->isPC);
$this->pset = ContactView::find_pset_redirect($qreq->pset, $viewer);
$this->runner = $this->pset->runner_by_key($qreq->run);
if (!$this->runner
|| (!$this->viewer->isPC && !$this->runner->visible)) {
self::quit("No such command");
}
$this->if_needed = str_ends_with($qreq->run, ".ifneeded") || $qreq->ifneeded;
}
static function go(Contact $user, Qrequest $qreq) {
$rreq = new RunRequest($user, $qreq);
if ($qreq->runmany) {
$rreq->runmany();
} else if ($qreq->download) {
$rreq->download();
} else {
json_exit($rreq->run());
}
}
/** @param bool $many
* @return ?string */
private function check_view($many) {
if (!$this->viewer->can_view_run($this->pset, $this->runner, $many ? null : $this->user)) {
if (!$this->viewer->isPC && !$this->runner->visible) {
return "No such command";
} else if ($this->runner->disabled) {
return "Command disabled";
} else {
return "Can’t run command now";
}
} else {
return null;
}
}
function run() {
$qreq = $this->qreq;
if ($qreq->run === null || !$qreq->valid_post()) {
return self::error("Permission error");
} else if (($err = $this->check_view(false))) {
return self::error($err);
}
$info = PsetView::make($this->pset, $this->user, $this->viewer, $qreq->newcommit ?? $qreq->commit);
if ($qreq->queueid === "") {
unset($qreq->queueid);
} else if (isset($qreq->queueid) && !ctype_digit($qreq->queueid)) {
return self::error("Bad queueid");
}
if ($qreq->check === "") {
unset($qreq->check);
} else if (isset($qreq->check) && !ctype_digit($qreq->check)) {
return self::error("Bad check timestamp");
}
// can we run this?
if (isset($qreq->check) && !$this->runner->evaluate_function
? !$this->viewer->can_view_run($this->pset, $this->runner, $this->user)
: !$this->viewer->can_run($this->pset, $this->runner, $this->user)) {
return self::error("You can’t run that command");
}
if ($this->runner->command
&& $info->repo
&& !$info->can_view_repo_contents()) {
return self::error("You can’t view this repository");
}
// load queue item
$qi = null;
if (isset($qreq->queueid) && isset($qreq->check)) {
$qi = QueueItem::by_id($this->conf, intval($qreq->queueid), $info);
}
if (!$qi && isset($qreq->check)) {
$rr = $info->run_logger()->job_response(intval($qreq->check));
if (!$rr
|| ($rr->pset !== $info->pset->urlkey
&& $this->conf->pset_by_key($rr->pset) !== $info->pset)
|| $rr->runner !== $this->runner->name) {
return self::error("Unknown check timestamp {$qreq->check}");
}
$qi = QueueItem::for_run_response($info, $rr);
}
if (!$qi && isset($qreq->queueid)) {
if (!($qi = QueueItem::by_id($this->conf, intval($qreq->queueid), $info))) {
return self::error("Unknown queueid {$qreq->queueid}");
}
}
if (!$qi && $this->if_needed) {
$qi = QueueItem::for_complete_job($info, $this->runner);
}
// complain if unrunnable
if ($this->runner->command
&& (!$info->repo || !$info->commit())) {
if ($qi) {
$qi->cancel();
}
if (!$info->repo) {
return self::error("No repository");
} else if ($qreq->newcommit ?? $qreq->commit) {
return self::error("Commit " . ($qreq->newcommit ?? $qreq->commit) . " isn’t connected to this repository");
} else {
return self::error("No commits in repository");
}
}
// check with existing queue item
if ($qi) {
if ($qi->psetid !== $info->pset->id
|| $qi->repoid !== ($info->repo ? $info->repo->repoid : 0)
|| $qi->runnername !== $this->runner->name) {
return self::error("Wrong runner");
}
if ($qi->has_response()) {
return $qi->full_response(cvtint($qreq->offset, 0), $qreq->write ?? "", !!$qreq->stop);
}
}
// maybe evaluate
if (!$this->runner->command) {
$qi = $qi ?? QueueItem::make_info($info, $this->runner);
$qi->step(new QueueState);
return $qi->full_response();
}
// otherwise check runnability and enqueue
if (!$qi) {
if (!$this->pset->run_dirpattern) {
return self::error("Configuration error (run_dirpattern)");
} else if (!$this->pset->run_jailfiles) {
return self::error("Configuration error (run_jailfiles)");
} else if (!$info->repo || !$info->pset) {
return self::error("Nothing to do");
}
$qi = QueueItem::make_info($info, $this->runner);
if ($this->viewer->privChair) {
$qi->flags |= QueueItem::FLAG_UNWATCHED;
}
if ($this->if_needed) {
$qi->ifneeded = 1;
}
$qi->schedule(100, $this->viewer->contactId);
} else {
$qi->update();
}
assert($qi->scheduled() && $qi->runat === 0 && $qi->runorder > 0);
session_write_close();
// process queue
$qs = new QueueState;
$result = $info->conf->qe("select * from ExecutionQueue
where (status>=? and status<? and runorder<=?) or queueid=?
order by runorder asc, queueid asc limit 500",
QueueItem::STATUS_SCHEDULED, QueueItem::STATUS_CANCELLED,
$qi->runorder, $qi->queueid);
$qs = QueueState::fetch_list($info->conf, $result);
$nahead = 0;
$is_ahead = true;
while (($qix = $qs->shift())) {
if ($qix->queueid === $qi->queueid) {
$qix = $qi;
$is_ahead = false;
} else if ($is_ahead) {
++$nahead;
}
if (!$qix->step($qs)) {
if ($qix === $qi) {
return self::error($qix->last_error);
} else {
error_log($qix->unparse_key() . ": " . $qix->last_error);
}
} else if ($qix === $qi && $qi->has_response()) {
return $qi->full_response();
}
}
return [
"queueid" => $qi->queueid,
"onqueue" => true,
"nahead" => $nahead
];
}
function runmany() {
if (!$this->viewer->isPC) {
self::quit("Command reserved for TFs");
} else if (($err = $this->check_view(true))) {
self::quit($err);
} else if (isset($this->qreq->chain) && ctype_digit($this->qreq->chain)) {
if ($this->pset->has_xterm_js) {
$this->conf->add_stylesheet("stylesheets/xterm.css");
$this->conf->add_javascript("scripts/xterm.js");
}
$t = $this->pset->title;
if ($this->if_needed) {
$t .= " (if needed)";
}
$t .= " {$this->runner->title}";
$this->conf->header(htmlspecialchars($t), "home");
echo '<h2 id="pa-runmany-who"></h2>',
Ht::form($this->conf->hoturl("=run"), ["id" => "pa-runmany-form"]),
'<div class="f-contain">',
Ht::hidden("u", ""),
Ht::hidden("pset", $this->pset->urlkey),
Ht::hidden("commit", ""),
Ht::hidden("jobs", "", ["disabled" => 1]);
if ($this->if_needed) {
echo Ht::hidden("ifneeded", 1);
}
echo Ht::hidden("run", $this->runner->name, ["id" => "pa-runmany"]),
'</div></form>';
echo '<div id="run-', $this->runner->name, '">',
'<div class="pa-run pa-run-short"',
$this->runner->div_attributes($this->pset), '>',
'<pre class="pa-runpre"></pre></div>',
'</div>';
Ht::stash_script("\$pa.runmany({$this->qreq->chain})");
echo "<hr class=\"c\">\n";
$this->conf->footer();
} else if (!$this->qreq->valid_post()) {
self::quit("Session out of date");
} else {
$users = [];
foreach ($this->qreq as $k => $v) {
if (substr($k, 0, 2) === "s:"
&& $v
&& ($uname = urldecode(substr($k, 2)))) {
$users[] = $uname;
}
}
if (empty($users) && ($this->qreq->slist ?? $this->qreq->users)) {
$users = preg_split('/\s+/', $this->qreq->slist ?? $this->qreq->users, -1, PREG_SPLIT_NO_EMPTY);
}
$runorder = QueueItem::unscheduled_runorder();
$chain = QueueItem::new_chain();
$commitq = PsetView::parse_commit_query($this->qreq->commitq);
foreach ($users as $uname) {
if (!($u = $this->conf->user_by_whatever($uname))) {
continue;
}
$info = PsetView::make($this->pset, $u, $this->viewer);
if ($this->runner->command && !$info->repo) {
continue;
}
if ($commitq && !$info->select_commit($commitq)) {
continue;
}
$qi = QueueItem::make_info($info, $this->runner);
$qi->chain = $chain;
$qi->runorder = $runorder;
$qi->flags |= QueueItem::FLAG_UNWATCHED;
if ($this->if_needed) {
$qi->ifneeded = 1;
}
$qi->enqueue();
$runorder += 10;
}
$this->conf->redirect_hoturl("run", ["pset" => $this->pset->urlkey, "run" => $this->runner->name, "runmany" => 1, "chain" => $chain]);
}
}
function download() {
$qreq = $this->qreq;
if ($qreq->run === null || !$qreq->valid_token()) {
return self::error("Permission error");
} else if (($err = $this->check_view(true))) {
return self::error($err);
} else if (!$qreq->jobs
|| !($jobs = json_decode($qreq->jobs))
|| !is_array($jobs)) {
return self::error("Bad `jobs` parameter");
}
$boundary = "--peteramati-" . base64url_encode(random_bytes(24));
$mf = fopen("php://temp", "w+b");
foreach ($jobs as $i => $run) {
if (!is_object($run)
|| !is_string($run->u ?? null)
|| (!is_string($run->pset ?? null) && !is_int($run->pset ?? null))
|| !is_string($run->run ?? null)
|| !is_int($run->timestamp ?? null)) {
return self::error("Bad `jobs[{$i}]` parameter");
}
$pset = $this->conf->pset_by_key($run->pset);
if (!$pset) {
return self::error("Bad `jobs[{$i}].pset` parameter");
}
$user = ContactView::find_user($run->u, $this->viewer, $pset->anonymous);
if (!$user) {
return self::error("No such user `jobs[{$i}].u` ({$run->u})");
}
$runner = $pset->runner_by_key($run->run);
if (!$runner) {
return self::error("No such runner `jobs[{$i}].run` ({$run->run})");
}
if (!$this->viewer->can_view_run($pset, $runner, $user)) {
fwrite($mf, "{$boundary}\n" . json_encode($run) . ": Cannot view\n");
continue;
}
$repo = $user->repo($pset->id);
$rr = (new RunLogger($pset, $repo))->job_response($run->timestamp, 0, 100 << 20);
if (!$rr) {
fwrite($mf, "{$boundary}\n" . json_encode($run) . ": No such job\n");
} else {
fwrite($mf, "{$boundary}\n" . json_encode($run) . "\n");
fwrite($mf, $rr->data);
}
$rr = null;
}
fwrite($mf, "{$boundary}--\n");
header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=" . mime_quote_string($this->conf->download_prefix . $pset->urlkey . "-" . $this->runner->name . ".out"));
rewind($mf);
fpassthru($mf);
fclose($mf);
}
}
ContactView::set_path_request($Qreq, ["/@", "/@/p", "/@/p/h", "/p", "/p/h", "/p/u/h"], $Conf);
RunRequest::go($Me, $Qreq);