1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// This file is part of BOINC. |
4
|
|
|
// http://boinc.berkeley.edu |
5
|
|
|
// Copyright (C) 2011 University of California |
6
|
|
|
// |
7
|
|
|
// BOINC is free software; you can redistribute it and/or modify it |
8
|
|
|
// under the terms of the GNU Lesser General Public License |
9
|
|
|
// as published by the Free Software Foundation, |
10
|
|
|
// either version 3 of the License, or (at your option) any later version. |
11
|
|
|
// |
12
|
|
|
// BOINC is distributed in the hope that it will be useful, |
13
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
15
|
|
|
// See the GNU Lesser General Public License for more details. |
16
|
|
|
// |
17
|
|
|
// You should have received a copy of the GNU Lesser General Public License |
18
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
|
20
|
|
|
// server-side utility functions for remote job submission and control |
21
|
|
|
|
22
|
|
|
require_once("../inc/submit_db.inc"); |
23
|
|
|
|
24
|
|
|
// The status of a workunit. |
25
|
|
|
// Not stored in the DB; |
26
|
|
|
// it's computed by get_batch_params() and added to the workunit object |
27
|
|
|
// |
28
|
|
|
define('WU_UNSENT', 0); |
29
|
|
|
define('WU_IN_PROGRESS', 1); |
30
|
|
|
define('WU_SUCCESS', 2); |
31
|
|
|
define('WU_ERROR', 3); |
32
|
|
|
|
33
|
|
|
// write status and error messages to log |
34
|
|
|
// |
35
|
|
|
function log_write($x) { |
36
|
|
|
static $enabled, $log_file; |
37
|
|
|
|
38
|
|
|
if (!isset($enabled)) { |
39
|
|
|
$enabled = false; |
40
|
|
|
$filename = parse_config(get_config(), "<remote_submit_log>"); |
41
|
|
|
if (!$filename) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
$log_dir = parse_config(get_config(), "<log_dir>"); |
45
|
|
|
if (!$log_dir) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
$log_file = fopen("$log_dir/$filename", "a"); |
49
|
|
|
if (!$log_file) return; |
|
|
|
|
50
|
|
|
$enabled = true; |
51
|
|
|
} |
52
|
|
|
if (!$enabled) return; |
53
|
|
|
fwrite($log_file, sprintf("%s: %s\n", strftime("%c"), $x)); |
54
|
|
|
fflush($log_file); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// in remote job submission, |
58
|
|
|
// for input files of type local, semilocal, and inline, |
59
|
|
|
// we need to give a unique physical name based on its content. |
60
|
|
|
// Prepend the jf_ to make the origin of the file clear |
61
|
|
|
// |
62
|
|
|
function job_file_name($md5) { |
63
|
|
|
return "jf_$md5"; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// can user upload files? |
67
|
|
|
// |
68
|
|
|
function has_file_access($user) { |
69
|
|
|
$us = BoincUserSubmit::lookup_userid($user->id); |
70
|
|
|
if (!$us) return false; |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// can user submit to given app? |
75
|
|
|
// |
76
|
|
|
function has_submit_access($user, $app_id) { |
77
|
|
|
$us = BoincUserSubmit::lookup_userid($user->id); |
78
|
|
|
if (!$us) return false; |
79
|
|
|
if ($us->submit_all) return true; |
80
|
|
|
$usa = BoincUserSubmitApp::lookup("user_id=$user->id and app_id=$app_id"); |
81
|
|
|
if (!$usa) return false; |
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// can user manage given app (or all apps if zero)? |
86
|
|
|
// |
87
|
|
|
function has_manage_access($user, $app_id) { |
88
|
|
|
$us = BoincUserSubmit::lookup_userid($user->id); |
89
|
|
|
if (!$us) return false; |
90
|
|
|
if ($us->manage_all) return true; |
91
|
|
|
$usa = BoincUserSubmitApp::lookup("user_id=$user->id and app_id=$app_id"); |
92
|
|
|
if (!$usa) return false; |
93
|
|
|
return $usa->manage; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// check whether user has permissions for a remote job submission |
97
|
|
|
// or job file request. |
98
|
|
|
// $r is a request message that includes an 'authenticator' field |
99
|
|
|
// $app is the app being submitted to (or null if file op) |
100
|
|
|
// returns user, or give XML error and quit |
101
|
|
|
// |
102
|
|
|
function check_remote_submit_permissions($r, $app) { |
103
|
|
|
$auth = (string)$r->authenticator; |
104
|
|
|
if (!$auth) { |
105
|
|
|
log_write("no authenticator"); |
106
|
|
|
xml_error(-1, "no authenticator"); |
107
|
|
|
} |
108
|
|
|
$auth = BoincDb::escape_string($auth); |
109
|
|
|
$user = BoincUser::lookup("authenticator='$auth'"); |
110
|
|
|
if (!$user) { |
111
|
|
|
log_write("bad authenticator"); |
112
|
|
|
xml_error(-1, "bad authenticator"); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// check access |
116
|
|
|
// |
117
|
|
|
if ($app) { |
118
|
|
|
if (!has_submit_access($user, $app->id)) { |
119
|
|
|
log_write("no submit access"); |
120
|
|
|
xml_error(-1, "no submit access"); |
121
|
|
|
} |
122
|
|
|
} else { |
123
|
|
|
if (!has_file_access($user)) { |
124
|
|
|
log_write("no file access"); |
125
|
|
|
xml_error(-1, "no file access"); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
return $user; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// remove all of user's permissions |
132
|
|
|
// |
133
|
|
|
function delete_remote_submit_user($user) { |
134
|
|
|
BoincUserSubmit::delete_user($user->id); |
135
|
|
|
BoincUserSubmitApp::delete_user($user->id); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// given its WUs, compute parameters of the batch: |
139
|
|
|
// credit_canonical: credit granted to canonical instances |
140
|
|
|
// fraction_done: frac of jobs that are done (success or failed) |
141
|
|
|
// state: whether complete (all jobs done) |
142
|
|
|
// completion_time: if newly complete |
143
|
|
|
// nerror_jobs: # of failed jobs |
144
|
|
|
// Update the above in DB. |
145
|
|
|
// Also compute (not in DB): |
146
|
|
|
// njobs_success: # of jobs with canonical instance |
147
|
|
|
// |
148
|
|
|
// return the batch object, with these values |
149
|
|
|
// |
150
|
|
|
// Also add the status field to WUs |
151
|
|
|
// |
152
|
|
|
// if $wu_detail, also return |
153
|
|
|
// njobs_in_prog: # of jobs not success or fail, |
154
|
|
|
// and at least one result in progress |
155
|
|
|
// and for each WU, set wu.status |
156
|
|
|
// |
157
|
|
|
// TODO: update est_completion_time |
|
|
|
|
158
|
|
|
// |
159
|
|
|
function get_batch_params($batch, $wus, $wu_detail) { |
160
|
|
|
if ($batch->state == BATCH_STATE_INIT) { |
161
|
|
|
// a batch in INIT state has no jobs |
162
|
|
|
// |
163
|
|
|
return $batch; |
164
|
|
|
} |
165
|
|
|
if (!$wus) { |
166
|
|
|
if ($batch->njobs) { |
167
|
|
|
$batch->update('njobs=0'); |
168
|
|
|
$batch->njobs = 0; |
169
|
|
|
} |
170
|
|
|
return $batch; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// make list of WU IDs with an in-progress result |
174
|
|
|
if ($wu_detail) { |
175
|
|
|
$res_in_prog = BoincResult::enum_fields( |
176
|
|
|
'workunitid', |
177
|
|
|
sprintf('batch=%d and server_state=%d', |
178
|
|
|
$batch->id, RESULT_SERVER_STATE_IN_PROGRESS |
179
|
|
|
) |
180
|
|
|
); |
181
|
|
|
$wus_in_prog = []; |
182
|
|
|
foreach ($res_in_prog as $res) { |
183
|
|
|
$wus_in_prog[$res->workunitid] = true; |
184
|
|
|
} |
185
|
|
|
unset($res_in_progress); // does this do anything? |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$fp_total = 0; |
189
|
|
|
$fp_done = 0; |
190
|
|
|
$completed = true; |
191
|
|
|
$batch->nerror_jobs = 0; |
192
|
|
|
$batch->credit_canonical = 0; |
193
|
|
|
$njobs_success = 0; |
194
|
|
|
$njobs_in_prog = 0; |
195
|
|
|
foreach ($wus as $wu) { |
196
|
|
|
$fp_total += $wu->rsc_fpops_est; |
197
|
|
|
if ($wu->canonical_resultid) { |
198
|
|
|
$fp_done += $wu->rsc_fpops_est; |
199
|
|
|
$njobs_success++; |
200
|
|
|
$batch->credit_canonical += $wu->canonical_credit; |
201
|
|
|
$wu->status = WU_SUCCESS; |
202
|
|
|
} else if ($wu->error_mask) { |
203
|
|
|
$batch->nerror_jobs++; |
204
|
|
|
$wu->status = WU_ERROR; |
205
|
|
|
} else { |
206
|
|
|
$completed = false; |
207
|
|
|
if ($wu_detail) { |
208
|
|
|
if (array_key_exists($wu->id, $wus_in_prog)) { |
|
|
|
|
209
|
|
|
$njobs_in_prog++; |
210
|
|
|
$wu->status = WU_IN_PROGRESS; |
211
|
|
|
} else { |
212
|
|
|
$wu->status = WU_UNSENT; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
$njobs = count($wus); |
218
|
|
|
$batch->njobs = $njobs; |
219
|
|
|
$batch->fraction_done = ($njobs_success + $batch->nerror_jobs)/$batch->njobs; |
220
|
|
|
if ($completed && $batch->state == BATCH_STATE_IN_PROGRESS) { |
221
|
|
|
$batch->state = BATCH_STATE_COMPLETE; |
222
|
|
|
$batch->completion_time = time(); |
223
|
|
|
} |
224
|
|
|
$batch->update("fraction_done = $batch->fraction_done, nerror_jobs = $batch->nerror_jobs, state=$batch->state, completion_time = $batch->completion_time, credit_canonical = $batch->credit_canonical, njobs=$njobs"); |
225
|
|
|
|
226
|
|
|
$batch->njobs_success = $njobs_success; |
227
|
|
|
if ($wu_detail) { |
228
|
|
|
$batch->njobs_in_prog = $njobs_in_prog; |
229
|
|
|
} |
230
|
|
|
return $batch; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
// get the physical names of a result's output files. |
234
|
|
|
// |
235
|
|
|
function get_outfile_phys_names($result) { |
236
|
|
|
$names = []; |
237
|
|
|
$xml = "<a>".$result->xml_doc_out."</a>"; |
238
|
|
|
$r = simplexml_load_string($xml); |
239
|
|
|
if (!$r) return $names; |
|
|
|
|
240
|
|
|
foreach ($r->file_info as $fi) { |
241
|
|
|
$names[] = (string)($fi->name); |
242
|
|
|
} |
243
|
|
|
return $names; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
function get_outfile_log_names($result) { |
247
|
|
|
$names = []; |
248
|
|
|
$xml = "<a>".$result->xml_doc_in."</a>"; |
249
|
|
|
$r = simplexml_load_string($xml); |
250
|
|
|
if (!$r) return $names; |
|
|
|
|
251
|
|
|
foreach ($r->result->file_ref as $fr) { |
252
|
|
|
$names[] = (string)($fr->open_name); |
253
|
|
|
} |
254
|
|
|
return $names; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
function get_outfile_paths($result) { |
258
|
|
|
$fanout = parse_config(get_config(), "<uldl_dir_fanout>"); |
259
|
|
|
$upload_dir = parse_config(get_config(), "<upload_dir>"); |
260
|
|
|
|
261
|
|
|
$paths = array(); |
262
|
|
|
$xml = "<a>".$result->xml_doc_out."</a>"; |
263
|
|
|
$r = simplexml_load_string($xml); |
264
|
|
|
if (!$r) return $paths; |
|
|
|
|
265
|
|
|
foreach ($r->file_info as $fi) { |
266
|
|
|
$path = dir_hier_path((string)($fi->name), $upload_dir, $fanout); |
267
|
|
|
$paths[] = $path; |
268
|
|
|
} |
269
|
|
|
return $paths; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
function abort_workunit($wu) { |
273
|
|
|
BoincResult::update_aux( |
274
|
|
|
sprintf( |
275
|
|
|
'server_state=%d, outcome=%d where server_state=%d and workunitid=%d', |
276
|
|
|
RESULT_SERVER_STATE_OVER, RESULT_OUTCOME_DIDNT_NEED, |
277
|
|
|
RESULT_SERVER_STATE_UNSENT, |
278
|
|
|
$wu->id |
279
|
|
|
) |
280
|
|
|
); |
281
|
|
|
$wu->update("error_mask=error_mask|16"); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
function abort_batch($batch) { |
285
|
|
|
$wus = BoincWorkunit::enum_fields( |
286
|
|
|
'id', |
287
|
|
|
"batch=$batch->id" |
288
|
|
|
); |
289
|
|
|
foreach ($wus as $wu) { |
290
|
|
|
abort_workunit($wu); |
291
|
|
|
} |
292
|
|
|
$batch->update( |
293
|
|
|
sprintf('state=%d', BATCH_STATE_ABORTED) |
294
|
|
|
); |
295
|
|
|
return 0; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
// delete input files, output files, WUs, and results |
299
|
|
|
// for apps that use assim copy |
300
|
|
|
// (i.e. the result files are in results/batchid) |
301
|
|
|
// |
302
|
|
|
function retire_batch($batch) { |
303
|
|
|
$wus = BoincWorkunit::enum("batch=$batch->id"); |
304
|
|
|
$bstr = "batch_$batch->id"; |
305
|
|
|
$del_files = []; |
306
|
|
|
foreach ($wus as $wu) { |
307
|
|
|
$doc = simplexml_load_string("<foo>$wu->xml_doc</foo>"); |
308
|
|
|
foreach ($doc->file_info as $fi) { |
309
|
|
|
if (strstr($fi->name, $bstr)) { |
310
|
|
|
$del_files[] = (string)$fi->name; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
BoincResult::delete_aux("workunitid=$wu->id"); |
314
|
|
|
} |
315
|
|
|
system("rm -r ../../results/$batch->id"); |
316
|
|
|
|
317
|
|
|
$fanout = parse_config(get_config(), "<uldl_dir_fanout>"); |
318
|
|
|
$download_dir = parse_config(get_config(), "<download_dir>"); |
319
|
|
|
$del_files = array_unique($del_files); |
320
|
|
|
foreach ($del_files as $f) { |
321
|
|
|
$path = dir_hier_path($f, $download_dir, $fanout); |
322
|
|
|
unlink($path); |
323
|
|
|
unlink("$path.md5"); |
324
|
|
|
} |
325
|
|
|
BoincWorkunit::delete_aux("batch=$batch->id"); |
326
|
|
|
$batch->delete(); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
function expire_batch($batch) { |
330
|
|
|
abort_batch($batch); |
331
|
|
|
retire_batch($batch); |
332
|
|
|
$batch->update("state=".BATCH_STATE_EXPIRED); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
function batch_state_string($state) { |
336
|
|
|
switch ($state) { |
337
|
|
|
case BATCH_STATE_INIT: return "new"; |
338
|
|
|
case BATCH_STATE_IN_PROGRESS: return "in progress"; |
339
|
|
|
case BATCH_STATE_COMPLETE: return "completed"; |
340
|
|
|
case BATCH_STATE_ABORTED: return "aborted"; |
341
|
|
|
case BATCH_STATE_RETIRED: return "retired"; |
342
|
|
|
} |
343
|
|
|
return "unknown state $state"; |
344
|
|
|
} |
345
|
|
|
// get the total size of output files of a batch |
346
|
|
|
// |
347
|
|
|
function batch_output_file_size($batchid) { |
348
|
|
|
$batch_td_size=0; |
349
|
|
|
$wus = BoincWorkunit::enum("batch=$batchid"); |
350
|
|
|
$fanout = parse_config(get_config(), "<uldl_dir_fanout>"); |
351
|
|
|
$upload_dir = parse_config(get_config(), "<upload_dir>"); |
352
|
|
|
foreach ($wus as $wu) { |
353
|
|
|
if (!$wu->canonical_resultid) continue; |
354
|
|
|
$result = BoincResult::lookup_id($wu->canonical_resultid); |
355
|
|
|
$names = get_outfile_phys_names($result); |
356
|
|
|
foreach ($names as $name) { |
357
|
|
|
$path = dir_hier_path($name, $upload_dir, $fanout); |
358
|
|
|
if (is_file($path)) { |
359
|
|
|
$batch_td_size += filesize($path); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
return $batch_td_size; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
function boinc_get_output_file_url($user, $result, $i) { |
367
|
|
|
$name = $result->name; |
368
|
|
|
$auth_str = md5($user->authenticator.$name); |
369
|
|
|
return "get_output.php?cmd=result_file&result_name=$name&file_num=$i&auth_str=$auth_str"; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
function boinc_get_output_files_url($user, $batch_id) { |
373
|
|
|
$auth_str = md5($user->authenticator.$batch_id); |
374
|
|
|
return "get_output.php?cmd=batch_files&batch_id=$batch_id&auth_str=$auth_str"; |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
function boinc_get_wu_output_files_url($user, $wu_id) { |
378
|
|
|
$auth_str = md5($user->authenticator.$wu_id); |
|
|
|
|
379
|
|
|
return "get_output.php?cmd=workunit_files&wu_id=$wu_id&auth_str=$auth_str"; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
////////////////// FILE INFO FILES ////////////// |
383
|
|
|
|
384
|
|
|
// these are used: |
385
|
|
|
// 1) in user file sandbox |
386
|
|
|
// 2) in BUDA app variant dirs |
387
|
|
|
// in each case a file dir/foo has an info file dir/.md5/foo |
388
|
|
|
// containing its md5 and size |
389
|
|
|
// (same format as .md5 files in download hierarchy) |
390
|
|
|
|
391
|
|
|
// get the MD5 and size of a file |
392
|
|
|
// |
393
|
|
|
function get_file_info($path) { |
394
|
|
|
$md5 = md5_file($path); |
395
|
|
|
$s = stat($path); |
396
|
|
|
$size = $s['size']; |
397
|
|
|
return [$md5, $size]; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
// write a "info file" containing MD5 and size |
401
|
|
|
// |
402
|
|
|
function write_info_file($path, $md5, $size) { |
403
|
|
|
file_put_contents($path, "$md5 $size"); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
// parse info file and return [md5, size] |
407
|
|
|
// |
408
|
|
|
function parse_info_file($path) { |
409
|
|
|
if (!file_exists($path)) return null; |
410
|
|
|
$x = file_get_contents($path); |
411
|
|
|
$n = sscanf($x, "%s %d", $md5, $size); |
|
|
|
|
412
|
|
|
if ($n != 2 || strlen($md5)!=32) { |
413
|
|
|
return null; |
414
|
|
|
} |
415
|
|
|
return [$md5, $size]; |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
///////////////// TEMPLATE CREATION ////////////// |
419
|
|
|
|
420
|
|
|
function file_ref_in($fname) { |
421
|
|
|
return(sprintf( |
422
|
|
|
' <file_ref> |
423
|
|
|
<open_name>%s</open_name> |
424
|
|
|
<copy_file/> |
425
|
|
|
</file_ref> |
426
|
|
|
', |
427
|
|
|
$fname |
428
|
|
|
)); |
429
|
|
|
} |
430
|
|
|
function file_info_out($i) { |
431
|
|
|
return sprintf( |
432
|
|
|
' <file_info> |
433
|
|
|
<name><OUTFILE_%d/></name> |
434
|
|
|
<generated_locally/> |
435
|
|
|
<upload_when_present/> |
436
|
|
|
<max_nbytes>5000000</max_nbytes> |
437
|
|
|
<url><UPLOAD_URL/></url> |
438
|
|
|
</file_info> |
439
|
|
|
', |
440
|
|
|
$i |
441
|
|
|
); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
function file_ref_out($i, $fname) { |
445
|
|
|
return sprintf( |
446
|
|
|
' <file_ref> |
447
|
|
|
<file_name><OUTFILE_%d/></file_name> |
448
|
|
|
<open_name>%s</open_name> |
449
|
|
|
<copy_file/> |
450
|
|
|
</file_ref> |
451
|
|
|
', $i, $fname |
452
|
|
|
); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
?> |
456
|
|
|
|