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

#
4
# Copyright (c) 2003-2017 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 23
# 
# {{{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/>.
# 
# }}}
24 25 26 27 28 29
#

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

30
use lib '@prefix@/lib';
31 32

use libdb;
33
use libtestbed;
34
use libxmlrpc;
35
use NodeType;
Robert Ricci's avatar
Robert Ricci committed
36
use English;
37
use OSImage;
38
use Getopt::Std;
39
use EmulabFeatures;
40 41 42
use EmulabConstants;
use Interface;
use Node;
43 44 45

use strict;

46 47
sub fatal($);

48 49
my $TB         = "@prefix@";
my $ELABINELAB = @ELABINELAB@;
50
my $GENIRACK   = @PROTOGENI_GENIRACK@;
51 52 53
my $RPCSERVER  = "@OUTERBOSS_NODENAME@";
my $RPCPORT    = "@OUTERBOSS_XMLRPCPORT@";
my $RPCCERT    = "@OUTERBOSS_SSLCERTNAME@";
54 55

my $switchmac = "$TB/sbin/switchmac";
56 57 58 59
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";
60 61 62
my $nalloc = "$TB/bin/nalloc";
my $nfree = "$TB/bin/nfree";
my $dhcpd_makeconf = "$TB/sbin/dhcpd_makeconf";
63
my $exports_setup = "$TB/sbin/exports_setup";
64 65 66

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

67 68
my $useScriptFeature = $GENIRACK ||
    EmulabFeatures->FeatureEnabled("ScriptNewNodes", undef, undef, undef);
69

70
#
71
# MFS to boot the nodes into initially
72
#
73 74
my $MFS_INITIAL = TB_OSID_FREEBSD_MFS();
my $OS_OPSNODE  = "OPSNODE-BSD";
75 76

#
77
# Initial event system state to put the nodes into
78
#
79 80
my $STATE_INITIAL = TBDB_NODESTATE_SHUTDOWN;
my $STATE_OPSNODE = TBDB_NODESTATE_ISUP;
81 82 83 84 85 86

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

87 88 89 90 91
#
# Default number of vnodes that can be hosted on any pnode
#
my $DEFAULT_PCVM_COUNT = 20;

92 93 94 95 96 97 98 99
#
# Experiments we might put nodes into
#
my $PID_HWDOWN = NODEDEAD_PID();
my $EID_HWDOWN = NODEDEAD_EID();
my $PID_OPSNODE = TBOPSPID();
my $EID_OPSNODE = "opsnodes";

100 101
# Turn off line buffering on output
$| = 1;
102

103 104 105 106
if (!TBAdmin()) {
    die "Sorry, only testbed administrators can run this script!\n";
}

107 108 109
#
# Handle command-line options
#
110 111 112
sub usage() {
    die "Usage: $0 [-f] <node_id> ...\n";
}
113 114
my $noreboot = 0;
my $quiet    = 0;
115 116
my $force_unconnected_interfaces = 0;
my %options = ();
117
if (! getopts("fnqw", \%options)) {
118 119 120 121 122
    usage();
}
if ($options{f}) {
    $force_unconnected_interfaces = 1;
}
123 124 125 126 127 128
if ($options{'n'}) {
    $noreboot = 1;
}
if ($options{'q'}) {
    $quiet = 1;
}
129 130 131 132 133
if ($options{'w'}) {
    # See the web page that calls this. From that interface, we
    # use the old style newnode. 
    $useScriptFeature = 0;
}
134

135 136 137 138 139 140
#
# We seem to have to do this so that ssh gets proper root permissions to read
# the key file. Argh.
#
$UID = $EUID = 0;

141
if (@ARGV < 1) {
142
    usage();
143 144
}

145 146 147 148 149 150 151 152 153 154
#
# 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"
}

155 156 157 158 159
my @node_ids = @ARGV;

#
# Now, loop through the nodes given, and add each one
#
160
my (@succeeded_nodes, @reboot_IPs);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
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
    #
177
    $query_result = DBQueryFatal("SELECT new_node_id, type, IP, temporary_IP, ".
178
	"floor, building, loc_x, loc_y, contact, phone, room, role " .
179
	"FROM new_nodes WHERE node_id='$node_id'");
180 181 182 183
    if (!$query_result->num_rows()) {
	warn "Node $node_id failed: No pending node with that name exists!\n";
	next NODE;
    }
184
    my ($new_node_id, $type, $IP, $tempIP, $floor, $building, $loc_x, $loc_y,
185
	$contact, $phone, $room, $role) = $query_result->fetchrow();
186

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    #
    # 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;
    }

208 209 210 211
    #
    # Make sure that the new node is of a valid type, and grab a few other
    # things to fill in as initial values
    #
212 213 214 215 216
    my $typeinfo = NodeType->Lookup($type);
    if (!defined($typeinfo)) {
	warn "No such nodetype $type is defined in the DB!\n";
	next NODE;
    }
217
    my ($control_iface,$virtnode_capacity,$adminmfs,$adminmfs_osid);
218

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
    #
    # If the control interface was explicitly specified, use that.
    # Otherwise, look for a node_type default.
    #
    $query_result = DBQueryFatal("SELECT card from new_interfaces ".
				 "WHERE new_node_id='$new_node_id'".
				 "  AND role='ctrl'");
    if ($query_result->num_rows()) {
	my ($card) = $query_result->fetchrow();
	$control_iface = "eth$card";
    } else {
	$typeinfo->control_interface(\$control_iface);
    }
    if (!$control_iface) {
	    warn "No control interface for $type is defined in the DB!\n";
	    next NODE;
235
    }
236

237 238 239 240
    if ($typeinfo->virtnode_capacity(\$virtnode_capacity)) {
	warn "Failure looking up virtnode_capacity for $type in the DB!\n";
	next NODE;
    }
241
    if ($typeinfo->adminmfs_osid(\$adminmfs_osid)) {
242
	warn "Failure looking up adminmfs_osid for $type in the DB!\n";
243 244
	next NODE;
    }
245 246
    $virtnode_capacity = 0
	if (!defined($virtnode_capacity));
247 248 249

    # Find object for the adminfs.
    if (defined($adminmfs_osid)) {
250
	$adminmfs = OSImage->Lookup($adminmfs_osid);
251 252
    }
    else {
253
	$adminmfs = OSImage->Lookup(TBOPSPID(), $MFS_INITIAL);
254
    }
255 256 257 258

    #
    # Grab the node's MACs from the new_interfaces table
    #
259
    $query_result = DBQueryFatal("SELECT card, mac, guid, interface_type, " .
260
	"switch_id, switch_card, switch_port, cable, len, role, port, IP " .
261
	"FROM new_interfaces WHERE new_node_id='$new_node_id'");
262 263 264 265 266 267
    if (!$query_result->num_rows()) {
	warn "Node $node_id failed: Must have at least one interface!\n";
	next NODE;
    }

    my %interfaces;
268
    while (my ($card, $MAC, $guid, $iface_type, $switch_id, $switch_card,
269
	    $switch_port, $cable, $len, $ifrole, $port, $iface_IP) = $query_result->fetchrow()) {
270 271 272
	#
	# Get some more information about this interface type
	#
273
	my $iface_query = DBQueryFatal("SELECT max_speed, full_duplex, " .
274
	    "connector FROM interface_types WHERE type='$iface_type'");
275
	if (!$iface_query->num_rows()) {
276
	    warn "Node $node_id failed: Interface $card is of unknown type " .
277
	    	"$iface_type\n";
278 279 280
	    next NODE;
	}

281
	my ($max_speed, $full_duplex, $connector) = $iface_query->fetchrow();
282

283 284 285 286
	#
	# Do a sanity check - make sure that we have a switch recorded for all
	# experimental interfaces
	#
287
	unless ($force_unconnected_interfaces || ($role eq "ctrlnode")) {
288
	    my $iface = "eth$card";
289 290
	    if (($iface ne $control_iface) && ($connector !~ /wireless/i)
		&& !$switch_id) {
291 292 293 294 295 296
		warn "Node $node_id failed: Don't know which switch " .
		    "card $iface is connected to\n";
		next NODE;
	    }
	}

297 298 299
	#
	# Stash it away...
	#
300
	$interfaces{$card} = [$MAC, $guid, $iface_type, $max_speed, $full_duplex,
301 302
			      $switch_id, $switch_card, $switch_port, $cable,
			      $len, $ifrole, $port, $iface_IP];
303 304 305 306 307 308

    }

    #
    # Make up a priority (just used for sorting)
    #
309 310
    $node_id =~ /^(.*\D)(\d+)$/;
    my ($prefix,$nodenum) = ($1, $2);
311
    my $priority;
312 313
    if (defined $nodenum) {
	$priority = $nodenum;
314 315 316 317
    } else {
	$priority = 1;
    }

318 319 320 321 322 323 324 325 326 327 328 329 330
    #
    # 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;
    }

    #
331
    # Figure out what osid/opmode this node should run
332
    #
333
    my ($osid,$osid_vers,$opmode);
334
    
335
    if ($role eq "ctrlnode") {
336
	my $osimage = OSImage->Lookup(TBOPSPID(), $OS_OPSNODE);
337

338 339 340
	$osid      = $osimage->osid();
	$osid_vers = $osimage->version();
	$opmode    = $osimage->op_mode();
341 342
    }
    else {
343
	$osid      = $adminmfs->osid();
344
	$osid_vers = $adminmfs->vers();
345
	$opmode    = $adminmfs->op_mode();
346 347 348 349 350 351 352 353 354 355 356 357
    }

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

358 359 360 361 362 363 364
    #
    # 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;
    
365
    if ($ELABINELAB && $role ne "ctrlnode") {
366 367 368 369 370
        libxmlrpc::Config({"server"  => $RPCSERVER,
			   "verbose" => 0,
			   "cert"    => $RPCCERT,
			   "portnum" => $RPCPORT});

371 372 373 374 375 376 377 378
	#
	# 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]);

379
	my $rval = libxmlrpc::CallMethod("elabinelab", "newnode_info",
380
					 {"mac" => $macstr});
381 382

	if (!defined($rval)) {
383
	    print "XMLRPC server did not return control iface info!\n";
384 385 386 387 388 389 390 391 392 393
	    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)) {
394 395
		my $aref   = $iface_array{$mac};
		my $ifrole = $aref->{"role"};
396

397
		if ($ifrole eq TBDB_IFACEROLE_OUTER_CONTROL()) {
398 399 400 401 402 403 404 405 406 407 408
		    $outer_control_ip = $aref->{"IP"};
		    last;
		}
	    }
	}
	if (!defined($outer_control_ip)) {
	    print "Could not determine outer control interface IP!\n";
	    next NODE;
	}
    }

409 410 411
    #
    # Okay, time to actually add the node!
    #
412 413 414 415 416
    my $uuid = NewUUID();
    if (!defined($uuid)) {
	print "Could not generate a UUID!\n";
	next NODE;
    }
417 418
    
    DBQueryFatal("INSERT INTO nodes SET node_id='$node_id', type='$type', " .
419
	"phys_nodeid='$node_id', role='$role', priority=$priority, " .
420
    	"eventstate='$state', op_mode='$opmode', uuid='$uuid', " .
421
	"def_boot_osid='$osid',def_boot_osid_vers='$osid_vers', " .
422
	"inception=now(), ".
423 424
	"state_timestamp=unix_timestamp(NOW()), " .
	"op_mode_timestamp=unix_timestamp(NOW())");
425

426 427
    DBQueryFatal("insert into node_hostkeys (node_id) ".
		 "values ('$node_id')");
428 429 430 431 432
    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')");
433 434
    DBQueryFatal("insert into node_utilization ".
		 "(node_id) values ('$node_id')");
435

436 437 438 439 440
    my $node = Node->Lookup($node_id);
    if (!defined($node)) {
	fatal("Could not lookup node after creating it");
    }

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
    #
    # 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;");
    }

471 472 473
    #
    # Add some vnodes
    #
474
    if (defined $nodenum && ($role eq "testnode")) {
475
	DBQueryFatal("INSERT INTO node_auxtypes set node_id='$node_id', " .
476
	    "type='pcvm', count=$virtnode_capacity");
477 478
    }

479
    while (my ($card, $aref) = each %interfaces) {
480 481 482
	my ($MAC, $guid, $iface_type, $max_speed, $duplex,
	    $switch_id, $switch_card, $switch_port, $cable, $len,
	    $ifrole, $port, $iface_IP) = @$aref;
483
	my $iface = "eth$card";
484 485
	my $wire_type;
	my $iface_role;
486
	my $current_speed = $max_speed;
487 488

	# Role from new_interfaces overrides anything we decide here.
489 490
	if (defined($ifrole)) {
	    $iface_role = $ifrole;
491 492
	}
	elsif ($iface eq $control_iface) {
493
	    $iface_role = TBDB_IFACEROLE_CONTROL();
494
	}
495 496 497 498 499 500 501 502 503 504 505 506 507
	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";
	}
508 509
	else {
	    # Experimental interfaces start with no speed set. 
510
	    $current_speed = 0;
511
	    $wire_type = "Node";
512
	}
513 514 515
	if (! defined($iface_IP)) {
	    $iface_IP  = "";
	}
Leigh Stoller's avatar
Leigh Stoller committed
516
	if (!$useScriptFeature || !defined($port))
517 518 519
	{
	    $port = 1;
	}
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
	my $ifaceargs = {
	    "node_id"     => $node_id,
	    "mac"         => $MAC,
	    "IP"          => $iface_IP,
	    "type"        => $iface_type,
	    "iface"       => $iface,
	    "max_speed"   => $current_speed / 1000,
	    "duplex"      => $duplex ? "full" : "half",
	    "role"        => $iface_role,
	};
	if (defined($guid)) {
	    $ifaceargs->{"guid"} = $guid;
	}
	my $interface1 = Interface->Create($node, $ifaceargs);
	if (!defined($interface1)) {
	    fatal("Could not create interface for node");
	}
537
	if (!$switch_id) {
Jonathon Duerig's avatar
Jonathon Duerig committed
538
	    print "No switch found for ${node_id}:$iface - skipping wire\n";
539 540
	    next;
	}
541 542 543 544 545 546
	#
	# Create interface for switch side.
	#
	my $switch = Node->Lookup($switch_id);
	if (!defined($switch)) {
	    fatal("Cannot lookup switch $switch_id");
547
	}
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
	my $interface2 = Interface->Lookup($switch, $switch_card, $switch_port);
	if (!defined($interface2)) {
	    my $itype = Interface::Type->GenericType($max_speed);
	    if (!defined($itype)) {
		fatal("Cannot determing a generic interface type for $max_speed");
	    }
	    $ifaceargs = {
		"node_id"     => $switch_id,
		"type"        => $itype->type(),
		"iface"       => $switch_card . "/" . $switch_port,
		"max_speed"   => $max_speed / 1000,
		"duplex"      => $duplex ? "full" : "half",
		"role"        => TBDB_IFACEROLE_OTHER(),
		"mac"         => "000000000000",
	    };
	    $interface2 = Interface->Create($switch, $ifaceargs);
	    if (!defined($interface2)) {
		fatal("Could not create interface for switch side");
	    }
567
	}
568
	if (! $useScriptFeature) {
569 570 571 572 573 574
	    my $wireargs = {
		"card1"       => $card,
		"port1"       => $port,
		"card2"       => $switch_card,
		"port2"	      => $switch_port,
	    };
575 576 577 578 579 580 581 582
	    $wireargs->{"cable"}  = $cable if (defined($cable));
	    $wireargs->{"length"} = $len if (defined($len));

	    my $wire = Interface::Wire->Create($interface1, $interface2,
					       $wire_type, $wireargs);
	    if (!defined($wire)) {
		fatal("Could not create wire for $interface1,$interface2");
	    }
583
	}
584 585 586
    }

    #
587
    # Put it into hwdown for now - I would put them in reloading, but I'm
588
    # afraid the reload_daemon might do the wrong thing to them
589
    #
590
    system "$nalloc -f $pid $eid $node_id";
591 592 593 594

    #
    # Remove the node from the new_ tables
    #
595
    DBQueryFatal("DELETE FROM new_nodes WHERE new_node_id=$new_node_id");
596
    DBQueryFatal("DELETE FROM new_interfaces WHERE new_node_id=$new_node_id");
597

598
    print "$node_id succesfully added!\n";
599 600

    push @succeeded_nodes, $node_id;
601 602 603 604

    #
    # Only reboot regular nodes, not ops nodes
    #
Jonathon Duerig's avatar
Jonathon Duerig committed
605
    if ($role ne "ctrlnode" && defined($tempIP)) {
606 607
	push @reboot_IPs, $tempIP;
    }
608 609 610 611 612 613 614 615 616 617 618 619
}

#
# 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
#
620
print "Re-generating dhcpd.conf\n";
621
if (system("$dhcpd_makeconf -i -r")) {
622 623
    die "Unable to open regenerate dhcpd config file\n";
}
624 625 626 627 628 629

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

631 632 633 634 635 636 637 638 639
#
# 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";
}
640

641 642 643
#
# Start rebooting nodes
#
644
if (!$noreboot && @reboot_IPs) {
645 646 647 648 649 650 651 652 653 654 655
    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";
656
}
657 658 659 660 661 662 663 664 665 666
exit(0);

sub fatal($)
{
    my ($mesg) = @_;

    print STDERR "*** $0:\n".
	         "    $mesg\n";
    exit(-1);
}
667

668
#
669 670 671
# TODO -
#    disable interfaces
#    console setup
672
#