template_swapin.php 21.3 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 45 46 47 48 49 50 51 52 53 54 55
#
# Verify page arguments
#
$reqargs = RequiredPageArguments("template",   PAGEARG_TEMPLATE);
$optargs = OptionalPageArguments("swapin",     PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY,
				 "parameters", PAGEARG_ARRAY,
				 "replay_instance_idx", PAGEARG_INTEGER,
				 "replay_run_idx",      PAGEARG_INTEGER);

# Need these below.
$guid = $template->guid();
$vers = $template->vers();
$pid  = $template->pid();
$unix_gid = $template->UnixGID();
56 57
$project  = $template->GetProject();
$unix_pid = $project->unix_gid();
58 59 60 61 62 63

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

64 65 66
# Used below
unset($parameter_xmlfile);
$deletexmlfile = 0;
67
$batchmode     = 0;
68
$preload       = 0;
69 70 71 72

#
# Spit the form out using the array of data.
#
73
function SPITFORM($template, $formfields, $parameters, $errors)
74
{
75 76
    global $TBDB_EIDLEN, $EXPOSELINKTEST, $EXPOSESTATESAVE, $TBDOCBASE;
    global $TBVALIDDIRS_HTML, $linktest_levels;
77
    global $WIKIDOCURL;
78 79 80 81

    PAGEHEADER("Instantiate an Experiment Template");

    echo "<center>\n";
82
    $template->Show();
83
    echo "</center>\n";
84
    echo "<br>\n";
85

86
    $guid = $template->guid();
87
    $vers = $template->vers();
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    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";
    }
110 111 112 113 114 115 116 117
    else {
	echo "<blockquote><blockquote><font size=+1>";
	echo "Template Instantiation will map your template onto actual ".
	    "testbed hardware. This used to be known as ".
	    "<em>experiment swapin</em> ".
	    "but we decided we like <em>instantiation</em> better.";
	echo "</font></blockquote></blockquote><br>\n";
    }
118

119 120
    $url = CreateURL("template_swapin", $template);
    echo "<form action='$url' method=post>\n";
121 122
    echo "<table align=center border=1>\n";

123 124 125 126 127 128 129 130 131 132 133 134 135
    #
    # Pass along replay info, but no need to display it.
    #
    if (isset($formfields["replay_instance_idx"])) {
	echo "<input type=hidden name=\"formfields[replay_instance_idx]\"
                     value='" . $formfields["replay_instance_idx"] . "'>\n";

	if (isset($formfields["replay_run_idx"])) {
	    echo "<input type=hidden name=\"formfields[replay_run_idx]\"
                         value='" . $formfields["replay_run_idx"] . "'>\n";
	}
    }

136 137 138 139 140 141 142 143 144
    #
    # EID:
    #
    echo "<tr>
              <td class='pad4'>ID:
              <br><font size='-1'>(alphanumeric, no blanks)</font></td>
              <td class='pad4' class=left>
                  <input type=text
                         name=\"formfields[eid]\"
145
                         value=\"" . $formfields["eid"] . "\"
146 147 148 149 150 151 152 153 154 155 156 157 158
	                 size=$TBDB_EIDLEN
                         maxlength=$TBDB_EIDLEN>
              </td>
          </tr>\n";

    #
    # Swapping
    #
    # Add in hidden fields to send swappable and noswap_reason, since
    # they do not show on the form
    echo "<input type=hidden name=\"formfields[exp_swappable]\"
                 value='$formfields[exp_swappable]'>\n";
    echo "<input type=hidden name=\"formfields[exp_noswap_reason]\" value='";
159
    echo htmlspecialchars($formfields["exp_noswap_reason"], ENT_QUOTES);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    echo "'>\n";
    
    if (isset($view['hide_swap'])) {
	$idlevars = array('exp_idleswap','exp_noidleswap_reason',
			  'exp_idleswap_timeout',
	                  'exp_autoswap','exp_autoswap_timeout');
	while (list($index,$value) = each($idlevars)) {
	    if (isset($formfields[$value])) {
		echo "<input type='hidden' name='formfields[$value]'
                             value='$formfields[$value]'>\n";
	    }
	}
    }
    else {
	echo "<tr>
		  <td class='pad4'>
176
		    <a href='$WIKIDOCURL/Swapping#swapping'>
177 178 179 180 181 182
		    Swapping:</td>
		  <td>
		  <table cellpadding=0 cellspacing=0 border=0><tr>
		  <td><input type='checkbox'
			 name='formfields[exp_idleswap]'
			 value='1'";
183
	if ($formfields["exp_idleswap"] == "1") {
184 185 186
	    echo " checked='1'";
	}
	echo "></td>
187
		  <td><a href='$WIKIDOCURL/Swapping#idleswap'>
188 189 190 191
		  <b>Idle-Swap:</b></a> Swap out this experiment
		  after 
		  <input type='text' name='formfields[exp_idleswap_timeout]'
			 value='";
192
	echo htmlspecialchars($formfields["exp_idleswap_timeout"], ENT_QUOTES);
193 194 195 196 197 198
	echo "' size='3'> hours idle.</td>
		  </tr><tr>
		  <td> </td>
		  <td>If not, why not?<br><textarea rows=2 cols=50
			      name='formfields[exp_noidleswap_reason]'>";
			      
199
	echo htmlspecialchars($formfields["exp_noidleswap_reason"],ENT_QUOTES);
200 201 202 203 204
	echo "</textarea></td>
		  </tr><tr>
		  <td><input type='checkbox'
			 name='formfields[exp_autoswap]'
			 value='1' ";
205
	if ($formfields["exp_autoswap"] == "1") {
206 207 208
	    echo " checked='1'";
	}
	echo "></td>
209
		  <td><a href='$WIKIDOCURL/Swapping#autoswap'>
210 211 212
		  <b>Max. Duration:</b></a> Swap out after
		  <input type='text' name='formfields[exp_autoswap_timeout]'
			 value='";
213
	echo htmlspecialchars($formfields["exp_autoswap_timeout"], ENT_QUOTES);
214 215 216 217 218 219 220 221
	echo "' size='3'> hours, even if not idle.</td>
		  </tr>";

	if (STUDLY() || $EXPOSESTATESAVE) {
	    echo "<tr><td>
	         <input type=checkbox name='formfields[exp_savedisk]'
	         value='Yep'";

222 223
	    if (isset($formfields["exp_savedisk"]) &&
		strcmp($formfields["exp_savedisk"], "Yep") == 0) {
224 225 226 227
		    echo " checked='1'";
	    }

	    echo "></td>\n";
228
	    echo "<td><a href='$WIKIDOCURL/Swapping#swapstatesave'>
229 230 231 232 233 234 235 236
		  <b>State Saving:</b></a> Save disk state on swapout</td>
		  </tr>";
	
	}
	echo "</table></td></tr>";
    }
    
    if (count($parameters)) {
237 238 239 240 241 242
	#
	# Lets get an array of mouseovers to use in the form.
	#
	unset($mouseovers);
	$template->FormalParameterMouseOvers($mouseovers);
	
243 244 245 246 247 248 249 250 251 252 253
        #
	# Table of inputs.
	#
	echo "<tr>
		  <td class='pad4'>Formal Parameters:</td>
		  <td>
 		    <table cellpadding=0 cellspacing=0 border=0>\n";
	
	while (list ($name, $value) = each ($parameters)) {
	    if (!isset($value))
		$value = "&nbsp";
254
	    $mouseover = (isset($mouseovers[$name]) ? $mouseovers[$name] : "");
255 256

	    echo "<tr>
257
                    <td class='pad4' $mouseover>$name</td>
258 259 260 261 262 263 264 265 266 267 268 269 270
                    <td class='pad4' class=left>
                        <input type=text
                               name=\"parameters[$name]\"
                               value=\"" . $value . "\"
	                       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>
271
                           <font size='-1'>($TBVALIDDIRS_HTML)</font></td>
272 273 274
                  <td class='pad4'>
	              <input type=text
                             name=\"formfields[parameter_xmlfile]\"
275
                             value=\"" . $formfields["parameter_xmlfile"] . "\"
276 277 278 279 280
	                     size=60>\n";
	echo "</td></tr>\n";
	echo "</table></td></tr>";
    }

281 282 283 284 285 286 287 288 289 290 291 292
    #
    # Description
    #
    echo "<tr>
              <td colspan=2>
               Use this text area for an (optional) description:
              </td>
          </tr>
          <tr>
              <td colspan=2 align=center class=left>
                  <textarea name=\"formfields[description]\"
                    rows=5 cols=80>" .
293
	            str_replace("\r", "", $formfields["description"]) .
294 295 296 297
	           "</textarea>
              </td>
          </tr>\n";

298 299 300 301 302 303 304
    #
    # Batch Experiment?
    #
    echo "<tr>
	      <td class='pad4' colspan=2>
	      <input type=checkbox name='formfields[batched]' value='Yep'";

305 306
    if (isset($formfields["batched"]) &&
	strcmp($formfields["batched"], "Yep") == 0) {
307 308 309 310 311
	echo " checked='1'";
    }
    echo ">\n";
    echo "Batch Mode Instantiation &nbsp;
	  <font size='-1'>(See
312
          <a href='$WIKIDOCURL/Tutorial#BatchMode'>Tutorial</a>
313 314 315 316
          for more information)</font>
	  </td>
	  </tr>\n";

317 318 319 320 321 322 323 324
    #
    # Preload?
    #
    echo "<tr>
	      <td class='pad4' colspan=2>
		  <input type=checkbox name='formfields[preload]'
                         value='Yep'";

325 326
    if (isset($formfields["preload"]) &&
	strcmp($formfields["preload"], "Yep") == 0) {
327 328 329 330 331 332
	echo " checked='1'";
    }
    echo ">\n";
    echo "Do Not Swap In</td>
	 </tr>\n";

333 334 335 336 337
    #
    # Run linktest, and level. 
    #
    if (STUDLY() || $EXPOSELINKTEST) {
	echo "<tr>
338
              <td><a href='$WIKIDOCURL/linktest'>Linktest</a> Option:</td>
339 340 341 342 343 344
              <td><select name=\"formfields[exp_linktest]\">
                          <option value=0>Skip Linktest </option>\n";

	for ($i = 1; $i <= TBDB_LINKTEST_MAX; $i++) {
	    $selected = "";

345
	    if (strcmp($formfields["exp_linktest"], "$i") == 0)
346 347 348 349 350 351
		$selected = "selected";
	
	    echo "        <option $selected value=$i>Level $i - " .
		$linktest_levels[$i] . "</option>\n";
	}
	echo "       </select>";
352
	echo "    (<a href='$WIKIDOCURL/linktest'><b>What is this?</b></a>)";
353 354 355 356
	echo "    </td>
              </tr>\n";
    }

357 358 359 360 361 362 363 364 365 366 367 368 369
    echo "<tr>
              <td class='pad4' align=center colspan=2>
                <b><input type=submit name=swapin value='Instantiate'></b>
              </td>
         </tr>
        </form>
        </table>\n";
}

#
# On first load, display virgin form and exit.
#
if (!isset($swapin)) {
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
    $defaults = array();
    
    $defaults["eid"]		       = $template->tid();
    $defaults["description"]           = "";
    $defaults["preload"]               = "no";
    $defaults["savedisk"]              = "";
    $defaults["batched"]               = "";
    $defaults["exp_swappable"]         = "1";
    $defaults["parameter_xmlfile"]     = "";
    $defaults["exp_noswap_reason"]     = "";
    $defaults["exp_idleswap"]          = "1";
    $defaults["exp_linktest"]          = 3;    
    $defaults["exp_noidleswap_reason"] = "";
    $defaults["exp_idleswap_timeout"]  = TBGetSiteVar("idle/threshold");
    $defaults["exp_autoswap"]          = TBGetSiteVar("general/autoswap_mode");
    $defaults["exp_autoswap_timeout"]  = TBGetSiteVar("general/autoswap_threshold");
386

387
    #
388
    # Optional replay instance/run.
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
    unset($replay_instance);

    if (isset($replay_instance_idx)) {
	if (!TBvalid_integer($replay_instance_idx)) {
	    PAGEARGERROR("Invalid characters in replay instance IDX!");
	}
    
	$replay_instance =
	    TemplateInstance::LookupByExptidx($replay_instance_idx);
	if (!$replay_instance) {
	    USERERROR("No such instance $replay_instance_idx in template!", 1);
	}
	$defaults["replay_instance_idx"] = $replay_instance_idx;

	if (isset($replay_run_idx)) {
	    if (!TBvalid_integer($replay_run_idx)) {
		PAGEARGERROR("Invalid characters in replay run IDX!");
	    }
	    $replay_instance->RunBindings($parameters);
	    $defaults["replay_run_idx"] = $replay_run_idx;
	}
	else {
	    $replay_instance->Bindings($parameters);	
	}
    }
    else {
        #
        # Look for formal parameters that the user can specify.
        #
	$template->FormalParameters($parameters);
    }
421 422 423 424 425 426 427 428 429 430
    
    #
    # Allow formfields that are already set to override defaults
    #
    if (isset($formfields)) {
	while (list ($field, $value) = each ($formfields)) {
	    $defaults[$field] = $formfields[$field];
	}
    }

431
    SPITFORM($template, $defaults, $parameters, 0);
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
    PAGEFOOTER();
    return;
}
elseif (! isset($formfields)) {
    PAGEARGERROR();
}

#
# Okay, validate form arguments.
#
$errors = array();
$eid    = "";

#
# EID:
#
448
if (!isset($formfields["eid"]) || $formfields["eid"] == "") {
449 450
    $errors["ID"] = "Missing Field";
}
451
elseif (!TBvalid_eid($formfields["eid"])) {
452 453
    $errors["ID"] = TBFieldErrorString();
}
454
elseif (Experiment::Lookup($pid, $formfields["eid"])) {
455 456 457
    $errors["ID"] = "Already in use";
}
else {
458
    $eid = $formfields["eid"];
459 460 461 462 463 464 465 466
}

# Set up command options
$command_options = "";

#
# Swappable
#
467 468 469
if (!isset($formfields["exp_swappable"]) ||
    strcmp($formfields["exp_swappable"], "1")) {
    $formfields["exp_swappable"] = 0;
470

471 472
    if (!isset($formfields["exp_noswap_reason"]) ||
        !strcmp($formfields["exp_noswap_reason"], "")) {
473

474
        if (! $isadmin) {
475 476 477
	    $errors["Not Swappable"] = "No justification provided";
        }
	else {
478
	    $formfields["exp_noswap_reason"] = "ADMIN";
479 480
        }
    }
481
    elseif (!TBvalid_description($formfields["exp_noswap_reason"])) {
482 483 484 485
	$errors["Not Swappable"] = TBFieldErrorString();
    }
    else {
	$command_options .=
486
	    " -S " . escapeshellarg($formfields["exp_noswap_reason"]);
487 488 489 490
    }
}
else {
    # For form redisplay.
491 492
    $formfields["exp_swappable"]     = 1;
    $formfields["exp_noswap_reason"] = "";
493 494 495 496 497
}

#
# Idle swap
#
498 499 500
if (!isset($formfields["exp_idleswap"]) ||
    strcmp($formfields["exp_idleswap"], "1")) {
    $formfields["exp_idleswap"] = 0;
501

502 503
    if (!isset($formfields["exp_noidleswap_reason"]) ||
	!strcmp($formfields["exp_noidleswap_reason"], "")) {
504
	if (! $isadmin) {
505 506 507
	    $errors["Not Idle-Swappable"] = "No justification provided";
	}
	else {
508
	    $formfields["exp_noidleswap_reason"] = "ADMIN";
509 510
	}
    }
511
    elseif (!TBvalid_description($formfields["exp_noidleswap_reason"])) {
512 513 514 515
	$errors["Not Idle-Swappable"] = TBFieldErrorString();
    }
    else {
	$command_options .= " -L " .
516
	    escapeshellarg($formfields["exp_noidleswap_reason"]);
517 518 519
    }
}
else {
520 521
    $formfields["exp_idleswap"]          = 1;
    $formfields["exp_noidleswap_reason"] = "";
522 523 524 525 526

    # Need this below;
    $idleswaptimeout = TBGetSiteVar("idle/threshold");

    # Proper idleswap timeout must be provided.
527 528 529 530
    if (!isset($formfields["exp_idleswap_timeout"]) ||
	!preg_match("/^[\d]+$/", $formfields["exp_idleswap_timeout"]) ||
	($formfields["exp_idleswap_timeout"] + 0) <= 0 ||
	($formfields["exp_idleswap_timeout"] + 0) > $idleswaptimeout) {
531 532 533
	$errors["Idleswap"] = "Invalid time provided - ".
	    "must be non-zero and less than $idleswaptimeout";
    }
534
    $command_options .= " -l " . (60 * $formfields["exp_idleswap_timeout"]);
535 536 537 538 539
}

#
# Autoswap.
#
540 541 542
if (!isset($formfields["exp_autoswap"]) ||
    strcmp($formfields["exp_autoswap"], "1")) {
    $formfields["exp_autoswap"] = 0;
543 544
}
else {
545
    $formfields["exp_autoswap"] = 1;
546
    
547 548 549
    if (!isset($formfields["exp_autoswap_timeout"]) ||
	!preg_match("/^[\d]+$/", $formfields["exp_idleswap_timeout"]) ||
	($formfields["exp_autoswap_timeout"] + 0) <= 0) {
550 551 552
	$errors["Max. Duration"] = "No or invalid time provided";
    }
    else {
553
	$command_options .= " -a " . (60*$formfields["exp_autoswap_timeout"]);
554 555 556 557 558 559
    }
}

#
# Save Disk
#
560 561
if (isset($formfields["exp_savedisk"]) &&
    strcmp($formfields["exp_savedisk"], "Yep") == 0) {
562 563 564 565 566 567
    $command_options .= " -s";
}

#
# Parameters. The XML file overrides stuff in the form. 
#
568
$template->FormalParameters($parameter_masterlist);
569
if (count($parameter_masterlist)) {
570 571
    if (isset($formfields["parameter_xmlfile"]) &&
	$formfields["parameter_xmlfile"] != "") {
572

573
	$parameter_xmlfile = $formfields["parameter_xmlfile"];
574 575 576 577 578
	    
	if (!preg_match("/^([-\@\w\.\/]+)$/", $parameter_xmlfile)) {
	    $errors["Parameter XML File"] =
		"Pathname includes illegal characters";
	}
579 580 581
	elseif (! VALIDUSERPATH($parameter_xmlfile)) {
	    $errors["Parameter XML File"] =
		"Must reside in one of: $TBVALIDDIRS";
582 583 584 585 586
	}
    	$deletexmlfile = 0;
    }
    else {
	#
587
	# Lets confirm that the user did not forget to set at least one value. 
588
	#
589
	$gotone = 0;
590
	while (list ($name, $default_value) = each ($parameter_masterlist)) {
591 592
	    if (isset($parameters[$name]) && $parameters[$name] != "") {
		$gotone = 1;
593
	    }
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
	}
	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);
611
	    }
612 613 614 615 616 617 618 619 620 621 622
	
	    fwrite($fp, "<template_parameters>\n");

	    reset($parameter_masterlist);
	    while (list ($name,$default_value) = each($parameter_masterlist)) {
		if (isset($parameters[$name])) {
		    $value = $parameters[$name];
		}
		else {
		    $value = $default_value;
		}
623
	    
624 625 626 627 628 629
		fwrite($fp, " <parameter name=\"$name\">");
		fwrite($fp, "<value>$value</value></parameter>\n");
	    }
	    fwrite($fp, "</template_parameters>\n");
	    fclose($fp);
	    chmod($parameter_xmlfile, 0666);
630 631
	}
    }
632
    $command_options .= " -x $parameter_xmlfile";
633 634
}

635 636 637
#
# Description:
# 
638 639
if (isset($formfields["description"]) && $formfields["description"] != "") {
    if (!TBvalid_template_description($formfields["description"])) {
640 641 642
	$errors["Description"] = TBFieldErrorString();
    }
    else {
643
	$command_options .= " -E ". escapeshellarg($formfields["description"]);
644
    }
645 646
}

647 648 649
#
# Batchmode
#
650
if (isset($formfields["batched"]) && $formfields["batched"] == "Yep") {
651 652 653 654
    $command_options .= " -b";
    $batchmode = 1;
}

655 656 657
#
# Preload?
#
658
if (isset($formfields["preload"]) && $formfields["preload"] == "Yep") {
659
    $command_options .= " -p";
660
    $preload = 1;
661 662
}

663 664 665
#
# LinkTest
#
666 667 668
if (isset($formfields["exp_linktest"]) && $formfields["exp_linktest"] != "") {
    if (!preg_match("/^[\d]+$/", $formfields["exp_linktest"]) ||
	$formfields["exp_linktest"] < 0 || $formfields["exp_linktest"] > 4) {
669 670 671
	$errors["Linktest Option"] = "Invalid level selection";
    }
    else {
672
	$command_options .= " -t " . $formfields["exp_linktest"];
673 674 675
    }
}

676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
#
# Replay info.
#
if (isset($formfields["replay_instance_idx"]) &&
    $formfields["replay_instance_idx"] != "") {
    if (!preg_match("/^[\d]+$/", $formfields["replay_instance_idx"])) {
	$errors["Replay Instance"] = "Invalid replay instance";
    }
    else {
	$command_options .= " -r " . $formfields["replay_instance_idx"];
    }
    if (isset($formfields["replay_run_idx"]) &&
	$formfields["replay_run_idx"] != "") {
	if (!preg_match("/^[\d]+$/", $formfields["replay_run_idx"])) {
	    $errors["Replay Run"] = "Invalid replay run";
	}
	else {
	    $command_options .= ":" . $formfields["replay_run_idx"];
	}
    }
}

698
if (count($errors)) {
699
    SPITFORM($template, $formfields, $parameters, $errors);
700 701 702 703 704 705 706 707 708 709 710 711
    PAGEFOOTER();
    exit(1);
}

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

# Okay, we can spit back a header now that there is no worry of redirect.
PAGEHEADER("Instantiate Experiment Template");

712 713 714 715 716
echo $template->PageHeader();
echo "<font size=+2>, Instance <b>" .
      MakeLink("project", "pid=$pid", $pid) . "/" .
      MakeLink("experiment", "pid=$pid&eid=$eid", $eid) . 
     "</b></font>";
717 718
echo "<br><br>\n";

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

723
STARTBUSY("Starting template instantiation!");
724 725 726 727

#
# Run the backend script.
#
728
$retval = SUEXEC($uid, "$unix_pid,$unix_gid",
729
		 "webtemplate_instantiate ".
730
		    "$command_options -e $eid $guid/$vers",
731 732
		 SUEXEC_ACTION_IGNORE);

733
HIDEBUSY();
734

735
if ($retval) {
736 737 738 739 740 741 742
    #
    # 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);
    }
743

744
    # User error. Tell user and exit.
745 746 747 748
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
    return;
}

749
#
750
# We need to locate this instance for STARTLOG() below.
751
#
752 753 754 755 756 757 758 759
if (!preg_match("/^Instance\s+[-\w]+\/[-\w]+\s+\((\d*)\)/",
		$suexec_output_array[count($suexec_output_array)-1],
		$matches)) {
    TBERROR("Could not locate instance object for $pid/$eid", 1);
}
$instance = TemplateInstance::LookupByIdx($matches[1]);
if (!$instance) {
    TBERROR("Could not map instance idx " . $matches[1] . " to its object!",1);
760 761
}

762 763 764
#
# This does both the log output, and the state change watcher popup
#
765
if ($batchmode && !$preload) {
766 767 768 769
    echo "You template instantation has been queued and will run when
          enough resources become available. This might happen
          immediately, or it may take hours or days; you will be
          notified via email when insantiation is complete, and again when
770
          your experiment has completed.\n";
771
}
772
STARTLOG($instance->GetLogfile());
773 774 775 776 777 778

#
# Standard Testbed Footer
# 
PAGEFOOTER();
?>