template_exprun.php 15.3 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2006, 2007 University of Utah and the Flux Group.
5 6 7
# All rights reserved.
#
include("defs.php3");
8
include_once("template_defs.php");
9 10 11 12 13 14 15 16

#
# No PAGEHEADER since we spit out a Location header later. See below.
#

#
# Only known and logged in users.
#
17 18 19
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
20

21 22 23
# This will not return if its a sajax request.
include("showlogfile_sup.php3");

24 25 26 27
# Used below
unset($parameter_xmlfile);
$deletexmlfile = 0;

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#
# Verify page arguments.
#
$reqargs  = RequiredPageArguments("instance",   PAGEARG_INSTANCE);
$optargs  = OptionalPageArguments("action",     PAGEARG_STRING,
				  "exprun",     PAGEARG_STRING,
				  "parameters", PAGEARG_ARRAY,
				  "formfields", PAGEARG_ARRAY);
$template = $instance->GetTemplate();

# Need these below.
$guid = $template->guid();
$vers = $template->vers();
$pid  = $template->pid();
$eid  = $instance->eid();
43 44 45
$unix_gid   = $template->UnixGID();
$exptidx    = $instance->exptidx();
$experiment = $instance->GetExperiment();
46 47 48 49 50 51

if (! $template->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
    USERERROR("You do not have permission to export in template ".
	      "$guid/$vers!", 1);
}

52 53 54 55 56
#
# Run the script backend
#
function DOIT($instance, $action, $command_options)
{
57
    global $guid, $vers, $pid, $unix_gid, $eid, $uid;
58
    global $deletexmlfile, $parameter_xmlfile;
59 60 61
    $message    = "";
    $template   = $instance->GetTemplate();
    $experiment = $instance->GetExperiment();
62 63 64 65

    $command_options = "-e $eid " . $command_options;
    
    if ($action == "start") {
66
	PAGEHEADER("Start new Run");
67 68 69
	$message = "Starting new experiment run";
	$command_options = "-a start " . $command_options;
    }
Leigh Stoller's avatar
Leigh Stoller committed
70
    elseif ($action == "abort") {
71
	PAGEHEADER("Abort Run");
Leigh Stoller's avatar
Leigh Stoller committed
72 73 74
	$message = "Aborting experiment run";
	$command_options = "-a abort " . $command_options;
    }
75 76 77 78 79
    elseif ($action == "modify") {
	PAGEHEADER("Modify Run Resources");
	$message = "Modifying resources for run";
	$command_options = "-a modify " . $command_options;
    }
80
    else {
81 82
	PAGEHEADER("Stop current Run");
	$message = "Stopping current run";
83 84 85 86 87 88 89 90
	$command_options = "-a stop " . $command_options;
    }	

    #
    # Avoid SIGPROF in child.
    #
    set_time_limit(0);

91
    echo $instance->ExpPageHeader();
92 93 94 95 96 97 98 99 100 101 102 103
    echo "<br><br>\n";

    echo "<script type='text/javascript' language='javascript' ".
	 "        src='template_sup.js'>\n";
    echo "</script>\n";

    STARTBUSY($message);

    #
    # Run the backend script.
    #
    $retval = SUEXEC($uid, "$pid,$unix_gid",
104
		     "webtemplate_exprun $command_options $guid/$vers",
105 106
		     SUEXEC_ACTION_IGNORE);

107
    HIDEBUSY();
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

    if ($deletexmlfile) {
	unlink($parameter_xmlfile);
    }

    #
    # Fatal Error. Report to the user, even though there is not much he can
    # do with the error. Also reports to tbops.
    # 
    if ($retval < 0) {
	SUEXECERROR(SUEXEC_ACTION_CONTINUE);
    }

    # User error. Tell user and exit.
    if ($retval) {
	SUEXECERROR(SUEXEC_ACTION_USERERROR);
	return;
    }

127
    STARTLOG($experiment);
128 129
}

130 131 132 133 134
#
# Run the script backend
#
function DOTIME($instance, $action)
{
135 136 137 138
    global $guid, $vers, $pid, $unix_gid, $eid, $uid;
    $message    = "";
    $template   = $instance->GetTemplate();
    $experiment = $instance->GetExperiment();
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

    if ($action == "pause") {
	PAGEHEADER("Pause Experiment Time");
	$message = "Pausing experiment runtime";
    }
    else {
	PAGEHEADER("Continue Experiment Time");
	$message = "Continuing experiment runtime";
    }	

    $command_options = "-e $eid -a $action ";
    
    #
    # Avoid SIGPROF in child.
    #
    set_time_limit(0);

156
    echo $instance->ExpPageHeader();
157 158 159 160 161 162 163 164 165 166 167 168
    echo "<br><br>\n";

    echo "<script type='text/javascript' language='javascript' ".
	 "        src='template_sup.js'>\n";
    echo "</script>\n";

    STARTBUSY($message);

    #
    # Run the backend script.
    #
    $retval = SUEXEC($uid, "$pid,$unix_gid",
169
		     "webtemplate_exprun $command_options $guid/$vers",
170 171
		     SUEXEC_ACTION_IGNORE);

172
    HIDEBUSY();
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

    #
    # Fatal Error. Report to the user, even though there is not much he can
    # do with the error. Also reports to tbops.
    # 
    if ($retval < 0) {
	SUEXECERROR(SUEXEC_ACTION_CONTINUE);
    }

    # User error. Tell user and exit.
    if ($retval) {
	SUEXECERROR(SUEXEC_ACTION_USERERROR);
	return;
    }

188
    PAGEREPLACE(CreateURL("showexp", $experiment));
189 190
}

191 192 193
#
# Spit the form out using the array of data.
#
194
function SPITFORM($action, $instance, $formfields, $parameters, $errors)
195 196
{
    global $TBDB_EIDLEN;
197
    global $TBVALIDDIRS_HTML;
198
    $template   = $instance->GetTemplate();
199

200 201 202 203 204 205
    if ($action == "modify") {
	PAGEHEADER("Modify Resources");
    }
    else {
	PAGEHEADER("Start New Run");
    }
206

207
    echo $instance->ExpPageHeader();
208
    echo "<br><br>\n";
209 210

    echo "<center>\n";
211
    $template->Show();
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
    echo "</center>\n";
    echo "<br><br>\n";

    if ($errors) {
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
              <tr>
                 <th align=center colspan=2>
                   <font size=+1 color=red>
                      &nbsp;Oops, please fix the following errors!&nbsp;
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
	    echo "<tr>
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
                  </tr>\n";
	}
	echo "</table><br>\n";
    }

237 238 239 240 241 242
    #
    # Get the default params for the template, instance and the previous run.
    # These go out for the user to select via a button, which will provide an
    # initial setting for the parameters.
    #
    $template->FormalParameters($formal_parameters);
243
    $template->FormalParameterMouseOvers($mouseovers);
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
    $instance->Bindings($instance_parameters);
    $instance->RunBindings($instance->LastRunIdx(), $lastrun_parameters);

    #
    # Spit out some inline script. Its not as if there will be hundreds
    # of parameters, right?
    #
    echo "<script type='text/javascript' language='javascript' ".
	 "        src='template_sup.js'>\n";
    echo "</script>\n";
    echo "<script language=JavaScript>\n";
    echo "var formal_names  = new Array();\n";
    echo "var template_values = new Array();\n";
    echo "var instance_values = new Array();\n";
    echo "var lastrun_values  = new Array();\n";
    $i = 0;
    while (list ($name, $value) = each ($formal_parameters)) {
	echo "formal_names[$i] = '$name';\n";
	echo "template_values[$i] = '$value';\n";
	$i++;
    }
    $i = 0;
    while (list ($name, $value) = each ($instance_parameters)) {
	echo "instance_values[$i] = '$value';\n";
	$i++;
    }
    $i = 0;
    while (list ($name, $value) = each ($lastrun_parameters)) {
	echo "lastrun_values[$i] = '$value';\n";
	$i++;
    }
    echo "</script>\n";

277 278
    $url = CreateURL("template_exprun", $instance);

279
    echo "<form action='${url}&action=$action' method=post>\n";
280
    echo "<table align=center border=1>\n";
281
    
282 283 284
    #
    # RunID:
    #
285 286 287 288 289 290 291 292
    if ($action == "start" ||
	($action == "modify" && !$instance->runidx())) {
	
	echo "<tr>
                  <td class='pad4'>ID:
                  <br><font size='-1'>(alphanumeric, no blanks)</font></td>
                  <td class='pad4' class=left>";
	echo "    <input type=text
293
                         name=\"formfields[runid]\"
294
                         value=\"" . $formfields["runid"] . "\"
295
	                 size=$TBDB_EIDLEN
296 297 298 299
                         maxlength=$TBDB_EIDLEN>\n";
	echo "    </td>
              </tr>\n";
    }
300

301 302 303 304 305 306 307 308
    #
    # Clean logs before starting run?
    #
    echo "<tr>
	      <td class='pad4'>Clean Logs:</td>
              <td class='pad4' class=left>
  	          <input type=checkbox name='formfields[clean]' value='Yep'";

309 310
    if (isset($formfields["clean"]) &&
	strcmp($formfields["clean"], "Yep") == 0) {
311 312 313 314 315 316 317
	echo " checked='1'";
    }
    echo ">";
    echo "&nbsp (run '<tt>loghole clean</tt>' before starting run)
	    </td>
	  </tr>\n";

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    #
    # Swapmod?
    #
    echo "<tr>
	      <td class='pad4'>Reparse NS file?:</td>
              <td class='pad4' class=left>
  	          <input type=checkbox name='formfields[swapmod]' value='Yep'";

    if (isset($formfields["swapmod"]) &&
	strcmp($formfields["swapmod"], "Yep") == 0) {
	echo " checked='1'";
    }
    echo ">";
    echo "&nbsp (effectively a '<tt>swap modify</tt>')
	    </td>
	  </tr>\n";

335 336
    echo "<tr>
              <td colspan=2>
337
               Use this text area for an (optional) description:
338 339 340 341 342
              </td>
          </tr>
          <tr>
              <td colspan=2 align=center class=left>
                  <textarea name=\"formfields[description]\"
343
                    rows=4 cols=80>" .
344
	            ereg_replace("\r", "", $formfields["description"]) .
345 346 347 348 349 350 351 352 353
	           "</textarea>
              </td>
          </tr>\n";

    if (count($parameters)) {
        #
	# Table of inputs.
	#
	echo "<tr>
354
		  <td class='pad4'>Formal Parameters:</td>
355 356
		  <td>
 		    <table cellpadding=0 cellspacing=0 border=0>\n";
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372

	echo "<tr><td>Choose Values:</td><td>";
	echo "<table cellpadding=0 cellspacing=0 border=0>\n";
	echo "<tr><td>\n";
	echo " <button name=formals type=button value=Formals ";
	echo "  onclick=\"SetRunParams(formal_names, template_values);\">";
	echo "Template</button>\n";
	echo "</td><td>\n";
	echo " <button name=instance type=button value=Instance ";
	echo "  onclick=\"SetRunParams(formal_names, instance_values);\">";
	echo "Instance</button>\n";
	echo "</td><td>\n";
	echo " <button name=lastrun type=button value='Previous Run' ";
	echo "  onclick=\"SetRunParams(formal_names, lastrun_values);\">";
	echo "Previous Run</button>\n";
	echo "</tr></table>\n";
373 374 375 376
	
	while (list ($name, $value) = each ($parameters)) {
	    if (!isset($value))
		$value = "&nbsp";
377
	    $mouseover = (isset($mouseovers[$name]) ? $mouseovers[$name] : "");
378 379

	    echo "<tr>
380
                    <td class='pad4' $mouseover>$name</td>
381 382
                    <td class='pad4' class=left>
                        <input type=text
383
                               id='parameter_$name'
384
                               name=\"parameters[$name]\"
Leigh Stoller's avatar
Leigh Stoller committed
385
                               value=\"" . $value . "\"
386 387 388 389 390 391 392 393 394
	                       size=60
                               maxlength=1024>
                    </td>
                  </tr>\n";
	}
	echo "<tr><td>&nbsp;&nbsp;<b>or</b> XML file:
               </td><td></td></tr>\n";
	echo "<tr>
                  <td class='pad4'>On Server<br>
395
                           <font size='-1'>($TBVALIDDIRS_HTML)</font></td>
396 397 398
                  <td class='pad4'>
	              <input type=text
                             name=\"formfields[parameter_xmlfile]\"
399
                             value=\"" . $formfields["parameter_xmlfile"] . "\"
400 401 402 403 404 405 406 407 408 409 410 411 412 413
	                     size=60>\n";
	echo "</td></tr>\n";
	echo "</table></td></tr>";
    }

    echo "<tr>
              <td class='pad4' align=center colspan=2>
                <b><input type=submit name=exprun value='Start'></b>
              </td>
         </tr>
        </form>
        </table>\n";
}

Leigh Stoller's avatar
Leigh Stoller committed
414
if (isset($action) && ($action == "stop" || $action == "abort")) {
415 416 417 418 419
    # Run the backend script.
    DOIT($instance, $action, "");
    PAGEFOOTER();
    return;
}
420 421 422 423 424 425
elseif (isset($action) && ($action == "pause" || $action == "continue")) {
    # Run the backend script.
    DOTIME($instance, $action, "");
    PAGEFOOTER();
    return;
}
426
elseif (! isset($formfields)) {
427 428
    $defaults = array();
    
429
    #
430
    # On first load, display virgin form and exit.
431
    #
432 433
    $defaults['runid']   = $instance->NextRunID();
    $defaults['clean']   = "";
434
    $defaults['swapmod'] = "";
435 436
    $defaults['description'] = "";
    $defaults['parameter_xmlfile'] = "";
437 438

    #
Leigh Stoller's avatar
Leigh Stoller committed
439
    # Get the current bindings of the current run.
440
    #
Leigh Stoller's avatar
Leigh Stoller committed
441
    $instance->RunBindings($instance->LastRunIdx(), $bindings);
442 443 444 445 446 447 448 449 450 451
    
    #
    # Allow formfields that are already set to override defaults
    #
    if (isset($formfields)) {
	while (list ($field, $value) = each ($formfields)) {
	    $defaults[$field] = $formfields[$field];
	}
    }

452
    SPITFORM($action, $instance, $defaults, $bindings, 0);
453 454 455 456 457 458 459 460
    PAGEFOOTER();
    return;
}

#
# Okay, validate form arguments.
#
$errors = array();
461 462

# Set up command options
463
$command_options = " ";
464 465 466 467

#
# RunID:
#
468 469 470 471 472 473 474 475 476 477 478 479 480 481
if ($action == "start" ||
    ($action == "modify" && !$instance->runidx())) {
    if (!isset($formfields["runid"]) || $formfields["runid"] == "") {
	$errors["ID"] = "Missing Field";
    }
    elseif (!TBvalid_eid($formfields["runid"])) {
	$errors["ID"] = TBFieldErrorString();
    }
    elseif (! $instance->UniqueRunID($formfields["runid"])) {
	$errors["ID"] = "Already in use";
    }
    else {
	$command_options .= " -r " . escapeshellarg($formfields["runid"]);
    }
482 483
}

484 485 486
#
# Clean?
#
487
if (isset($formfields["clean"]) && $formfields["clean"] == "Yep") {
488 489 490
    $command_options .= " -c";
}

491 492 493 494 495 496 497
#
# Swapmod?
#
if (isset($formfields["swapmod"]) && $formfields["swapmod"] == "Yep") {
    $command_options .= " -m";
}

498 499 500
#
# Description:
# 
501 502
if (isset($formfields["description"]) && $formfields["description"] != "") {
    if (!TBvalid_template_description($formfields["description"])) {
503 504 505
	$errors["Description"] = TBFieldErrorString();
    }
    else {
506
	$command_options .= " -E ". escapeshellarg($formfields["description"]);
507
    }
508 509 510 511 512
}

#
# Parameters. The XML file overrides stuff in the form. 
#
513
$template->FormalParameters($parameter_masterlist);
514
if (count($parameter_masterlist)) {
515 516
    if (isset($formfields["parameter_xmlfile"]) &&
	$formfields["parameter_xmlfile"] != "") {
517

518
	$parameter_xmlfile = $formfields["parameter_xmlfile"];
519 520 521 522 523
	    
	if (!preg_match("/^([-\@\w\.\/]+)$/", $parameter_xmlfile)) {
	    $errors["Parameter XML File"] =
		"Pathname includes illegal characters";
	}
524 525 526
	elseif (! VALIDUSERPATH($parameter_xmlfile)) {
	    $errors["Parameter XML File"] =
		"Must reside in one of: $TBVALIDDIRS";
527 528 529 530 531
	}
    	$deletexmlfile = 0;
    }
    else {
	#
532
	# Lets confirm that the user did not forget to set at least one value. 
533
	#
534
	$gotone = 0;
535
	while (list ($name, $default_value) = each ($parameter_masterlist)) {
536 537
	    if (isset($parameters[$name]) && $parameters[$name] != "") {
		$gotone = 1;
538
	    }
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	}
	if (! $gotone) {
	    $errors["Parameters"] = "You did not set any values";
	}
	else {
	    #
	    # Generate a temporary file and write in the XML goo.
	    #
	    list($usec, $sec) = explode(' ', microtime());
	    srand((float) $sec + ((float) $usec * 100000));
	    $foo = rand();

	    $parameter_xmlfile = "/tmp/$uid-$foo.xml";
	    $deletexmlfile = 1;
	    
	    if (! ($fp = fopen($parameter_xmlfile, "w"))) {
		TBERROR("Could not create temp file $parameter_xmlfile", 1);
556
	    }
557 558 559
	
	    fwrite($fp, "<template_parameters>\n");

560
	    reset($parameter_masterlist);
561 562 563 564 565 566 567
	    while (list($name,$default_value) = each ($parameter_masterlist)) {
		if (isset($parameters[$name])) {
		    $value = $parameters[$name];
		}
		else {
		    $value = $default_value;
		}
568
	    
569 570 571 572 573 574
		fwrite($fp, " <parameter name=\"$name\">");
		fwrite($fp, "<value>$value</value></parameter>\n");
	    }
	    fwrite($fp, "</template_parameters>\n");
	    fclose($fp);
	    chmod($parameter_xmlfile, 0666);
575 576
	}
    }
577
    $command_options .= " -x $parameter_xmlfile";
578 579 580
}

if (count($errors)) {
581
    SPITFORM($action, $instance, $formfields, $parameters, $errors);
582 583 584 585 586
    PAGEFOOTER();
    exit(1);
}

# Run the backend script.
587
DOIT($instance, $action, $command_options);
588

589 590 591 592 593
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>