beginexp.php3 27.9 KB
Newer Older
1
<?php
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5
6
# All rights reserved.
#
7
8
9
10
11
12
13
14
15
16
17
18
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);

19
$idleswaptimeout = TBGetSiteVar("idle/threshold");
20
21
22
23
24
25
26

#
# Helper function to respit on error.
#
function RESPIT($tag, $value)
{
    global $formfields;
27

28
29
30
31
32
33
34
    $errors[$tag] = $value;
    SPITFORM($formfields, $errors);
    PAGEFOOTER();
    die("");
}

#
35
36
# Spit the form out using the array of data.
#
37
38
function SPITFORM($formfields, $errors)
{
39
40
    global $TBDB_PIDLEN, $TBDB_GIDLEN, $TBDB_EIDLEN, $TBDOCBASE;
    global $nsdata, $nsref, $projlist, $exp_nsfile;
Chad Barb's avatar
   
Chad Barb committed
41
    global $uid, $guid;
42
    global $idleswaptimeout;
43
44
45
46
47
48
49
    global $view, $view_style;

    #
    # Handle pre-defined view styles
    #
    if ($view_style == "plab") {
	$view['hide_proj'] = $view['hide_group'] = $view['hide_swap'] =
50
51
	    $view['hide_preload'] = $view['hide_batch'] = $view['quiet'] =
	    $view['plab_ns_message'] = 1;
52
    }
53

54
55
56
    PAGEHEADER("Begin a Testbed Experiment");

    if ($errors) {
Chad Barb's avatar
   
Chad Barb committed
57
58
	echo "<table class=nogrid
                     align=center border=0 cellpadding=6 cellspacing=0>
59
              <tr>
Chad Barb's avatar
   
Chad Barb committed
60
                 <th align=center colspan=2>
61
                   <font size=+1 color=red>
Chad Barb's avatar
   
Chad Barb committed
62
                      &nbsp;Oops, please fix the following errors!&nbsp;
63
64
65
66
67
68
                   </font>
                 </td>
              </tr>\n";

	while (list ($name, $message) = each ($errors)) {
	    echo "<tr>
Chad Barb's avatar
   
Chad Barb committed
69
70
71
72
                     <td align=right>
                       <font color=red>$name:&nbsp;</font></td>
                     <td align=left>
                       <font color=red>$message</font></td>
73
74
75
76
                  </tr>\n";
	}
	echo "</table><br>\n";
    }
77
    else {
78
	    
79
80
	if (!isset($nsdata) && !isset($nsref) &&
	    !isset($view['quiet'])) {
81
82
83
84
85
86
87
88
89
	echo "<p><ul>
	    <li><b>If you have an NS file:</b><br> You may want to
                <b><a href='nscheck_form.php3'>syntax check it first</a></b>
	    <li><b>If you do not have an NS file:</b><br> You may want to
		<b><a href='buildui/bui.php3'>go to the NetBuild GUI</a></b>
		to graphically create one<font size=-2>
		(<a href='$TBDOCBASE/faq.php3#UTT-NETBUILD'>Additional 
		information</a>)</font>
	     </ul></p><br>";
Chad Barb's avatar
   
Chad Barb committed
90
	} else {
91
92
93
94
95
96
97
98
	    if (isset($view['plab_ns_message'])) {
		echo "<p>To finish creating your slice, edit the following " .
		     "information if you wish, and click Submit.</p>\n";
	    } else {
		echo "<p><b>Your automatically generated NS file has been " .
		     "uploaded.</b> To finish creating your experiment, " .
		     "please fill out the following information:</p>";
	    }
Chad Barb's avatar
   
Chad Barb committed
99
        }
100
    }
101

102
103
    echo "<table align=center border=1>\n";
    echo "<form name='form1' enctype=multipart/form-data
104
105
                action=beginexp.php3 method=post>\n";

106
107
108
109
110
111
112
113
    #
    # Include view_style in a hidden field so that it gets preserved if there
    # are errors
    #
    if ($view_style) {
	echo "<input type='hidden' name='view_style' value='$view_style'>\n";
    }

114
115
116
    #
    # Select Project
    #
117
    if (isset($view['hide_proj']) && (count($projlist) == 1)) {
118
119
120
121
	# Just include the project as a hidden field - since the user has
	# only a single project, grab that project, which is the first thing
	# in $projlist
	list($project) = each($projlist);
122
123
124
125
126
	echo "<input type='hidden' name='formfields[exp_pid]' value='$project'>\n";
    } else {
	echo "<tr>
		  <td class='pad4'>Select Project:</td>
		  <td class='pad4'><select name=\"formfields[exp_pid]\">\n";
127

128
129
130
131
	# If just one project, make sure just the one option.
	if (count($projlist) != 1) {
	    echo "<option value=''>Please Select &nbsp</option>\n";
	}
132

133
134
	while (list($project) = each($projlist)) {
	    $selected = "";
135

136
137
	    if (strcmp($formfields[exp_pid], $project) == 0)
		$selected = "selected";
138

139
140
141
142
143
144
	    echo "        <option $selected value=\"$project\">
				 $project </option>\n";
	}
	echo "       </select>";
	echo "    </td>
	      </tr>\n";
145
146
    }

147
148
    #
    # Select a group
149
    #
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    if (isset($view['hide_group'])) {
	if ($formfields['group']) {
	    $group = $formfields['group'];
	} else {
	    $group = "";
	}
	echo "<input type='hidden' name='formfields[exp_gid]' value='$group'>\n";
    } else {
	echo "<tr>
		  <td class='pad4'>Group:</td>
		  <td class='pad4'><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";
		}
180
181
	    }
	}
182
183
184
185
186
	echo "     </select>
	      <font size=-1>(Must be default or correspond to selected project)
	      </font>
		 </td>
	      </tr>\n";
187
188
    }

189
190
191
192
    #
    # Name:
    #
    echo "<tr>
193
              <td class='pad4'>Name:
194
195
              <br><font size='-1'>(No blanks)</font></td>
              <td class='pad4' class=left>
196
197
198
199
200
201
202
203
204
205
206
207
                  <input type=text
                         name=\"formfields[exp_id]\"
                         value=\"" . $formfields[exp_id] . "\"
	                 size=$TBDB_EIDLEN
                         maxlength=$TBDB_EIDLEN>
              </td>
          </tr>\n";

    #
    # Description
    #
    echo "<tr>
208
              <td class='pad4'>Description:<br>
209
210
                  <font size='-1'>(A concise sentence)</font></td>
              <td class='pad4' class=left>
211
212
213
                  <input type=text
                         name=\"formfields[exp_description]\"
                         value=\"" . $formfields[exp_description] . "\"
214
	                 size=60>
215
216
217
218
219
220
221
222
              </td>
          </tr>\n";

    #
    # NS file
    #
    if (isset($nsdata)) {
        echo "<tr>
223
                  <td class='pad4'>Your auto-generated NS file: &nbsp</td>
224
                      <input type=hidden name=nsdata value=$nsdata>
225
                  <td class='pad4'><a target=_blank href=spitnsdata.php3?nsdata=$nsdata>
226
227
                      View NS File</a></td>
              </tr>\n";
Chad Barb's avatar
   
Chad Barb committed
228
    } elseif (isset($nsref)) {
Chad Barb's avatar
   
Chad Barb committed
229
230
	if (isset($guid)) {
        echo "<tr>
231
                  <td class='pad4'>Your auto-generated NS file: &nbsp</td>
Chad Barb's avatar
   
Chad Barb committed
232
233
                      <input type=hidden name=nsref value=$nsref>
                      <input type=hidden name=guid value=$guid>
234
                  <td class='pad4'><a target=_blank href=\"spitnsdata.php3?nsref=$nsref&guid=$guid\">
Chad Barb's avatar
   
Chad Barb committed
235
236
237
                      View NS File</a></td>
              </tr>\n";
        } else {
Chad Barb's avatar
   
Chad Barb committed
238
        echo "<tr>
239
                  <td class='pad4'>Your auto-generated NS file: &nbsp</td>
Chad Barb's avatar
   
Chad Barb committed
240
                      <input type=hidden name=nsref value=$nsref>
241
                  <td class='pad4'><a target=_blank href=spitnsdata.php3?nsref=$nsref>
Chad Barb's avatar
   
Chad Barb committed
242
243
                      View NS File</a></td>
              </tr>\n";
Chad Barb's avatar
   
Chad Barb committed
244
        }
245
246
    }
    else {
247

248
	echo "<tr>
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
                  <td class='pad4'>Your NS file:<br>
<script language='JavaScript'>
<!--
function SyntaxCheck() {
  window.open('','nscheck','width=600,height=400,toolbar=no,resizeable=yes,scrollbars=yes,status=yes,menubar=yes');
  document.form1.target='nscheck';
  document.form1.submit();
}
function NormalSubmit() {
  document.form1.target='_self';
  document.form1.submit();
}
//-->
</script>
		  <input type=submit disabled id=syntax name='syntax' value='Syntax Check'
		   onclick='SyntaxCheck()'>
		  </td>
266
267
268
269
270
271
272
273
274
275

                  <td><table cellspacing=0 cellpadding=0 border=0>
                    <tr>
                      <td class='pad4'>Upload<br>
			<font size='-1'>(50k&nbsp;max)</font></td>
                      <td class='pad4'>
                        <input type=hidden name=MAX_FILE_SIZE value=51200>
	                <input type=file
                               name=exp_nsfile
                               value=\"" . $exp_nsfile . "\"
276
277
	                       size=30
			       onchange=\"this.form.syntax.disabled=(this.value=='')\">
278
279
280
281
282
283
284
285
286
287
                      </td>
                    </tr><tr>
                    <td>&nbsp;&nbsp;<b>or</b></td><td></td>
                    </tr><tr>
                      <td class='pad4'>On Server<br><font size='-1'>(<code>/proj</code>,
                        <code>/groups</code>, <code>/users</code>)</font></td>
                      <td class='pad4'>
	                <input type=text
                               name=\"formfields[exp_localnsfile]\"
                               value=\"" . $formfields[exp_localnsfile] . "\"
288
289
	                       size=40
			       onchange=\"this.form.syntax.disabled=(this.value=='')\">
290
291
                      </td>
                    </tr></table></td></tr>\n";
292
293
    }

294
295
296
    #
    # Swapping
    #
297
    # Add in hidden fields to send swappable and noswap_reason, since
298
    # they do not show on the form
299
300
    echo "<input type=hidden name='formfields[exp_swappable]' value='$formfields[exp_swappable]'>\n";
    echo "<input type=hidden name='formfields[exp_noswap_reason]' value='";
301
    echo htmlspecialchars($formfields[exp_noswap_reason], ENT_QUOTES);
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
355
356
    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'>
		    <a href='$TBDOCBASE/docwrapper.php3?docname=swapping.html#swapping'>
		    Swapping:</td>
		  <td>
		  <table cellpadding=0 cellspacing=0 border=0><tr>
		  <td><input type='checkbox'
			 name='formfields[exp_idleswap]'
			 value='1'";
	if ($formfields[exp_idleswap] == "1") {
	    echo " checked='1'";
	}
	echo "></td>
		  <td><a href='$TBDOCBASE/docwrapper.php3?docname=swapping.html#idleswap'>
		  <b>Idle-Swap:</b></a> Swap out this experiment
		  after 
		  <input type='text' name='formfields[exp_idleswap_timeout]'
			 value='";
	echo htmlspecialchars($formfields[exp_idleswap_timeout], ENT_QUOTES);
	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]'>";
			      
	echo htmlspecialchars($formfields[exp_noidleswap_reason], ENT_QUOTES);
	echo "</textarea></td>
		  </tr><tr>
		  <td><input type='checkbox'
			 name='formfields[exp_autoswap]'
			 value='1' ";
	if ($formfields[exp_autoswap] == "1") {
	    echo " checked='1'";
	}
	echo "></td>
		  <td><a href='$TBDOCBASE/docwrapper.php3?docname=swapping.html#autoswap'>
		  <b>Max. Duration:</b></a> Swap out after
		  <input type='text' name='formfields[exp_autoswap_timeout]'
			 value='";
	echo htmlspecialchars($formfields[exp_autoswap_timeout], ENT_QUOTES);
	echo "' size='3'> hours, even if not idle.</td>
		  </tr></table>
		  </td>
	       </tr>";
357
    }
358
359
360
361

    #
    # Batch Experiment?
    #
362
363
364
    if (isset($view['hide_batch'])) {
	if ($formfields['exp_batched']) {
	    echo "<input type='hidden' name='formfields[exp_batched]' value='Yep'\n";
365
	}
366
367
368
369
    } else {
	echo "<tr>
		  <td class='pad4' colspan=2>
		  <input type=checkbox name='formfields[exp_batched]' value='Yep'";
370

371
372
373
374
375
376
377
378
379
380
381
382
383
	if (isset($formfields[exp_batched]) &&
	    strcmp($formfields[exp_batched], "Yep") == 0) {
		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";
    }
384

385
386
387
    #
    # Preload?
    #
388
389
390
    if (isset($view['hide_preload'])) { 
	if ($formfields['exp_preload']) {
	    echo "<input type='hidden' name='formfields[exp_preload]' value='Yep'>\n";
391
	}
392
393
394
395
    } else {
	echo "<tr>
		  <td class='pad4' colspan=2>
		      <input type=checkbox name='formfields[exp_preload]' value='Yep'";
396

397
398
399
400
401
402
403
404
405
	if (isset($formfields[exp_preload]) &&
	    strcmp($formfields[exp_preload], "Yep") == 0) {
		echo " checked='1'";
	    }

	echo ">\n";
	echo "Do Not Swap In</td>
	      </tr>\n";
    }
406
407

    echo "<tr>
408
              <td class='pad4' align=center colspan=2>
409
410
                 <b><input type=submit value=Submit name=submit
			onclick='NormalSubmit()'></b></td>
411
412
413
414
         </tr>
        </form>
        </table>\n";

415
416
417
418
419
420
421
422
423
424
    if (!isset($view['quiet'])) {
	echo "<p>
	      <h3>Handy Links:</h3>
	      <ul>
		 <li> View a <a href='showosid_list.php3'>list of OSIDs</a>
		      that are available for you to use in your NS file.</li>
		 <li> Create your own <a href='newimageid_ez.php3'>
		      custom disk images</a>.</li>
	      </ul>\n";
    }
425
426
}

Chad Barb's avatar
   
Chad Barb committed
427
428
429
430
431
if (isset($nsref)) {
    if (strcmp($nsref, "") == 0 || !ereg("^[0-9]+$", $nsref))
	unset($nsref);
}

Chad Barb's avatar
   
Chad Barb committed
432
433
434
435
436
if (isset($guid)) {
    if (strcmp($guid, "") == 0 || !ereg("^[0-9]+$", $guid))
	unset($guid);
}

437
438
439
440
441
442
#
# 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.
#
Chad Barb's avatar
   
Chad Barb committed
443

444
if (isset($nsdata)) {
445
    if (strcmp($nsdata, "") == 0)
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
	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.
#
464
if (! isset($submit) && ! isset($syntax)) {
465
466
    $defaults = array();

467
468
469
470
    #
    # For users that are in one project and one subgroup, it is usually
    # the case that they should use the subgroup, and since they also tend
    # to be in the clueless portion of our users, give them some help.
471
    #
472
473
    if (count($projlist) == 1) {
	list($project, $grouplist) = each($projlist);
474

475
	if (count($grouplist) <= 2) {
476
	    $defaults[exp_pid] = $project;
477
	    if (count($grouplist) == 1 || strcmp($project, $grouplist[0]))
478
479
480
481
482
483
		$defaults[exp_gid] = $grouplist[0];
	    else
		$defaults[exp_gid] = $grouplist[1];
	}
	reset($projlist);
    }
484
485
486
487
488
489
490
491
492

    $defaults[exp_swappable] = "1";
    $defaults[exp_noswap_reason] = "";
    $defaults[exp_idleswap]  = "1";
    $defaults[exp_noidleswap_reason] = "";
    $defaults[exp_idleswap_timeout] = "$idleswaptimeout";
    $defaults[exp_autoswap]  = "0";
    $defaults[exp_autoswap_timeout] = "10";

493
494
495
496
497
498
499
500
501
    #
    # Allow formfields that are already set to override defaults
    #
    if (isset($formfields)) {
	while (list ($field, $value) = each ($formfields)) {
	    $defaults[$field] = $formfields[$field];
	}
    }

502
503
504
505
506
507
508
509
510
511
512
513
    SPITFORM($defaults, 0);
    PAGEFOOTER();
    return;
}

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

#
# Project:
514
#
515
516
517
518
519
520
521
522
523
524
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.
525
#
526
527
528
529
530
if (!isset($formfields[exp_id]) ||
    strcmp($formfields[exp_id], "") == 0) {
    $errors["Experiment Name"] = "Missing Field";
}
else {
531
532
533
    if (! ereg("^[-a-zA-Z0-9]+$", $formfields[exp_id])) {
	$errors["Experiment Name"] =
	    "Must be alphanumeric characters only (dash okay)!";
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
    }
    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
551
# Any of these which are not "1" become "0".
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
if (!isset($formfields[exp_swappable]) ||
    strcmp($formfields[exp_swappable], "1")) {
    $formfields[exp_swappable] = 0;

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

        if (! ISADMIN($uid)) {
          $errors["Not Swappable"] = "No justification provided";
        } else {
          $formfields[exp_noswap_reason] = "ADMIN";
        }
    }
}

if (!isset($formfields[exp_idleswap]) ||
    strcmp($formfields[exp_idleswap], "1")) {
    $formfields[exp_idleswap] = 0;

    if (!isset($formfields[exp_noidleswap_reason]) ||
	!strcmp($formfields[exp_noidleswap_reason], "")) {
	if (! ISADMIN($uid)) {
	    $errors["Not Idle-Swappable"] = "No justification provided";
	} else {
	    $formfields[exp_noidleswap_reason] = "ADMIN";
	}
    }
}

if (!isset($formfields[exp_idleswap_timeout]) ||
    ($formfields[exp_idleswap_timeout] + 0) <= 0 ||
    ($formfields[exp_idleswap_timeout] + 0) > $idleswaptimeout) {
    $errors["Idleswap"] = "No or invalid time provided - must be non-zero and <= $idleswaptimeout";
}

if (!isset($formfields[exp_noidleswap_reason])) {
    $formfields[exp_noidleswap_reason] = "";
}


if (!isset($formfields[exp_autoswap]) ||
    strcmp($formfields[exp_autoswap], "1")) {
    $formfields[exp_autoswap] = 0;
} else {
    if (!isset($formfields[exp_autoswap_timeout]) ||
	($formfields[exp_autoswap_timeout] + 0) == 0) {
	$errors["Max. Duration"] = "No or invalid time provided";
    }
601
602
}

603
604
605
# None of the above errors count during a syntax check
if (isset($syntax)) { $errors = array(); }

606
607
608
#
# If any errors, respit the form with the current values and the
# error messages displayed. Iterate until happy.
609
#
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
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.
#
629
if (TBValidExperiment($exp_pid, $exp_id) && !isset($syntax)) {
630
631
632
633
634
635
636
637
638
639
    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;
}
640
if (!TBValidGroup($exp_pid, $exp_gid) && !isset($syntax)) {
Chad Barb's avatar
   
Chad Barb committed
641
    RESPIT("Group", "Group '$exp_gid' is not in project '$exp_pid'!");
642
643
644
645
646
}

#
# Verify permissions.
#
647
648
if (! TBProjAccessCheck($uid, $exp_pid, $exp_gid, $TB_PROJECT_CREATEEXPT)
    && !isset($syntax)) {
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
    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) {
668
669
670
671
672
673
674
    if (isset($syntax)) {
	echo "<h3 style='color: #f00'>Error: \n".
	    "Specified both a local and remote file!</h3>\n";
	die("");
    } else {
	RESPIT("NS File", "Specified both a local and remote file");
    }
675
676
677
678
679
}
if (!$specupload && strcmp($exp_nsfile_name, "")) {
    #
    # Catch an invalid filename.
    #
680
681
682
683
684
685
686
    if (isset($syntax)) {
	echo "<h3 style='color: #f00'>Error: \n".
	    "Local filename does not appear to be valid!</h3>\n";
	die("");
    } else {
	RESPIT("NS File", "Local filename does not appear to be valid");
    }
687
688
689
690
691
692
693
694
695
}

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.
    #
696
    # Do not allow anything outside of special dirs. I do not think there
697
698
    # is a security worry, but good to enforce it anyway.
    #
699
700
701
702
703
704
705
706
707
708
    if (!preg_match("/^([-\@\w\.\/]+)$/", $exp_localnsfile)) {
	if (isset($syntax)) {
	    echo "<h3 style='color: #f00'>Error: \n".
		"Pathname includes illegal characters!</h3>\n";
	    die("");
	}
	else {
	    RESPIT("NS File", "Pathname includes illegal characters");
	}
    }
709
    if (! ereg("^$TBPROJ_DIR/.*" ,$exp_localnsfile) &&
710
711
        ! ereg("^$TBUSER_DIR/.*" ,$exp_localnsfile) &&
        ! ereg("^$TBGROUP_DIR/.*" ,$exp_localnsfile)) {
712
713
714
715
716
717
718
719
720
721
	if (isset($syntax)) {
	    echo "<h3 style='color: #f00'>Error: \n".
		"Server resident file must be in either ".
		"$TBUSER_DIR/, $TBPROJ_DIR/, or $TBGROUP_DIR/!</h3>\n";
	    die("");
	} else {
	    RESPIT("NS File",
		   "Server resident file must be in either ".
		   "$TBUSER_DIR/, $TBPROJ_DIR/, or $TBGROUP_DIR/");
	}
722
    }
723

724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
    $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;
}
Chad Barb's avatar
   
Chad Barb committed
739
elseif (isset($nsref) && strcmp($nsref, "")) {
Chad Barb's avatar
   
Chad Barb committed
740
741
742
743
744
    if (isset($guid)) {
      $nsfile = "/tmp/$guid-$nsref.nsfile";
    } else {
      $nsfile = "/tmp/$uid-$nsref.nsfile";
    }
Chad Barb's avatar
   
Chad Barb committed
745
746
747
    $delnsfile = 1;
    $nonsfile  = 0;
}
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
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);
764
    chmod($nsfile, 0666);
765
766
767
}
else {
    #
768
769
    # I am going to allow shell experiments to be created (No NS file),
    # but only by admin types.
770
    #
771
772
773
774
775
776
777
778
    if (isset($syntax)) {
	echo "<h3 style='color: #f00'>Error: \n".
	    "You must provide an NS file!</h3>\n";
	die("");
    } else {	
	if (! ISADMIN($uid)) {
	    RESPIT("NS File", "You must provide an NS file");
	}
779
780
    }
    $nonsfile = 1;
781
782
}

783
784
785
786
787
788
789
790
791
792
793
794
#
# Syntax Check:
# If this is a syntax check, do the magic now.
# Magic below courtesy of nscheck.php3
#

if (isset($syntax)) {
    #
    # Run the script. We use a script wrapper to deal with changing
    # to the proper directory and to keep most of these details out
    # of this.
    #
795
796
    $retval = SUEXEC($uid, $TBADMINGROUP, "webnscheck $nsfile",
		     SUEXEC_ACTION_IGNORE);
797
798
799
800
801
802
803

    echo "<div align=right>\n";
    echo "<a href='' onclick='window.close()'>Close Window</a>\n</div>\n";
    echo "<center>\n";
    echo "<h2>Syntax Check Results</h2>\n";
    echo "</center>\n";

804
    if ($retval < 0) {
805
806
807
808
809
	SUEXECERROR(SUEXEC_ACTION_DIE);
        #
        # Never returns ...
        #
        die("");
810
811
    }

812
813
814
815
816
817
818
819
820
821
822
    # Parse Error.	 
    if ($retval) {
	echo "<br><br><h2>
              Parse Failure($retval): Output as follows:
              </h2>
              <br>
              <XMP>$suexec_output</XMP>\n";
	PAGEFOOTER();
	die("");
    }

823
824
825
826
827
828
829
830
    echo "<center><br>";
    echo "<br>";
    echo "<h3>Your NS file looks good!</h3>";
    echo "</center>\n";
    
    exit();
}

831
#
832
# Convert Swappable, Batched params to arguments to script.
833
#
834
835
836
837
$exp_swappable = "";

if ($formfields[exp_swappable] == "1") {
    $exp_swappable .= " -s";
838
839
}

840
841
if ($formfields[exp_autoswap] == "1") {
    $exp_swappable .= " -a " . (60 * $formfields[exp_autoswap_timeout]);
842
}
843
844

if ($formfields[exp_idleswap] == "1") {
Mac Newbold's avatar
Mac Newbold committed
845
    $exp_swappable .= " -l " . (60 * $formfields[exp_idleswap_timeout]);
846
847
}

848
849
850
851
#
# All experiments are low priority for now.
#
$exp_priority  = "-n low";
852
853
854
$exp_batched   = 0;
$exp_preload   = 0;
$batcharg      = "-i";
855

856
857
858
859
if (isset($formfields[exp_batched]) &&
    strcmp($formfields[exp_batched], "Yep") == 0) {
    $exp_batched   = 1;
    $batcharg      = "";
860
861
862
863
864
}
if (isset($formfields[exp_preload]) &&
    strcmp($formfields[exp_preload], "Yep") == 0) {
    $exp_preload   = 1;
    $batcharg     .= " -f";
865
866
}

867
#
868
# I am not spitting out a redirection yet.
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
#
PAGEHEADER("Begin a Testbed Experiment");

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

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

#
886
887
# Okay, time to do it.
#
888
889
890
891
892
893
echo "<font size=+2>Experiment <b>".
     "<a href='showproject.php3?pid=$exp_pid'>$exp_pid</a>/".
     "<a href='showexp.php3?pid=$exp_pid&eid=$exp_id'>$exp_id</a></b>\n".
     "</font>\n";
echo "<br><br>\n";

894
895
896
897
898
899
900
echo "<center><br>";
echo "<h2>Starting experiment configuration. Please wait a moment ...
      </h2></center>";

flush();

#
901
# Run the backend script.
902
903
#
# Avoid SIGPROF in child.
904
#
905
906
set_time_limit(0);

907
908
909
910
911
912
$retval = SUEXEC($uid, $unix_gid,
		 "webbatchexp $batcharg -x \"$exp_expires\" -E \"$exp_name\" ".
		 "$exp_priority $exp_swappable ".
		 "-p $exp_pid -g $exp_gid -e $exp_id ".
		 ($nonsfile ? "" : "$nsfile"),
		 SUEXEC_ACTION_IGNORE);
913
914
915
916
917

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

918
919
920
921
#
# Fatal Error. Report to the user, even though there is not much he can
# do with the error. Also reports to tbops.
# 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
922
if ($retval < 0) {
923
924
925
926
    SUEXECERROR(SUEXEC_ACTION_DIE);
    #
    # Never returns ...
    #
927
928
929
    die("");
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
930
931
932
933
934
935
936
937
938
939
# User error. Tell user and exit.
if ($retval) {
    echo "<br>";
    echo "<h3>Experiment could not be created</h3>";
    echo "<blockquote><pre>$suexec_output<pre></blockquote>";

    PAGEFOOTER();
    exit();
}

940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
# add reasons to db, since we don't want to pass these on the command line.

if ($formfields[exp_noswap_reason]) {
    DBQueryFatal("UPDATE experiments " .
		 "SET noswap_reason='" .
                 addslashes($formfields[exp_noswap_reason]).
                 "' ".
		 "WHERE eid='$exp_id' and pid='$exp_pid'");
}

if ($formfields[exp_noidleswap_reason]) {
    DBQueryFatal("UPDATE experiments " .
		 "SET noidleswap_reason='" .
                 addslashes($formfields[exp_noidleswap_reason]).
                 "' ".
		 "WHERE eid='$exp_id' and pid='$exp_pid'");
}

958
959
echo "<br><br>";
echo "<font size=+1>
960
        Your experiment is configuring!<br><br>\n";
961

962
963
964
965
966
967
968
969
if ($nonsfile) {
    echo "Since you did not provide an NS script, no nodes have been
          allocated. You will not be able to modify or swap this experiment,
          nor do most other neat things you can do with a real experiment.
          <br><br>
          If this bothers you, <b>SUPPLY AN NS FILE!</b>\n";
}
elseif ($exp_preload) {
970
971
972
973
    echo "Since you are only pre-loading the experiment, this will typically
          take less than one minute. If you do not receive email notification
          within a reasonable amount of time, please contact $TBMAILADDR.<br>
          <br>
974
          While you are waiting, you can watch the log
975
976
977
          in <a target=_blank href=spewlogfile.php3?pid=$exp_pid&eid=$exp_id>
          realtime</a>.\n";
}
978
979
980
981
982
elseif ($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
983
984
985
986
987
988
          reasonable amount of time, please contact $TBMAILADDR.
          <br>
          <br>
          While you are waiting, you can watch the log of start attempts
          in <a target=_blank href=spewlogfile.php3?pid=$exp_pid&eid=$exp_id>
          realtime</a>.\n";
989
}
990
991
992
993
994
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
995
996
997
          of time, please contact $TBMAILADDR.<br>
          <br>
          While you are waiting, you can watch the log of experiment creation
998
          in <a target=_blank href=spewlogfile.php3?pid=$exp_pid&eid=$exp_id>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
999
          realtime</a>.\n";
1000
}