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 submissions and control |
21
|
|
|
|
22
|
|
|
require_once("../inc/submit_db.inc"); |
23
|
|
|
|
24
|
|
|
// write status and error messages to log |
25
|
|
|
// |
26
|
|
|
function log_write($x) { |
27
|
|
|
static $enabled, $log_file; |
28
|
|
|
|
29
|
|
|
if (!isset($enabled)) { |
30
|
|
|
$enabled = false; |
31
|
|
|
$filename = parse_config(get_config(), "<remote_submit_log>"); |
32
|
|
|
if (!$filename) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
$log_dir = parse_config(get_config(), "<log_dir>"); |
36
|
|
|
if (!$log_dir) { |
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
$log_file = fopen("$log_dir/$filename", "a"); |
40
|
|
|
if (!$log_file) return; |
|
|
|
|
41
|
|
|
$enabled = true; |
42
|
|
|
} |
43
|
|
|
if (!$enabled) return; |
44
|
|
|
fwrite($log_file, sprintf("%s: %s\n", strftime("%c"), $x)); |
45
|
|
|
fflush($log_file); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// in remote job submission, |
49
|
|
|
// for input files of type local, semilocal, and inline, |
50
|
|
|
// we need to give a unique physical name based on its content. |
51
|
|
|
// Prepend the jf_ to make the origin of the file clear |
52
|
|
|
// |
53
|
|
|
function job_file_name($md5) { |
54
|
|
|
return "jf_$md5"; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// does user have submit permissions? |
58
|
|
|
// |
59
|
|
|
function submit_permissions($user) { |
60
|
|
|
return BoincUserSubmit::lookup_userid($user->id); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// does user have submit permissions for given app? |
64
|
|
|
// |
65
|
|
|
function submit_permissions_app($user, $app) { |
66
|
|
|
return BoincUserSubmitApp::lookup("user_id=$user->id and app_id=$app->id"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// check whether user has permissions for a remote job submission |
70
|
|
|
// or job file request. |
71
|
|
|
// $r is a request message that includes an 'authenticator' field |
72
|
|
|
// $app is the app being submitted to (or null if file op) |
73
|
|
|
// returns [user, UserSubmit], or give XML error |
74
|
|
|
// |
75
|
|
|
function check_remote_submit_permissions($r, $app) { |
76
|
|
|
$auth = (string)$r->authenticator; |
77
|
|
|
if (!$auth) { |
78
|
|
|
log_write("no authenticator"); |
79
|
|
|
xml_error(-1, "no authenticator"); |
80
|
|
|
} |
81
|
|
|
$auth = BoincDb::escape_string($auth); |
82
|
|
|
$user = BoincUser::lookup("authenticator='$auth'"); |
83
|
|
|
if (!$user) { |
84
|
|
|
log_write("bad authenticator"); |
85
|
|
|
xml_error(-1, "bad authenticator"); |
86
|
|
|
} |
87
|
|
|
$user_submit = submit_permissions($user); |
88
|
|
|
if (!$user_submit) { |
89
|
|
|
log_write("no submit access"); |
90
|
|
|
xml_error(-1, "no submit access"); |
91
|
|
|
} |
92
|
|
|
if ($app && !$user_submit->submit_all) { |
93
|
|
|
$usa = submit_permissions_app($user, $app); |
94
|
|
|
if (!$usa) { |
95
|
|
|
log_write("no app submit access"); |
96
|
|
|
xml_error(-1, "no app submit access"); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return array($user, $user_submit); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
function delete_remote_submit_user($user) { |
103
|
|
|
BoincUserSubmit::delete_user($user->id); |
104
|
|
|
BoincUserSubmitApp::delete_user($user->id); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
// given its WUs, compute progress of a batch |
109
|
|
|
// (fraction done, est completion time etc.) |
110
|
|
|
// NOTE: this is inefficient because we need all the WUs. |
111
|
|
|
// it could be done by server components |
112
|
|
|
// (transitioner, validator etc.) as jobs complete or time out |
113
|
|
|
// |
114
|
|
|
// TODO: update est_completion_time |
|
|
|
|
115
|
|
|
// |
116
|
|
|
function get_batch_params($batch, $wus) { |
117
|
|
|
if ($batch->state == BATCH_STATE_INIT) { |
118
|
|
|
// a batch in INIT state has no jobs |
119
|
|
|
// |
120
|
|
|
return $batch; |
121
|
|
|
} |
122
|
|
|
$fp_total = 0; |
123
|
|
|
$fp_done = 0; |
124
|
|
|
$completed = true; |
125
|
|
|
$batch->nerror_jobs = 0; |
126
|
|
|
$batch->credit_canonical = 0; |
127
|
|
|
foreach ($wus as $wu) { |
128
|
|
|
$fp_total += $wu->rsc_fpops_est; |
129
|
|
|
if ($wu->canonical_resultid) { |
130
|
|
|
$fp_done += $wu->rsc_fpops_est; |
131
|
|
|
$batch->credit_canonical += $wu->canonical_credit; |
132
|
|
|
} else if ($wu->error_mask) { |
133
|
|
|
$batch->nerror_jobs++; |
134
|
|
|
} else { |
135
|
|
|
$completed = false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
if ($fp_total) { |
139
|
|
|
$batch->fraction_done = $fp_done / $fp_total; |
140
|
|
|
} |
141
|
|
|
if ($completed && $batch->state == BATCH_STATE_IN_PROGRESS) { |
142
|
|
|
$batch->state = BATCH_STATE_COMPLETE; |
143
|
|
|
$batch->completion_time = time(); |
144
|
|
|
} |
145
|
|
|
$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"); |
146
|
|
|
|
147
|
|
|
$batch->credit_estimate = flops_to_credit($fp_total); |
148
|
|
|
return $batch; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
function get_outfile_names($result) { |
152
|
|
|
$names = array(); |
153
|
|
|
$xml = "<a>".$result->xml_doc_out."</a>"; |
154
|
|
|
$r = simplexml_load_string($xml); |
155
|
|
|
if (!$r) return $names; |
|
|
|
|
156
|
|
|
foreach ($r->file_info as $fi) { |
157
|
|
|
$names[] = (string)($fi->name); |
158
|
|
|
} |
159
|
|
|
return $names; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
function get_outfile_paths($result) { |
163
|
|
|
$fanout = parse_config(get_config(), "<uldl_dir_fanout>"); |
164
|
|
|
$upload_dir = parse_config(get_config(), "<upload_dir>"); |
165
|
|
|
|
166
|
|
|
$paths = array(); |
167
|
|
|
$xml = "<a>".$result->xml_doc_out."</a>"; |
168
|
|
|
$r = simplexml_load_string($xml); |
169
|
|
|
if (!$r) return $paths; |
|
|
|
|
170
|
|
|
foreach ($r->file_info as $fi) { |
171
|
|
|
$path = dir_hier_path((string)($fi->name), $upload_dir, $fanout); |
172
|
|
|
$paths[] = $path; |
173
|
|
|
} |
174
|
|
|
return $paths; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
function abort_workunit($wu) { |
178
|
|
|
BoincResult::update_aux( |
179
|
|
|
"server_state=5, outcome=5 where server_state=2 and workunitid=$wu->id" |
180
|
|
|
); |
181
|
|
|
$wu->update("error_mask=error_mask|16"); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
function abort_batch($batch) { |
185
|
|
|
$wus = BoincWorkunit::enum("batch=$batch->id"); |
186
|
|
|
foreach ($wus as $wu) { |
187
|
|
|
abort_workunit($wu); |
188
|
|
|
} |
189
|
|
|
$batch->update("state=".BATCH_STATE_ABORTED); |
190
|
|
|
return 0; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// mark WUs as assimilated; this lets them be purged |
194
|
|
|
// |
195
|
|
|
function retire_batch($batch) { |
196
|
|
|
$wus = BoincWorkunit::enum("batch=$batch->id"); |
197
|
|
|
$now = time(); |
198
|
|
|
foreach ($wus as $wu) { |
199
|
|
|
$wu->update( |
200
|
|
|
"assimilate_state=".ASSIMILATE_DONE.", transition_time=$now" |
201
|
|
|
); |
202
|
|
|
// remove output template if it's a temporary |
203
|
|
|
// |
204
|
|
|
if (strstr($wu->result_template_file, "templates/tmp/")) { |
205
|
|
|
@unlink($wu->result_template_file); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
$batch->update("state=".BATCH_STATE_RETIRED); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
function expire_batch($batch) { |
212
|
|
|
abort_batch($batch); |
213
|
|
|
retire_batch($batch); |
214
|
|
|
$batch->update("state=".BATCH_STATE_EXPIRED); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
function batch_state_string($state) { |
218
|
|
|
switch ($state) { |
219
|
|
|
case BATCH_STATE_INIT: return "new"; |
220
|
|
|
case BATCH_STATE_IN_PROGRESS: return "in progress"; |
221
|
|
|
case BATCH_STATE_COMPLETE: return "completed"; |
222
|
|
|
case BATCH_STATE_ABORTED: return "aborted"; |
223
|
|
|
case BATCH_STATE_RETIRED: return "retired"; |
224
|
|
|
} |
225
|
|
|
return "unknown state $state"; |
226
|
|
|
} |
227
|
|
|
// get the total size of output files of a batch |
228
|
|
|
// |
229
|
|
|
function batch_output_file_size($batchid) { |
230
|
|
|
$batch_td_size=0; |
231
|
|
|
$wus = BoincWorkunit::enum("batch=$batchid"); |
232
|
|
|
$fanout = parse_config(get_config(), "<uldl_dir_fanout>"); |
233
|
|
|
$upload_dir = parse_config(get_config(), "<upload_dir>"); |
234
|
|
|
foreach ($wus as $wu) { |
235
|
|
|
if (!$wu->canonical_resultid) continue; |
236
|
|
|
$result = BoincResult::lookup_id($wu->canonical_resultid); |
237
|
|
|
$names = get_outfile_names($result); |
238
|
|
|
foreach ($names as $name) { |
239
|
|
|
$path = dir_hier_path($name, $upload_dir, $fanout); |
240
|
|
|
if (is_file($path)) { |
241
|
|
|
$s=stat($path); |
242
|
|
|
$size=$s['size']; |
243
|
|
|
$batch_td_size+=$size; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
return $batch_td_size; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
function boinc_get_output_file_url($user, $result, $i) { |
251
|
|
|
$name = $result->name; |
252
|
|
|
$auth_str = md5($user->authenticator.$name); |
253
|
|
|
return "get_output.php?cmd=result_file&result_name=$name&file_num=$i&auth_str=$auth_str"; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
function boinc_get_output_files_url($user, $batch_id) { |
257
|
|
|
$auth_str = md5($user->authenticator.$batch_id); |
258
|
|
|
return "get_output.php?cmd=batch_files&batch_id=$batch_id&auth_str=$auth_str"; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
function boinc_get_wu_output_files_url($user, $wu_id) { |
262
|
|
|
$auth_str = md5($user->authenticator.$wu_id); |
|
|
|
|
263
|
|
|
return "get_output.php?cmd=workunit_files&wu_id=$wu_id&auth_str=$auth_str"; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
?> |
267
|
|
|
|