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 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
<?php
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2011 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
// example of a web interface to remote job submission
//
// Although the architecture is intended to support remote portals,
// this example must run on the BOINC project server.
// In particular:
// - It assumes cookie-based authentication,
// as is done by the BOINC code.
// You'll need to adapt/extend this considerably to run this
// on a server other than the BOINC project server.
// - It uses some functions from BOINC (page_head() etc.).
// When you adapt this to your own purposes,
// you can strip out this stuff if the web site doesn't use BOINC
require_once("../inc/submit.inc");
require_once("../inc/common_defs.inc");
require_once("../inc/submit_db.inc");
require_once("../inc/submit_util.inc");
// needed for access control stuff
require_once("../inc/util.inc");
require_once("../project/project.inc");
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
// hardwired app name for now
define('APP_NAME', 'remote_test');
$project = $master_url; // from project.inc
$user = get_logged_in_user();
$auth = $user->authenticator;
// the job submission "home page":
// show the user's in-progress and completed batches,
// and a button for creating a new batch
//
function handle_main() {
global $project, $auth;
$req = new StdClass;
$req->project = $project;
$req->authenticator = $auth;
list($batches, $errmsg) = boinc_query_batches($req);
if ($errmsg) error_page(htmlentities($errmsg));
page_head("Job submission and control");
echo "
This is an example of a web interface
for remote submission of BOINC jobs.
It lets you
<ul>
<li> Upload files
<li> submit batches of jobs,
<li> see the status of in-progress and completed batches.
</ul>
<p>
To use this, you must be logged in as a user
with permission to submit jobs.
<p>
";
show_button("submit_example.php?action=create_form", "Create new batch");
$first = true;
foreach ($batches as $batch) {
if ($batch->state != BATCH_STATE_IN_PROGRESS) continue;
if ($first) {
$first = false;
echo "<h2>Batches in progress</h2>\n";
start_table();
table_header("name", "ID", "app", "# jobs", "progress", "submitted");
}
$pct_done = (int)($batch->fraction_done*100);
table_row(
"<a href=submit_example.php?action=query_batch&batch_id=$batch->id>$batch->name</a>",
"<a href=submit_example.php?action=query_batch&batch_id=$batch->id>$batch->id</a>",
$batch->app_name,
$batch->njobs,
"$pct_done%",
local_time_str($batch->create_time)
);
}
if ($first) {
echo "<p>You have no in-progress batches.\n";
} else {
end_table();
}
$first = true;
foreach ($batches as $batch) {
if ($batch->state != BATCH_STATE_COMPLETE) continue;
if ($first) {
$first = false;
echo "<h2>Completed batches</h2>\n";
start_table();
table_header("name", "ID", "# jobs", "submitted");
}
table_row(
"<a href=submit_example.php?action=query_batch&batch_id=$batch->id>$batch->name</a>",
"<a href=submit_example.php?action=query_batch&batch_id=$batch->id>$batch->id</a>",
$batch->njobs,
local_time_str($batch->create_time)
);
}
if ($first) {
echo "<p>You have no completed batches.\n";
} else {
end_table();
}
$first = true;
foreach ($batches as $batch) {
if ($batch->state != BATCH_STATE_ABORTED) continue;
if ($first) {
$first = false;
echo "<h2>Aborted batches</h2>\n";
start_table();
table_header("name", "ID", "# jobs", "submitted");
}
table_row(
"<a href=submit_example.php?action=query_batch&batch_id=$batch->id>$batch->name</a>",
"<a href=submit_example.php?action=query_batch&batch_id=$batch->id>$batch->id</a>",
$batch->njobs,
local_time_str($batch->create_time)
);
}
if (!$first) {
end_table();
}
echo "<p><a href=submit_example.php>Return to job control page</a>\n";
page_tail();
}
// return an array of the apps this user is allowed to submit jobs for
//
function eligible_apps() {
global $user;
$apps = BoincApp::enum("deprecated = 0");
$user_submit = BoincUserSubmit::lookup_userid($user->id);
if (!$user_submit) return null;
$a = array();
foreach($apps as $app) {
if ($user_submit->submit_all) {
$a[] = $app;
} else {
if (BoincUserSubmitApp::lookup("user_id=$user->id and app_id=$app->id")) {
$a[] = $app;
}
}
}
return $a;
}
// return HTML for a popup menu of apps
//
function app_select($apps) {
$x = '<select class="form-control" name="app_name">\n';
foreach ($apps as $app) {
$x .= "<option value=$app->name>$app->user_friendly_name</option>\n";
}
$x .= "</select>\n";
return $x;
}
// show a form for creating a batch.
//
function handle_create_form() {
global $project, $auth;
$apps = eligible_apps();
if (!$apps) error_page("You are not allowed to submit jobs");
page_head("Create batch");
echo "
This form lets you specify a batch of jobs,
and either submit it or get and estimate of its completion time.
<p>
The job runs an application that
<ul>
<li> Reads an input file, converts it to upper case,
and writes this to an output file.
You give a URL from which this file can be read.
<li> Takes a command-line parameter, and uses that
number of seconds of CPU time
You can specify a range of values for the parameter;
this determines the number of jobs in the batch.
</ul>
<p>
<form action=submit_example.php>
<input type=hidden name=action value=create_action>
";
start_table();
row2("Batch name", "<input name=batch_name value=\"enter name\">");
// row2("Application", app_select($apps));
row2("Input file URL", "<input name=input_url size=60 value=\"https://google.com/\">");
row2("Parameter low value (0..60)", "<input name=param_lo value=10>");
row2("Parameter high value (0..60)", "<input name=param_hi value=20>");
row2("Parameter increment (>= 1)", "<input name=param_inc value=1>");
row2("",
"<input class=\"btn btn-default\" type=submit name=get_estimate value=\"Get completion time estimate\">"
);
row2("",
"<input class=\"btn btn-primary\" type=submit name=submit value=Submit>"
);
end_table();
echo "</form>\n";
echo "<p><a href=submit_example.php>Return to job control page</a>\n";
page_tail();
}
// build a request object for boinc_*_batch() from form variables
//
function form_to_request() {
global $project, $auth;
$input_url = get_str('input_url');
if (!$input_url) error_page("missing input URL");
$param_lo = (double)get_str('param_lo');
if ($param_lo<0 || $param_lo>60) error_page("param lo must be in 0..60");
$param_hi = (double)get_str('param_hi');
if ($param_hi<0 || $param_hi>60 || $param_hi <= $param_lo) {
error_page("param hi must be in 0..60 and > param lo");
}
$param_inc = (double)get_str('param_inc');
if ($param_inc < 1) error_page("param inc must be >= 1");
$req = new StdClass;
$req->project = $project;
$req->authenticator = $auth;
$req->app_name = APP_NAME;
$req->batch_name = get_str('batch_name');
$req->jobs = array();
$f = new StdClass;
$f->source = $input_url;
$f->mode = 'semilocal';
for ($x=$param_lo; $x<$param_hi; $x += $param_inc) {
$job = new StdClass;
$job->rsc_fpops_est = $x*1e9;
$job->command_line = "--t $x";
$job->input_files = array($f);
$req->jobs[] = $job;
}
return $req;
}
// create and submit a batch
//
function handle_create_action() {
global $project, $auth;
$get_estimate = get_str('get_estimate', true);
if ($get_estimate) {
$req = form_to_request($project, $auth);
list($e, $errmsg) = boinc_estimate_batch($req);
if ($errmsg) error_page(htmlentities($errmsg));
page_head("Batch estimate");
echo sprintf("Estimate: %.0f seconds", $e);
echo "<p><a href=submit_example.php>Return to job control page</a>\n";
page_tail();
} else {
$req = form_to_request($project, $auth);
list($id, $errmsg) = boinc_submit_batch($req);
if ($errmsg) error_page(htmlentities($errmsg));
page_head("Batch submitted");
echo "Batch created, ID: $id\n";
echo "<p><a href=submit_example.php>Return to job control page</a>\n";
page_tail();
}
}
// show the details of an existing batch
//
function handle_query_batch() {
global $project, $auth;
$req = (object)array(
'project' => $project,
'authenticator' => $auth,
'batch_id' => get_int('batch_id'),
);
list($batch, $errmsg) = boinc_query_batch($req);
if ($errmsg) error_page(htmlentities($errmsg));
page_head("Batch $req->batch_id");
start_table();
row2("name", $batch->name);
row2("application", $batch->app_name);
row2("state", batch_state_string($batch->state));
row2("# jobs", $batch->njobs);
row2("# error jobs", $batch->nerror_jobs);
row2("progress", sprintf("%.0f%%", $batch->fraction_done*100));
if ($batch->completion_time) {
row2("completed", local_time_str($batch->completion_time));
}
row2("GFLOP/hours, estimated", number_format(credit_to_gflop_hours($batch->credit_estimate), 2));
row2("GFLOP/hours, actual", number_format(credit_to_gflop_hours($batch->credit_canonical), 2));
end_table();
$url = boinc_get_output_files($req);
show_button($url, "Get zipped output files");
switch ($batch->state) {
case BATCH_STATE_IN_PROGRESS:
echo "<br>";
show_button(
"submit_example.php?action=abort_batch_confirm&batch_id=$req->batch_id",
"Abort batch"
);
break;
case BATCH_STATE_COMPLETE:
case BATCH_STATE_ABORTED:
echo "<br>";
show_button(
"submit_example.php?action=retire_batch_confirm&batch_id=$req->batch_id",
"Retire batch"
);
break;
}
echo "<h2>Jobs</h2>\n";
start_table();
table_header(
"Job ID<br><p class=\"text-muted\">click for details or to get output files</p>",
"status",
"Canonical instance<br><p class=\"text-muted\">click to see result page on BOINC server</p>"
);
foreach($batch->jobs as $job) {
$id = (int)$job->id;
$resultid = (int)$job->canonical_instance_id;
if ($resultid) {
$x = "<a href=result.php?resultid=$resultid>$resultid</a>";
$y = "completed";
} else {
$x = "---";
$y = "in progress";
}
echo "<tr>
<td><a href=submit_example.php?action=query_job&job_id=$id>$id</a></td>
<td>$y</td>
<td>$x</td>
</tr>
";
}
end_table();
echo "<p><a href=submit_example.php>Return to job control page</a>\n";
page_tail();
}
// show the details of a job, including links to see the output files
//
function handle_query_job() {
global $project, $auth;
$req = new StdClass;
$req->project = $project;
$req->authenticator = $auth;
$req->job_id = get_int('job_id');
list($reply, $errmsg) = boinc_query_job($req);
if ($errmsg) error_page(htmlentities($errmsg));
page_head("Job $req->job_id");
echo "<a href=$project/workunit.php?wuid=$req->job_id>View workunit page on BOINC server</a>\n";
echo "<h2>Instances</h2>\n";
start_table();
table_header(
"Instance ID<br><p class=\"text-muted\">click for result page on BOINC server</p>",
"State", "Output files"
);
foreach($reply->instances as $inst) {
echo "<tr>
<td><a href=result.php?resultid=$inst->id>$inst->id</a></td>
<td>$inst->state</td>
<td>
";
$i = 0;
foreach ($inst->outfiles as $outfile) {
$req->instance_name = $inst->name;
$req->file_num = $i;
$url = boinc_get_output_file($req);
echo "<a href=$url>$outfile->size bytes</a>";
$i++;
}
echo "</td></tr>\n";
}
end_table();
echo "<p><a href=submit_example.php>Return to job control page</a>\n";
page_tail();
}
function handle_abort_batch_confirm() {
$batch_id = get_int('batch_id');
page_head("Confirm abort batch");
echo "
Aborting a batch will cancel all unstarted jobs.
Are you sure you want to do this?
<p>
";
show_button(
"submit_example.php?action=abort_batch&batch_id=$batch_id",
"Yes - abort batch"
);
echo "<p><a href=submit_example.php>Return to job control page</a>\n";
page_tail();
}
function handle_abort_batch() {
global $project, $auth;
$req->project = $project;
$req->authenticator = $auth;
$req->batch_id = get_int('batch_id');
$errmsg = boinc_abort_batch($req);
if ($errmsg) error_page(htmlentities($errmsg));
page_head("Batch aborted");
echo "<p><a href=submit_example.php>Return to job control page</a>\n";
page_tail();
}
function handle_retire_batch_confirm() {
$batch_id = get_int('batch_id');
page_head("Confirm retire batch");
echo "
Retiring a batch will remove all of its output files.
Are you sure you want to do this?
<p>
";
show_button(
"submit_example.php?action=retire_batch&batch_id=$batch_id",
"Yes - retire batch"
);
echo "<p><a href=submit_example.php>Return to job control page</a>\n";
page_tail();
}
function handle_retire_batch() {
global $project, $auth;
$req->project = $project;
$req->authenticator = $auth;
$req->batch_id = get_int('batch_id');
$errmsg = boinc_retire_batch($req);
if ($errmsg) error_page(htmlentities($errmsg));
page_head("Batch retired");
echo "<p><a href=submit_example.php>Return to job control page</a>\n";
page_tail();
}
$action = get_str('action', true);
switch ($action) {
case '': handle_main(); break;
case 'abort_batch': handle_abort_batch(); break;
case 'abort_batch_confirm': handle_abort_batch_confirm(); break;
case 'create_action': handle_create_action(); break;
case 'create_form': handle_create_form(); break;
case 'query_batch': handle_query_batch(); break;
case 'query_job': handle_query_job(); break;
case 'retire_batch': handle_retire_batch(); break;
case 'retire_batch_confirm': handle_retire_batch_confirm(); break;
default:
error_page('no such action');
}
?>
|