newnodes_list.php3 9.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
<?PHP
#
# EMULAB-COPYRIGHT
# Copyright (c) 2003 University of Utah and the Flux Group.
# 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
#
$uid = GETLOGIN();
LOGGEDINORDIE($uid);
$isadmin = ISADMIN($uid);
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);
}

#
# 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"];
	if (preg_match("/^(.*\D)(\d+)$/",$name,$matches)) {
	    $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";
    passthru("$TBSUEXEC_PATH $uid $gid newnode $nodelist 2>&1");
    echo "</pre>\n";
    echo "<hr>\n";
}

#
# Look for the nodes on the switch again
#
if ($research) {
    #
    # Get the MACs we're supposed to be looking for
    #
    $query_result = DBQueryFatal("SELECT i.mac, i.new_node_id, n.node_id, " .
117 118 119 120 121
	"i.card, i.card=t.control_net AS is_control " .
	"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");
122 123
    $mac_list = array();
    while ($row = mysql_fetch_array($query_result)) {
124 125 126 127 128
        if ($row["is_control"]) {
	    $class = "control";
	} else {
	    $class = "experimental";
	}
129
        $mac_list[$row["mac"]] = array( "new_node_id" => $row["new_node_id"],
130 131
	    "node_id" => $row["node_id"], "card" => $row["card"],
	    "class" => $class);
132 133 134 135 136
    }

    echo "<h3>Looking for MACs, this could take a while...</h3>";
    find_switch_macs($mac_list);
    foreach ($mac_list as $mac => $switchport) {
137
        if ($switchport["switch"]) {
138 139
	    DBQueryFatal("UPDATE new_interfaces SET " .
		"switch_id='$switchport[switch]', " .
140 141
		"switch_card='$switchport[switch_card]', " .
		"switch_port='$switchport[switch_port]' ".
142
		"WHERE new_node_id='$switchport[new_node_id]' " .
143
		"AND card=$switchport[card]");
144
	} else {
145
	    echo "<h4>Unable to find $switchport[node_id]:$switchport[card] " .
146 147 148 149 150 151 152 153 154 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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		"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'];
	if (preg_match("/^(.*\D)(\d+)$/",$name,$matches)) {
	    $prefix = $matches[1];
	    $number = $matches[2];
	    if ($addnumber) {
	        $number = $number + $addnumber;
	    }
	    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";
	}
    }
}

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 240
#
# 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
#
241 242 243 244 245 246
$query_result = DBQueryFatal("SELECT n.new_node_id, node_id, n.type, IP, " .
	"DATE_FORMAT(created,'%M %e %H:%i:%s') as created, i.MAC, " .
	"i.switch_id, i.switch_card, i.switch_port " .
	"FROM new_nodes AS n " .
	"LEFT JOIN node_types AS t on n.type=t.type " .
	"LEFT JOIN new_interfaces AS i ON n.new_node_id=i.new_node_id " .
247
	"    AND t.control_net = i.card " .
248 249 250 251 252 253 254 255 256 257 258 259 260 261 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 287 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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
	"ORDER BY n.new_node_id");

?>

<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>
	    <th>Type</th>
	    <th>IP</th>
	    <th>Control MAC</th>
	    <th>Control Port</th>
	    <th>Interfaces</th>
	    <th>Created</th>
	</tr>

<?

while ($row = mysql_fetch_array($query_result)) {
	$id         = $row["new_node_id"];
	$node_id    = $row["node_id"];
	$type       = $row["type"];
	$IP         = $row["IP"];
	$created    = $row["created"];
	$mac        = $row["MAC"];
	if ($row["switch_id"]) {
	    $port = "$row[switch_id].$row[switch_card]/$row[switch_port]";
	} else {
	    $port = "unknown";
	}
	$checked = in_array($id,$selected_nodes) ? "checked" : "";

        $iface_query_result = DBQueryFatal("SELECT COUNT(*) AS count " .
	    "FROM new_interfaces WHERE new_node_id=$id");
        $iface_row = mysql_fetch_array($iface_query_result);
	$interfaces = $iface_row["count"];

	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";
	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";
	echo "		<td>$created</td>\n";
	echo "	</tr>\n";
}

?>

<tr>
    <td align="center" colspan=9>
    <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>

<br>

332
<table>
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
<tr>
    <th>Set Type</th>
    <td><input type="text" width="10" name="newtype"></td>
</tr>

<tr>
    <th>Set Node ID Prefix</th>
    <td><input type="text" width="10" name="newprefix"></td>
</tr>

<tr>
    <th>Add to Node ID</th>
    <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>
353 354 355
</table>

<br><br>
356

357
<table>
358
<tr>
359 360
    <th>0</th>
    <td><input type="text" size=2 name="remap[0]"></td>
361 362
</tr>
<tr>
363 364
    <th>1</th>
    <td><input type="text" size=2 name="remap[1]"></td>
365 366
</tr>
<tr>
367 368
    <th>2</th>
    <td><input type="text" size=2 name="remap[2]"></td>
369 370
</tr>
<tr>
371 372 373 374 375 376 377 378 379 380 381
    <th>3</th>
    <td><input type="text" size=2 name="remap[3]"></td>
</tr>
<tr>
    <th>4</th>
    <td><input type="text" size=2 name="remap[4]"></td>
</tr>
<tr>
    <td colspan=2>
    <input type="submit" value="Re-number interfaces of selected nodes"
	name="renumber">
382 383 384
    </td>
</tr>
</table>
385 386 387 388 389 390 391 392 393 394 395 396

<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>
<input type="submit" value="Re-search switches for selected nodes" name="research">
<br><br>
<input type="submit" value="Create selected nodes" name="create">
<br><br>
<input type="submit" value="Delete selected nodes" name="delete">
397 398 399 400 401 402 403 404 405 406 407

</form>

<?

#
# Standard Testbed Footer
# 
PAGEFOOTER();

?>