template_swapin.php 20.6 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#
# 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();

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

45 46 47
# Used below
unset($parameter_xmlfile);
$deletexmlfile = 0;
48
$batchmode     = 0;
49
$preload       = 0;
50 51 52 53

#
# Spit the form out using the array of data.
#
54
function SPITFORM($template, $formfields, $parameters, $errors)
55
{
56 57
    global $TBDB_EIDLEN, $EXPOSELINKTEST, $EXPOSESTATESAVE, $TBDOCBASE;
    global $TBVALIDDIRS_HTML, $linktest_levels;
58
    global $WIKIDOCURL;
59 60 61 62

    PAGEHEADER("Instantiate an Experiment Template");

    echo "<center>\n";
63
    $template->Show();
64
    echo "</center>\n";
65
    echo "<br>\n";
66

67
    $guid = $template->guid();
68
    $vers = $template->vers();
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
    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";
    }
91 92 93 94 95 96 97 98
    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";
    }
99

100 101
    $url = CreateURL("template_swapin", $template);
    echo "<form action='$url' method=post>\n";
102 103
    echo "<table align=center border=1>\n";

104 105 106 107 108 109 110 111 112 113 114 115 116
    #
    # 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";
	}
    }

117 118 119 120 121 122 123 124 125
    #
    # 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]\"
126
                         value=\"" . $formfields["eid"] . "\"
127 128 129 130 131 132 133 134 135 136 137 138 139
	                 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='";
140
    echo htmlspecialchars($formfields["exp_noswap_reason"], ENT_QUOTES);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    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'>
157
		    <a href='$WIKIDOCURL/Swapping#swapping'>
158 159 160 161 162 163
		    Swapping:</td>
		  <td>
		  <table cellpadding=0 cellspacing=0 border=0><tr>
		  <td><input type='checkbox'
			 name='formfields[exp_idleswap]'
			 value='1'";
164
	if ($formfields["exp_idleswap"] == "1") {
165 166 167
	    echo " checked='1'";
	}
	echo "></td>
168
		  <td><a href='$WIKIDOCURL/Swapping#idleswap'>
169 170 171 172
		  <b>Idle-Swap:</b></a> Swap out this experiment
		  after 
		  <input type='text' name='formfields[exp_idleswap_timeout]'
			 value='";
173
	echo htmlspecialchars($formfields["exp_idleswap_timeout"], ENT_QUOTES);
174 175 176 177 178 179
	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]'>";
			      
180
	echo htmlspecialchars($formfields["exp_noidleswap_reason"],ENT_QUOTES);
181 182 183 184 185
	echo "</textarea></td>
		  </tr><tr>
		  <td><input type='checkbox'
			 name='formfields[exp_autoswap]'
			 value='1' ";
186
	if ($formfields["exp_autoswap"] == "1") {
187 188 189
	    echo " checked='1'";
	}
	echo "></td>
190
		  <td><a href='$WIKIDOCURL/Swapping#autoswap'>
191 192 193
		  <b>Max. Duration:</b></a> Swap out after
		  <input type='text' name='formfields[exp_autoswap_timeout]'
			 value='";
194
	echo htmlspecialchars($formfields["exp_autoswap_timeout"], ENT_QUOTES);
195 196 197 198 199 200 201 202
	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'";

203 204
	    if (isset($formfields["exp_savedisk"]) &&
		strcmp($formfields["exp_savedisk"], "Yep") == 0) {
205 206 207 208
		    echo " checked='1'";
	    }

	    echo "></td>\n";
209
	    echo "<td><a href='$WIKIDOCURL/Swapping#swapstatesave'>
210 211 212 213 214 215 216 217
		  <b>State Saving:</b></a> Save disk state on swapout</td>
		  </tr>";
	
	}
	echo "</table></td></tr>";
    }
    
    if (count($parameters)) {
218 219 220 221 222 223
	#
	# Lets get an array of mouseovers to use in the form.
	#
	unset($mouseovers);
	$template->FormalParameterMouseOvers($mouseovers);
	
224 225 226 227 228 229 230 231 232 233 234
        #
	# 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";
235
	    $mouseover = (isset($mouseovers[$name]) ? $mouseovers[$name] : "");
236 237

	    echo "<tr>
238
                    <td class='pad4' $mouseover>$name</td>
239 240 241 242 243 244 245 246 247 248 249 250 251
                    <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>
252
                           <font size='-1'>($TBVALIDDIRS_HTML)</font></td>
253 254 255
                  <td class='pad4'>
	              <input type=text
                             name=\"formfields[parameter_xmlfile]\"
256
                             value=\"" . $formfields["parameter_xmlfile"] . "\"
257 258 259 260 261
	                     size=60>\n";
	echo "</td></tr>\n";
	echo "</table></td></tr>";
    }

262 263 264 265 266 267 268 269 270 271 272 273
    #
    # 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>" .
274
	            ereg_replace("\r", "", $formfields["description"]) .
275 276 277 278
	           "</textarea>
              </td>
          </tr>\n";

279 280 281 282 283 284 285
    #
    # Batch Experiment?
    #
    echo "<tr>
	      <td class='pad4' colspan=2>
	      <input type=checkbox name='formfields[batched]' value='Yep'";

286 287
    if (isset($formfields["batched"]) &&
	strcmp($formfields["batched"], "Yep") == 0) {
288 289 290 291 292
	echo " checked='1'";
    }
    echo ">\n";
    echo "Batch Mode Instantiation &nbsp;
	  <font size='-1'>(See
293
          <a href='$WIKIDOCURL/Tutorial#BatchMode'>Tutorial</a>
294 295 296 297
          for more information)</font>
	  </td>
	  </tr>\n";

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

306 307
    if (isset($formfields["preload"]) &&
	strcmp($formfields["preload"], "Yep") == 0) {
308 309 310 311 312 313
	echo " checked='1'";
    }
    echo ">\n";
    echo "Do Not Swap In</td>
	 </tr>\n";

314 315 316 317 318
    #
    # Run linktest, and level. 
    #
    if (STUDLY() || $EXPOSELINKTEST) {
	echo "<tr>
319
              <td><a href='$WIKIDOCURL/linktest'>Linktest</a> Option:</td>
320 321 322 323 324 325
              <td><select name=\"formfields[exp_linktest]\">
                          <option value=0>Skip Linktest </option>\n";

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

326
	    if (strcmp($formfields["exp_linktest"], "$i") == 0)
327 328 329 330 331 332
		$selected = "selected";
	
	    echo "        <option $selected value=$i>Level $i - " .
		$linktest_levels[$i] . "</option>\n";
	}
	echo "       </select>";
333
	echo "    (<a href='$WIKIDOCURL/linktest'><b>What is this?</b></a>)";
334 335 336 337
	echo "    </td>
              </tr>\n";
    }

338 339 340 341 342 343 344 345 346 347 348 349 350
    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)) {
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    $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");
367

368
    #
369
    # Optional replay instance/run.
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
    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);
    }
402 403 404 405 406 407 408 409 410 411
    
    #
    # Allow formfields that are already set to override defaults
    #
    if (isset($formfields)) {
	while (list ($field, $value) = each ($formfields)) {
	    $defaults[$field] = $formfields[$field];
	}
    }

412
    SPITFORM($template, $defaults, $parameters, 0);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
    PAGEFOOTER();
    return;
}
elseif (! isset($formfields)) {
    PAGEARGERROR();
}

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

#
# EID:
#
429
if (!isset($formfields["eid"]) || $formfields["eid"] == "") {
430 431
    $errors["ID"] = "Missing Field";
}
432
elseif (!TBvalid_eid($formfields["eid"])) {
433 434
    $errors["ID"] = TBFieldErrorString();
}
435
elseif (Experiment::Lookup($pid, $formfields["eid"])) {
436 437 438
    $errors["ID"] = "Already in use";
}
else {
439
    $eid = $formfields["eid"];
440 441 442 443 444 445 446 447
}

# Set up command options
$command_options = "";

#
# Swappable
#
448 449 450
if (!isset($formfields["exp_swappable"]) ||
    strcmp($formfields["exp_swappable"], "1")) {
    $formfields["exp_swappable"] = 0;
451

452 453
    if (!isset($formfields["exp_noswap_reason"]) ||
        !strcmp($formfields["exp_noswap_reason"], "")) {
454

455
        if (! $isadmin) {
456 457 458
	    $errors["Not Swappable"] = "No justification provided";
        }
	else {
459
	    $formfields["exp_noswap_reason"] = "ADMIN";
460 461
        }
    }
462
    elseif (!TBvalid_description($formfields["exp_noswap_reason"])) {
463 464 465 466
	$errors["Not Swappable"] = TBFieldErrorString();
    }
    else {
	$command_options .=
467
	    " -S " . escapeshellarg($formfields["exp_noswap_reason"]);
468 469 470 471
    }
}
else {
    # For form redisplay.
472 473
    $formfields["exp_swappable"]     = 1;
    $formfields["exp_noswap_reason"] = "";
474 475 476 477 478
}

#
# Idle swap
#
479 480 481
if (!isset($formfields["exp_idleswap"]) ||
    strcmp($formfields["exp_idleswap"], "1")) {
    $formfields["exp_idleswap"] = 0;
482

483 484
    if (!isset($formfields["exp_noidleswap_reason"]) ||
	!strcmp($formfields["exp_noidleswap_reason"], "")) {
485
	if (! $isadmin) {
486 487 488
	    $errors["Not Idle-Swappable"] = "No justification provided";
	}
	else {
489
	    $formfields["exp_noidleswap_reason"] = "ADMIN";
490 491
	}
    }
492
    elseif (!TBvalid_description($formfields["exp_noidleswap_reason"])) {
493 494 495 496
	$errors["Not Idle-Swappable"] = TBFieldErrorString();
    }
    else {
	$command_options .= " -L " .
497
	    escapeshellarg($formfields["exp_noidleswap_reason"]);
498 499 500
    }
}
else {
501 502
    $formfields["exp_idleswap"]          = 1;
    $formfields["exp_noidleswap_reason"] = "";
503 504 505 506 507

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

    # Proper idleswap timeout must be provided.
508 509 510 511
    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) {
512 513 514
	$errors["Idleswap"] = "Invalid time provided - ".
	    "must be non-zero and less than $idleswaptimeout";
    }
515
    $command_options .= " -l " . (60 * $formfields["exp_idleswap_timeout"]);
516 517 518 519 520
}

#
# Autoswap.
#
521 522 523
if (!isset($formfields["exp_autoswap"]) ||
    strcmp($formfields["exp_autoswap"], "1")) {
    $formfields["exp_autoswap"] = 0;
524 525
}
else {
526
    $formfields["exp_autoswap"] = 1;
527
    
528 529 530
    if (!isset($formfields["exp_autoswap_timeout"]) ||
	!preg_match("/^[\d]+$/", $formfields["exp_idleswap_timeout"]) ||
	($formfields["exp_autoswap_timeout"] + 0) <= 0) {
531 532 533
	$errors["Max. Duration"] = "No or invalid time provided";
    }
    else {
534
	$command_options .= " -a " . (60*$formfields["exp_autoswap_timeout"]);
535 536 537 538 539 540
    }
}

#
# Save Disk
#
541 542
if (isset($formfields["exp_savedisk"]) &&
    strcmp($formfields["exp_savedisk"], "Yep") == 0) {
543 544 545 546 547 548
    $command_options .= " -s";
}

#
# Parameters. The XML file overrides stuff in the form. 
#
549
$template->FormalParameters($parameter_masterlist);
550
if (count($parameter_masterlist)) {
551 552
    if (isset($formfields["parameter_xmlfile"]) &&
	$formfields["parameter_xmlfile"] != "") {
553

554
	$parameter_xmlfile = $formfields["parameter_xmlfile"];
555 556 557 558 559
	    
	if (!preg_match("/^([-\@\w\.\/]+)$/", $parameter_xmlfile)) {
	    $errors["Parameter XML File"] =
		"Pathname includes illegal characters";
	}
560 561 562
	elseif (! VALIDUSERPATH($parameter_xmlfile)) {
	    $errors["Parameter XML File"] =
		"Must reside in one of: $TBVALIDDIRS";
563 564 565 566 567
	}
    	$deletexmlfile = 0;
    }
    else {
	#
568
	# Lets confirm that the user did not forget to set at least one value. 
569
	#
570
	$gotone = 0;
571
	while (list ($name, $default_value) = each ($parameter_masterlist)) {
572 573
	    if (isset($parameters[$name]) && $parameters[$name] != "") {
		$gotone = 1;
574
	    }
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	}
	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);
592
	    }
593 594 595 596 597 598 599 600 601 602 603
	
	    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;
		}
604
	    
605 606 607 608 609 610
		fwrite($fp, " <parameter name=\"$name\">");
		fwrite($fp, "<value>$value</value></parameter>\n");
	    }
	    fwrite($fp, "</template_parameters>\n");
	    fclose($fp);
	    chmod($parameter_xmlfile, 0666);
611 612
	}
    }
613
    $command_options .= " -x $parameter_xmlfile";
614 615
}

616 617 618
#
# Description:
# 
619 620
if (isset($formfields["description"]) && $formfields["description"] != "") {
    if (!TBvalid_template_description($formfields["description"])) {
621 622 623
	$errors["Description"] = TBFieldErrorString();
    }
    else {
624
	$command_options .= " -E ". escapeshellarg($formfields["description"]);
625
    }
626 627
}

628 629 630
#
# Batchmode
#
631
if (isset($formfields["batched"]) && $formfields["batched"] == "Yep") {
632 633 634 635
    $command_options .= " -b";
    $batchmode = 1;
}

636 637 638
#
# Preload?
#
639
if (isset($formfields["preload"]) && $formfields["preload"] == "Yep") {
640
    $command_options .= " -p";
641
    $preload = 1;
642 643
}

644 645 646
#
# LinkTest
#
647 648 649
if (isset($formfields["exp_linktest"]) && $formfields["exp_linktest"] != "") {
    if (!preg_match("/^[\d]+$/", $formfields["exp_linktest"]) ||
	$formfields["exp_linktest"] < 0 || $formfields["exp_linktest"] > 4) {
650 651 652
	$errors["Linktest Option"] = "Invalid level selection";
    }
    else {
653
	$command_options .= " -t " . $formfields["exp_linktest"];
654 655 656
    }
}

657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
#
# 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"];
	}
    }
}

679
if (count($errors)) {
680
    SPITFORM($template, $formfields, $parameters, $errors);
681 682 683 684 685 686 687 688 689 690 691 692
    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");

693 694 695 696 697
echo $template->PageHeader();
echo "<font size=+2>, Instance <b>" .
      MakeLink("project", "pid=$pid", $pid) . "/" .
      MakeLink("experiment", "pid=$pid&eid=$eid", $eid) . 
     "</b></font>";
698 699
echo "<br><br>\n";

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

704
STARTBUSY("Starting template instantiation!");
705 706 707 708 709

#
# Run the backend script.
#
$retval = SUEXEC($uid, "$pid,$unix_gid",
710
		 "webtemplate_instantiate ".
711
		    "$command_options -e $eid $guid/$vers",
712 713
		 SUEXEC_ACTION_IGNORE);

714
HIDEBUSY();
715

716
if ($retval) {
717 718 719 720 721 722 723
    #
    # 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);
    }
724

725
    # User error. Tell user and exit.
726 727 728 729
    SUEXECERROR(SUEXEC_ACTION_USERERROR);
    return;
}

730
#
731
# We need to locate this instance for STARTLOG() below.
732
#
733 734 735 736 737 738 739 740
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);
741 742
}

743 744 745
#
# This does both the log output, and the state change watcher popup
#
746
if ($batchmode && !$preload) {
747 748 749 750
    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
751
          your experiment has completed.\n";
752
}
753
STARTLOG($instance->GetLogfile());
754 755 756 757 758 759

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