template_exprun.php 16.1 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2012 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file 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 Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24
#
include("defs.php3");
25
include_once("template_defs.php");
26 27 28 29 30 31 32 33

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

#
# Only known and logged in users.
#
34 35 36
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
37

38 39 40
# This will not return if its a sajax request.
include("showlogfile_sup.php3");

41 42 43 44
# Used below
unset($parameter_xmlfile);
$deletexmlfile = 0;

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#
# 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();
60 61 62
$unix_gid   = $template->UnixGID();
$exptidx    = $instance->exptidx();
$experiment = $instance->GetExperiment();
63 64
$project  = $template->GetProject();
$unix_pid = $project->unix_gid();
65 66 67 68 69 70

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

71 72 73 74 75
#
# Run the script backend
#
function DOIT($instance, $action, $command_options)
{
76
    global $guid, $vers, $pid, $unix_pid, $unix_gid, $eid, $uid;
77
    global $deletexmlfile, $parameter_xmlfile;
78 79 80
    $message    = "";
    $template   = $instance->GetTemplate();
    $experiment = $instance->GetExperiment();
81 82 83 84

    $command_options = "-e $eid " . $command_options;
    
    if ($action == "start") {
85
	PAGEHEADER("Start new Run");
86 87 88
	$message = "Starting new experiment run";
	$command_options = "-a start " . $command_options;
    }
Leigh Stoller's avatar
Leigh Stoller committed
89
    elseif ($action == "abort") {
90
	PAGEHEADER("Abort Run");
Leigh Stoller's avatar
Leigh Stoller committed
91 92 93
	$message = "Aborting experiment run";
	$command_options = "-a abort " . $command_options;
    }
94 95 96 97 98
    elseif ($action == "modify") {
	PAGEHEADER("Modify Run Resources");
	$message = "Modifying resources for run";
	$command_options = "-a modify " . $command_options;
    }
99
    else {
100 101
	PAGEHEADER("Stop current Run");
	$message = "Stopping current run";
102 103 104 105 106 107 108 109
	$command_options = "-a stop " . $command_options;
    }	

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

Kevin Atkinson's avatar
Kevin Atkinson committed
110
    echo $instance->ExpPageHeader();
111 112 113 114 115 116 117 118 119 120 121
    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.
    #
122
    $retval = SUEXEC($uid, "$unix_pid,$unix_gid",
123
		     "webtemplate_exprun $command_options $guid/$vers",
124 125
		     SUEXEC_ACTION_IGNORE);

126
    HIDEBUSY();
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

    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;
    }

146
    STARTLOG($experiment);
147 148
}

149 150 151 152 153
#
# Run the script backend
#
function DOTIME($instance, $action)
{
154
    global $guid, $vers, $pid, $unix_pid, $unix_gid, $eid, $uid;
155 156 157
    $message    = "";
    $template   = $instance->GetTemplate();
    $experiment = $instance->GetExperiment();
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

    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);

Kevin Atkinson's avatar
Kevin Atkinson committed
175
    echo $instance->ExpPageHeader();
176 177 178 179 180 181 182 183 184 185 186
    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.
    #
187
    $retval = SUEXEC($uid, "$unix_pid,$unix_gid",
188
		     "webtemplate_exprun $command_options $guid/$vers",
189 190
		     SUEXEC_ACTION_IGNORE);

191
    HIDEBUSY();
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

    #
    # 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;
    }

207
    PAGEREPLACE(CreateURL("showexp", $experiment));
208 209
}

210 211 212
#
# Spit the form out using the array of data.
#
213
function SPITFORM($action, $instance, $formfields, $parameters, $errors)
214 215
{
    global $TBDB_EIDLEN;
216
    global $TBVALIDDIRS_HTML;
217
    $template   = $instance->GetTemplate();
218

219 220 221 222 223 224
    if ($action == "modify") {
	PAGEHEADER("Modify Resources");
    }
    else {
	PAGEHEADER("Start New Run");
    }
225

Kevin Atkinson's avatar
Kevin Atkinson committed
226
    echo $instance->ExpPageHeader();
227
    echo "<br><br>\n";
228 229

    echo "<center>\n";
230
    $template->Show();
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
    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";
    }

256 257 258 259 260 261
    #
    # 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);
262
    $template->FormalParameterMouseOvers($mouseovers);
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
    $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";

296 297
    $url = CreateURL("template_exprun", $instance);

298
    echo "<form action='${url}&action=$action' method=post>\n";
299
    echo "<table align=center border=1>\n";
300
    
301 302 303
    #
    # RunID:
    #
304 305 306 307 308 309 310 311
    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
312
                         name=\"formfields[runid]\"
313
                         value=\"" . $formfields["runid"] . "\"
314
	                 size=$TBDB_EIDLEN
315 316 317 318
                         maxlength=$TBDB_EIDLEN>\n";
	echo "    </td>
              </tr>\n";
    }
319

320 321 322 323 324 325 326 327
    #
    # 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'";

328 329
    if (isset($formfields["clean"]) &&
	strcmp($formfields["clean"], "Yep") == 0) {
330 331 332 333 334 335 336
	echo " checked='1'";
    }
    echo ">";
    echo "&nbsp (run '<tt>loghole clean</tt>' before starting run)
	    </td>
	  </tr>\n";

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    #
    # 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";

354 355
    echo "<tr>
              <td colspan=2>
356
               Use this text area for an (optional) description:
357 358 359 360 361
              </td>
          </tr>
          <tr>
              <td colspan=2 align=center class=left>
                  <textarea name=\"formfields[description]\"
362
                    rows=4 cols=80>" .
363
	            str_replace("\r", "", $formfields["description"]) .
364 365 366 367 368 369 370 371 372
	           "</textarea>
              </td>
          </tr>\n";

    if (count($parameters)) {
        #
	# Table of inputs.
	#
	echo "<tr>
373
		  <td class='pad4'>Formal Parameters:</td>
374 375
		  <td>
 		    <table cellpadding=0 cellspacing=0 border=0>\n";
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391

	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";
392 393 394 395
	
	while (list ($name, $value) = each ($parameters)) {
	    if (!isset($value))
		$value = "&nbsp";
396
	    $mouseover = (isset($mouseovers[$name]) ? $mouseovers[$name] : "");
397 398

	    echo "<tr>
399
                    <td class='pad4' $mouseover>$name</td>
400 401
                    <td class='pad4' class=left>
                        <input type=text
402
                               id='parameter_$name'
403
                               name=\"parameters[$name]\"
Leigh Stoller's avatar
Leigh Stoller committed
404
                               value=\"" . $value . "\"
405 406 407 408 409 410 411 412 413
	                       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>
414
                           <font size='-1'>($TBVALIDDIRS_HTML)</font></td>
415 416 417
                  <td class='pad4'>
	              <input type=text
                             name=\"formfields[parameter_xmlfile]\"
418
                             value=\"" . $formfields["parameter_xmlfile"] . "\"
419 420 421 422 423 424 425 426 427 428 429 430 431 432
	                     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
433
if (isset($action) && ($action == "stop" || $action == "abort")) {
434 435 436 437 438
    # Run the backend script.
    DOIT($instance, $action, "");
    PAGEFOOTER();
    return;
}
439 440 441 442 443 444
elseif (isset($action) && ($action == "pause" || $action == "continue")) {
    # Run the backend script.
    DOTIME($instance, $action, "");
    PAGEFOOTER();
    return;
}
445
elseif (! isset($formfields)) {
446 447
    $defaults = array();
    
448
    #
449
    # On first load, display virgin form and exit.
450
    #
451 452
    $defaults['runid']   = $instance->NextRunID();
    $defaults['clean']   = "";
453
    $defaults['swapmod'] = "";
454 455
    $defaults['description'] = "";
    $defaults['parameter_xmlfile'] = "";
456 457

    #
Leigh Stoller's avatar
Leigh Stoller committed
458
    # Get the current bindings of the current run.
459
    #
Leigh Stoller's avatar
Leigh Stoller committed
460
    $instance->RunBindings($instance->LastRunIdx(), $bindings);
461 462 463 464 465 466 467 468 469 470
    
    #
    # Allow formfields that are already set to override defaults
    #
    if (isset($formfields)) {
	while (list ($field, $value) = each ($formfields)) {
	    $defaults[$field] = $formfields[$field];
	}
    }

471
    SPITFORM($action, $instance, $defaults, $bindings, 0);
472 473 474 475 476 477 478 479
    PAGEFOOTER();
    return;
}

#
# Okay, validate form arguments.
#
$errors = array();
480 481

# Set up command options
482
$command_options = " ";
483 484 485 486

#
# RunID:
#
487 488 489 490 491 492 493 494 495 496 497 498 499 500
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"]);
    }
501 502
}

503 504 505
#
# Clean?
#
506
if (isset($formfields["clean"]) && $formfields["clean"] == "Yep") {
507 508 509
    $command_options .= " -c";
}

510 511 512 513 514 515 516
#
# Swapmod?
#
if (isset($formfields["swapmod"]) && $formfields["swapmod"] == "Yep") {
    $command_options .= " -m";
}

517 518 519
#
# Description:
# 
520 521
if (isset($formfields["description"]) && $formfields["description"] != "") {
    if (!TBvalid_template_description($formfields["description"])) {
522 523 524
	$errors["Description"] = TBFieldErrorString();
    }
    else {
525
	$command_options .= " -E ". escapeshellarg($formfields["description"]);
526
    }
527 528 529 530 531
}

#
# Parameters. The XML file overrides stuff in the form. 
#
532
$template->FormalParameters($parameter_masterlist);
533
if (count($parameter_masterlist)) {
534 535
    if (isset($formfields["parameter_xmlfile"]) &&
	$formfields["parameter_xmlfile"] != "") {
536

537
	$parameter_xmlfile = $formfields["parameter_xmlfile"];
538 539 540 541 542
	    
	if (!preg_match("/^([-\@\w\.\/]+)$/", $parameter_xmlfile)) {
	    $errors["Parameter XML File"] =
		"Pathname includes illegal characters";
	}
543 544 545
	elseif (! VALIDUSERPATH($parameter_xmlfile)) {
	    $errors["Parameter XML File"] =
		"Must reside in one of: $TBVALIDDIRS";
546 547 548 549 550
	}
    	$deletexmlfile = 0;
    }
    else {
	#
551
	# Lets confirm that the user did not forget to set at least one value. 
552
	#
553
	$gotone = 0;
554
	while (list ($name, $default_value) = each ($parameter_masterlist)) {
555 556
	    if (isset($parameters[$name]) && $parameters[$name] != "") {
		$gotone = 1;
557
	    }
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
	}
	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);
575
	    }
576 577 578
	
	    fwrite($fp, "<template_parameters>\n");

579
	    reset($parameter_masterlist);
580 581 582 583 584 585 586
	    while (list($name,$default_value) = each ($parameter_masterlist)) {
		if (isset($parameters[$name])) {
		    $value = $parameters[$name];
		}
		else {
		    $value = $default_value;
		}
587
	    
588 589 590 591 592 593
		fwrite($fp, " <parameter name=\"$name\">");
		fwrite($fp, "<value>$value</value></parameter>\n");
	    }
	    fwrite($fp, "</template_parameters>\n");
	    fclose($fp);
	    chmod($parameter_xmlfile, 0666);
594 595
	}
    }
596
    $command_options .= " -x $parameter_xmlfile";
597 598 599
}

if (count($errors)) {
600
    SPITFORM($action, $instance, $formfields, $parameters, $errors);
601 602 603 604 605
    PAGEFOOTER();
    exit(1);
}

# Run the backend script.
606
DOIT($instance, $action, $command_options);
607

608 609 610 611 612
#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>