editexp.php3 18.7 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
    #
    # Standard Testbed Header
    #
49
    PAGEHEADER("Edit Experiment Settings");
50

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
elseif (!isset($formfields["idleswap_timeout"]) ||
Russ Fish's avatar
Russ Fish committed
444
	!preg_match("/^[\d]+$/", $formfields["idleswap_timeout"]) ||
445
446
	($formfields["idleswap_timeout"] + 0) <= 0 ||
	( (($formfields["idleswap_timeout"] + 0) > $idleswaptimeout) &&
447
	  !ISADMIN()) ) {
448
449
450
451
    $errors["Idleswap"] = "Invalid time provided (0 < X <= $idleswaptimeout)";
}
else {
    $inserts[] = "idleswap=1";
452
    $inserts[] = "idleswap_timeout=" . 60 * $formfields["idleswap_timeout"];
453
    $inserts[] = "noidleswap_reason='" .
454
	         addslashes($formfields["noidleswap_reason"]) . "'";
455
456
457
458
459
}

#
# AutoSwap
#
460
461
462
if (!isset($formfields["autoswap"]) ||
    strcmp($formfields["autoswap"], "1")) {
    $formfields["autoswap"] = 0;
463
464
465
    $inserts[] = "autoswap=0";
    $inserts[] = "autoswap_timeout=0";
}
466
elseif (!isset($formfields["autoswap_timeout"]) ||
Russ Fish's avatar
Russ Fish committed
467
	!preg_match("/^[\d]+$/", $formfields["autoswap_timeout"]) ||
468
	($formfields["autoswap_timeout"] + 0) == 0) {
469
470
471
472
    $errors["Max Duration"] = "Invalid time provided";
}
else {
    $inserts[] = "autoswap=1";
473
    $inserts[] = "autoswap_timeout=" . 60 * $formfields["autoswap_timeout"];
474
475
}

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

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

Russ Fish's avatar
Russ Fish committed
495
496
497
498
    if (!preg_match("/^[\d]+$/", $formfields["cpu_usage"])) {
	$errors["CPU Usage"] = "Invalid character";
    }
    elseif (($formfields["cpu_usage"] + 0) < 0 ||
499
	($formfields["cpu_usage"] + 0) > 5) {
500
501
502
	$errors["CPU Usage"] = "Invalid (0 <= X <= 5)";
    }
    else {
503
	$inserts[] = "cpu_usage=" . $formfields["cpu_usage"];
504
505
506
507
508
509
510
511
    }
}
else {
    $inserts[] = "cpu_usage=0";
}

#
# Mem Usage
512
#
513
514
if (isset($formfields["mem_usage"]) &&
    strcmp($formfields["mem_usage"], "")) {
515

Russ Fish's avatar
Russ Fish committed
516
517
518
519
    if (!preg_match("/^[\d]+$/", $formfields["mem_usage"])) {
	$errors["Mem Usage"] = "Invalid character";
    }
    elseif (($formfields["mem_usage"] + 0) < 0 ||
520
	($formfields["mem_usage"] + 0) > 5) {
521
522
523
	$errors["Mem Usage"] = "Invalid (0 <= X <= 5)";
    }
    else {
524
	$inserts[] = "mem_usage=" . $formfields["mem_usage"];
525
526
527
528
529
530
    }
}
else {
    $inserts[] = "mem_usage=0";
}

531
532
533
#
# Linktest level
#
534
535
if (isset($formfields["linktest_level"]) &&
    strcmp($formfields["linktest_level"], "")) {
536

Russ Fish's avatar
Russ Fish committed
537
538
539
540
    if (!preg_match("/^[\d]+$/", $formfields["linktest_level"])) {
	$errors["Linktest Level"] = "Invalid character";
    }
    elseif (($formfields["linktest_level"] + 0) < 0 ||
541
	($formfields["linktest_level"] + 0) > 4) {
Russ Fish's avatar
Russ Fish committed
542
	$errors["Linktest Level"] = "Invalid (0 <= X <= 4)";
543
544
    }
    else {
545
	$inserts[] = "linktest_level=" . $formfields["linktest_level"];
546
547
548
549
550
551
    }
}
else {
    $inserts[] = "linktest_level=0";
}

552
553
#
# Spit any errors before dealing with batchmode, which changes the DB.
554
#
555
if (count($errors)) {
556
    SPITFORM($experiment, $formfields, $errors);
557
558
559
560
561
562
563
    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
564
# the swapped state. If the query fails, we know that the experiment
565
566
# was in transition.
#
567
568
if (!isset($formfields["batchmode"])) {
    $formfields["batchmode"] = 0;
569
}
570
if ($defaults["batchmode"] != $formfields["batchmode"]) {
571
    $success  = 0;
572

573
    if (strcmp($formfields["batchmode"], "1")) {
574
	$batchmode = 0;
575
	$formfields["batchmode"] = 0;
576
577
578
    }
    else {
	$batchmode = 1;
579
	$formfields["batchmode"] = 1;
580
    }
581
    if ($experiment->SetBatchMode($batchmode) != 0) {
582
583
	$errors["Batch Mode"] = "Experiment is running or in transition; ".
	    "try again later";
584

585
	SPITFORM($experiment, $formfields, $errors);
586
587
588
589
590
591
592
593
	PAGEFOOTER();
	return;
    }
}

#
# Otherwise, do the other inserts.
#
594
595
596
597
598
599
if ($experiment->UpdateOldStyle($inserts) != 0) {
    $errors["Updating"] = "Error updating experiment; please try again later";
    SPITFORM($experiment, $formfields, $errors);
    PAGEFOOTER();
    return;
}
600
601
602
603
604
605
606
607
608

#
# 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)))) {

609
610
    $target_creator = $experiment->GetCreator();
    $target_swapper = $experiment->GetSwapper();
611
612
613
614
615
616
617
618
    # 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;
619
620
621
622
623
624
625

    $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();
626

627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
    $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"];
642

643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
    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.
667
#
668
header("Location: " . CreateURL("showexp", $experiment));
669
670

?>