editexp.php3 18.2 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
5 6 7 8 9 10
# All rights reserved.
#
include("defs.php3");

#
# No PAGEHEADER since we spit out a Location header later. See below.
11
#
12 13 14 15

#
# Only known and logged in users can end experiments.
#
16 17 18
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
19 20 21 22
$idleswaptimeout = TBGetSiteVar("idle/threshold");

#
# Verify page arguments.
23
#
24 25 26
$reqargs = RequiredPageArguments("experiment", PAGEARG_EXPERIMENT);
$optargs = OptionalPageArguments("submit",     PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY);
27

28 29 30 31
# Need below
$pid = $experiment->pid();
$eid = $experiment->eid();

32 33 34
#
# Verify Permission.
#
35
if (! $experiment->AccessCheck($this_user, $TB_EXPT_MODIFY)) {
36 37 38 39
    USERERROR("You do not have permission to modify experiment $pid/$eid!", 1);
}

#
40 41
# Spit the form out using the array of data.
#
42
function SPITFORM($experiment, $formfields, $errors)
43
{
44
    global $TBDOCBASE, $linktest_levels, $EXPOSELINKTEST;
45

46 47 48 49 50
    #
    # Standard Testbed Header
    #
    PAGEHEADER("Edit Experiment Metadata");

51
    echo $experiment->PageHeader();
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    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";
    }

76
    $url = CreateURL("editexp", $experiment);
77
    echo "<table align=center border=1>
78
          <form action='$url' method=post>\n";
79 80 81 82 83 84

    echo "<tr>
             <td>Description:</td>
             <td class=left>
                 <input type=text
                        name=\"formfields[description]\"
85
                        value='" . htmlspecialchars($formfields['description'],
86
						    ENT_QUOTES) . "'
87 88 89 90 91 92
	                size=30>
             </td>
          </tr>\n";

    #
    # Swapping goo.
93
    #
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    $swaplink = "$TBDOCBASE/docwrapper.php3?docname=swapping.html";

    echo "<tr>
	      <td class='pad4'>
		  <a href='${swaplink}#swapping'>Swapping:
              </td>
	      <td>
	          <table cellpadding=0 cellspacing=0 border=0>\n";
    if (ISADMIN()) {
        #
        # Batch Experiment?
        #
	echo "    <tr>
  	              <td>
                           <input type=checkbox
                                  name='formfields[idle_ignore]'
                                  value=1";

112 113
	if (isset($formfields["idle_ignore"]) &&
	    strcmp($formfields["idle_ignore"], "1") == 0) {
114 115 116 117 118 119 120 121 122
	    echo " checked='1'";
	}

	echo ">
                      </td>
                      <td>
	                   Idle Ignore
                      </td>
                  </tr>\n";
123

124 125 126 127 128 129
	echo "    <tr>
                       <td>
                          <input type='checkbox'
	                         name='formfields[swappable]'
	                         value='1'";

130
	if ($formfields["swappable"] == "1") {
131 132 133 134 135 136 137
	    echo " checked='1'";
	}
	echo ">
                      </td>
                      <td>
                          <a href='{$swaplink}#swapping'>
	                     <b>Swappable:</b></a>
138
                             This experiment can be swapped.
139 140 141 142 143 144 145
                      </td>
	              </tr>
	              <tr>
                      <td></td>
   	              <td>If not, why not (administrators option)?<br>
                          <textarea rows=2 cols=50
                                    name='formfields[noswap_reason]'>" .
146
	                    htmlspecialchars($formfields["noswap_reason"],
147
					     ENT_QUOTES) .
148 149 150 151 152 153 154 155 156
	                 "</textarea>
                      </td>
	              </tr><tr>\n";
    }
    echo "            <td>
                      <input type='checkbox'
	                     name='formfields[idleswap]'
	                     value='1'";

157
    if ($formfields["idleswap"] == "1") {
158 159 160 161 162 163 164
	echo " checked='1'";
    }
    echo ">
                  </td>
                  <td>
                      <a href='{$swaplink}#idleswap'>
	                 <b>Idle-Swap:</b></a>
165
                         Swap out this experiment after
166 167
                         <input type='text'
                                name='formfields[idleswap_timeout]'
168
		                value='" . $formfields["idleswap_timeout"] . "'
169 170 171 172 173 174 175 176 177
                                size='3'>
                         hours idle.
                  </td>
	          </tr>
	          <tr>
                  <td></td>
   	          <td>If not, why not?<br>
                      <textarea rows=2 cols=50
                                name='formfields[noidleswap_reason]'>" .
178
	                    htmlspecialchars($formfields["noidleswap_reason"],
179
					     ENT_QUOTES) .
180 181 182 183 184 185 186
	             "</textarea>
                  </td>
	          </tr><tr>
  	          <td>
                      <input type='checkbox'
		             name='formfields[autoswap]'
		             value='1' ";
187

188
    if ($formfields["autoswap"] == "1") {
189 190 191 192 193 194 195 196 197 198
	echo " checked='1'";
    }
    echo ">
                  </td>
	          <td>
                      <a href='${swaplink}#autoswap'>
	                 <b>Max. Duration:</b></a>
                      Swap out after
                        <input type='text'
                               name='formfields[autoswap_timeout]'
199
		               value='" . $formfields["autoswap_timeout"] . "'
200 201 202
                               size='3'>
                      hours, even if not idle.
                  </td>
203 204 205 206 207 208 209 210 211 212 213 214 215
                  </tr>";

    #
    # Swapout disk state saving
    # XXX requires more work, need to create/remove sigs as appropriate
    #
    if (0) {
    echo "<tr>
		  <td>
	              <input type='checkbox'
		             name='formfields[savedisk]'
	                     value='1' ";

216
    if ($formfields["savedisk"] == "1") {
217 218 219 220 221 222 223 224 225 226 227 228 229
	echo " checked='1'";
    }
    echo ">
                  </td>
                  <td>
                      <a href='${swaplink}#swapstatesave'>
		         <b>State Saving:</b></a>
                      Save disk state on swapout
		  </td>
	       </tr>";
    }

    echo "</table></td></tr>";
230 231 232 233 234 235 236 237 238

    #
    # Resource usage.
    #
    echo "<tr>
              <td class='pad4'>CPU Usage:</td>
              <td class=left>
                  <input type=text
                         name=\"formfields[cpu_usage]\"
239
                         value=\"" . $formfields["cpu_usage"] . "\"
240
	                 size=2>
241
                  (PlanetLab Nodes Only: 1 &lt= X &lt= 5)
242 243
              </td>
          </tr>\n";
244

245 246 247 248 249
    echo "<tr>
              <td class='pad4'>Mem Usage:</td>
              <td class=left>
                  <input type=text
                         name=\"formfields[mem_usage]\"
250
                         value=\"" . $formfields["mem_usage"] . "\"
251 252 253 254
	                 size=2>
                  (PlanetLab Nodes Only: 1 &lt= X &lt= 5)
              </td>
          </tr>\n";
255

256 257 258 259 260 261 262
    #
    # Batch Experiment?
    #
    echo "<tr>
  	      <td class=left colspan=2>
              <input type=checkbox name='formfields[batchmode]' value='1'";

263 264
    if (isset($formfields["batchmode"]) &&
	strcmp($formfields["batchmode"], "1") == 0) {
265 266 267 268 269 270 271 272 273 274 275
	    echo " checked='1'";
    }

    echo ">\n";
    echo "Batch Mode Experiment &nbsp;
          <font size='-1'>(See
          <a href='$TBDOCBASE/tutorial/tutorial.php3#BatchMode'>Tutorial</a>
          for more information)</font>
          </td>
          </tr>\n";

276 277 278
    #
    # Run linktest, and level. 
    # 
279
    if (STUDLY() || $EXPOSELINKTEST) {
280 281 282 283 284 285 286 287 288
    echo "<tr>
              <td><a href='$TBDOCBASE/doc/docwrapper.php3?".
	                  "docname=linktest.html'>Linktest</a> Option:</td>
              <td><select name=\"formfields[linktest_level]\">
                          <option value=0>Skip Linktest </option>\n";

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

289
	if (strcmp($formfields["linktest_level"], "$i") == 0)
290 291 292 293 294 295
	    $selected = "selected";
	
	echo "        <option $selected value=$i>Level $i - " .
	    $linktest_levels[$i] . "</option>\n";
    }
    echo "       </select>";
296
    echo "    (<a href='$TBDOCBASE/doc/docwrapper.php3?".
297
	"docname=linktest.html'><b>What is this?</b></a>)";
298 299
    echo "    </td>
          </tr>\n";
300
    }
301

302 303 304 305 306 307 308 309 310 311 312 313 314
    echo "<tr>
              <td colspan=2 align=center>
                 <b><input type=submit name=submit value=Submit></b>
              </td>
          </tr>\n";

    echo "</form>
          </table>\n";
}

#
# We might need these later for email.
#
315 316
$creator = $experiment->creator();
$swapper = $experiment->swapper();
317 318 319 320 321
$doemail = 0;

#
# Construct a defaults array based on current DB info. Used for the initial
# form, and to determine if any changes were made and to send email.
322
#
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
$defaults                      = array();
$defaults["description"]       = $experiment->description();
$defaults["idle_ignore"]       = $experiment->idle_ignore();
$defaults["batchmode"]         = $experiment->batchmode();
$defaults["swappable"]         = $experiment->swappable();
$defaults["noswap_reason"]     = $experiment->noswap_reason();
$defaults["idleswap"]          = $experiment->idleswap();
$defaults["idleswap_timeout"]  = $experiment->idleswap_timeout() / 60.0;
$defaults["noidleswap_reason"] = $experiment->noidleswap_reason();
$defaults["autoswap"]          = $experiment->autoswap();
$defaults["autoswap_timeout"]  = $experiment->autoswap_timeout() / 60.0;
$defaults["savedisk"]          = $experiment->savedisk();
$defaults["mem_usage"]         = $experiment->mem_usage();
$defaults["cpu_usage"]         = $experiment->cpu_usage();
$defaults["linktest_level"]    = $experiment->linktest_level();
338 339 340 341

#
# A couple of defaults for turning things on.
#
342 343
if (!$defaults["autoswap"]) {
     $defaults["autoswap_timeout"] = 10;
344
}
345 346
if (!$defaults["idleswap"]) {
     $defaults["idleswap_timeout"] = $idleswaptimeout;
347 348 349 350 351 352
}

#
# On first load, display initial values.
#
if (! isset($submit)) {
353
    SPITFORM($experiment, $defaults, 0);
354 355 356 357 358 359
    PAGEFOOTER();
    return;
}

#
# Otherwise, must validate and redisplay if errors. Build up a DB insert
360
# string as we go.
361 362 363 364 365 366 367
#
$errors  = array();
$inserts = array();

#
# Description
#
368 369
if (!isset($formfields["description"]) ||
    strcmp($formfields["description"], "") == 0) {
370 371 372
    $errors["Description"] = "Missing Field";
}
else {
373
    $inserts[] = "expt_name='" . addslashes($formfields["description"]) . "'";
374 375 376 377 378 379 380 381
}

#
# Swappable/Idle Ignore
# Any of these which are not "1" become "0".
#
# Idle Ignore
#
382 383 384
if (!isset($formfields["idle_ignore"]) ||
    strcmp($formfields["idle_ignore"], "1")) {
    $formfields["idle_ignore"] = 0;
385 386 387
    $inserts[] = "idle_ignore=0";
}
else {
388
    $formfields["idle_ignore"] = 1;
389 390 391 392 393 394
    $inserts[] = "idle_ignore=1";
}

#
# Swappable
#
395 396 397
if (ISADMIN() && (!isset($formfields["swappable"]) ||
    strcmp($formfields["swappable"], "1"))) {
    $formfields["swappable"] = 0;
398

399 400
    if (!isset($formfields["noswap_reason"]) ||
        !strcmp($formfields["noswap_reason"], "")) {
401

402
        if (!ISADMIN()) {
403 404 405
	    $errors["Swappable"] = "No justification provided";
        }
	else {
406
	    $formfields["noswap_reason"] = "ADMIN";
407 408
        }
    }
409
    if ($defaults["swappable"])
410 411 412
	$doemail = 1;
    $inserts[] = "swappable=0";
    $inserts[] = "noswap_reason='" .
413
	         addslashes($formfields["noswap_reason"]) . "'";
414 415 416 417 418 419 420 421
}
else {
    $inserts[] = "swappable=1";
}

#
# IdleSwap
#
422 423 424
if (!isset($formfields["idleswap"]) ||
    strcmp($formfields["idleswap"], "1")) {
    $formfields["idleswap"] = 0;
425

426 427
    if (!isset($formfields["noidleswap_reason"]) ||
	!strcmp($formfields["noidleswap_reason"], "")) {
428 429

	if (! ISADMIN()) {
430 431 432
	    $errors["IdleSwap"] = "No justification provided";
	}
	else {
433
	    $formfields["noidleswap_reason"] = "ADMIN";
434 435
	}
    }
436
    if ($defaults["idleswap"])
437 438 439 440
	$doemail = 1;
    $inserts[] = "idleswap=0";
    $inserts[] = "idleswap_timeout=0";
    $inserts[] = "noidleswap_reason='" .
441
	         addslashes($formfields["noidleswap_reason"]) . "'";
442
}
443 444 445
elseif (!isset($formfields["idleswap_timeout"]) ||
	($formfields["idleswap_timeout"] + 0) <= 0 ||
	( (($formfields["idleswap_timeout"] + 0) > $idleswaptimeout) &&
446
	  !ISADMIN()) ) {
447 448 449 450
    $errors["Idleswap"] = "Invalid time provided (0 < X <= $idleswaptimeout)";
}
else {
    $inserts[] = "idleswap=1";
451
    $inserts[] = "idleswap_timeout=" . 60 * $formfields["idleswap_timeout"];
452
    $inserts[] = "noidleswap_reason='" .
453
	         addslashes($formfields["noidleswap_reason"]) . "'";
454 455 456 457 458
}

#
# AutoSwap
#
459 460 461
if (!isset($formfields["autoswap"]) ||
    strcmp($formfields["autoswap"], "1")) {
    $formfields["autoswap"] = 0;
462 463 464
    $inserts[] = "autoswap=0";
    $inserts[] = "autoswap_timeout=0";
}
465 466
elseif (!isset($formfields["autoswap_timeout"]) ||
	($formfields["autoswap_timeout"] + 0) == 0) {
467 468 469 470
    $errors["Max Duration"] = "Invalid time provided";
}
else {
    $inserts[] = "autoswap=1";
471
    $inserts[] = "autoswap_timeout=" . 60 * $formfields["autoswap_timeout"];
472 473
}

474 475 476
#
# Swapout disk state saving
#
477 478 479
if (!isset($formfields["savedisk"]) ||
    strcmp($formfields["savedisk"], "1")) {
    $formfields["savedisk"] = 0;
480 481 482
    $inserts[] = "savedisk=0";
}
else {
483
    $formfields["savedisk"] = 1;
484 485 486
    $inserts[] = "savedisk=1";
}

487 488
#
# CPU Usage
489
#
490 491
if (isset($formfields["cpu_usage"]) &&
    strcmp($formfields["cpu_usage"], "")) {
492

493 494
    if (($formfields["cpu_usage"] + 0) < 0 ||
	($formfields["cpu_usage"] + 0) > 5) {
495 496 497
	$errors["CPU Usage"] = "Invalid (0 <= X <= 5)";
    }
    else {
498
	$inserts[] = "cpu_usage=" . $formfields["cpu_usage"];
499 500 501 502 503 504 505 506
    }
}
else {
    $inserts[] = "cpu_usage=0";
}

#
# Mem Usage
507
#
508 509
if (isset($formfields["mem_usage"]) &&
    strcmp($formfields["mem_usage"], "")) {
510

511 512
    if (($formfields["mem_usage"] + 0) < 0 ||
	($formfields["mem_usage"] + 0) > 5) {
513 514 515
	$errors["Mem Usage"] = "Invalid (0 <= X <= 5)";
    }
    else {
516
	$inserts[] = "mem_usage=" . $formfields["mem_usage"];
517 518 519 520 521 522
    }
}
else {
    $inserts[] = "mem_usage=0";
}

523 524 525
#
# Linktest level
#
526 527
if (isset($formfields["linktest_level"]) &&
    strcmp($formfields["linktest_level"], "")) {
528

529 530
    if (($formfields["linktest_level"] + 0) < 0 ||
	($formfields["linktest_level"] + 0) > 4) {
531 532 533
	$errors["Linktest Level"] = "Invalid linktest level";
    }
    else {
534
	$inserts[] = "linktest_level=" . $formfields["linktest_level"];
535 536 537 538 539 540
    }
}
else {
    $inserts[] = "linktest_level=0";
}

541 542
#
# Spit any errors before dealing with batchmode, which changes the DB.
543
#
544
if (count($errors)) {
545
    SPITFORM($experiment, $formfields, $errors);
546 547 548 549 550 551 552
    PAGEFOOTER();
    return;
}

#
# Converting the batchmode is tricky, but we can let the DB take care
# of it by requiring that the experiment not be locked, and it be in
553
# the swapped state. If the query fails, we know that the experiment
554 555
# was in transition.
#
556 557
if (!isset($formfields["batchmode"])) {
    $formfields["batchmode"] = 0;
558
}
559
if ($defaults["batchmode"] != $formfields["batchmode"]) {
560
    $success  = 0;
561

562
    if (strcmp($formfields["batchmode"], "1")) {
563
	$batchmode = 0;
564
	$formfields["batchmode"] = 0;
565 566 567
    }
    else {
	$batchmode = 1;
568
	$formfields["batchmode"] = 1;
569
    }
570
    if ($experiment->SetBatchMode($batchmode) != 0) {
571 572
	$errors["Batch Mode"] = "Experiment is running or in transition; ".
	    "try again later";
573

574
	SPITFORM($experiment, $formfields, $errors);
575 576 577 578 579 580 581 582
	PAGEFOOTER();
	return;
    }
}

#
# Otherwise, do the other inserts.
#
583 584 585 586 587 588
if ($experiment->UpdateOldStyle($inserts) != 0) {
    $errors["Updating"] = "Error updating experiment; please try again later";
    SPITFORM($experiment, $formfields, $errors);
    PAGEFOOTER();
    return;
}
589 590 591 592 593 594 595 596 597

#
# Do not send this email if the user is an administrator
# (adminmode does not matter), and is changing an expt he created
# or swapped in. Pointless email.
if ($doemail &&
    ! (ISADMINISTRATOR() &&
       (!strcmp($uid, $creator) || !strcmp($uid, $swapper)))) {

598 599
    $target_creator = $experiment->GetCreator();
    $target_swapper = $experiment->GetSwapper();
600 601 602 603 604 605 606 607
    # Needed below.
    $swappable          = $experiment->swappable();
    $idleswap           = $experiment->idleswap();
    $noswap_reason      = $experiment->noswap_reason();
    $idleswap_timeout   = $experiment->idleswap_timeout() / 60.0;
    $noidleswap_reason  = $experiment->noidleswap_reason();
    $autoswap           = $experiment->autoswap();
    $autoswap_timeout   = $experiment->autoswap_timeout() / 60.0;
608 609 610 611 612 613 614

    $user_name  = $this_user->name();
    $user_email = $this_user->email();
    $cname      = $target_creator->name();
    $cemail     = $target_creator->email();
    $sname      = $target_swapper->name();
    $semail     = $target_swapper->email();
615

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    $olds = ($defaults["swappable"] ? "Yes" : "No");
    $oldsr= $defaults["noswap_reason"];
    $oldi = ($defaults["idleswap"] ? "Yes" : "No");
    $oldit= $defaults["idleswap_timeout"];
    $oldir= $defaults["noidleswap_reason"];
    $olda = ($defaults["autoswap"] ? "Yes" : "No");
    $oldat= $defaults["autoswap_timeout"];

    $s    = ($formfields["swappable"] ? "Yes" : "No");
    $sr   = $formfields["noswap_reason"];
    $i    = ($formfields["idleswap"] ? "Yes" : "No");
    $it   = $formfields["idleswap_timeout"];
    $ir   = $formfields["noidleswap_reason"];
    $a    = ($formfields["autoswap"] ? "Yes" : "No");
    $at   = $formfields["autoswap_timeout"];
631

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
    TBMAIL($TBMAIL_OPS,"$pid/$eid swap settings changed",
	   "\nThe swap settings for $pid/$eid have changed.\n".
	   "\nThe old settings were:\n".
	   "Swappable:\t$olds\t($oldsr)\n".
	   "Idleswap:\t$oldi\t(after $oldit hrs)\t($oldir)\n".
	   "MaxDuration:\t$olda\t(after $oldat hrs)\n".
	   "\nThe new settings are:\n".
	   "Swappable:\t$s\t($sr)\n".
	   "Idleswap:\t$i\t(after $it hrs)\t($ir)\n".
	   "MaxDuration:\t$a\t(after $at hrs)\n".
	   "\nCreator:\t$creator ($cname <$cemail>)\n".
	   "Swapper:\t$swapper ($sname <$semail>)\n".
	   "\nIf it is necessary to change these settings, ".
	   "please reply to this message \nto notify the user, ".
	   "then change the settings here:\n\n".
	   "$TBBASE/showexp.php3?pid=$pid&eid=$eid\n\n".
	   "Thanks,\nTestbed WWW\n",
	   "From: $user_name <$user_email>\n".
	   "Errors-To: $TBMAIL_WWW");
}

#
# Spit out a redirect so that the history does not include a post
# in it. The back button skips over the post and to the form.
656
#
657
header("Location: " . CreateURL("showexp", $experiment));
658 659

?>