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

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

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

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

25
$optargs = OptionalPageArguments("submit",     PAGEARG_STRING,
26
				 "node_type",  PAGEARG_STRING,
27 28 29 30 31 32
				 "new_type",   PAGEARG_STRING,
				 "formfields", PAGEARG_ARRAY,
				 "deletes",    PAGEARG_ARRAY,
				 "attributes", PAGEARG_ARRAY,
				 "newattribute_type",  PAGEARG_STRING,
				 "newattribute_name",  PAGEARG_STRING,
33
				 "newattribute_value", PAGEARG_ANYTHING);
Russ Fish's avatar
Russ Fish committed
34 35
if (!isset($node_type)) { $node_type = ""; }
if (!isset($attributes)) { $attributes = array(); }
36

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
# This belongs elsewhere!
$initial_attributes = array(
    array("attrkey" => "adminmfs_osid", "attrvalue" => "FREEBSD-MFS",
	  "attrtype" => "string"),
    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"),
    array("attrkey" => "default_imageid", "attrvalue" => "",
	  "attrtype" => "string"),
    array("attrkey" => "default_osid", "attrvalue" => "RHL-STD",
	  "attrtype" => "string"),
    array("attrkey" => "delay_capacity", "attrvalue" => "2",
	  "attrtype" => "integer"),
    array("attrkey" => "delay_osid", "attrvalue" => "FBSD-STD",
	  "attrtype" => "string"),
    array("attrkey" => "diskloadmfs_osid", "attrvalue" => "FRISBEE-MFS",
	  "attrtype" => "string"),
    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"),
    array("attrkey" => "jail_osid", "attrvalue" => "FBSD-STD",
	  "attrtype" => "string"),
    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"),
    );

87 88 89
#
# Spit the form out using the array of data.
#
90
function SPITFORM($node_type, $formfields, $attributes, $deletes, $errors)
91
{
92
    global $osid_result, $imageid_result, $mfsosid_result, $new_type;
93
    global $newattribute_name, $newattribute_value, $newattribute_type;
94 95 96 97

    #
    # Standard Testbed Header
    #
98
    if (! isset($new_type)) {
99 100 101 102 103 104
	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";
105

106 107 108 109 110
	echo "<br><br>\n";
    }
    else {
	PAGEHEADER("Create Node Type");
    }
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

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

134 135

    $formargs = "node_type=$node_type";
136
    if (isset($new_type)) {
137 138 139
	$formargs .= "&new_type=1";
    }

140
    echo "<table align=center border=1>
141 142
          <form action=editnodetype.php3?$formargs method=post " .
	  " name=typeform>\n";
143 144

    echo "<tr>
145
              <td colspan=2>Type:</td>\n";
146
    if (isset($new_type)) {
147 148 149 150 151 152 153 154
        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>
155
          </tr>\n";
156
    }
157 158

    echo "<tr>
159
              <td colspan=2>Class:</td>\n";
160
    if (isset($new_type)) {
161 162 163 164 165 166 167
        echo "<td class=left>
                 <input type=text
                        name=\"formfields[class]\"
                        value=\"" . $formfields['class'] . "\"
	                size=10>
             </td>\n";
    } else {
168
	echo "<td class=left>" . $formfields["class"] . "</td>
169
          </tr>\n";
170
    }
171 172

    echo "<tr>
173
             <td colspan=2>isvirtnode:</td>
174 175 176
             <td class=left>
                 <input type=text
                        name=\"formfields[isvirtnode]\"
177
                        value=\"" . $formfields["isvirtnode"] . "\"
178 179 180 181
	                size=2>
             </td>
          </tr>\n";

182
    echo "<tr>
183
             <td colspan=2>isjailed:</td>
184 185
             <td class=left>
                 <input type=text
186
                        name=\"formfields[isjailed]\"
187
                        value=\"" . $formfields["isjailed"] . "\"
188 189 190 191 192
	                size=2>
             </td>
          </tr>\n";

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

202
    echo "<tr>
203
             <td colspan=2>isremotenode:</td>
204 205 206
             <td class=left>
                 <input type=text
                        name=\"formfields[isremotenode]\"
207
                        value=\"" . $formfields["isremotenode"] . "\"
208 209 210 211 212
	                size=2>
             </td>
          </tr>\n";

    echo "<tr>
213
             <td colspan=2>issubnode:</td>
214 215 216
             <td class=left>
                 <input type=text
                        name=\"formfields[issubnode]\"
217
                        value=\"" . $formfields["issubnode"] . "\"
218 219 220 221 222
	                size=2>
             </td>
          </tr>\n";

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

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

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    #
    # 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)) {
	if ($key == "default_osid" ||
	    $key == "jail_osid" ||
	    $key == "delay_osid") {
	    WRITEOSIDMENU($key, "attributes[$key]", $osid_result, $val,
			  "deletes[$key]", $deletes[$key]);
	}
257 258 259 260 261
	elseif ($key == "adminmfs_osid" ||
		$key == "diskloadmfs_osid") {
	    WRITEOSIDMENU($key, "attributes[$key]", $mfsosid_result, $val,
			  "deletes[$key]", $deletes[$key]);
	}
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
	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";

287
    echo "<tr>
288 289 290 291 292 293 294 295 296 297
           <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>
298 299
          </tr>\n";

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    $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";

321
    echo "<tr>
322
              <td colspan=3 align=center>
323 324 325 326 327 328 329 330
                 <b><input type=submit name=submit value=Submit></b>
              </td>
          </tr>\n";

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

331
if (isset($new_type)) {
332
    #
333
    # Starting a new node type - give some reasonable defaults
334
    #
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    $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']] = "";
    }
}
350
elseif (isset($node_type)) {
351
    #
352
    # Editing an existing type - suck the current info out of the
353 354 355 356 357 358 359
    # 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'");
360

361 362 363
    if (($defaults = mysql_fetch_array($query_result)) == 0) {
	USERERROR("$node_type is not a valid node type!", 1);
    }
364 365 366 367 368 369 370
    
    #
    # And the attributes ... needed below.
    #
    $default_attributes = array();
    $attribute_types = array();
    $attribute_deletes = array();
371

372 373 374 375 376 377 378 379 380
    $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']] = "";
    }
}
381 382 383 384
else {
    PAGEARGERROR("Must provide one of node_type or new_type");
    return;
}
385

386 387 388 389 390 391
#
# We need a list of osids and imageids.
#
$osid_result =
    DBQueryFatal("select osid,osname,pid from os_info ".
		 "where (path='' or path is NULL) ".
392
		 "order by pid,osname");
393

394 395 396 397 398
$mfsosid_result =
    DBQueryFatal("select osid,osname,pid from os_info ".
		 "where (path is not NULL and path!='') ".
		 "order by pid,osname");

399 400
$imageid_result =
    DBQueryFatal("select imageid,imagename,pid from images ".
401
		 "order by pid,imagename");
402 403 404 405 406

#
# On first load, display initial values.
#
if (! isset($submit)) {
407 408
    SPITFORM($node_type, $defaults,
	     $default_attributes, $attribute_deletes, 0);
409 410 411 412 413 414 415
    PAGEFOOTER();
    return;
}

#
# We do not allow these to be changed.
#
416
if (!isset($new_type)) {
417 418
    $formfields["class"] = $defaults["class"];
}
419 420 421 422 423 424 425 426

#
# Otherwise, must validate and redisplay if errors. Build up a DB insert
# string as we go. 
#
$errors  = array();
$inserts = array();

427
# Class (only for new types)
428 429
if (isset($new_type) &&
    isset($formfields['class']) && $formfields['class'] != "") {
430 431 432 433 434 435 436 437
    if (! TBvalid_description($formfields['class'])) {
	$errors["Class"] = TBFieldErrorString();
    }
    else {
	$inserts["class"] = addslashes($formfields["class"]);
    }
}

438
# isvirtnode
439 440
if (isset($formfields["isvirtnode"]) && $formfields["isvirtnode"] != "") {
    if (! TBvalid_boolean($formfields["isvirtnode"])) {
441 442 443
	$errors["isvirtnode"] = TBFieldErrorString();
    }
    else {
444
	$inserts["isvirtnode"] = $formfields["isvirtnode"];
445 446 447
    }
}

448
# isjailed
449 450
if (isset($formfields["isjailed"]) && $formfields["isjailed"] != "") {
    if (! TBvalid_boolean($formfields["isjailed"])) {
451
	$errors["isjailed"] = TBFieldErrorString();
452 453
    }
    else {
454
	$inserts["isjailed"] = $formfields["isjailed"];
455 456 457 458
    }
}

# isdynamic
459 460
if (isset($formfields["isdynamic"]) && $formfields["isdynamic"] != "") {
    if (! TBvalid_boolean($formfields["isdynamic"])) {
461 462 463
	$errors["isdynamic"] = TBFieldErrorString();
    }
    else {
464
	$inserts["isdynamic"] = $formfields["isdynamic"];
465 466 467
    }
}

468
# isremotenode
469 470
if (isset($formfields["isremotenode"]) && $formfields["isremotenode"] != "") {
    if (! TBvalid_boolean($formfields["isremotenode"])) {
471 472 473
	$errors["isremotenode"] = TBFieldErrorString();
    }
    else {
474
	$inserts["isremotenode"] = $formfields["isremotenode"];
475 476 477 478
    }
}

# issubnode
479 480
if (isset($formfields["issubnode"]) && $formfields["issubnode"] != "") {
    if (! TBvalid_boolean($formfields["issubnode"])) {
481 482 483
	$errors["issubnode"] = TBFieldErrorString();
    }
    else {
484
	$inserts["issubnode"] = $formfields["issubnode"];
485 486 487 488
    }
}

# isplabdslice
489 490
if (isset($formfields["isplabdslice"]) && $formfields["isplabdslice"] != "") {
    if (! TBvalid_boolean($formfields["isplabdslice"])) {
491 492 493
	$errors["isplabdslice"] = TBFieldErrorString();
    }
    else {
494
	$inserts["isplabdslice"] = $formfields["isplabdslice"];
495 496 497 498
    }
}

# issimnode
499 500
if (isset($formfields["issimnode"]) && $formfields["issimnode"] != "") {
    if (! TBvalid_boolean($formfields["issimnode"])) {
501 502 503
	$errors["issimnode"] = TBFieldErrorString();
    }
    else {
504
	$inserts["issimnode"] = $formfields["issimnode"];
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
# 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;
    }
    
    $attrtype = $attribute_types[$key];
    $valid    = 1;

    if ($val == "") {
	$errors[$key] = "No value provided for $key";
	continue;
    }
    elseif ($attrtype == "boolean") {
	$valid = TBvalid_boolean($val);
    }
    elseif ($attrtype == "float") {
	$valid = TBvalid_float($val);
    }
    elseif ($attrtype == "integer") {
	$valid = TBvalid_integer($val);
    }
    elseif ($attrtype == "string") {
	$valid = TBvalid_description($val);
537 538
    }
    else {
539 540 541 542 543
	$errors[$key] = "Invalid type information: $attrtype";
	continue;
    }
    if (!$valid) {
	$errors[$key] = TBFieldErrorString();
544 545 546 547 548 549 550
    }
}

#
# Spit any errors now.
#
if (count($errors)) {
551
    SPITFORM($node_type, $formfields, $attributes, $deletes, $errors);
552 553 554 555
    PAGEFOOTER();
    return;
}

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
#
# Form allows for a single new attribute, but someday be more fancy.
#
if (isset($newattribute_name) && $newattribute_name != "" &&
    isset($newattribute_value) && $newattribute_value != "" &&
    isset($newattribute_type) && $newattribute_type != "") {

    if (!preg_match("/^[-\w]+$/", $newattribute_name)) {
	$errors["New Attribute Name"] = "Invalid characters in attribute name";
    }
    else {
	$valid    = 1;

	if ($newattribute_type == "boolean") {
	    $valid = TBvalid_boolean($newattribute_value);
	}
	elseif ($newattribute_type == "float") {
	    $valid = TBvalid_float($newattribute_value);
	}
	elseif ($newattribute_type == "integer") {
	    $valid = TBvalid_integer($newattribute_value);
	}
	elseif ($newattribute_type == "string") {
	    $valid = TBvalid_description($newattribute_value);
	}
	else {
	    $errors["New Attribute Type"] = "Invalid type: $newattribute_type";
	}
	if (!$valid) {
	    $errors["New Attribute Type"] = TBFieldErrorString();
	}
    }

    #
    # Spit any errors now.
    #
    if (count($errors)) {
	SPITFORM($node_type, $formfields, $attributes, $deletes, $errors);
	PAGEFOOTER();
	return;
    }
    # Set up for loops below.
    $attributes[$newattribute_name] = $newattribute_value;
    $attribute_types[$newattribute_name] = $newattribute_type;
}

602 603 604 605 606 607 608 609
#
# Otherwise, do the inserts.
#
$insert_data = array();
foreach ($inserts as $name => $value) {
    $insert_data[] = "$name='$value'";
}

610
if (isset($new_type)) {
611 612
    DBQueryFatal("insert into node_types set type='$node_type', ".
		 implode(",", $insert_data));
613
    if ($formfields["class"] == "pc" || $formfields["isremotenode"] == 1) {
614 615 616 617 618
	$vnode_type = $node_type;
	$vnode_type = preg_replace("/pc/","pcvm",$vnode_type);
	if ($vnode_type == $node_type) {
	    $vnode_type = "$vnode_type-vm";
	}
619 620
	$pcvmtype = ($formfields["isremotenode"] == 1 ? "pcvwa" : "pcvm");
	
621
	DBQueryFatal("insert into node_types_auxtypes set " .
622
		     "  auxtype='$vnode_type', type='$pcvmtype'");
623
    }
624 625 626 627 628 629 630 631 632 633 634 635 636 637
    foreach ($attributes as $key => $value) {
        # Skip if scheduled for deletion
	if (isset($deletes[$key]) && $deletes[$key] == "checked") 
	    continue;
	
	$key   = addslashes($key);
	$type  = addslashes($attribute_types[$key]);
	$value = addslashes($value);
	
	DBQueryFatal("insert into node_type_attributes set ".
		     "   type='$node_type', ".
		     "   attrkey='$key', attrtype='$type', ".
		     "   attrvalue='$value' ");
    }
638 639 640 641
} else {
    DBQueryFatal("update node_types set ".
		 implode(",", $insert_data) . " ".
		 "where type='$node_type'");
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659

    foreach ($attributes as $key => $value) {
	$key   = addslashes($key);
	$type  = addslashes($attribute_types[$key]);
	$value = addslashes($value);

        # Remove if scheduled for deletion
	if (isset($deletes[$key]) && $deletes[$key] == "checked") {
	    DBQueryFatal("delete from node_type_attributes ".
			 "where type='$node_type' and attrkey='$key'");
	}
	else {
	    DBQueryFatal("replace into node_type_attributes set ".
			 "   type='$node_type', ".
			 "   attrkey='$key', attrtype='$type', ".
			 "   attrvalue='$value' ");
	}
    }
660
}
661 662 663 664 665 666 667 668

#
# 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.
#
header("Location: editnodetype.php3?node_type=$node_type");

?>