libossetup_switch.pm.in 15.3 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2000-2010, 2018 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 35 36 37 38 39 40
#

package libossetup_switch;

use strict;

use lib '@prefix@/lib';
use libossetup;

use base qw(libossetup_handler);

use vars qw($AUTOLOAD);

use libdb;
use libtestbed;
use libossetup;
# XXX only works with newer version of libosload!
use libosload_new;
41
use libtblog_simple;
42
use Node;
43
use libreboot;
44 45 46 47 48 49 50
use English;
use Data::Dumper;
use overload ('""' => 'Stringify');

#
# A constructor for an object to handle all nodes of this type.
#
51 52 53 54
sub New($$$) {
    my ($class, $type, $parent) = @_;
    
    my $self = $class->SUPER::New($type, $parent);
55 56 57 58 59

    $self->{'OPLIST'} = {};

    bless($self, $class);

60
    print STDERR "$self: new object for $type\n";
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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
    return $self;
}

sub AUTOLOAD {
    my $self = shift;
    my $type = ref($self) or die("$self is not an object\n");

    my $name = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    if (@_) {
	return $self->{'HASH'}->{$name} = shift;
    }
    elsif (exists($self->{'HASH'}->{$name})) {
	return $self->{'HASH'}->{$name};
    }
    print STDERR "libossetup_switch: tried to access unknown slot $name\n";
    return undef;
}

sub AddNode($$)
{
    my ($self, $node) = @_;
    my $node_id = $node->node_id();

    $self->SUPER::AddNode($node);

    print STDERR "$self AddNode($node_id): so=".$node->_setupoperation()." and as=" . $node->allocstate() . "\n";

    #
    # Setup multi-operation list so that when WaitDone
    # gets called on a node, we don't free the node, but 
    # rather do the next operation!
    #
    if ($node->_setupoperation() == $libossetup::RELOAD) {
	$self->{OPLIST}->{$node_id} = [ $libossetup::RELOAD ];
	if ($node->allocstate() eq TBDB_ALLOCSTATE_RES_INIT_DIRTY()) {
	    # need to do a reconfig after the reload
	    push @{$self->{OPLIST}->{$node_id}}, $libossetup::RECONFIG;
	    print STDERR "$self AddNode($node_id): so=".$node->_setupoperation()." and as=" . $node->allocstate() . "\n";
	}
    }
    else {
	if ($node->allocstate() eq TBDB_ALLOCSTATE_RES_RECONFIG()
	    || $node->allocstate() eq TBDB_ALLOCSTATE_RES_INIT_DIRTY()) {
	    $self->{OPLIST}->{$node_id} = [ $libossetup::RECONFIG ];
	    $node->_setupoperation($libossetup::RECONFIG);
	}
	elsif ($node->allocstate() ne TBDB_ALLOCSTATE_RES_READY()) {
	    # only reboot node if assign_wrapper just pulled it into expt.
	    # (e.g. it isnt ALLOCSTATE_RES_READY)
	    $self->{OPLIST}->{$node_id} = [ $libossetup::REBOOT ];
	    $node->_setupoperation($libossetup::REBOOT);
	}
    }

    print STDERR "$self AddNode OPLIST: " . Dumper($self->{OPLIST});

    # don't ever retry switches
    $node->_retrycount(0);
    #
    # XXX should ask libosload_new
    # These guys take a long time
    #
    $node->_maxwait(15*60);
    $node->_setupstatus($libossetup::SETUP_OKAY);

    return 0;
}

sub LightUpNodes($@)
{
    #
    # This needs to trigger reloads and reconfigs as appropriate.
    # If a reload is going to be followed by a reconfig, then we need to 
    # tell osload that, AND make the reconfig happen via the multi-op thing.
    #
    my ($self, @nodelist) = @_;
    my @children = ();
    my @failed   = ();
    my $parent = $self->parent();

    #
    # Set up lists of what we are going to do.
    #
    my %reloads   = ();
    my %reboots   = ();
    my %reconfigs = ();
    my %rebooted  = ();

    foreach my $node (@nodelist) {
	my $node_id = $node->node_id();
	my $op      = $node->_setupoperation();
	my $action;

	# Nothing to do.
	next
	    if ($op == $NOSTATE);

	if ($op == $RELOAD) {
	    my $image = $node->_loadimage();
		
	    if (!exists($reloads{$image->imageid()})) {
		$reloads{$image->imageid()} = [];
	    }
	    push(@{ $reloads{$image->imageid()} }, $node);
	    $action = "reloaded with $image";
	}
	elsif ($op == $REBOOT) {
	    $reboots{$node_id} = $node;
	    $action = "rebooted";
	}
	elsif ($op == $RECONFIG) {
	    $reconfigs{$node_id} = $node;
	    $action = "reconfiged";
	}
	print STDERR "$node_id will be $action\n";
    }

    # XXX Caller wants a list. 
    return 0
	if ($parent->impotent());
    
    #
    # Now fire them off.
    #
    foreach my $imageid ( keys(%reloads) ) {
	my @nlist = @{ $reloads{$imageid} };
	my @list  = ();
	my %nodeflags = ();

	foreach my $node (@nlist) {
	    my $node_id = $node->node_id();
	    # The osload library gets ids.
	    push(@list, $node_id);
	    
	    $node->SetAllocState(TBDB_ALLOCSTATE_RES_RELOAD());
	    # No point in reboot/reconfig obviously, since node will reboot!
	    delete $reboots{$node_id};
	    delete $reconfigs{$node_id};
	    $rebooted{$node_id} = 1;
202 203 204 205 206 207 208 209 210

	    # if we're going to do a reconfig after the load, stop in the ROM
	    # monitor so we can avoid an extra reboot
	    my $oplistref = $self->{OPLIST}->{$node_id};
	    if (scalar(@$oplistref) > 1 
		&& $oplistref->[0] == $libossetup::RELOAD
		&& $oplistref->[1] == $libossetup::RECONFIG) {
		$nodeflags{$node_id}{'reconfig_will_follow'} = 1;
	    }
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	}

	my %reload_args     = ();
	my $reload_failures = {};

	$reload_args{'debug'}     = $parent->debug();
	$reload_args{'asyncmode'} = 1;
	$reload_args{'imageid'}   = $imageid;
	$reload_args{'nodelist'}  = [ @list ];
	$reload_args{'nodeflags'} = \%nodeflags;

	my $pid;
	my $coderef;
	if (EmulabFeatures->FeatureEnabled("NewOsload",$parent->user(),
					   $parent->group(),
					   $parent->experiment())) {
	    ($parent->loadobj())->debug($parent->debug());
228 229 230 231 232 233

	    # add a few more things for feature checks down the line:
	    $reload_args{user} = $parent->user();
	    $reload_args{group} = $parent->group();
	    $reload_args{experiment} = $parent->experiment();

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 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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	    $pid = ($parent->loadobj())->osload(\%reload_args,$reload_failures);
	    print STDERR "$self: kicking off wait for osload pid $pid\n";
	    $coderef = sub {
		my $childpid = shift;
		return ($parent->loadobj())->osload_wait($childpid);
	    };
	}
	else {
	    $pid = osload(\%reload_args, $reload_failures);
	    $coderef = \&osload_wait;
	}
	push(@children, [ $pid, $coderef,			  ,
			  [ @list ], $reload_failures ]);
	sleep(5);
    }

    #
    # Fire off the reboots.
    # 
    if (keys(%reboots)) {
	foreach my $node_id (keys(%reboots)) {
	    my $node = $self->node($node_id);

	    if (defined($node->allocstate()) &&
		$node->allocstate() eq TBDB_ALLOCSTATE_RES_INIT_CLEAN()) {
		$node->SetAllocState(TBDB_ALLOCSTATE_RES_REBOOT_CLEAN());
	    }
	    else {
		$node->SetAllocState(TBDB_ALLOCSTATE_RES_REBOOT_DIRTY());
	    }
	    # Needed for vnode_setup.
	    $rebooted{$node_id} = 1;
	}

	my @list            = keys(%reboots);
	my %reboot_args     = ();
	my $reboot_failures = {};

	$reboot_args{'debug'}     = $parent->debug();
	$reboot_args{'waitmode'}  = 0;
	$reboot_args{'asyncmode'} = 1;
	$reboot_args{'nodelist'}  = [ @list ];

	my $pid = nodereboot(\%reboot_args, $reboot_failures);
	push(@children, [ $pid, \&nodereboot_wait,
			  [ @list ], $reboot_failures ]);
	sleep(2);
    }

    #
    # Fire off the reconfigs.
    #
    if (keys(%reconfigs)) {
	my @list            = keys(%reconfigs);
	my $reconfig_failures = {};

	my $switchobj = $self->switchobj();

	foreach my $node_id (@list) {
	    my $node = Node->Lookup($node_id);
	    $switchobj->AddNode($node,undef,undef);
	    $switchobj->SetupReconfigure($node);
	    my $pid = $switchobj->Reconfigure($node,0);
	    push @children, [ $pid, sub {
		my ($waitstatus,$retval,@output);
		while (1) { 
		    $switchobj->WaitForNode($node);
		    print STDERR "$switchobj->WaitForNode($node_id) -> $waitstatus";
		    if ($waitstatus > 0) {
			return $retval;
		    }
		    elsif ($waitstatus == 0) {
			# we need to keep waiting for this node, so don't check
			# the db
			;
		    }
		    sleep(2);
		}
			      },
			      [ $node_id ], $reconfig_failures ];
	}
    }

    #
    # Wait for all of the children to exit. We look at the $pid to know if
    # command failed/ended immediately; otherwise we need to wait on it.
    # For any failures, record the node failures for later so that we do
    # not wait for them needlessly.
    #
    while (@children) {
	my ($pid, $waitfunc, $listref, $hashref) = @{ pop(@children) };

	# This is not likely to happen.
	next
	    if ($pid == 0);

	if ($pid > 0) {
	    next
		if (! &$waitfunc($pid));
	}
	
	#
	# Failure. Record the failures for later. If the $pid<0 then the
	# entire list failed. Otherwise, have to scan the return hash to
	# find the failures.
	#
	my @nlist = ();
	
	if ($pid < 0) {
	    @nlist = @{ $listref };
	}
	else {
	    foreach my $node_id (keys(%{ $hashref })) {
		push(@nlist, $node_id)
		    if ($hashref->{$node_id});
	    }
	}

	#
	# Mark all of the failed nodes so that the caller knows there
	# was a failure. 
	# 
	foreach my $node_id (@nlist) {
	    my $node = $self->node($node_id);

	    $node->SetAllocState(TBDB_ALLOCSTATE_DOWN());
	}
    }

    return scalar(@failed);
}

#
# Wait function signals some local cluster nodes are done waiting.
#
sub WaitDone($@)
{
    my ($self, @nodelist) = @_;
    my $parent     = $self->parent();
    my $experiment = $parent->experiment();
    my $pid        = $experiment->pid();
    my $eid        = $experiment->eid();

    #
    # Then per node processing.
    #
    foreach my $node (@nodelist) {
	my $node_id     = $node->node_id();
	my $setupstatus = $node->_setupstatus();
	my $eventstate  = $node->eventstate();

	print STDERR "$self: WaitDone: $node_id,$setupstatus,$eventstate\n";

387 388
	if ($eventstate eq TBDB_NODESTATE_ISUP()
	    && $setupstatus == $libossetup::SETUP_OKAY) {
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 417 418 419 420 421 422 423 424 425 426
	    #
	    # If there is another op in our list, keep going with that op -- 
	    # i.e., keep the node in our todo list!
	    #
	    my $op = shift @{$self->{OPLIST}->{$node_id}};
	    if (@{$self->{OPLIST}->{$node_id}}) {
		my $opname = "RELOAD";
		if ($op == $libossetup::RECONFIG) {
		    $opname = "RECONFIG";
		}
		elsif ($op == $libossetup::REBOOT) {
		    $opname = "REBOOT";
		}
		print STDERR "$self OPLIST: " . Dumper($self->{OPLIST});
		print STDERR "$self WaitDone: $node_id finished $opname; doing next op!\n";

		$node->_setupoperation($self->{OPLIST}->{$node_id}->[0]);
		next;
	    }

	    #
	    # Otherwise, handle the node normally!
	    #

	    #
	    # Must call the generic WaitDone handler too.
	    #
	    $self->SUPER::WaitDone(@nodelist);

	    print "$node_id is alive and well\n";
	    $node->SetBootStatus(NODEBOOTSTATUS_OKAY);
	    $node->SetAllocState(TBDB_ALLOCSTATE_RES_READY());
	    # Set this so we know a successful reboot was done.
	    # Important for VMs that depend on this node.
	    $node->_rebooted(1)
		if ($node->_setupoperation() != $libossetup::NOSTATE);
	    next;
	}
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	elsif ($setupstatus != $libossetup::SETUP_OKAY) {
	    #
	    # Our last op failed; don't continue!
	    #
	    my $op = shift @{$self->{OPLIST}->{$node_id}};
	    my $opname = "RELOAD";
	    if ($op == $libossetup::RECONFIG) {
		$opname = "RECONFIG";
	    }
	    elsif ($op == $libossetup::REBOOT) {
		$opname = "REBOOT";
	    }
	    tbwarn "$self WaitDone: $node_id failed $opname; NOT doing more ops!\n";
	    $self->{OPLIST}->{$node_id} = [];
	}
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 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

	#
	# If we had an error, don't continue!  SUPER method handles that.
	#
	# Must call the generic WaitDone handler too.
	#
	$self->SUPER::WaitDone(@nodelist);

	# Fall through on failure.	
	if ($eventstate eq TBDB_NODESTATE_TBFAILED()) {
	    tbwarn("$node_id reported a TBFAILED event\n");
	}
	else {
	    tbwarn("$node_id failed to boot\n");
	}
	$node->SetBootStatus(NODEBOOTSTATUS_FAILED);

	#
	# Reload failures are terminal.
	#
	if ($node->_canfail() && $setupstatus != $libossetup::RELOAD_FAILED &&
	    !($experiment->canceled() || $parent->noretry())) {

	    $parent->add_failed_node_inform_user($node_id);
	    $parent->add_failed_node_nonfatal($node_id);
	    tbnotice("Continuing with experiment setup anyway ...\n");
	    next;
	}

	#
	# XXX don't do this for now!
	#
	# If the user has picked a standard image and it fails to boot,
	# something is wrong, so reserve it to checkup experiment. If the
	# image belongs to the user, then we assume its the image at fault,
	# and allow it to be returned to the pool (caller, tbswap will end
	# doing the nfree on nodes with a DOWN allocstate).
	#
	my $osinfo = $node->_bootosinfo();
	if (0 && (! defined($osinfo->pid()) || $osinfo->pid() eq TBOPSPID())) {
	    $node->MarkAsIll();
	    $node->InsertNodeLogEntry($parent->user(),
				      TB_DEFAULT_NODELOGTYPE(),
				      "'Moved to hwcheckup by os_setup; ".
				      "failed to boot image for $osinfo " .
				      "in $pid/$eid'");

	    $parent->add_failed_node_inform_tbopsfatal($node_id);
	}
	else {
	    $parent->add_failed_node_inform_tbopswarn($node_id);
	}
	print STDERR "$self: WaitDone: setting alloc state DOWN for $node_id\n";
	$node->SetAllocState(TBDB_ALLOCSTATE_DOWN());
	$self->IncrFailCount();
	$parent->IncrFailCount();
	if ($setupstatus == $libossetup::RELOAD_FAILED) {
	    $parent->add_failed_node_reload($node_id);
	}
	else {
	    $parent->add_failed_node_fatal($node_id);
	}
    }
    return 0;
}

#
# Function to retry a node, called by the wait function. 
#
sub Retry($$)
{
    my ($self, $node) = @_;
    my $node_id = $node->node_id();

    return -1;
}

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
package libossetup_hp5406;
use base qw(libossetup_switch);

sub New($$)
{
    my ($class, $parent) = @_;
    my $type = "hp5406";
    require libosload_hp5406;

    my $self = $class->SUPER::New($type, $parent);
    bless($self, $class);

    my $switchobj = libosload_hp5406->New(undef, $type);
    $self->switchobj($switchobj);
    $switchobj->debug(($self->parent())->debug());

    return $self;
}

package libossetup_mlnx_sn2410;
use base qw(libossetup_switch);

sub New($$)
{
    my ($class, $parent) = @_;
    my $type = "mlnx_sn2410";
    require libosload_mlnx_sn2410;

    my $self = $class->SUPER::New($type, $parent);
    bless($self, $class);

    my $switchobj = libosload_mlnx_sn2410->New(undef, $type);
    $self->switchobj($switchobj);
    $switchobj->debug(($self->parent())->debug());

    return $self;
}

package libossetup_dell_s3048;
use base qw(libossetup_switch);

sub New($$)
{
    my ($class, $parent) = @_;
    my $type = "dell_s3048";
    require libosload_dell_s3048;

    my $self = $class->SUPER::New($type, $parent);
    bless($self, $class);

    my $switchobj = libosload_dell_s3048->New(undef, $type);
    $self->switchobj($switchobj);
    $switchobj->debug(($self->parent())->debug());

    return $self;
}

package libossetup_dell_s4048;
use base qw(libossetup_switch);

sub New($$)
{
    my ($class, $parent) = @_;
    my $type = "dell_s4048";
    require libosload_dell_s4048;

    my $self = $class->SUPER::New($type, $parent);
    bless($self, $class);

    my $switchobj = libosload_dell_s4048->New(undef, $type);
    $self->switchobj($switchobj);
    $switchobj->debug(($self->parent())->debug());

    return $self;
}

595
1;