plab_ez.php3 14.7 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
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
<?php
#
# EMULAB-COPYRIGHT
# Copyright (c) 2003 University of Utah and the Flux Group.
# All rights reserved.
#
include("defs.php3");

#
# The program we use to make the NS file, and the template we use
#
$NSGEN         = "webnsgen";
$PLAB_TEMPLATE = "$TBDIR/etc/nsgen/planetlab.xml";

#
# Figure out which view to present
#
if (isset($advanced) || $advanced || (isset($submit)
	&& ($submit != "Create it"))) {
    $advanced = 1;
}

#
# Only known and logged in users can get this page
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid,0,"$TBBASE/login_plab.php3?refer=1");

#
# The reason for this apparently redundant pair of arrays is so that we can
# keep things in order - I don't beleive PHP's each() guarantees any order
# when using strings for array indices
#
$plab_types = array('pcplab', 'pcplabdsl', 'pcplabinet', 'pcplabintl',
    'pcplabinet2');
$plab_type_descr = array('pcplab' => 'Any PlanetLab node',
			 'pcplabdsl' => 'Nodes on DSL lines',
			 'pcplabinet' => 'Commodity Internet, North America',
		         'pcplabintl' => 'Outisde North America',
			 'pcplabinet2' => 'Internet2');

#
# Spit out the form
#
function SPITFORM($advanced,$formfields, $errors = array()) {
    global $TBBASE;
    global $plab_types, $plab_type_descr;

    #
    # Header/footer view options
    #
    if ($advanced) {
	PAGEHEADER("Create a Slice on PlanetLab - Advanced Form");
    } else {
	PAGEHEADER("Create a Slice on PlanetLab");
    }

58
59
60
61
62
63
64
65
66
67
68
69
70
71
    #
    # If the user is not allowed to use planetlab nodes, print out a messages
    # telling them so
    #
    if (!NODETYPE_ALLOWED("pcplab")) {
	global $TBMAILADDR;
	echo "<p><b><font color=\"red\">NOTE:</font> You do not currently " .
	     "have permission to use PlanetLab nodes through Emulab. Please " .
	     "have your project leader contact $TBMAILADDR to request ".
	     "permission for your project to create PlanetLab slices. " .
	     "</b></p>\n";
    }


Robert Ricci's avatar
Robert Ricci committed
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
138
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
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
355
356
357
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
    #
    # Display any errors
    #
    if (count($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";
    }

    #
    # The actual form
    #
    echo "<form name='form1' action='plab_ez.php3' method='get'>\n";
    echo "<table align=center border=1>\n";

    #
    # Header
    #
    if ($advanced) {
	echo "<tr><th colspan=2>Basic Options</th></tr>\n";
    } else {
	echo "<tr><th colspan=2>Create a Slice on
	      <a href='http://www.planet-lab.org'>PlanetLab</a></th></tr>\n";
    }

    #
    # How many nodes
    #
    if ($formfields[count]) {
	$nnodes = $formfields[count];
    } else {
	$nnodes = 10;
    }

    #
    # Grab a list of how many are available
    #
    $plab_counts = TBPlabAvail();

    #
    # Spit out some JavaScript that can be used to to put the number of nodes
    # into the input box
    #
    echo "<script language='JavaScript'> <!--
	function AllNodes() {
	    document.form1['formfields[resusage]'].value = '1';\n";
    while (list ($type,$counts) = each($plab_counts)) {
	echo "if (document.form1['formfields[type]'].value == '$type') { 
	    document.form1['formfields[count]'].value = '$counts[0]';
	}\n";
    }
    echo "
	}
	function AllSites() {
	    document.form1['formfields[resusage]'].value = '1';\n";
    reset($plab_counts);
    while (list ($type,$counts) = each($plab_counts)) {
	echo "if (document.form1['formfields[type]'].value == '$type') { 
	    document.form1['formfields[count]'].value = '$counts[1]';
	}\n";
    }
    echo "
	}
	//-->
    </script>\n";

    echo "<tr>
	     <td>Number of
	       <a href='plab_ez_footnote8.html'
	          target='emulabfootnote'>nodes</a></td>
	     <td><nobr>
		 <input type='text' name='formfields[count]'
		         value='$nnodes' size=4>\n";
    echo "or
		  <input type=button id=allnodes name='allnodes'
		         value='All available nodes' onclick='AllNodes()'>
		  or
		  <input type=button id=allsites name='allsites'
		         value='One node at each site' onclick='AllSites()'>\n";
    echo "
		   </nobr></td>
	 </tr>\n";

     #
     # These will appear on the advanced form, but we need to default them on
     # the short version
     #
     if (!$advanced) {
	 echo "<input type='hidden' name='formfields[canfail]' value='Yep'>\n";
	 echo "<input type='hidden' name='formfields[type]' value='pcplab'>\n";
	 echo "<input type='hidden' name='formfields[resusage]' value='3'>\n";
     }

    #
    # Advanced options
    #
    if ($advanced) {
	echo "<tr>
		 <th colspan=2>Advanced Options</th>
	      </tr>\n";

	#
	# Type
	#
	echo "<tr>
	         <td>Type of PlanetLab nodes:</td>
		 <td>
		    <select name='formfields[type]'>\n";
	while (list($junk,$type) = each($plab_types)) {
	    $descr = $plab_type_descr[$type];
	    list($acount,$asites) = $plab_counts[$type];
	    if ($formfields[type] && ($formfields[type] == $type)) {
		$selected = "selected";
	    } else {
		$selected = "";
	    }
	    echo "<option value='$type' $selected>$descr " .
	         "($acount available at $asites sites)</option>\n";
	}
	echo "
		    </select>
		 </td>
	      </tr>\n";

	#
	# Resource usage
	# Boy, keeping the previous value in drop down boxes sure is ugly!
	#
	echo "<tr>
	         <td>Estimated
		    <a href='plab_ez_footnote1.html' target='emulabfootnote'>
		    CPU and memory use</a>:</td>
		 <td>
		    <select name='formfields[resusage]'>
		    <option value='5'";
	if ($formfields[resusage] == 5) { echo " selected"; }
	echo ">Very High</option>
		    <option value='4'";
	if ($formfields[resusage] == 4) { echo " selected"; }
	echo ">High</option>
		    <option value='3'";
	if (!$formfields[resusage] || $formfields[resusage] == 3) {
	    echo " selected";
	}
	echo ">Medium</option>
		    <option value='2'";
	if ($formfields[resusage] == 2) { echo " selected"; }
	echo ">Low</option>
		    <option value='1'";
	if ($formfields[resusage] == 1) { echo " selected"; }
	echo ">Very Low</option>
		    </select>
		 </td>
	      </tr>\n";

	#
	# Batch
	#
	if ($formfields[batched]) {
	    $checked = "checked";
	} else {
	    $checked = "";
	}
	echo "<tr>
	         <td><a href='plab_ez_footnote6.html'
		        target='emulabfootnote'>Retry</a> until nodes with
			sufficient resources are available:</td>
	         <td>
		    <input type='checkbox' name='formfields[batched]' $checked>
		 </td>
	      </tr>\n";

	#
	# Canfail
	# XXX - Take previous value
	#
	echo "<tr>
	         <td><a href='plab_ez_footnote5.html'
		     target='emulabfootnote'>Proceed</a> even if some nodes
		     fail to set up:</td>
	         <td>
		    <input type='checkbox' name='formfields[canfail]' checked>
		 </td>
	      </tr>\n";

        #
	# Auto-swap
	#
	if (!$formfields['when']) {
	    $when = "never";
	} else {
	    $when = $formfields['when'];
	}
	echo "<tr>
	         <td><a href='plab_ez_footnote7.html'
		     target='emulabfootnote'>Auto-terminate</a> slice after:
	         <td>
		    <input type='text' size=6 name='formfields[when]'
		           value='$when'>
		    <select name='formfields[units]'>
		    <option value='1' selected>Hours</option>
		    <option value='24'>Days</option>
		    <option value='168'>Weeks</option>
		    </select>
		 </td>
	      </tr>\n";

	#
	# Header
	# 
	echo "<tr>
		 <th colspan=2>Files to Install and Maintain</th>
	      </tr>\n";

	#
	# Tarballs
	#
	echo "<tr>
	         <td><a href='plab_ez_footnote2.html'
		        target='emulabfootnote'>Tarball(s)</a> to install:</td>
		 <td>
		    <input type='text' size=50 name=formfields[tarball]
		           value='$formfields[tarball]'>
		 </td>
	      </tr>\n";

	#
	# RPMs
	#
	echo "<tr>
	         <td><a href='plab_ez_footnote3.html'
		        target='emulabfootnote'>RPM(s)</a> to install:</td>
	         <td>
		    <input type='text' name='formfields[rpm]'
		           value='$formfields[rpm]' size=50>
	         </td>
              </tr>\n";

	#
	# Startup commands
	#
	echo "<tr>
                 <td><a href='plab_ez_footnote4.html'
		        target='emulabfootnote'>Command</a> to run on startup:
		</td>
	         <td>
		    <input type='text' name='formfields[startupcmd]'
		           value='$formfields[startupcmd]' size=50>
		 </td>
               </tr>\n";
    } # if ($advanced)

    #
    # Submit buttons
    #
    echo "<tr>
             <th colspan=2>\n";

    if ($advanced) {
	echo "<center><input type=submit name=submit value='Submit'>&nbsp;\n";
	echo "<input type=reset value='Reset'></center>\n";
    } else {
	echo "<br>\n";
	echo "<center><input type=submit name=submit value='Create it'>&nbsp;\n";
	echo "<input type=submit name=submit value='More options'></center>\n";
    }
    echo "</th>\n";
    echo "</tr>\n";

    #
    # Finish the table off
    #
    echo "</table>\n";
    echo "</form>\n";

    PAGEFOOTER();
}

#
# Check tarballs (and RPMs) to make sure that they are fetchable, if URLs were
# given. Returns any errors.
#
function CHECKTARS($formfields) {

    #
    # Make a list of files that will ahve to be fetched
    #
    $tofetch = array();

    #
    # Check RPMs
    #
    if ($formfields['rpm']) {
	$rpms = explode(" ",$formfields['rpm']);
	while (list($junk,$rpm) = each($rpms)) {
	    if (preg_match("/^(http|ftp)/",$rpm,$matches)) {
		$tofetch[$rpm] = 1;
	    } else if (!preg_match("/^\//",$rpm,$matches)) {
		$errors[$rpm] = "RPM should be a http or ftp URL, or a path " .
			"on users.emulab.net";
	    }
	}
    }

    #
    # Tarballs are much like RPMs, but they need a path to untar to, too.
    #
    if ($formfields['tarball']) {
	$tarballs = explode(" ",$formfields['tarball']);
	if (count($tarballs) % 2) {
	    $errors['tarballs'] = "Should be a space-separated list of " .
		"&lt;directory&gt; &lt;tarball&gt; pairs";
	} else {
	    while (list($index,$tarball) = each($tarballs)) {
		if (!($index % 2)) {
		    if (!preg_match("/^\//",$tarball,$matches)) {
			$errors[$tarball] = "Should be a path to start the " .
			    "untar from";
		    }
		    continue;
		}
		if (preg_match("/^(http|ftp)/",$tarball,$matches)) {
		    $tofetch[$tarball] = 1;
		} else if (!preg_match("/^\//",$tarball,$matches)) {
		    $errors[$tarball] = "Tarball should be a http or ftp URL, or a path " .
			"on users.emulab.net";
		}
	    }
	}
    }

    #
    # Now take all the URLs, and make sure they're fetch-able. Don't actually
    # fecth them - that will be done later.
    #
    while (list($URL,$localfile) = each($tofetch)) {
	$fhandle = @fopen($URL,"r");
	if (!$fhandle) {
	    $errors[$URL] = "Unable to fetch file\n";
	} else {
	    fclose($fhandle);
	}
    }

    return $errors;

}

#
# Make an NS file with the supplied data
#
function MAKENS($formfields) {
    global $NSGEN, $PLAB_TEMPLATE;
    #
    # Pick out some defaults for the exp. creation page
    #

    $expname = "planetlab" . $formfields['count'];
    $descr   = "A planetlab experiment with " . $formfields['count'] .
	" nodes started by " . GETUID();

    #
    # Build up a URL to send the user to
    #
    $url = "beginexp.php3?view_style=plab&formfields[exp_id]=" .
           urlencode($expname) . "&formfields[exp_description]=" .
	   urlencode($descr) . "&formfields[exp_idleswap]=0" .
	   "&formfields[exp_noidleswap_reason]=" .
	   urlencode("PlanetLab experiment");

    #
    # Batched?
    #
    if ($formfields['batched']) {
	$url .= '&formfields[exp_batched]=Yep';
    }

    #
    # Determine the auto-swap time
    #
    if ($formfields['when'] && ($formfields['when'] != 'never')) {
	$swaptime = $formfields['when'] * $formfields['units'];
	$url .= "&formfields[exp_autoswap]=1";
	$url .= "&formfields[exp_autoswap_timeout]=$swaptime";
    }

    #
    # Run nsgen - make up a random nsref for use with it
    # NOTE: Stuff that is being used as command-line arguments was already
    # checked for bad characters by CHECKFORM()
    #
    list($usec, $sec) = explode(' ', microtime());
    srand((float) $sec + ((float) $usec * 100000));
    $nsref = rand();
    $url .= "&nsref=$nsref";
    $outfile = "/tmp/" . GETUID() . "-$nsref.nsfile";
    $nsgen_args = "";
    if ($formfields['count']) {
	$nsgen_args .= "-v Count='$formfields[count]' ";
    }
    if ($formfields['type']) {
	$nsgen_args .= "-v HWType='$formfields[type]' ";
    }
    if ($formfields['resusage']) {
	$nsgen_args .= "-v ResUsage='$formfields[resusage]' ";
    }
    if ($formfields['canfail']) {
	$nsgen_args .= "-v FailOK=1 ";
    }
    if ($formfields['tarball']) {
	$nsgen_args .= "-v Tarballs='$formfields[tarball]' ";
    }
    if ($formfields['rpm']) {
	$nsgen_args .= "-v RPMs='$formfields[rpm]' ";
    }
    if ($formfields['startupcmd']) {
	$nsgen_args .= "-v Startup='$formfields[startupcmd]' ";
    }
    
    #
    # Note: We run this as nobody on purpose - this is really dumb, but later
    # the web interface needs to be able to remove this file, and /tmp
    # usually has the sticky bit set, so only the owner can remove it.
    #
    SUEXEC("nobody","nobody","$NSGEN -o $outfile $nsgen_args $PLAB_TEMPLATE",
	SUEXEC_ACTION_DIE);

    header("Location: $url");
}

#
# Check values the user submitted to make sure that they are valid
#
function CHECKFORM($formfields) {
    $errors = array();
    if (!preg_match("/^\d+$/",$formfields['count'],$matches)) {
	$errors['count'] = "Number of nodes must be a positive integer";
    }
    if ($formfields['when'] && ($formfields['when'] != "never") &&
        (!preg_match("/^\d*(\.\d+)?$/",$formfields['when'],$matches))) {
	$errors['when'] = "Auto-terminate time must be a positive decimal " .
	    "or 'never'";
    }
    if ($formfields['resusage'] &&
	    !preg_match("/^\d+$/",$formfields['resusage'],$matches)) {
	$errors['resusage'] = "Resource usage must be a positive integer";
    }
    if ($formfields['tarball'] &&
	    preg_match("/'/",$formfields['tarball'],$matches)) {
	$errors['tarball'] = "Invalid characters in tarball";
    }
    if ($formfields['rpm'] &&
	    preg_match("/'/",$formfields['rpm'],$matches)) {
	$errors['rpm'] = "Invalid characters in rpm";
    }
    if ($formfields['startupcmd'] &&
	    preg_match("/'/",$formfields['startupcmd'],$matches)) {
	$errors['startupcmd'] = "Invalid characters in startup command";
    }

    return $errors;
}

#
# Actually do it!
#
if (($submit == "Submit") || ($submit == "Create it")) {
    $errors = CHECKFORM($formfields);
    if (!count($errors)) {
	$errors = CHECKTARS($formfields);
    }
    if (!count($errors)) {
	MAKENS($formfields);
    } else {
	SPITFORM($advanced, $formfields, $errors);
    }
} else {
    SPITFORM($advanced, $formfields);
}

#
# Standard Testbed Footer
#
?>