newnode.in 14.1 KB
Newer Older
1 2 3 4
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2003-2010 University of Utah and the Flux Group.
6 7 8 9 10 11 12
# All rights reserved.
#

#
# newnode - a script for moving nodes from the new_ tables into production.
#

13
use lib '@prefix@/lib';
14 15

use libdb;
16
use libtestbed;
17
use libxmlrpc;
18
use NodeType;
Robert Ricci's avatar
Robert Ricci committed
19
use English;
20
use OSinfo;
21
use Getopt::Std;
22 23 24

use strict;

25 26 27 28 29
my $TB         = "@prefix@";
my $ELABINELAB = @ELABINELAB@;
my $RPCSERVER  = "@OUTERBOSS_NODENAME@";
my $RPCPORT    = "@OUTERBOSS_XMLRPCPORT@";
my $RPCCERT    = "@OUTERBOSS_SSLCERTNAME@";
30 31

my $switchmac = "$TB/sbin/switchmac";
32 33 34 35
my $os_load = "$TB/bin/os_load";
my $os_select = "$TB/bin/os_select";
my $newnode_reboot = "$TB/sbin/newnode_reboot";
my $named_setup = "$TB/sbin/named_setup";
36 37 38
my $nalloc = "$TB/bin/nalloc";
my $nfree = "$TB/bin/nfree";
my $dhcpd_makeconf = "$TB/sbin/dhcpd_makeconf";
39
my $exports_setup = "$TB/sbin/exports_setup";
40 41 42

my $dhcpd_rc = "/usr/local/etc/rc.d/2.dhcpd.sh";

43
#
44
# MFS to boot the nodes into initially
45
#
46 47
my $MFS_INITIAL = TB_OSID_FREEBSD_MFS();
my $OS_OPSNODE  = "OPSNODE-BSD";
48 49

#
50
# Initial event system state to put the nodes into
51
#
52 53
my $STATE_INITIAL = TBDB_NODESTATE_SHUTDOWN;
my $STATE_OPSNODE = TBDB_NODESTATE_ISUP;
54 55 56 57 58 59

#
# Number of vnodes to create for each physical node
#
my $NUM_VNODES = 50;

60 61 62 63 64
#
# Default number of vnodes that can be hosted on any pnode
#
my $DEFAULT_PCVM_COUNT = 20;

65 66 67 68 69 70 71 72
#
# Experiments we might put nodes into
#
my $PID_HWDOWN = NODEDEAD_PID();
my $EID_HWDOWN = NODEDEAD_EID();
my $PID_OPSNODE = TBOPSPID();
my $EID_OPSNODE = "opsnodes";

73 74
# Turn off line buffering on output
$| = 1;
75

76 77 78 79
if (!TBAdmin()) {
    die "Sorry, only testbed administrators can run this script!\n";
}

80 81 82
#
# Handle command-line options
#
83 84 85
sub usage() {
    die "Usage: $0 [-f] <node_id> ...\n";
}
86 87
my $noreboot = 0;
my $quiet    = 0;
88 89
my $force_unconnected_interfaces = 0;
my %options = ();
90
if (! getopts("fnq", \%options)) {
91 92 93 94 95
    usage();
}
if ($options{f}) {
    $force_unconnected_interfaces = 1;
}
96 97 98 99 100 101
if ($options{'n'}) {
    $noreboot = 1;
}
if ($options{'q'}) {
    $quiet = 1;
}
102

103 104 105 106 107 108
#
# We seem to have to do this so that ssh gets proper root permissions to read
# the key file. Argh.
#
$UID = $EUID = 0;

109
if (@ARGV < 1) {
110
    usage();
111 112
}

113 114 115 116 117 118 119 120 121 122
#
# For vnodes - figure out the jail IP base
#
my $IPBASE;
if (TBDB_JAILIPBASE =~ /^(\d+).(\d+).(\d+).(\d+)/) {
    $IPBASE = "$1.$2";
} else {
    die "Problem with JAILIPBASE\n"
}

123 124 125 126 127
my @node_ids = @ARGV;

#
# Now, loop through the nodes given, and add each one
#
Robert Ricci's avatar
Robert Ricci committed
128
my (@succeeded_nodes, @reboot_IPs);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
NODE: foreach my $node_id (@node_ids) {
    my $query_result;

    #
    # Check to make sure said node does not already exist!
    #
    $query_result = DBQueryFatal("SELECT node_id FROM nodes WHERE " .
	"node_id='$node_id'");
    if ($query_result->num_rows()) {
	warn "Node $node_id failed: a node with that name already exists!\n";
	next NODE;
    }

    #
    # Grab information about the node from the new_nodes table
    #
145
    $query_result = DBQueryFatal("SELECT new_node_id, type, IP, temporary_IP, ".
146
	"floor, building, loc_x, loc_y, contact, phone, room, role " .
147
	"FROM new_nodes WHERE node_id='$node_id'");
148 149 150 151
    if (!$query_result->num_rows()) {
	warn "Node $node_id failed: No pending node with that name exists!\n";
	next NODE;
    }
152
    my ($new_node_id, $type, $IP, $tempIP, $floor, $building, $loc_x, $loc_y,
153
	$contact, $phone, $room, $role) = $query_result->fetchrow();
154

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    #
    # Make sure they've given it an IP address that looks valid
    #
    if ($IP !~ /^(\d+).(\d+).(\d+).(\d+)/) {
	warn "Node $node_id has an invalid IP address ($IP) - skipping\n";
	next NODE;
    }

    #
    # Make sure some other node doesn't already have this IP address on its
    # control interface
    #
    $query_result = DBQueryFatal("SELECT node_id FROM interfaces WHERE " .
	"IP='$IP' AND role='" . TBDB_IFACEROLE_CONTROL() . "'");
    if ($query_result->num_rows()) {
	my ($existing_node_id) = $query_result->fetchrow();
	warn "Node $node_id failed: $existing_node_id is already using IP " .
		"address $IP\n";
	next NODE;
    }

176 177 178 179
    #
    # Make sure that the new node is of a valid type, and grab a few other
    # things to fill in as initial values
    #
180 181 182 183 184
    my $typeinfo = NodeType->Lookup($type);
    if (!defined($typeinfo)) {
	warn "No such nodetype $type is defined in the DB!\n";
	next NODE;
    }
185
    my ($control_iface,$virtnode_capacity,$adminmfs,$adminmfs_osid);
186 187 188 189 190 191 192 193 194

    if ($typeinfo->control_interface(\$control_iface) || !$control_iface) {
	warn "No control interface for $type is defined in the DB!\n";
	next NODE;
    }
    if ($typeinfo->virtnode_capacity(\$virtnode_capacity)) {
	warn "Failure looking up virtnode_capacity for $type in the DB!\n";
	next NODE;
    }
195
    if ($typeinfo->adminmfs_osid(\$adminmfs_osid)) {
196
	warn "Failure looking up adminmfs_osid for $type in the DB!\n";
197 198
	next NODE;
    }
199 200
    $virtnode_capacity = 0
	if (!defined($virtnode_capacity));
201 202 203 204 205 206 207 208

    # Find object for the adminfs.
    if (defined($adminmfs_osid)) {
	$adminmfs = OSinfo->Lookup($adminmfs_osid);
    }
    else {
	$adminmfs = OSinfo->Lookup(TBOPSPID(), $MFS_INITIAL);
    }
209 210 211 212

    #
    # Grab the node's MACs from the new_interfaces table
    #
213
    $query_result = DBQueryFatal("SELECT card, MAC, interface_type, " .
214
	"switch_id, switch_card, switch_port, cable, len, role " .
215
	"FROM new_interfaces WHERE new_node_id='$new_node_id'");
216 217 218 219 220 221
    if (!$query_result->num_rows()) {
	warn "Node $node_id failed: Must have at least one interface!\n";
	next NODE;
    }

    my %interfaces;
222
    while (my ($card, $MAC, $iface_type, $switch_id, $switch_card,
223
	    $switch_port, $cable, $len, $ifrole) = $query_result->fetchrow()) {
224 225 226
	#
	# Get some more information about this interface type
	#
227
	my $iface_query = DBQueryFatal("SELECT max_speed, full_duplex, " .
228
	    "connector FROM interface_types WHERE type='$iface_type'");
229
	if (!$iface_query->num_rows()) {
230
	    warn "Node $node_id failed: Interface $card is of unknown type " .
231
	    	"$iface_type\n";
232 233 234
	    next NODE;
	}

235
	my ($max_speed, $full_duplex, $connector) = $iface_query->fetchrow();
236

237 238 239 240
	#
	# Do a sanity check - make sure that we have a switch recorded for all
	# experimental interfaces
	#
241
	unless ($force_unconnected_interfaces || ($role eq "ctrlnode")) {
242
	    my $iface = "eth$card";
243 244
	    if (($iface ne $control_iface) && ($connector !~ /wireless/i)
		&& !$switch_id) {
245 246 247 248 249 250
		warn "Node $node_id failed: Don't know which switch " .
		    "card $iface is connected to\n";
		next NODE;
	    }
	}

251 252 253
	#
	# Stash it away...
	#
254
	$interfaces{$card} = [$MAC, $iface_type, $max_speed, $full_duplex,
255
		$switch_id, $switch_card, $switch_port, $cable, $len, $ifrole];
256 257 258 259 260 261

    }

    #
    # Make up a priority (just used for sorting)
    #
262 263
    $node_id =~ /^(.*\D)(\d+)$/;
    my ($prefix,$nodenum) = ($1, $2);
264
    my $priority;
265 266
    if (defined $nodenum) {
	$priority = $nodenum;
267 268 269 270
    } else {
	$priority = 1;
    }

271 272 273 274 275 276 277 278 279 280 281 282 283
    #
    # Figure out which project and experiment this node needs to go into
    #
    my ($pid, $eid);
    if ($role eq "ctrlnode") {
	$pid = $PID_OPSNODE;
	$eid = $EID_OPSNODE;
    } else {
	$pid = $PID_HWDOWN;
	$eid = $EID_HWDOWN;
    }

    #
284
    # Figure out what osid/opmode this node should run
285
    #
286 287
    my ($osid,$opmode);
    
288
    if ($role eq "ctrlnode") {
289
	my $osinfo = OSinfo->Lookup(TBOPSPID(), $OS_OPSNODE);
290

291
	$osid   = $osinfo->osid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
292
	$opmode = $osinfo->op_mode();
293 294 295
    }
    else {
	$osid   = $adminmfs->osid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
296
	$opmode = $adminmfs->op_mode();
297 298 299 300 301 302 303 304 305 306 307 308
    }

    #
    # Figure out what state to start the node in
    #
    my $state;
    if ($role eq "ctrlnode") {
	$state = $STATE_OPSNODE;
    } else {
	$state = $STATE_INITIAL;
    }

309 310 311 312 313 314 315 316 317 318 319 320 321
    #
    # When operating inside an inner elab, we need an extra bit of information
    # from the outer boss; which the IP of the outer control network. This is
    # strictly for bootinfo which has some really gross hacks in it.
    #
    my $outer_control_ip;
    
    if ($ELABINELAB) {
        libxmlrpc::Config({"server"  => $RPCSERVER,
			   "verbose" => 0,
			   "cert"    => $RPCCERT,
			   "portnum" => $RPCPORT});

322 323 324 325 326 327 328 329
	#
	# XXX force MAC to a string since simple XML parser may otherwise
	# think it is an int (i.e., all digits) or even a double (i.e.,
	# all digits with an 'e' in the middle).  Don't laugh, I spent a
	# lot of time figuring this out when it really happened!
	#
	my $macstr = RPC::XML::string->new($interfaces{0}[0]);

330
	my $rval = libxmlrpc::CallMethod("elabinelab", "newnode_info",
331
					 {"mac" => $macstr});
332 333 334 335 336 337 338 339 340 341 342 343 344

	if (!defined($rval)) {
	    print "XMPRPC server did not return control iface info!\n";
	    next NODE;
	}
	else {
	    #
	    # Grab what we need by searching the interface marked as
	    # the outer control network.
	    #
	    my %iface_array = %{ $rval->{"interfaces"} };
		
	    foreach my $mac (keys(%iface_array)) {
345 346
		my $aref   = $iface_array{$mac};
		my $ifrole = $aref->{"role"};
347

348
		if ($ifrole eq TBDB_IFACEROLE_OUTER_CONTROL()) {
349 350 351 352 353 354 355 356 357 358 359
		    $outer_control_ip = $aref->{"IP"};
		    last;
		}
	    }
	}
	if (!defined($outer_control_ip)) {
	    print "Could not determine outer control interface IP!\n";
	    next NODE;
	}
    }

360 361 362
    #
    # Okay, time to actually add the node!
    #
363 364 365 366 367
    my $uuid = NewUUID();
    if (!defined($uuid)) {
	print "Could not generate a UUID!\n";
	next NODE;
    }
368 369
    
    DBQueryFatal("INSERT INTO nodes SET node_id='$node_id', type='$type', " .
370
	"phys_nodeid='$node_id', role='$role', priority=$priority, " .
371
    	"eventstate='$state', op_mode='$opmode', uuid='$uuid', " .
372
	"def_boot_osid='$osid', " .
373
	"inception=now(), ".
374 375
	"state_timestamp=unix_timestamp(NOW()), " .
	"op_mode_timestamp=unix_timestamp(NOW())");
376

377 378
    DBQueryFatal("insert into node_hostkeys (node_id) ".
		 "values ('$node_id')");
379 380 381 382 383
    DBQueryFatal("insert into node_status ".
		 "(node_id, status, status_timestamp) ".
		 "values ('$node_id', 'down', now()) ");
    DBQueryFatal("insert into node_activity ".
		 "(node_id) values ('$node_id')");
384 385
    DBQueryFatal("insert into node_utilization ".
		 "(node_id) values ('$node_id')");
386

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    #
    # Copy data into the location_info table, if they provided any
    #
    if ($floor || $building || $loc_x || $loc_y || $contact
	|| $phone || $room) {

	#
	# Handle columns that could have come to us NULL (and thus are undef)
	#
	if (defined $floor) {
	    $floor = "'$floor'";
	} else {
	    $floor = "NULL";
	}
	if (defined $building) {
	    $building = "'$building'";
	} else {
	    $building = "NULL";
	}
	if (defined $room) {
	    $room = "'$room'";
	} else {
	    $room = "NULL";
	}

	DBQueryFatal("INSERT INTO location_info SET node_id='$node_id', " .
	    "floor=$floor, building=$building, loc_x=$loc_x, loc_y=$loc_y, ".
	    "contact='$contact', phone='$phone', room=$room;");
    }

417 418 419
    #
    # Add some vnodes
    #
420
    if (defined $nodenum && ($role eq "testnode")) {
421
	DBQueryFatal("INSERT INTO node_auxtypes set node_id='$node_id', " .
422
	    "type='pcvm', count=$virtnode_capacity");
423 424
    }

425
    while (my ($card, $aref) = each %interfaces) {
426
	my ($MAC, $iface_type, $max_speed, $duplex, $switch_id, $switch_card,
427
	    $switch_port, $cable, $len, $ifrole) = @$aref;
428
	my $iface = "eth$card";
429 430 431 432 433
	my $iface_IP;
	my $wire_type;
	my $iface_role;

	# Role from new_interfaces overrides anything we decide here.
434 435
	if (defined($ifrole)) {
	    $iface_role = $ifrole;
436 437
	}
	elsif ($iface eq $control_iface) {
438
	    $iface_role = TBDB_IFACEROLE_CONTROL();
439
	}
440 441 442 443 444 445 446 447 448 449 450 451 452
	else {
	    $iface_role = TBDB_IFACEROLE_EXPERIMENT();
	}

	# Okay, now set up some stuff for the inserts.
	if ($iface_role eq TBDB_IFACEROLE_CONTROL()) {
	    $iface_IP  = $IP;
	    $wire_type = "Control";
	}
	elsif ($iface_role eq TBDB_IFACEROLE_OUTER_CONTROL()) {
	    $iface_IP  = $outer_control_ip;
	    $wire_type = "OuterControl";
	}
453 454
	else {
	    # Experimental interfaces start with no speed set. 
Kirk Webb's avatar
Kirk Webb committed
455
	    $max_speed = 0;
456 457
	    $iface_IP  = "";
	    $wire_type = "Node";
458
	}
459

460 461
	DBQueryFatal("INSERT INTO interfaces SET node_id='$node_id', " .
	    "card=$card, port=1, mac='$MAC', IP='$iface_IP', " .
462
	    "interface_type='$iface_type', iface='$iface', uuid=UUID(), " .
Kirk Webb's avatar
Kirk Webb committed
463
	    "current_speed='$max_speed', duplex=$duplex, role='$iface_role'");
464

465 466 467
	DBQueryFatal("INSERT INTO interface_state SET node_id='$node_id', " .
	    "card=$card, port=1, iface='$iface'");

468 469 470 471
	if (!$switch_id) {
	    print "No switch found for ${node_id}:$iface - skipping\n";
	    next;
	}
472 473 474 475 476 477 478 479

	my $cable_len = "";
	if ($cable) {
	    $cable_len .= ", cable=$cable";
	}
	if ($len) {
	    $cable_len .= ", len=$len";
	}
480 481 482
	DBQueryFatal("INSERT INTO wires SET type='$wire_type', " .
	    "node_id1='$node_id', card1=$card, port1=1, " .
	    "node_id2='$switch_id', card2='$switch_card', " .
483
	    "port2='$switch_port' $cable_len");
484 485 486
    }

    #
487 488
    # Put it into hwdown for now - I would put them in reloading, but I'm
    # afriad the reload_daemon might do the wrong thing to them
489
    #
490
    system "$nalloc $pid $eid $node_id";
491 492 493 494

    #
    # Remove the node from the new_ tables
    #
495
    DBQueryFatal("DELETE FROM new_nodes WHERE new_node_id=$new_node_id");
496
    DBQueryFatal("DELETE FROM new_interfaces WHERE new_node_id=$new_node_id");
497

498
    print "$node_id succesfully added!\n";
499 500

    push @succeeded_nodes, $node_id;
Robert Ricci's avatar
Robert Ricci committed
501 502 503 504 505 506 507

    #
    # Only reboot regular nodes, not ops nodes
    #
    if ($role ne "ctrlnode") {
	push @reboot_IPs, $tempIP;
    }
508 509 510 511 512 513 514 515 516 517 518 519
}

#
# No point in restarting dhcpd, etc. if there are no nodes that succeeded
#
if (!@succeeded_nodes) {
    die "No nodes succeeded, exiting early\n";
}

#
# Re-generate dhcpd.conf
#
520
print "Re-generating dhcpd.conf\n";
521
if (system("$dhcpd_makeconf -i -r")) {
522 523
    die "Unable to open regenerate dhcpd config file\n";
}
524 525 526 527 528 529

print "Setting up nameserver\n";
my $named_rv = system "$named_setup";
if ($named_rv) {
    die "Error running named_setup - return value was $named_rv\n";
}
530

531 532 533 534 535 536 537 538 539
#
# Before we boot nodes into the MFS, we have to make sure they can mount
# NFS filesystems
#
print "Running exports_setup\n";
my $exports_rv = system "$exports_setup";
if ($exports_rv) {
    warn "WARNING - exports_setup returned $exports_rv";
}
540

541 542 543
#
# Start rebooting nodes
#
544 545 546 547 548 549 550 551 552 553 554 555
if (!$noreboot) {
    print "Rebooting nodes...\n";
    foreach my $IP (@reboot_IPs) {
	print "Rebooting $IP\n";
	system "$newnode_reboot $IP\n";
    }
}
if (!$quiet) {
    print "\n\n";
    print "Finished - when you are satisifed that the nodes are working\n";
    print "correctly, use nfree on boss to free them from the emulab-ops/hwdown\n";
    print "experiment.\n";
556
}
557

558
#
559 560 561
# TODO -
#    disable interfaces
#    console setup
562
#