newnodes_list.php3 13.9 KB
Newer Older
1 2 3
<?PHP
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2003, 2005, 2006, 2007 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# All rights reserved.
#
require("defs.php3");
require("newnode-defs.php3");

#
# List the nodes that have checked in and are awaint being added the the real
# testbed
#

#
# Standard Testbed Header
#
PAGEHEADER("New Testbed Nodes");

#
# Only admins can see this page
#
23 24 25 26
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();

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
if (! $isadmin) {
    USERERROR("You do not have admin privileges!", 1);
}

#
# XXX - a hack
#
$gid = "nobody";

#
# Start out by performing any operations they may have asked for
#
if ($selected) {
    $selected_nodes = $selected;
} else {
    $selected_nodes = array();
}

#
# Build up a handy little clause for these nodes to be used in WHEREs
#
if (count($selected_nodes)) {
    $equal_clauses = array();
    foreach ($selected_nodes as $node) {
	$equal_clauses[] = "new_node_id=$node";
	$equal_clauses_qualified[] = "n.new_node_id=$node";
    }
    $whereclause = implode(" OR ",$equal_clauses);
    $whereclause_qualified = implode(" OR ",$equal_clauses_qualified);
56 57 58 59 60
} else {
    if ($delete || $calc || $create || $research || $swap || $newtype ||
	    $newprefix || $addnumber || $renumber) {
	USERERROR("At least one node must be selected!",1);
    }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
}

#
# Delete nodes
#
if ($delete) {
    DBQueryFatal("DELETE FROM new_nodes WHERE $whereclause");
    DBQueryFatal("DELETE FROM new_interfaces WHERE $whereclause");
}

#
# Recalculate IPs
#
if ($calc) {
   $query_result = DBQueryFatal("SELECT new_node_id, node_id FROM new_nodes " .
   	"WHERE $whereclause ORDER BY new_node_id");
    while ($row = mysql_fetch_array($query_result)) {
        $id   = $row["new_node_id"];
        $name = $row["node_id"];
80 81
	if (preg_match("/^(.*\D)(\d+)$/",$name,$matches) ||
	    preg_match("/^(.*-)([a-zA-Z])$/",$name,$matches)) {
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
	    $prefix = $matches[1];
	    $number = $matches[2];
	    $newIP = guess_IP($prefix,$number);
	    if (!$newIP) {
	       echo "<h4>Failed to guess IP for $name!</h4>";
	    } else {
		DBQueryFatal("UPDATE new_nodes SET IP='$newIP' WHERE " .
		    "new_node_id='$id'");
	    }
	} else {
	    echo "Unable to understand the node name $name";
	}
    }
}

#
# Create the nodes in the real database
#
if ($create) {
    $nodenames = array();
    $query_result = DBQueryFatal("SELECT node_id FROM new_nodes " .
	"WHERE $whereclause");
    while ($row = mysql_fetch_array($query_result)) {
        $nodenames[] = $row["node_id"];
    }
    $nodelist = implode(" ",$nodenames);
    echo "<h3>Creating nodes - this could take a while, please wait</h3>\n";
    echo "<hr>\n";
    echo "<pre>\n";
111
    passthru("$TBSUEXEC_PATH $uid $gid webnewnode $nodelist 2>&1");
112 113 114 115 116 117 118 119 120
    echo "</pre>\n";
    echo "<hr>\n";
}

#
# Look for the nodes on the switch again
#
if ($research) {
    #
121
    # Get the MACs we are supposed to be looking for
122
    #
123 124 125 126 127 128 129
    $query_result =
	DBQueryFatal("select i.mac, i.new_node_id, n.node_id, i.card, n.type ".
		     "   from new_interfaces as i ".
		     "left join new_nodes as n on ".
		     "    i.new_node_id = n.new_node_id " .
		     "left join node_types as t on n.type = t.type " .
		     "where $whereclause_qualified");
130 131
    $mac_list = array();
    while ($row = mysql_fetch_array($query_result)) {
132 133 134 135 136 137 138
	$type  = $row["is_control"];
	$card  = $row["card"];
	$iface = "eth${card}";

	# Figure out if this interface is the control interface for the type.
	NodeTypeAttribute($type, "control_interface", $control_iface);
	
139 140 141 142
        if ($ELABINELAB) {
            # See find_switch_macs().
            $class = NULL;
        }
143
        elseif ($iface == $control_iface) {
144 145 146 147
	    $class = TBDB_IFACEROLE_CONTROL;
	}
	else {
	    $class = TBDB_IFACEROLE_EXPERIMENT;
148
	}
149
        $mac_list[$row["mac"]] = array( "new_node_id" => $row["new_node_id"],
150 151
	    "node_id" => $row["node_id"], "card" => $row["card"],
	    "class" => $class);
152 153 154
    }

    echo "<h3>Looking for MACs, this could take a while...</h3>";
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
    $retval = find_switch_macs($mac_list);
    if ($ELABINELAB && $retval == 0) {
	#
	# Ick, Ick, Ick. Must reorder the interfaces so that they are
	# the same as the outside Emulab, so that when we request the
	# outer emulab to create a vlan, both are talking about the same
	# interface. This is of course, bogus. I think I will have to
	# change it so that we use the MACs instead of the iface name.
	# That should be an easy change to snmpit_remote and the xmlrpc
	# server stub (or the proxy I guess).
	#
	#  Move them out of the way
	#
	foreach ($mac_list as $mac => $switchport) {
	    DBQueryFatal("update new_interfaces set card = card + 100 " .
			 "where new_node_id='$switchport[new_node_id]' and " .
			 "      card=$switchport[card]");
	}
	#
	# Now move them back to proper location, as specifed by the iface
	# that the outer emulab return to us. 
	# 
	foreach ($mac_list as $mac => $switchport) {
	    $iface = $switchport["iface"];

	    if (preg_match("/^(.*\D)(\d+)$/", $iface, $matches)) {
		$newcard = $matches[2];
		$oldcard = $switchport["card"] + 100;

		DBQueryFatal("update new_interfaces set card = $newcard " .
			     "where new_node_id='$switchport[new_node_id]' " .
			     "  and card=$oldcard");

		# Remember, $switchport is a *copy*, so must change $mac_list
		$mac_list[$mac]["card"] = $newcard;
	    }
	}
    }
193
    foreach ($mac_list as $mac => $switchport) {
194
        if ($switchport["switch"]) {
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	    $extra_set = "";
	    
            if ($ELABINELAB) {
                #
                # The reason for these ELABINELAB tests is that we cannot
		# use the node_types table to determine which interface is
		# the control network, since assign can pick any old interface
		# for each inner node. Generally speaking, we should not do
		# this at all, but rely on an outside mechanism to tell us
		# which interface is the control network. Anyway, I am using
		# the "role" slot of the new_interfaces table to override
		# what utils/newnode is going to set them too. 
		#
		$extra_set = "role='$switchport[class]', ";
            }
	    DBQueryFatal("UPDATE new_interfaces SET $extra_set " .
211
		"switch_id='$switchport[switch]', " .
212 213
		"switch_card='$switchport[switch_card]', " .
		"switch_port='$switchport[switch_port]' ".
214
		"WHERE new_node_id='$switchport[new_node_id]' " .
215
		"AND card=$switchport[card]");
216
	} else {
217
	    echo "<h4>Unable to find $switchport[node_id]:$switchport[card] " .
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
		"on switches, not updating</h4>\n";
	}
    }
}

#
# Swap the IDs and IPs of two nodes
#
if ($swap) {
    if (count($selected_nodes) != 2) {
       USERERROR("Exactly two nodes must be selected for swapping",0);
    } else {
	$id1 = $selected_nodes[0];
	$id2 = $selected_nodes[1];
	$query_result = DBQueryFatal("SELECT node_id, IP FROM new_nodes " .
		"WHERE new_node_id=$id1");
	$row = mysql_fetch_array($query_result);
	$node_id1 = $row["node_id"];
	$IP1 = $row["IP"];
	$query_result = DBQueryFatal("SELECT node_id, IP FROM new_nodes " .
		"WHERE new_node_id=$id2");
	$row = mysql_fetch_array($query_result);
	$node_id2 = $row["node_id"];
	$IP2 = $row["IP"];

	DBQueryFatal("UPDATE new_nodes SET node_id='$node_id2', IP='$IP2' " .
		"WHERE new_node_id=$id1");
	DBQueryFatal("UPDATE new_nodes SET node_id='$node_id1', IP='$IP1' " .
		"WHERE new_node_id=$id2");
    }
}

#
# Change node types
#
if ($newtype) {
    DBQueryFatal("UPDATE new_nodes SET type='$newtype' WHERE $whereclause");
}

#
# Change node name prefix
#
if ($newprefix || $addnumber) {
   $query_result = DBQueryFatal("SELECT new_node_id, node_id FROM new_nodes " .
   	"WHERE $whereclause");
    while ($row = mysql_fetch_array($query_result)) {
    	$id    = $row['new_node_id'];
	$name  = $row['node_id'];
266 267
	if (preg_match("/^(.*\D)(\d+)$/",$name,$matches) ||
	    preg_match("/^(.*-)([a-zA-Z])$/",$name,$matches)) {
268 269 270
	    $prefix = $matches[1];
	    $number = $matches[2];
	    if ($addnumber) {
271
	        if (is_numeric($number)) {
272
		    $number = $number + $addnumber;
273 274
		} else {
		    $number = chr(ord($number) + $addnumber);
275
		}
276 277 278 279 280 281 282 283 284 285 286 287 288
	    }
	    if ($newprefix) {
	        $prefix = $newprefix;
	    }
	    $newname = $prefix . $number;
	    DBQueryFatal("UPDATE new_nodes SET node_id='$newname' " .
		"WHERE new_node_id='$id'");
	} else {
	    echo "Unable to understand the node name $name";
	}
    }
}

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
#
# Re-number interfaces
#
if ($renumber) {
    #
    # Move them out of the way
    #
    foreach ($remap as $index => $value) {
        if ($value != "") {
	    DBQueryFatal("UPDATE new_interfaces SET card = card + 100 WHERE " .
	        "card = $index AND ($whereclause)");
	}
    }

    #
    # Move them back to the correct location
    #
    foreach ($remap as $index => $value) {
        if ($value != "") {
	    DBQueryFatal("UPDATE new_interfaces SET card = $value WHERE " .
	        "card = " . ($index + 100) . " AND ($whereclause)");
	}
    }

}

#
# Okay, now get the node information and display the form
#
318 319 320 321 322 323
$nodes_result =
    DBQueryFatal("select n.new_node_id, node_id, n.type, IP, " .
		 "    DATE_FORMAT(created,'%M %e %H:%i:%s') as created, ".
		 "    n.temporary_IP, n.dmesg, n.identifier, n.building " .
		 "  from new_nodes as n " .
		 "order BY n.new_node_id");
324 325
?>

326 327
<h3><a href="newnodes_list.php3">Refresh this page</a></h3>

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
<form action="newnodes_list.php3" method="get" name="nodeform">

<script language="JavaScript">
<!--
function selectAll(form) {
    for (i = 0; i < form.length; i++)
	form[i].checked = true ;
}

function deselectAll(form) {
    for (i = 0; i < form.length; i++)
	form[i].checked = false ;
}
-->
</script>

<table>
	<tr>
	    <th></th>
	    <th>ID</th>
	    <th>Node ID</th>
349
	    <th>Identifier</th>
350 351 352 353 354
	    <th>Type</th>
	    <th>IP</th>
	    <th>Control MAC</th>
	    <th>Control Port</th>
	    <th>Interfaces</th>
355
	    <th>Temporary IP</th>
356
	    <th>Created</th>
357
	    <th>Warnings</th>
358
	    <th>Location</th>
359 360 361 362
	</tr>

<?

363
while ($row = mysql_fetch_array($nodes_result)) {
364 365 366 367 368
	$id         = $row["new_node_id"];
	$node_id    = $row["node_id"];
	$type       = $row["type"];
	$IP         = $row["IP"];
	$created    = $row["created"];
369
	$tempIP     = $row["temporary_IP"];
370
	$dmesg      = $row["dmesg"];
371
	$identifier = $row["identifier"];
372
	$building   = $row["building"];
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

	# Get the control interface token for the type.
	NodeTypeAttribute($type, "control_interface", $control_iface);

	# Grab the interfaces for the node.
	$ifaces_result =
	    DBQueryFatal("select * from new_interfaces ".
			 "where new_node_id='$id'");

	# The total count ...
	$interfaces = mysql_num_rows($ifaces_result);

	# Search through to find the control interface.
	while ($irow = mysql_fetch_array($ifaces_result)) {
	    $card  = $irow["card"];
	    $iface = "eth${card}";

	    if ($iface == $control_iface) {
		$mac = $irow["mac"];
		
		if ($irow["switch_id"]) {
		    $switch_id   = $irow["switch_id"];
		    $switch_card = $irow["switch_card"];
		    $switch_port = $irow["switch_port"];
			
		    $port = "${switch_id}.${switch_card}/${switch_port}";
		}
		else {
		    $port = "unknown";
		}
		break;
	    }
405
	}
406
	
407 408 409 410 411 412 413
	$checked = in_array($id,$selected_nodes) ? "checked" : "";

	echo "	<tr>\n";
	echo "		<td><input type=\"checkbox\" name=\"selected[]\" " .
	    "value='$id' $checked></td>\n";
	echo "		<td><a href=\"newnode_edit.php3?id=$id\">$id</a></td>\n";
	echo "		<td>$node_id</td>\n";
414
	echo "          <td>$identifier</td>\n";
415 416 417 418 419
	echo "		<td>$type</td>\n";
	echo "		<td>$IP</td>\n";
	echo "          <td>$mac</td>\n";
	echo "          <td>$port</td>\n";
	echo "		<td>$interfaces</td>\n";
420
	echo "		<td>$tempIP</td>\n";
421
	echo "		<td>$created</td>\n";
422 423 424 425 426
	if ($dmesg) {
	    echo "		<td><img src=\"redball.gif\"></td>\n";
	} else {
	    echo "		<td><img src=\"greenball.gif\"></td>\n";
	}
427 428 429 430 431 432 433
	echo "          <td><a href=setnodeloc.php3?node_id=$id&isnewid=1>";
	if ($building) {
	    echo "		<img src=\"greenball.gif\" border=0>";
	} else {
	    echo "		<img src=\"redball.gif\" border=0>";
	}
	echo "</a></td>\n";
434 435 436 437 438 439
	echo "	</tr>\n";
}

?>

<tr>
440
    <td align="center" colspan=13>
441 442 443 444 445 446 447 448 449 450
    <input type="button" name="SelectAll" value="Select All"
	onClick="selectAll(document.nodeform.elements['selected[]'])">
    &nbsp;
    <input type="button" name="DeselectAll" value="Deselect All"
	onClick="deselectAll(document.nodeform.elements['selected[]'])">
    </td>
</tr>

</table>

451
<h3>Actions</h3>
452

453
<table>
454 455 456 457 458 459
<tr>
    <th>Set Type</th>
    <td><input type="text" width="10" name="newtype"></td>
</tr>

<tr>
460
    <th>Set Node ID Prefix (eg. 'pc')</th>
461 462 463 464
    <td><input type="text" width="10" name="newprefix"></td>
</tr>

<tr>
465
    <th>Add to Node ID Suffix (integer, can be negative)</th>
466 467 468 469 470 471 472 473
    <td><input type="text" width="10" name="addnumber"></td>
</tr>

<tr>
    <td colspan=2 align="center">
    <input type="submit" value="Update selected nodes" name="submit">
    </td>
</tr>
474 475 476
</table>

<br><br>
477

478
<table>
479
<tr>
480 481 482 483 484
    <th>FreeBSD interface number</th>
    <th>Linux interface number</th>
</tr>
<tr>
    <td>0</td>
485
    <td><input type="text" size=2 name="remap[0]"></td>
486 487
</tr>
<tr>
488
    <td>1</td>
489
    <td><input type="text" size=2 name="remap[1]"></td>
490 491
</tr>
<tr>
492
    <td>2</td>
493
    <td><input type="text" size=2 name="remap[2]"></td>
494 495
</tr>
<tr>
496
    <td>3</td>
497 498 499
    <td><input type="text" size=2 name="remap[3]"></td>
</tr>
<tr>
500
    <td>4</td>
501 502
    <td><input type="text" size=2 name="remap[4]"></td>
</tr>
503 504 505 506 507 508 509 510 511 512 513 514 515 516
<tr>
    <td>5</td>
    <td><input type="text" size=2 name="remap[5]"></td>
</tr>
<tr>
    <td>6</td>
    <td><input type="text" size=2 name="remap[6]"></td>
</tr>
<tr>
    <td>7</td>
    <td><input type="text" size=2 name="remap[6]"></td>
</tr>
<tr>
    <td colspan=2 align="center">
517 518
    <input type="submit" value="Re-number interfaces of selected nodes"
	name="renumber">
519 520 521
    </td>
</tr>
</table>
522 523 524 525 526 527 528

<br><br>

<input type="submit" value="Recalculate IPs for selected nodes" name="calc">
<br><br>
<input type="submit" value="Swap Node IDs and IPs for selected nodes" name="swap">
<br><br>
529
<input type="submit" value="Search switch ports for selected nodes" name="research">
530 531 532 533
<br><br>
<input type="submit" value="Create selected nodes" name="create">
<br><br>
<input type="submit" value="Delete selected nodes" name="delete">
534 535 536 537 538 539 540 541 542 543 544

</form>

<?

#
# Standard Testbed Footer
# 
PAGEFOOTER();

?>