editnodetype.php3 19.4 KB
Newer Older
1 2 3
<?php
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2007 University of Utah and the Flux Group.
5 6 7
# All rights reserved.
#
include("defs.php3");
8 9
include_once("imageid_defs.php");
include_once("osinfo_defs.php");
10 11 12 13 14 15 16 17 18
include("osiddefs.php3");

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

#
# Only known and logged in users can end experiments.
#
19 20 21
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();
22 23 24 25 26

if (! $isadmin) {
    USERERROR("You do not have permission to edit node types!", 1);
}

27 28
$optargs = OptionalPageArguments("submit",     PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY,
29 30 31 32 33 34 35

				 # Send new_type=1 to create new nodetype.
				 "new_type",   PAGEARG_STRING,
				 # Optional if new_type, required if not.
				 "node_type",  PAGEARG_STRING,

				 # Attribute creation and deletion.
36 37 38 39
				 "deletes",    PAGEARG_ARRAY,
				 "attributes", PAGEARG_ARRAY,
				 "newattribute_type",  PAGEARG_STRING,
				 "newattribute_name",  PAGEARG_STRING,
40
				 "newattribute_value", PAGEARG_ANYTHING);
Russ Fish's avatar
Russ Fish committed
41 42
if (!isset($node_type)) { $node_type = ""; }
if (!isset($attributes)) { $attributes = array(); }
Russ Fish's avatar
Russ Fish committed
43
if (!isset($deletes)) { $deletes = array(); }
44

45 46 47 48 49 50 51
$emulab_ops = Project::LookupByPid("emulab-ops");
$freebsd_mfs = OSinfo::LookupByName($emulab_ops,"FREEBSD-MFS");
$fbsd410_rhl90_std = Image::LookupByName($emulab_ops,"FBSD410+RHL90-STD");
$rhl_std = OSinfo::LookupByName($emulab_ops, "RHL-STD");
$fbsd_std = OSinfo::LookupByName($emulab_ops,"FBSD-STD");
$frisbee_mfs = OSinfo::LookupByName($emulab_ops,"FRISBEE-MFS");

52 53 54 55 56 57
if ($freebsd_mfs == null || $fbsd410_rhl90_std == null ||
    $rhl_std == null || $fbsd_std == null || $frisbee_mfs == null) {
    PAGEERROR("You must add images from Utah into your database" .
              " before adding a nodetype. See installation documentation for details!",1);
}

58 59
# This belongs elsewhere!
$initial_attributes = array(
60 61
    array("attrkey" => "adminmfs_osid", "attrvalue" => $freebsd_mfs->osid(),
	  "attrtype" => "integer"),
62 63 64 65 66 67 68 69
    array("attrkey" => "bios_waittime", "attrvalue" => "60",
	  "attrtype" => "integer"),
    array("attrkey" => "bootdisk_unit", "attrvalue" => "0",
	  "attrtype" => "integer"),
    array("attrkey" => "control_interface", "attrvalue" => "ethX",
	  "attrtype" => "string"),
    array("attrkey" => "control_network", "attrvalue" => "X",
	  "attrtype" => "integer"),
70 71 72 73 74
    array("attrkey" => "default_imageid",
	  "attrvalue" => $fbsd410_rhl90_std->imageid(),
	  "attrtype" => "integer"),
    array("attrkey" => "default_osid", "attrvalue" => $rhl_std->osid(),
	  "attrtype" => "integer"),
75 76
    array("attrkey" => "delay_capacity", "attrvalue" => "2",
	  "attrtype" => "integer"),
77 78 79 80
    array("attrkey" => "delay_osid", "attrvalue" => $fbsd_std->osid(),
	  "attrtype" => "integer"),
    array("attrkey" => "diskloadmfs_osid", "attrvalue" => $frisbee_mfs->osid(),
	  "attrtype" => "integer"),
81 82 83 84 85 86 87 88
    array("attrkey" => "disksize", "attrvalue" => "0.00",
	  "attrtype" => "float"),
    array("attrkey" => "disktype", "attrvalue" => "ad",
	  "attrtype" => "string"),
    array("attrkey" => "frequency", "attrvalue" => "XXX",
	  "attrtype" => "integer"),
    array("attrkey" => "imageable", "attrvalue" => "1",
	  "attrtype" => "boolean"),
89 90
    array("attrkey" => "jail_osid", "attrvalue" =>  $fbsd_std->osid(),
	  "attrtype" => "integer"),
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    array("attrkey" => "max_interfaces", "attrvalue" => "X",
	  "attrtype" => "integer"),
    array("attrkey" => "memory", "attrvalue" => "XXX",
	  "attrtype" => "integer"),
    array("attrkey" => "power_delay", "attrvalue" => "60",
	  "attrtype" => "integer"),
    array("attrkey" => "processor", "attrvalue" => "PIII",
	  "attrtype" => "string"),
    array("attrkey" => "rebootable", "attrvalue" => "1",
	  "attrtype" => "boolean"),
    array("attrkey" => "simnode_capacity", "attrvalue" => "650",
	  "attrtype" => "integer"),
    array("attrkey" => "trivlink_maxspeed", "attrvalue" => "400000",
	  "attrtype" => "integer"),
    array("attrkey" => "virtnode_capacity", "attrvalue" => "20",
	  "attrtype" => "integer"),
    );

109 110 111
#
# Spit the form out using the array of data.
#
112
function SPITFORM($node_type, $formfields, $attributes, $deletes, $errors)
113
{
114
    global $osid_result, $imageid_result, $mfsosid_result, $new_type;
115
    global $newattribute_name, $newattribute_value, $newattribute_type;
116 117 118 119

    #
    # Standard Testbed Header
    #
120
    if (! isset($new_type)) {
121 122 123 124 125 126
	PAGEHEADER("Edit Node Type");

	echo "<font size=+2>Node Type <b>".
              "<a href='shownodetype.php3?node_type=$node_type'>$node_type
                 </a></b>\n".
	       "</font>\n";
127

128 129 130 131 132
	echo "<br><br>\n";
    }
    else {
	PAGEHEADER("Create Node Type");
    }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

    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";
    }

156 157

    $formargs = "node_type=$node_type";
158
    if (isset($new_type)) {
159 160 161
	$formargs .= "&new_type=1";
    }

162
    echo "<table align=center border=1>
163 164
          <form action=editnodetype.php3?$formargs method=post " .
	  " name=typeform>\n";
165 166

    echo "<tr>
167
              <td colspan=2>Type:</td>\n";
168
    if (isset($new_type)) {
169 170 171 172 173 174 175 176
        echo "<td class=left>
                 <input type=text
                        name=\"node_type\"
                        value=\"" . $node_type . "\"
	                size=10>
             </td>\n";
    } else {
         echo "<td class=left>$node_type</td>
177
          </tr>\n";
178
    }
179 180

    echo "<tr>
181
              <td colspan=2>Class:</td>\n";
182
    if (isset($new_type)) {
183 184 185 186 187 188 189
        echo "<td class=left>
                 <input type=text
                        name=\"formfields[class]\"
                        value=\"" . $formfields['class'] . "\"
	                size=10>
             </td>\n";
    } else {
190
	echo "<td class=left>" . $formfields["class"] . "</td>
191
          </tr>\n";
192
    }
193 194

    echo "<tr>
195
             <td colspan=2>isvirtnode:</td>
196 197 198
             <td class=left>
                 <input type=text
                        name=\"formfields[isvirtnode]\"
199
                        value=\"" . $formfields["isvirtnode"] . "\"
200 201 202 203
	                size=2>
             </td>
          </tr>\n";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
204
    echo "<tr>
205
             <td colspan=2>isjailed:</td>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
206 207
             <td class=left>
                 <input type=text
208
                        name=\"formfields[isjailed]\"
209
                        value=\"" . $formfields["isjailed"] . "\"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
210 211 212 213 214
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
215
             <td colspan=2>isdynamic:</td>
Leigh B. Stoller's avatar
Leigh B. Stoller committed
216 217 218
             <td class=left>
                 <input type=text
                        name=\"formfields[isdynamic]\"
219
                        value=\"" . $formfields["isdynamic"] . "\"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
220 221 222 223
	                size=2>
             </td>
          </tr>\n";

224
    echo "<tr>
225
             <td colspan=2>isremotenode:</td>
226 227 228
             <td class=left>
                 <input type=text
                        name=\"formfields[isremotenode]\"
229
                        value=\"" . $formfields["isremotenode"] . "\"
230 231 232 233 234
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
235
             <td colspan=2>issubnode:</td>
236 237 238
             <td class=left>
                 <input type=text
                        name=\"formfields[issubnode]\"
239
                        value=\"" . $formfields["issubnode"] . "\"
240 241 242 243 244
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
245
             <td colspan=2>isplabdslice:</td>
246 247 248
             <td class=left>
                 <input type=text
                        name=\"formfields[isplabdslice]\"
249
                        value=\"" . $formfields["isplabdslice"] . "\"
250 251 252 253 254
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
255
             <td colspan=2>issimnode:</td>
256 257 258
             <td class=left>
                 <input type=text
                        name=\"formfields[issimnode]\"
259
                        value=\"" . $formfields["issimnode"] . "\"
260 261 262 263
	                size=2>
             </td>
          </tr>\n";

264 265 266 267 268 269 270 271 272
    #
    # Now do attributes.
    #
    echo "<tr></tr>
          <tr></tr>
           <td align=center><font size=-1>Delete?</font></td>
           <td align=center colspan=2><b>Node Attributes</b></td></tr>\n";

    while (list ($key, $val) = each ($attributes)) {
273 274 275 276
	if (!isset($deletes[$key])) {
	    # Somehow this doesn't get initialized in the Create Node case.
	    $deletes[$key] = "";
	}
277 278 279 280 281 282
	if ($key == "default_osid" ||
	    $key == "jail_osid" ||
	    $key == "delay_osid") {
	    WRITEOSIDMENU($key, "attributes[$key]", $osid_result, $val,
			  "deletes[$key]", $deletes[$key]);
	}
283 284 285 286 287
	elseif ($key == "adminmfs_osid" ||
		$key == "diskloadmfs_osid") {
	    WRITEOSIDMENU($key, "attributes[$key]", $mfsosid_result, $val,
			  "deletes[$key]", $deletes[$key]);
	}
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
	elseif ($key == "default_imageid") {
	    WRITEIMAGEIDMENU($key, "attributes[$key]", $imageid_result, $val,
			     "deletes[$key]", $deletes[$key]);
	}
	else {
	    echo "<tr>
                      <td align=center>
                            <input type=checkbox value=checked
                                    name=\"deletes[$key]\" $deletes[$key]></td>
                      <td>${key}:</td>
                      <td class=left>
                          <input type=text
                                 name=\"attributes[$key]\"
                                 value=\"" . $val . "\"></td>
                  </tr>\n";
	}
    }

    #
    # Provide area for adding a new attribute.
    # 
    echo "<tr></tr>
          <tr></tr>
           <td align=center colspan=3><b>Add New Attribute</b></td></tr>\n";

313
    echo "<tr>
314 315 316 317 318 319 320 321 322 323
           <td colspan=2>Attribute Name:</td>
           <td class=left>
               <input type=text name=newattribute_name
                      value=\"$newattribute_name\"></td>
          </tr>\n";
    echo "<tr>
           <td colspan=2>Attribute Value:</td>
           <td class=left>
               <input type=text name=newattribute_value
                      value=\"$newattribute_value\"></td>
324 325
          </tr>\n";

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    $typenames   = array();
    $typenames[] = "boolean";
    $typenames[] = "integer";
    $typenames[] = "float";
    $typenames[] = "string";
    
    echo "<tr>
           <td colspan=2>Attribute Type:</td>
           <td class=left>\n";

    foreach ($typenames as $i => $type) {
	$checked = "";

	if ($newattribute_type == $type)
	    $checked = "checked";
	
	echo "<input type=radio $checked name=newattribute_type
                     value=$type>$type\n";
    }
    echo "</td></tr>\n";

347
    echo "<tr>
348
              <td colspan=3 align=center>
349 350 351 352 353 354 355 356
                 <b><input type=submit name=submit value=Submit></b>
              </td>
          </tr>\n";

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

357
if (isset($new_type)) {
358
    #
359
    # Starting a new node type - give some reasonable defaults
360
    #
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    $defaults = array("class" => "pc", "isvirtnode" => 0,
		      "isremotenode" => 0, "issubnode" => 0,
		      "isplabdslice" => 0, "isjailed" => 0, "isdynamic" => 0,
		      "issimnode" => 0);

    $default_attributes = array();
    $attribute_types = array();
    $attribute_deletes = array();
    
    foreach ($initial_attributes as $entry) {
	$default_attributes[$entry['attrkey']] = $entry['attrvalue'];
	$attribute_types[$entry['attrkey']] = $entry['attrtype'];
	$attribute_deletes[$entry['attrkey']] = "";
    }
}
376
elseif (isset($node_type)) {
377
    #
378
    # Editing an existing type - suck the current info out of the
379 380 381 382 383 384 385
    # database.
    #
    if (!preg_match("/^[-\w]+$/", $node_type)) {
	USERERROR("$node_type contains illegal characters!", 1);    
    }
    $query_result =
	DBQueryFatal("select * from node_types where type='$node_type'");
386

387 388 389
    if (($defaults = mysql_fetch_array($query_result)) == 0) {
	USERERROR("$node_type is not a valid node type!", 1);
    }
390 391 392 393 394 395 396
    
    #
    # And the attributes ... needed below.
    #
    $default_attributes = array();
    $attribute_types = array();
    $attribute_deletes = array();
397

398 399 400 401 402 403 404 405 406
    $query_result =
	DBQueryFatal("select * from node_type_attributes ".
		     "where type='$node_type'");
    while ($row = mysql_fetch_array($query_result)) {
	$default_attributes[$row['attrkey']] = $row['attrvalue'];
	$attribute_types[$row['attrkey']] = $row['attrtype'];
	$attribute_deletes[$row['attrkey']] = "";
    }
}
407 408 409 410
else {
    PAGEARGERROR("Must provide one of node_type or new_type");
    return;
}
411

412
#
413
# We need lists of osids and imageids for selection.
414 415 416 417
#
$osid_result =
    DBQueryFatal("select osid,osname,pid from os_info ".
		 "where (path='' or path is NULL) ".
418
		 "order by pid,osname");
419

420 421 422 423 424
$mfsosid_result =
    DBQueryFatal("select osid,osname,pid from os_info ".
		 "where (path is not NULL and path!='') ".
		 "order by pid,osname");

425 426
$imageid_result =
    DBQueryFatal("select imageid,imagename,pid from images ".
427
		 "order by pid,imagename");
428 429 430 431 432

#
# On first load, display initial values.
#
if (! isset($submit)) {
433 434
    SPITFORM($node_type, $defaults,
	     $default_attributes, $attribute_deletes, 0);
435 436 437 438 439 440 441
    PAGEFOOTER();
    return;
}

#
# We do not allow these to be changed.
#
442
if (!isset($new_type)) {
443 444
    $formfields["class"] = $defaults["class"];
}
445 446

#
447
# Otherwise, must validate and redisplay if errors.
448 449 450
#
$errors  = array();

451 452 453 454 455 456 457 458 459
# Check the attributes.
while (list ($key, $val) = each ($attributes)) {
    # Skip checks if scheduled for deletion
    if (isset($deletes[$key]) && $deletes[$key] == "checked") 
	continue;

    if (!isset($attribute_types[$key])) {
	$errors[$key] = "Unknown Attribute";
	continue;
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
    
    if ($val == "") {
	$errors[$key] = "No value provided for $key";
	continue;
    }

    # Probably redundant with the XML keyfields checking...
    $attrtype = $attribute_types[$key];
    if ($attrtype == "") {	# Shouldn't happen...
	$attrtype = $attribute_types[$key] = "integer";
    }
    if (strpos(":boolean:float:integer:string:", ":$attrtype:")===FALSE) {
	$errors[$key] = "Invalid type information: $attrtype";
	continue;
    }

    # New attributes require type and value.
    if (isset($newattribute_name) && $newattribute_name != "" &&
	!(isset($newattribute_type) && $newattribute_type != "")) {
	$errors[$newattribute_name] = "Missing type";
    }
    if (isset($newattribute_name) && $newattribute_name != "" &&
	!(isset($newattribute_value) && $newattribute_value != "")) {
	$errors[$newattribute_name] = "Missing value";
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
#
# If any errors, respit the form with the current values and the
# error messages displayed. Iterate until happy.
# 
if (count($errors)) {
    SPITFORM($node_type, $formfields, $attributes, $deletes, $errors);
    PAGEFOOTER();
    return;
}

#
# Build up argument array to pass along.
#
$args = array();

# Class (only for new types.)
if (isset($new_type) &&
    isset($formfields['class']) && $formfields['class'] != "") {
    $args["new_type"] = "1";
    $args["class"] = $formfields["class"];
}

510
# isvirtnode
511
if (isset($formfields["isvirtnode"]) && $formfields["isvirtnode"] != "") {
512
    $args["isvirtnode"] = $formfields["isvirtnode"];
513 514
}

515
# isjailed
516
if (isset($formfields["isjailed"]) && $formfields["isjailed"] != "") {
517
    $args["isjailed"] = $formfields["isjailed"];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
518 519 520
}

# isdynamic
521
if (isset($formfields["isdynamic"]) && $formfields["isdynamic"] != "") {
522
    $args["isdynamic"] = $formfields["isdynamic"];
Leigh B. Stoller's avatar
Leigh B. Stoller committed
523 524
}

525
# isremotenode
526
if (isset($formfields["isremotenode"]) && $formfields["isremotenode"] != "") {
527
    $args["isremotenode"] = $formfields["isremotenode"];
528 529 530
}

# issubnode
531
if (isset($formfields["issubnode"]) && $formfields["issubnode"] != "") {
532
    $args["issubnode"] = $formfields["issubnode"];
533 534 535
}

# isplabdslice
536
if (isset($formfields["isplabdslice"]) && $formfields["isplabdslice"] != "") {
537
    $args["isplabdslice"] = $formfields["isplabdslice"];
538 539 540
}

# issimnode
541
if (isset($formfields["issimnode"]) && $formfields["issimnode"] != "") {
542
    $args["issimnode"] = $formfields["issimnode"];
543 544
}

545 546 547 548 549 550
# Existing attributes.
foreach ($attributes as $attr_key => $attr_val) {
    if (isset($deletes[$attr_key]) && $deletes[$attr_key] == "checked") 
	$args["delete_${attr_key}"] = "1";
    $attr_type = $attribute_types[$attr_key];
    $args["attr_${attr_type}_${attr_key}"] = $attr_val;
551 552 553
}

#
554
# Form allows for adding a single new attribute, but someday be more fancy.
555
#
556 557 558 559 560 561 562 563 564 565 566 567 568
if (isset($newattribute_name) && $newattribute_name != "" &&
    isset($newattribute_value) && $newattribute_value != "" &&
    isset($newattribute_type) && $newattribute_type != "") {

    $args["new_attr"] = $newattribute_name;
    # The following is matched by wildcards on the other side of XML,
    # including checking its type and value, just like existing attributes.
    $args["attr_${newattribute_type}_$newattribute_name"] = $newattribute_value;
}

if (! ($result = SetNodeType($node_type, $args, $errors))) {
    # Always respit the form so that the form fields are not lost.
    # I just hate it when that happens so lets not be guilty of it ourselves.
569
    SPITFORM($node_type, $formfields, $attributes, $deletes, $errors);
570 571 572 573
    PAGEFOOTER();
    return;
}

Russ Fish's avatar
Russ Fish committed
574
PAGEHEADER(isset($new_type) ? "Create" : "Edit" . " Node Type");
575

576
#
577 578
# 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.
579
#
580
PAGEREPLACE("editnodetype.php3?node_type=$node_type");
581

582 583 584 585
#
# Standard Testbed Footer
# 
PAGEFOOTER();
586

587
#
588
# Create or edit a nodetype.  (No class for that at present.)
589
#
590 591
function SetNodeType($node_type, $args, &$errors) {
    global $suexec_output, $suexec_output_array;
592

593 594 595 596 597 598 599 600
    #
    # Generate a temporary file and write in the XML goo.
    #
    $xmlname = tempnam("/tmp", "editnodetype");
    if (! $xmlname) {
	TBERROR("Could not create temporary filename", 0);
	$errors[] = "Transient error(1); please try again later.";
	return null;
601
    }
602 603 604 605
    if (! ($fp = fopen($xmlname, "w"))) {
	TBERROR("Could not open temp file $xmlname", 0);
	$errors[] = "Transient error(2); please try again later.";
	return null;
606
    }
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

    # Add these. Maybe caller should do this?
    $args["node_type"] = $node_type;
    
    fwrite($fp, "<nodetype>\n");
    foreach ($args as $name => $value) {
	fwrite($fp, "<attribute name=\"$name\">");
	fwrite($fp, "  <value>" . htmlspecialchars($value) . "</value>");
	fwrite($fp, "</attribute>\n");
    }
    fwrite($fp, "</nodetype>\n");
    fclose($fp);
    chmod($xmlname, 0666);

    $retval = SUEXEC("nobody", "nobody", "webeditnodetype $xmlname",
		     SUEXEC_ACTION_IGNORE);

    if ($retval) {
	if ($retval < 0) {
626
	    $errors[] = "Transient error(3, $retval); please try again later.";
627
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
628 629
	}
	else {
630 631 632 633 634 635 636 637 638 639 640 641 642
	    # unlink($xmlname);
	    if (count($suexec_output_array)) {
		for ($i = 0; $i < count($suexec_output_array); $i++) {
		    $line = $suexec_output_array[$i];
		    if (preg_match("/^([-\w]+):\s*(.*)$/",
				   $line, $matches)) {
			$errors[$matches[1]] = $matches[2];
		    }
		    else
			$errors[] = $line;
		}
	    }
	    else
643
		$errors[] = "Transient error(4, $retval); please try again later.";
644
	}
645
	return null;
646
    }
647
    # There are no return value(s) to parse at the end of the output.
648

649 650 651 652 653
    # Unlink this here, so that the file is left behind in case of error.
    # We can then create the nodetype by hand from the xmlfile, if desired.
    unlink($xmlname);
    return true;
}
654 655

?>