newnodes_list.php3 14.4 KB
Newer Older
1 2
<?PHP
#
3
# Copyright (c) 2003-2013 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34
#
require("defs.php3");
require("newnode-defs.php3");

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

#
# Only admins can see this page
#
35 36 37 38
$this_user = CheckLoginOrDie();
$uid       = $this_user->uid();
$isadmin   = ISADMIN();

39 40 41 42
if (! $isadmin) {
    USERERROR("You do not have admin privileges!", 1);
}

43 44 45 46 47 48 49
#
# Verify page arguments.
#
$optargs = OptionalPageArguments("selected",     PAGEARG_ARRAY,
				 "delete",       PAGEARG_STRING,
				 "calc",         PAGEARG_STRING,
				 "create",       PAGEARG_STRING,
50
				 "ignoreifaces", PAGEARG_BOOLEAN,
51 52 53 54
				 "research",     PAGEARG_STRING,
				 "swap",         PAGEARG_STRING,
				 "newtype",      PAGEARG_STRING,
				 "newprefix",    PAGEARG_STRING,
55
				 "addnumber",    PAGEARG_STRING);
56

57 58 59 60 61
#
# Standard Testbed Header
#
PAGEHEADER("New Testbed Nodes");

62 63 64 65 66 67 68 69
#
# XXX - a hack
#
$gid = "nobody";

#
# Start out by performing any operations they may have asked for
#
70
if (isset($selected)) {
71 72 73 74 75
    $selected_nodes = $selected;
} else {
    $selected_nodes = array();
}

Leigh Stoller's avatar
Leigh Stoller committed
76 77 78 79 80 81 82 83 84 85 86
# Remove these to make the code below easier.
if (isset($newtype) && $newtype == "") {
    unset($newtype);
}
if (isset($newprefix) && $newprefix == "") {
    unset($newprefix);
}
if (isset($addnumber) && $addnumber == "") {
    unset($addnumber);
}

87 88 89 90 91 92
#
# 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) {
Leigh Stoller's avatar
Leigh Stoller committed
93
	if (!is_numeric($node)) {
94 95
	    USERERROR("Invalid node id", 1);
	}
96 97 98 99 100
	$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);
101
} else {
102 103
    if (isset($delete) || isset($calc) || isset($create) ||
	isset($research) || isset($swap) || isset($newtype) ||
104
	isset($newprefix) || isset($addnumber)) {
105 106
	USERERROR("At least one node must be selected!",1);
    }
107 108 109 110 111
}

#
# Delete nodes
#
112
if (isset($delete)) {
113 114 115 116 117 118 119
    DBQueryFatal("DELETE FROM new_nodes WHERE $whereclause");
    DBQueryFatal("DELETE FROM new_interfaces WHERE $whereclause");
}

#
# Recalculate IPs
#
120
if (isset($calc)) {
121 122 123 124 125
   $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"];
126 127
	if (preg_match("/^(.*\D)(\d+)$/",$name,$matches) ||
	    preg_match("/^(.*-)([a-zA-Z])$/",$name,$matches)) {
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	    $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
#
146
if (isset($create)) {
147 148 149 150 151 152 153
    $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);
154
    $option   = ((isset($ignoreifaces) && $ignoreifaces) ? "-f" : "");
155 156 157
    echo "<h3>Creating nodes - this could take a while, please wait</h3>\n";
    echo "<hr>\n";
    echo "<pre>\n";
158
    passthru("$TBSUEXEC_PATH $uid $gid webnewnode $option -w $nodelist 2>&1");
159 160 161 162 163 164 165
    echo "</pre>\n";
    echo "<hr>\n";
}

#
# Look for the nodes on the switch again
#
166
if (isset($research)) {
167
    #
168
    # Get the MACs we are supposed to be looking for
169
    #
170 171 172 173 174 175 176
    $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");
177 178
    $mac_list = array();
    while ($row = mysql_fetch_array($query_result)) {
Leigh Stoller's avatar
Leigh Stoller committed
179
	$type  = $row["type"];
180 181 182 183 184 185
	$card  = $row["card"];
	$iface = "eth${card}";

	# Figure out if this interface is the control interface for the type.
	NodeTypeAttribute($type, "control_interface", $control_iface);
	
186 187 188 189
        if ($ELABINELAB) {
            # See find_switch_macs().
            $class = NULL;
        }
190
        elseif ($iface == $control_iface) {
191 192 193 194
	    $class = TBDB_IFACEROLE_CONTROL;
	}
	else {
	    $class = TBDB_IFACEROLE_EXPERIMENT;
195
	}
196
        $mac_list[$row["mac"]] = array( "new_node_id" => $row["new_node_id"],
197 198
	    "node_id" => $row["node_id"], "card" => $row["card"],
	    "class" => $class);
199 200 201
    }

    echo "<h3>Looking for MACs, this could take a while...</h3>";
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
    $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;
	    }
	}
    }
240
    foreach ($mac_list as $mac => $switchport) {
241
        if (isset($switchport["switch"])) {
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	    $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 " .
258
		"switch_id='$switchport[switch]', " .
259 260
		"switch_card='$switchport[switch_card]', " .
		"switch_port='$switchport[switch_port]' ".
261
		"WHERE new_node_id='$switchport[new_node_id]' " .
262
		"AND card=$switchport[card]");
263
	} else {
264
	    echo "<h4>Unable to find $switchport[node_id]:$switchport[card] " .
265 266 267 268 269 270 271 272
		"on switches, not updating</h4>\n";
	}
    }
}

#
# Swap the IDs and IPs of two nodes
#
273
if (isset($swap)) {
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
    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
#
300
if (isset($newtype)) {
301
    $newtype = addslashes($newtype);
302 303 304 305 306 307
    DBQueryFatal("UPDATE new_nodes SET type='$newtype' WHERE $whereclause");
}

#
# Change node name prefix
#
308
if (isset($newprefix) || isset($addnumber)) {
309 310 311 312 313
   $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'];
314 315
	if (preg_match("/^(.*\D)(\d+)$/",$name,$matches) ||
	    preg_match("/^(.*-)([a-zA-Z])$/",$name,$matches)) {
316 317
	    $prefix = $matches[1];
	    $number = $matches[2];
Leigh Stoller's avatar
Leigh Stoller committed
318
	    if (isset($addnumber)) {
319
	        if (is_numeric($number)) {
320
		    $number = $number + $addnumber;
321 322
		} else {
		    $number = chr(ord($number) + $addnumber);
323
		}
324
	    }
325
	    if (isset($newprefix)) {
326 327
	        $prefix = $newprefix;
	    }
328
	    $newname = addslashes($prefix . $number);
329 330 331 332 333 334 335 336
	    DBQueryFatal("UPDATE new_nodes SET node_id='$newname' " .
		"WHERE new_node_id='$id'");
	} else {
	    echo "Unable to understand the node name $name";
	}
    }
}

337 338 339
#
# Okay, now get the node information and display the form
#
340 341 342 343 344
$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 " .
345
		 "order BY node_id");
346 347
?>

348 349
<h3><a href="newnodes_list.php3">Refresh this page</a></h3>

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
<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>

366 367
<table id="node_list_table">
    <thead class="sort"><tr>
368 369 370
	    <th></th>
	    <th>ID</th>
	    <th>Node ID</th>
371
	    <th>Identifier</th>
372 373 374 375 376
	    <th>Type</th>
	    <th>IP</th>
	    <th>Control MAC</th>
	    <th>Control Port</th>
	    <th>Interfaces</th>
377
	    <th>Temporary IP</th>
378
	    <th>Created</th>
379
	    <th>Warnings</th>
380
	    <th>Location</th>
381
	</tr></thead>
382 383 384

<?

385
while ($row = mysql_fetch_array($nodes_result)) {
386 387 388 389 390
	$id         = $row["new_node_id"];
	$node_id    = $row["node_id"];
	$type       = $row["type"];
	$IP         = $row["IP"];
	$created    = $row["created"];
391
	$tempIP     = $row["temporary_IP"];
392
	$dmesg      = $row["dmesg"];
393
	$identifier = $row["identifier"];
394
	$building   = $row["building"];
395 396
	$mac        = "";
	$port       = "";
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

	# 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;
	    }
429
	}
430
	
431 432 433 434 435 436 437
	$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";
438
	echo "          <td>$identifier</td>\n";
439 440 441 442 443
	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";
444
	echo "		<td>$tempIP</td>\n";
445
	echo "		<td>$created</td>\n";
446 447 448 449 450
	if ($dmesg) {
	    echo "		<td><img src=\"redball.gif\"></td>\n";
	} else {
	    echo "		<td><img src=\"greenball.gif\"></td>\n";
	}
451 452 453 454 455 456 457
	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";
458 459 460 461 462 463
	echo "	</tr>\n";
}

?>

<tr>
464
    <td align="center" colspan=13>
465 466 467 468 469 470 471 472 473 474
    <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>

475 476 477 478 479
<script type='text/javascript' language='javascript'>
sorttable.makeSortable(getObjbyName('node_list_table'));
</script>


480
<h3>Actions</h3>
481

482
<table>
483 484 485 486 487 488
<tr>
    <th>Set Type</th>
    <td><input type="text" width="10" name="newtype"></td>
</tr>

<tr>
489
    <th>Set Node ID Prefix (eg. 'pc')</th>
490 491 492 493
    <td><input type="text" width="10" name="newprefix"></td>
</tr>

<tr>
494
    <th>Add to Node ID Suffix (integer, can be negative)</th>
495 496 497 498 499 500 501 502
    <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>
503 504 505
</table>

<br><br>
506

507 508 509 510
<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>
511
<input type="submit" value="Search switch ports for selected nodes" name="research">
512 513
<br><br>
<input type="submit" value="Create selected nodes" name="create">
514 515
  &nbsp <input type="checkbox" value=1 name=ignoreifaces>
    Ignore unconnected interfaces (not attached to a switch)
516 517
<br><br>
<input type="submit" value="Delete selected nodes" name="delete">
518 519 520 521 522 523 524 525 526 527 528

</form>

<?

#
# Standard Testbed Footer
# 
PAGEFOOTER();

?>