beginexp.php3 19.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
include("defs.php3");

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

#
# Only known and logged in users can begin experiments.
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);

#
# Default priorities Needs to move someplace else!
# 
$priorities         = array();
$priorities["high"] = "high";
$priorities["low"]  = "low";

#
# Helper function to respit on error.
#
function RESPIT($tag, $value)
{
    global $formfields;
    
    $errors[$tag] = $value;
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    die("");
}

#
# Spit the form out using the array of data. 
# 
function SPITFORM($formfields, $errors)
{
    global $TBDB_PIDLEN, $TBDB_GIDLEN, $TBDB_EIDLEN;
    global $nsdata, $projlist, $priorities, $exp_nsfile;
    
    PAGEHEADER("Begin a Testbed Experiment");

    if ($errors) {
	echo "<table align=center border=0 cellpadding=0 cellspacing=2>
              <tr>
                 <td nowrap align=center colspan=3>
                   <font size=+1 color=red>
                      Oops, please fix the following errors!
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
	    echo "<tr>
                     <td align=right><font color=red>$name:</font></td>
                     <td>&nbsp &nbsp</td>
                     <td align=left><font color=red>$message</font></td>
                  </tr>\n";
	}
	echo "</table><br>\n";
    }

    echo "<table align=center border=1> 
          <tr>
              <td align=center colspan=3>
                 <em>(Fields marked with * are required)</em>
              </td>
          </tr>\n";

    echo "<form enctype=multipart/form-data
                action=beginexp.php3 method=post>\n";

    #
    # Select Project
    #
    echo "<tr>
              <td colspan=2>*Select Project:</td>
              <td><select name=\"formfields[exp_pid]\">
                      <option value=''>Please Select &nbsp</option>\n";
    
    while (list($project) = each($projlist)) {
	$selected = "";

	if (strcmp($formfields[exp_pid], $project) == 0)
	    $selected = "selected";
	
	echo "        <option $selected value=\"$project\">
                             $project </option>\n";
    }
    echo "       </select>";
    echo "    </td>
          </tr>\n";

    #
    # Name:
    #
    echo "<tr>
              <td colspan=2>*Name (no blanks):</td>
              <td class=left>
                  <input type=text
                         name=\"formfields[exp_id]\"
                         value=\"" . $formfields[exp_id] . "\"
	                 size=$TBDB_EIDLEN
                         maxlength=$TBDB_EIDLEN>
              </td>
          </tr>\n";

    #
    # Description
    #
    echo "<tr>
              <td colspan=2>*Description:<br>
                  (a short pithy sentence)</td>
              <td class=left>
                  <input type=text
                         name=\"formfields[exp_description]\"
                         value=\"" . $formfields[exp_description] . "\"
	                 size=50>
              </td>
          </tr>\n";

    #
    # NS file
    #
    if (isset($nsdata)) {
        echo "<tr>
                  <td colspan=2>*Your Auto Generated NS file: &nbsp</td>
                      <input type=hidden name=nsdata value=$nsdata>
                  <td><a target=_blank href=spitnsdata.php3?nsdata=$nsdata>
                      View NS File</a></td>
              </tr>\n";
    }
    else {
	echo "<tr>
                  <td rowspan>*Your NS file: &nbsp</td>

138
                  <td rowspan><center>Upload (20K max)[<b>1</b>]<br>
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
                                   <br>
                                   Or<br>
                                   <br>
                               On Server (/proj or /users)
                              </center></td>

                  <td rowspan>
                      <input type=hidden name=MAX_FILE_SIZE value=20000>
	              <input type=file
                             name=exp_nsfile
                             value=\"" . $exp_nsfile . "\"
	                     size=40>
                      <br>
                      <br>
	              <input type=text
                             name=\"formfields[exp_localnsfile]\"
                             value=\"" . $formfields[exp_localnsfile] . "\"
	                     size=50>
                  </td>
              </tr>\n";	
    }

    #
    # Swappable?
    # 
    echo "<tr>
165
  	      <td colspan=2>Swappable?[<b>2</b>]:</td>
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
              <td class=left>
                  <input type=checkbox
                         name=\"formfields[exp_swappable]\"
                         value=Yep";

    if (isset($formfields[exp_swappable]) &&
	strcmp($formfields[exp_swappable], "Yep") == 0)
	echo "           checked";
	
    echo "               > Yes
              </td>
          </tr>\n";

    #
    # Priority
    #
    echo "<tr>
183
	      <td colspan=2>Priority[<b>3</b>]:</td>
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
              <td class=left>\n";

    reset($priorities);
    while (list ($prio, $value) = each($priorities)) {
	$checked = "";
	
	if (isset($formfields["exp_priority"]) &&
	    ! strcmp($formfields["exp_priority"], $prio))
	    $checked = "checked";

	echo "<input $checked type=radio value=$prio
                     name=\"formfields[exp_priority]\">
                     $prio &nbsp\n";
    }
    echo "    </td>
          </tr>\n";

    #
    # Select a group
    # 
    echo "<tr>
205
              <td colspan=2>Group[<b>4</b>]:</td>
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
              <td><select name=\"formfields[exp_gid]\">
                    <option value=''>Default Group </option>\n";

    reset($projlist);
    while (list($project, $grouplist) = each($projlist)) {
	for ($i = 0; $i < count($grouplist); $i++) {
	    $group    = $grouplist[$i];

	    if (strcmp($project, $group)) {
		$selected = "";

		if (isset($formfields[exp_gid]) &&
		    isset($formfields[exp_pid]) &&
		    strcmp($formfields[exp_pid], $project) == 0 &&
		    strcmp($formfields[exp_gid], $group) == 0)
		    $selected = "selected";
		
		echo "<option $selected value=\"$group\">
                           $project/$group</option>\n";
	    }
	}
    }
    echo "     </select>
             </td>
          </tr>\n";

    #
    # Batch Experiment?
    #
    echo "<tr>
236
  	      <td colspan=2>Batch Experiment?[<b>5</b>]:</td>
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
              <td class=left>
                  <input type=checkbox
                         name=\"formfields[exp_batched]\"
                         value=Yep";

	if (isset($formfields[exp_batched]) &&
	    strcmp($formfields[exp_batched], "Yep") == 0)
	    echo "           checked";
	
	echo "                       > Yes
                  </td>
              </tr>\n";

    echo "<tr>
              <td align=center colspan=3>
                 <b><input type=submit value=Submit name=submit></b></td>
         </tr>
        </form>
        </table>\n";

    echo "<blockquote><blockquote><blockquote>
          <ol>
259
260
261
262
             <li>Note to <a href=http://www.opera.com><b>Opera 5</b></a> users:
                 The file upload mechanism is broken in Opera, so you cannot
                 specify a local file for upload. Instead, please specify a
                 file that is resident on the server. 
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
             <li>Check if your experiment can be swapped out and swapped back 
	         in without harm to your experiment. Useful for scheduling when
	         resources are tight. More information on swapping
	         is contained in the 
	         <a href='$TBDOCBASE/faq.php3#UTT-Swapping'>Emulab FAQ</a>.
             <li>You get brownie points for marking your experiments as Low
                 Priority, which indicates that we can swap you out before high
	         priority experiments.
             <li>Leave as the default group, or pick a subgroup that
                 corresponds to the project you selected.
             <li>Check this if you want to create a
                 <a href='$TBDOCBASE/tutorial/tutorial.php3#BatchMode'>
                 batch mode</a> experiment.
         </ol>
         </blockquote></blockquote></blockquote>\n";

    echo "<p><blockquote>
          <ul>
281
282
283
284
	     <li> Looking for a
                  <a href='$TBDOCBASE/faq.php3#UTT-NETBUILD'>GUI</a>
                  to help you create your topology?
                  <a href='buildui/bui.php3'>Check it out!</a>
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
             <li> Please <a href='nscheck_form.php3'>syntax check</a> your NS
                  file first!
             <li> You can view a <a href='showosid_list.php3'>list of OSIDs</a>
                  that are available for you to use in your NS file.
             <li> Create your own <a href='newimageid_explain.php3'>
                  custom disk images</a>.
          </ul>
          </blockquote>\n";
}

#
# See if nsdata was provided. Clear it if an empty string, otherwise
# reencode it *only* if not from the form. It appears that php decodes
# it for you when it comes in as GET argument, but leaves it encoded
# when its a POST argument.
#
if (isset($nsdata)) {
    if (strcmp($nsdata, "") == 0) 
	unset($nsdata);
    elseif (! isset($submit))
	$nsdata = rawurlencode($nsdata);
}

#
# See what projects the uid can create experiments in. Must be at least one.
#
$projlist = TBProjList($uid, $TB_PROJECT_CREATEEXPT);

if (! count($projlist)) {
    USERERROR("You do not appear to be a member of any Projects in which ".
	      "you have permission to create new experiments.", 1);
}

#
# On first load, display a virgin form and exit.
#
if (! isset($submit)) {
    $defaults = array();

    $defaults[exp_swappable] = "Yep";
    $defaults[exp_priority]  = "low";
    SPITFORM($defaults, 0);
    PAGEFOOTER();
    return;
}

#
# Otherwise, must validate and redisplay if errors
#
$errors = array();

#
# Project:
# 
if (!isset($formfields[exp_pid]) ||
    strcmp($formfields[exp_pid], "") == 0) {
    $errors["Project"] = "Not Selected";
}
elseif (!TBValidProject($formfields[exp_pid])) {
    $errors["Project"] = "No such project";
}

#
# EID.
# 
if (!isset($formfields[exp_id]) ||
    strcmp($formfields[exp_id], "") == 0) {
    $errors["Experiment Name"] = "Missing Field";
}
else {
355
356
357
    if (! ereg("^[-a-zA-Z0-9]+$", $formfields[exp_id])) {
	$errors["Experiment Name"] =
	    "Must be alphanumeric characters only (dash okay)!";
358
359
360
361
362
363
364
365
366
367
368
369
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
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
698
699
700
701
702
703
704
705
706
707
708
    }
    elseif (strlen($formfields[exp_id]) > $TBDB_EIDLEN) {
	$errors["Experiment Name"] =
	    "Too long! Must be less than or equal to $TBDB_EIDLEN";
    }
}

#
# Description
#
if (!isset($formfields[exp_description]) ||
    strcmp($formfields[exp_description], "") == 0) {
    $errors["Description"] = "Missing Field";
}

#
# Swappable
#
if (isset($formfields[exp_swappable]) &&
    strcmp($formfields[exp_swappable], "") &&
    strcmp($formfields[exp_swappable], "Yep")) {
    $errors["Swappable"] = "Bad Value";
}

#
# Priority
#
if (isset($formfields[exp_priority]) &&
    strcmp($formfields[exp_priority], "") &&
    ! isset($priorities[$formfields[exp_priority]])) {
    $errors["Priority"] = "Bad Value";
}

#
# If any errors, respit the form with the current values and the
# error messages displayed. Iterate until happy.
# 
if (count($errors)) {
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    return;
}

$exp_name    = addslashes($formfields[exp_description]);
$exp_pid     = $formfields[exp_pid];
$exp_id      = $formfields[exp_id];
if (isset($formfields[exp_gid])) {
    $exp_gid = $formfields[exp_gid];
}
if (isset($formfields[exp_localnsfile])) {
    $exp_localnsfile = $formfields[exp_localnsfile];
}

#
# Continue with error checks.
#
if (TBValidExperiment($exp_pid, $exp_id)) {
    RESPIT("Experiment Name", "Already in use");
}

#
# Check group. If none specified, then use default group.
#
if (!isset($exp_gid) ||
    strcmp($exp_gid, "") == 0) {
    $exp_gid = $exp_pid;
}
if (!TBValidGroup($exp_pid, $exp_gid)) {
    RESPIT("Group", "No such Group");
}

#
# Verify permissions.
#
if (! TBProjAccessCheck($uid, $exp_pid, $exp_gid, $TB_PROJECT_CREATEEXPT)) {
    RESPIT("Project/Group", "Not enough permission to create experiment");
}

#
# Not allowed to specify both a local and an upload!
#
$speclocal  = 0;
$specupload = 0;
$delnsfile  = 0;

if (isset($exp_localnsfile) && strcmp($exp_localnsfile, "")) {
    $speclocal = 1;
}
if (isset($exp_nsfile) && strcmp($exp_nsfile, "") &&
    strcmp($exp_nsfile, "none")) {
    $specupload = 1;
}

if ($speclocal && $specupload) {
    RESPIT("NS File", "Specified both a local and remote file");
}
if (!$specupload && strcmp($exp_nsfile_name, "")) {
    #
    # Catch an invalid filename.
    #
    RESPIT("NS File", "Local filename does not appear to be valid");
}

if ($speclocal) {
    #
    # No way to tell from here if this file actually exists, since
    # the web server runs as user nobody. The startexp script checks
    # for the file before going to ground, so the user will get immediate
    # feedback if the filename is bogus.
    #
    # Do not allow anything outside of /users or /proj. I do not think there
    # is a security worry, but good to enforce it anyway.
    #
    if (! ereg("^$TBPROJ_DIR/.*" ,$exp_localnsfile) &&
        ! ereg("^$TBUSER_DIR/.*" ,$exp_localnsfile)) {
	RESPIT("NS File",
	       "Server resident file must be in either ".
	       "$TBUSER_DIR/ or $TBPROJ_DIR/");
    }
    
    $nsfile = $exp_localnsfile;
    $nonsfile = 0;
}
elseif ($specupload) {
    #
    # XXX
    # Set the permissions on the NS file so that the scripts can get to it.
    # It is owned by nobody, and most likely protected. This leaves the
    # script open for a short time. A potential security hazard we should
    # deal with at some point.
    #
    chmod($exp_nsfile, 0666);
    $nsfile = $exp_nsfile;
    $nonsfile = 0;
}
elseif (isset($nsdata) && strcmp($nsdata, "")) {
    #
    # The NS file is encoded in the URL. Must create a temp file
    # to hold it, and pass through to the backend.
    #
    # XXX Hard to believe, but there is no equiv of mkstemp in php3.
    #
    $nsfile    = tempnam("/tmp", "$exp_pid-$exp_id.nsfile.");
    $delnsfile = 1;
    $nonsfile  = 0;

    if (! ($fp = fopen($nsfile, "w"))) {
	TBERROR("Could not create temporary file $nsfile", 1);
    }
    fwrite($fp, urldecode($nsdata));
    fclose($fp);
}
else {
    #
    # I am going to allow shell experiments to be created (No NS file),
    # but only by admin types.
    #
    if (! ISADMIN($uid)) {
	RESPIT("NS File", "You must provide an NS file");
    }
    $nonsfile = 1;
}

#
# Convert Priority, Swappable, Batched params to arguments to script.
#
if (isset($formfields[exp_swappable]) &&
    strcmp($formfields[exp_swappable], "Yep") == 0) {
    $exp_swappable = "-s";
}
else {
    $exp_swappable = "";
}

if (! isset($formfields[exp_priority]) ||
    strcmp($formfields[exp_priority], "") == 0) {
    $exp_priority  = "-n low";
}
else {
    $exp_priority  = "-n " . $priorities[$formfields[exp_priority]];
}

if (isset($formfields[exp_batched]) &&
    strcmp($formfields[exp_batched], "Yep") == 0) {
    $exp_batched   = 1;
    $batcharg      = "";
}
else {
    $exp_batched   = 0;
    $batcharg      = "-i";
}

#
# I'm not spitting out a redirection yet.
#
PAGEHEADER("Begin a Testbed Experiment");

#
# We need the email address for messages below.
#
TBUserInfo($uid, $user_name, $user_email);

#
# Set the experiment state bit to "new".
#
$exp_state   = "new";

# Shared experiments. (Deprecated for now!)
$exp_shared  = 0;

# Expiration is hardwired for now.
$exp_expires = date("Y:m:d", time() + (86400 * 120));

#
# Grab the unix GID for running scripts.
#
TBGroupUnixInfo($exp_pid, $exp_gid, $unix_gid, $unix_name);

#
# If an experiment "shell" give some warm fuzzies and be done with it.
# The user is responsible for running the tb scripts on his/her own!
# The user will need to come back and terminate the experiment though
# to clear it out of the database.
#
if ($nonsfile) {
    #
    # Stub Experiment record. Should do this elsewhere?
    #
    DBQueryFatal("INSERT INTO experiments ".
		 "(eid, pid, gid, expt_created, expt_expires, expt_name, ".
		 "expt_head_uid, state, shared) ".
		 "VALUES ('$exp_id', '$exp_pid', '$exp_gid', now(), ".
		 "        '$exp_expires', ".
		 "        '$exp_name', '$uid', '$exp_state', $exp_shared)");

    #
    # This is where the experiment hierarchy is going to be created.
    #
    $dirname = "$TBPROJ_DIR/$exp_pid/exp/$exp_id";
    
    SUEXEC($uid, $unix_gid, "mkexpdir $exp_pid $exp_gid $exp_id", 0);

    echo "<center><br>
          <h2>Experiment Configured!</h2>
          </center><br><br>
          <h3>
          The ID for your experiment in project `$exp_pid' is `$exp_id.'
          <br><br>
          Since you did not provide an NS script, no nodes have been
          allocated. You must log in and run the tbsetup scripts
          yourself. For your convenience, we have created a directory
          hierarchy on the control node: $dirname
          </h3>\n";

    if (1) {
	TBMAIL("$user_name '$uid' <$user_email>", 
	     "New Experiment Created: $exp_pid/$exp_id",
	     "User:        $uid\n".
             "EID:         $exp_id\n".
             "PID:         $exp_pid\n".
             "GID:         $exp_gid\n".
             "Name:        $exp_name\n".
             "Expires:     $exp_expires\n",
             "From: $TBMAIL_WWW\n".
	     "Bcc: $TBMAIL_LOGS\n".
             "Errors-To: $TBMAIL_WWW");
    }
    #
    # Standard Testbed Footer
    # 
    PAGEFOOTER();
    die("");
}

echo "<center><br>";
echo "<h2>Starting experiment configuration. Please wait a moment ...
      </h2></center>";

flush();

#
# Run the scripts. We use a script wrapper to deal with changing
# to the proper directory and to keep most of these details out
# of this. The user will be notified later. Its possible that the
# script will return non-zero status, but there are just a couple
# of conditions. Generally, the script exits and the user is told
# of errors later. 
#
$output = array();
$retval = 0;
$last   = time();

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

$result = exec("$TBSUEXEC_PATH $uid $unix_gid ".
	       "webbatchexp $batcharg -x \"$exp_expires\" -E \"$exp_name\" ".
	       "$exp_priority $exp_swappable ".
	       "-p $exp_pid -g $exp_gid -e $exp_id $nsfile",
 	       $output, $retval);

if ($delnsfile) {
    unlink($nsfile);
}

if ($retval) {
    echo "<br><br><h2>
          Setup Failure($retval): Output as follows:
          </h2>
          <br>
          <XMP>\n";
          for ($i = 0; $i < count($output); $i++) {
              echo "$output[$i]\n";
          }
    echo "</XMP>\n";
    
    PAGEFOOTER();
    die("");
}

echo "<br><br>";
echo "<font size=+1>
        Experiment
        <a href='showexp.php3?pid=$exp_pid&eid=$exp_id'>$exp_id</a>
        in project <A href='showproject.php3?pid=$exp_pid'>$exp_pid</A>
        is configuring!<br><br>\n";

if ($exp_batched) {
    echo "Batch Mode experiments will be run when enough resources become
          available. This might happen immediately, or it may take hours
	  or days. You will be notified via email when the experiment has
          been run. If you do not receive email notification within a
          reasonable amount of time, please contact $TBMAILADDR.\n";
}
else {
    echo "You will be notified via email when the experiment has been fully
	  configured and you are able to proceed. This typically takes less
          than 10 minutes, depending on the number of nodes you have requested.
          If you do not receive email notification within a reasonable amount
          of time, please contact $TBMAILADDR.\n";
}
echo "<br>
      </font>\n";

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