editnodetype.php3 19.1 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
# This belongs elsewhere!
$initial_attributes = array(
54 55
    array("attrkey" => "adminmfs_osid", "attrvalue" => $freebsd_mfs->osid(),
	  "attrtype" => "integer"),
56 57 58 59 60 61 62 63
    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"),
64 65 66 67 68
    array("attrkey" => "default_imageid",
	  "attrvalue" => $fbsd410_rhl90_std->imageid(),
	  "attrtype" => "integer"),
    array("attrkey" => "default_osid", "attrvalue" => $rhl_std->osid(),
	  "attrtype" => "integer"),
69 70
    array("attrkey" => "delay_capacity", "attrvalue" => "2",
	  "attrtype" => "integer"),
71 72 73 74
    array("attrkey" => "delay_osid", "attrvalue" => $fbsd_std->osid(),
	  "attrtype" => "integer"),
    array("attrkey" => "diskloadmfs_osid", "attrvalue" => $frisbee_mfs->osid(),
	  "attrtype" => "integer"),
75 76 77 78 79 80 81 82
    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"),
83 84
    array("attrkey" => "jail_osid", "attrvalue" =>  $fbsd_std->osid(),
	  "attrtype" => "integer"),
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    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"),
    );

103 104 105
#
# Spit the form out using the array of data.
#
106
function SPITFORM($node_type, $formfields, $attributes, $deletes, $errors)
107
{
108
    global $osid_result, $imageid_result, $mfsosid_result, $new_type;
109
    global $newattribute_name, $newattribute_value, $newattribute_type;
110 111 112 113

    #
    # Standard Testbed Header
    #
114
    if (! isset($new_type)) {
115 116 117 118 119 120
	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";
121

122 123 124 125 126
	echo "<br><br>\n";
    }
    else {
	PAGEHEADER("Create Node Type");
    }
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

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

150 151

    $formargs = "node_type=$node_type";
152
    if (isset($new_type)) {
153 154 155
	$formargs .= "&new_type=1";
    }

156
    echo "<table align=center border=1>
157 158
          <form action=editnodetype.php3?$formargs method=post " .
	  " name=typeform>\n";
159 160

    echo "<tr>
161
              <td colspan=2>Type:</td>\n";
162
    if (isset($new_type)) {
163 164 165 166 167 168 169 170
        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>
171
          </tr>\n";
172
    }
173 174

    echo "<tr>
175
              <td colspan=2>Class:</td>\n";
176
    if (isset($new_type)) {
177 178 179 180 181 182 183
        echo "<td class=left>
                 <input type=text
                        name=\"formfields[class]\"
                        value=\"" . $formfields['class'] . "\"
	                size=10>
             </td>\n";
    } else {
184
	echo "<td class=left>" . $formfields["class"] . "</td>
185
          </tr>\n";
186
    }
187 188

    echo "<tr>
189
             <td colspan=2>isvirtnode:</td>
190 191 192
             <td class=left>
                 <input type=text
                        name=\"formfields[isvirtnode]\"
193
                        value=\"" . $formfields["isvirtnode"] . "\"
194 195 196 197
	                size=2>
             </td>
          </tr>\n";

198
    echo "<tr>
199
             <td colspan=2>isjailed:</td>
200 201
             <td class=left>
                 <input type=text
202
                        name=\"formfields[isjailed]\"
203
                        value=\"" . $formfields["isjailed"] . "\"
204 205 206 207 208
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
209
             <td colspan=2>isdynamic:</td>
210 211 212
             <td class=left>
                 <input type=text
                        name=\"formfields[isdynamic]\"
213
                        value=\"" . $formfields["isdynamic"] . "\"
214 215 216 217
	                size=2>
             </td>
          </tr>\n";

218
    echo "<tr>
219
             <td colspan=2>isremotenode:</td>
220 221 222
             <td class=left>
                 <input type=text
                        name=\"formfields[isremotenode]\"
223
                        value=\"" . $formfields["isremotenode"] . "\"
224 225 226 227 228
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
229
             <td colspan=2>issubnode:</td>
230 231 232
             <td class=left>
                 <input type=text
                        name=\"formfields[issubnode]\"
233
                        value=\"" . $formfields["issubnode"] . "\"
234 235 236 237 238
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
239
             <td colspan=2>isplabdslice:</td>
240 241 242
             <td class=left>
                 <input type=text
                        name=\"formfields[isplabdslice]\"
243
                        value=\"" . $formfields["isplabdslice"] . "\"
244 245 246 247 248
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
249
             <td colspan=2>issimnode:</td>
250 251 252
             <td class=left>
                 <input type=text
                        name=\"formfields[issimnode]\"
253
                        value=\"" . $formfields["issimnode"] . "\"
254 255 256 257
	                size=2>
             </td>
          </tr>\n";

258 259 260 261 262 263 264 265 266
    #
    # 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)) {
267 268 269 270
	if (!isset($deletes[$key])) {
	    # Somehow this doesn't get initialized in the Create Node case.
	    $deletes[$key] = "";
	}
271 272 273 274 275 276
	if ($key == "default_osid" ||
	    $key == "jail_osid" ||
	    $key == "delay_osid") {
	    WRITEOSIDMENU($key, "attributes[$key]", $osid_result, $val,
			  "deletes[$key]", $deletes[$key]);
	}
277 278 279 280 281
	elseif ($key == "adminmfs_osid" ||
		$key == "diskloadmfs_osid") {
	    WRITEOSIDMENU($key, "attributes[$key]", $mfsosid_result, $val,
			  "deletes[$key]", $deletes[$key]);
	}
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
	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";

307
    echo "<tr>
308 309 310 311 312 313 314 315 316 317
           <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>
318 319
          </tr>\n";

320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    $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";

341
    echo "<tr>
342
              <td colspan=3 align=center>
343 344 345 346 347 348 349 350
                 <b><input type=submit name=submit value=Submit></b>
              </td>
          </tr>\n";

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

351
if (isset($new_type)) {
352
    #
353
    # Starting a new node type - give some reasonable defaults
354
    #
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
    $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']] = "";
    }
}
370
elseif (isset($node_type)) {
371
    #
372
    # Editing an existing type - suck the current info out of the
373 374 375 376 377 378 379
    # 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'");
380

381 382 383
    if (($defaults = mysql_fetch_array($query_result)) == 0) {
	USERERROR("$node_type is not a valid node type!", 1);
    }
384 385 386 387 388 389 390
    
    #
    # And the attributes ... needed below.
    #
    $default_attributes = array();
    $attribute_types = array();
    $attribute_deletes = array();
391

392 393 394 395 396 397 398 399 400
    $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']] = "";
    }
}
401 402 403 404
else {
    PAGEARGERROR("Must provide one of node_type or new_type");
    return;
}
405

406
#
407
# We need lists of osids and imageids for selection.
408 409 410 411
#
$osid_result =
    DBQueryFatal("select osid,osname,pid from os_info ".
		 "where (path='' or path is NULL) ".
412
		 "order by pid,osname");
413

414 415 416 417 418
$mfsosid_result =
    DBQueryFatal("select osid,osname,pid from os_info ".
		 "where (path is not NULL and path!='') ".
		 "order by pid,osname");

419 420
$imageid_result =
    DBQueryFatal("select imageid,imagename,pid from images ".
421
		 "order by pid,imagename");
422 423 424 425 426

#
# On first load, display initial values.
#
if (! isset($submit)) {
427 428
    SPITFORM($node_type, $defaults,
	     $default_attributes, $attribute_deletes, 0);
429 430 431 432 433 434 435
    PAGEFOOTER();
    return;
}

#
# We do not allow these to be changed.
#
436
if (!isset($new_type)) {
437 438
    $formfields["class"] = $defaults["class"];
}
439 440

#
441
# Otherwise, must validate and redisplay if errors.
442 443 444
#
$errors  = array();

445 446 447 448 449 450 451 452 453
# 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;
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
    
    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";
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
#
# 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"];
}

504
# isvirtnode
505
if (isset($formfields["isvirtnode"]) && $formfields["isvirtnode"] != "") {
506
    $args["isvirtnode"] = $formfields["isvirtnode"];
507 508
}

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

# isdynamic
515
if (isset($formfields["isdynamic"]) && $formfields["isdynamic"] != "") {
516
    $args["isdynamic"] = $formfields["isdynamic"];
517 518
}

519
# isremotenode
520
if (isset($formfields["isremotenode"]) && $formfields["isremotenode"] != "") {
521
    $args["isremotenode"] = $formfields["isremotenode"];
522 523 524
}

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

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

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

539 540 541 542 543 544
# 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;
545 546 547
}

#
548
# Form allows for adding a single new attribute, but someday be more fancy.
549
#
550 551 552 553 554 555 556 557 558 559 560 561 562
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.
563
    SPITFORM($node_type, $formfields, $attributes, $deletes, $errors);
564 565 566 567
    PAGEFOOTER();
    return;
}

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

570
#
571 572
# 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.
573
#
574
PAGEREPLACE("editnodetype.php3?node_type=$node_type");
575

576 577 578 579
#
# Standard Testbed Footer
# 
PAGEFOOTER();
580

581
#
582
# Create or edit a nodetype.  (No class for that at present.)
583
#
584 585
function SetNodeType($node_type, $args, &$errors) {
    global $suexec_output, $suexec_output_array;
586

587 588 589 590 591 592 593 594
    #
    # 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;
595
    }
596 597 598 599
    if (! ($fp = fopen($xmlname, "w"))) {
	TBERROR("Could not open temp file $xmlname", 0);
	$errors[] = "Transient error(2); please try again later.";
	return null;
600
    }
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621

    # 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) {
	    $errors[] = "Transient error(3); please try again later.";
	    SUEXECERROR(SUEXEC_ACTION_CONTINUE);
622 623
	}
	else {
624 625 626 627 628 629 630 631 632 633 634 635 636 637
	    # 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
		$errors[] = "Transient error(4); please try again later.";
638
	}
639
	return null;
640
    }
641
    # There are no return value(s) to parse at the end of the output.
642

643 644 645 646 647
    # 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;
}
648 649

?>