libossetup.pm.in 56.6 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2000-2015 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
#
package libossetup;

use strict;
use Exporter;
28 29 30 31 32
use vars qw(@EXPORT $AUTOLOAD
	    $NOSTATE $RELOAD $RECONFIG $REBOOT
	    $RELOAD_FAILED $RECONFIG_FAILED $REBOOT_FAILED $SETUP_FAILED
	    $SETUP_OKAY);
use base qw( Exporter );
33

34 35 36 37
@EXPORT = qw(&die_noretry
	     $NOSTATE $RELOAD $RECONFIG $REBOOT
	     $RELOAD_FAILED $RECONFIG_FAILED $REBOOT_FAILED $SETUP_FAILED
	     $SETUP_OKAY);
38 39 40 41 42 43

use libdb;
use libtestbed;
use libreboot;
use libosload;
use Node;
44
use NodeType;
45
use libtblog;
46
use Logfile;
47 48
use English;
use Data::Dumper;
49
use EmulabFeatures;
50
use Image;
51 52 53 54 55

# Configure variables
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $NODEREBOOT  = "$TB/bin/node_reboot";
Leigh B Stoller's avatar
Leigh B Stoller committed
56
my $VNODESETUP  = "$TB/sbin/vnode_setup";
57
my $ELABINELAB  = "$TB/sbin/elabinelab";
58
my $TBADB       = "$TB/bin/tbadb";
59

60
# Setup Operation Flags
Leigh B Stoller's avatar
Leigh B Stoller committed
61
$NOSTATE    = 0x00;
62
$RELOAD     = 0x01;
63 64 65
$RECONFIG   = 0x02;
$REBOOT     = 0x04;

66 67 68 69 70 71 72
# Setup Result Flags
$SETUP_OKAY      = 0x00;
$RELOAD_FAILED   = 0x01;
$RECONFIG_FAILED = 0x02;
$REBOOT_FAILED   = 0x04;
$SETUP_FAILED    = 0x08;

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
#
# Used to die with a -1 return code, to indicate to caller (tbswap)
# that the failure is not likely to be fixed with another attempt.
#
sub die_noretry($;$)
{
    my $parms = {};
    $parms = shift if ref $_[0] eq 'HASH';
    my ($mesg) = shift;
    tberror($parms, $mesg);
    KillChildren();
    exit(-1);
}

#
# The point of this is to create a data structure that we can
# pass around. 
#
sub New($$$@)
{
    my ($class, $user, $experiment, @nodelist) = @_;

    my $self              = {};
    $self->{'USER'}       = $user;
    $self->{'EXPT'}       = $experiment;
Leigh B Stoller's avatar
Leigh B Stoller committed
98 99
    $self->{'GROUP'}      = (defined($experiment) ?
			     $experiment->GetGroup() : undef);
100 101
    $self->{'HASH'}       = {};
    $self->{'NODES'}      = {};
Leigh B Stoller's avatar
Leigh B Stoller committed
102
    $self->{'PHYSNODES'}  = {};
103
    $self->{'FAILED'}     = {};
104
    $self->{'FAILCOUNT'}  = 0;
105 106
    $self->{'OSMAP'}      = {};
    $self->{'TYPECACHE'}  = {};
Leigh B Stoller's avatar
Leigh B Stoller committed
107
    $self->{'OPLIST'}     = {};
108 109 110 111 112 113 114 115

    # For informing users of failed nodes.
    $self->{'INFORM_USER'}       = {};
    $self->{'INFORM_TBOPSWARN'}  = {};
    $self->{'INFORM_TBOPSFATAL'} = {};

    foreach my $node (@nodelist) {
	$self->{'NODES'}->{$node->node_id()} = $node;
Leigh B Stoller's avatar
Leigh B Stoller committed
116 117 118

	# Init some per-node stuff.
	$node->_rebooted(0);
119
	$node->_vnodecount(0);
120
	$node->_reloaded(0);
Leigh B Stoller's avatar
Leigh B Stoller committed
121
	$node->_bootlog(undef);
122 123 124 125 126
    }

    bless($self, $class);
    # XXX
    $self->noretry(0);
Leigh B Stoller's avatar
Leigh B Stoller committed
127
    $self->canceled(0);
128
    $self->aborted(0);
129
    $self->realreboot(0);
130 131 132 133 134 135 136 137

    if (EmulabFeatures->FeatureEnabled("NewOsload",$user,
				       $self->{'GROUP'},$experiment)) {
	require libosload_new;
	# XXX -- @nodelist
	$self->loadobj(libosload_new->New());
    }

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    return $self;
}
# Break circular reference someplace to avoid exit errors.
sub DESTROY {
    my $self = shift;

    foreach my $key (keys(%{ $self })) {
	$self->{$key} = undef;
    }
}
sub nodelist($)		{ return $_[0]->{'NODES'}; }
sub failedlist($)	{ return $_[0]->{'FAILED'}; }
sub virtnodes($)        { return $_[0]->{'VIRTNODES'}; }
sub physnodes($)        { return $_[0]->{'PHYSNODES'}; }
sub geninodes($)        { return $_[0]->{'GENINODES'}; }
sub sharednodes($)      { return $_[0]->{'SHAREDNODES'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
154
sub oplist($)		{ return $_[0]->{'OPLIST'}; }
155 156
sub user($)		{ return $_[0]->{'USER'}; }
sub experiment($)	{ return $_[0]->{'EXPT'}; }
157
sub group($)		{ return $_[0]->{'GROUP'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
158
sub IncrFailCount($)    { $_[0]->{'FAILCOUNT'}++ }
159
sub failed($)           { return $_[0]->{'FAILCOUNT'}; }
160 161 162 163 164 165

# Grab a node by node_id.
sub node($$)
{
    my ($self, $node_id) = @_;

Leigh B Stoller's avatar
Leigh B Stoller committed
166 167 168 169 170 171 172
    if (exists($self->{'NODES'}->{$node_id})) {
	return $self->{'NODES'}->{$node_id};
    }
    # Phys nodes are nodes we do not actually own; they are shared
    if (exists($self->{'PHYSNODES'}->{$node_id})) {
	return $self->{'PHYSNODES'}->{$node_id};
    }
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    return undef
}

# Get/Set the osid->osinfo mapping. Nice to keep this to avoid lookups.
sub osmap($$;$)
{
    my ($self, $osid, $osinfo) = @_;

    if (defined($osinfo)) {
	return $self->{'OSMAP'}->{$osid} = $osinfo;
    }
    elsif (exists($self->{'OSMAP'}->{$osid})) {
	return $self->{'OSMAP'}->{$osid};
    }
    return undef;
}

Leigh B Stoller's avatar
Leigh B Stoller committed
190
# To avoid writing out all the methods.
191 192
sub AUTOLOAD {
    my $self = shift;
193
    my $type = ref($self) or die("$self is not an object");
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

    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 "$self: tried to access unknown slot $name\n";
    return undef;
}

#
#
Leigh B Stoller's avatar
Leigh B Stoller committed
210
sub OperationList($)
211 212 213 214 215 216 217 218 219 220
{
    my ($self) = @_;
    
    return [ values(%{ $self->{'TYPECACHE'} }) ];
}

#
# Setup a reload of a node if we can find an image.
# This goo constructs a hashed array of lists.
#
221
sub SetupReload($$$$)
222
{
223
    my ($self, $node, $osinfo, $auxload) = @_;
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    my $type = $node->type();

    #
    # We use a special type, "pcvm", for *any* subOS that can boot on a vnode.
    # For now, we're not going to mess with vnode types -- type checking is 
    # pretty pointless for this, since what we really want to check for a subOS
    # is whether it can boot on a specific parentOS.
    # So, users who make subOSes have to select that they can boot on type
    # pcvm and that's all that's necessary.
    #
    if ($node->isvirtnode()) {
	$type = "pcvm";
    }

    if ((my $image = $osinfo->MapToImage($type))) {
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	#
	# We support aux image loading, but have to distingush between a boot
	# load and an aux load, since an aux load should not change what is
	# actually booted.
	#
	if ($auxload) {
	    if (!defined($node->_loadimages())) {
		$node->_loadimages([]);
	    }
	    my @list = @{ $node->_loadimages() };
	    push(@list, $image);
	    $node->_loadimages(\@list);
	}
	else {
	    $node->_loadimages(undef);
	    $node->_loadimage($image);
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
256
	$node->_setupoperation($RELOAD);
257
	$node->_reloaded(1);
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
    }
    else {
	die_noretry({type => 'primary', severity => SEV_ERROR,
		     error => ['file_not_found', 'image', $osinfo, $node]},
		    "No image can be found for $osinfo on $node ($type)!");
    }
}

#
# keep track of the failed nodes of all types.
#   values = ['boot'|'osload', 'fatal'|'nonfatal']
#
sub add_failed_node_fatal($$) {
    $_[0]->{'FAILED'}->{$_[1]} = ['boot', 'fatal'];
}
sub add_failed_node_nonfatal($$) {
    $_[0]->{'FAILED'}->{$_[1]} = ['boot', 'nonfatal'];
}
sub add_failed_node_reload($$) {
    $_[0]->{'FAILED'}->{$_[1]} = ['reload', 'fatal'];
}
#
# Add node to lists for email messages.
#
sub add_failed_node_inform_user($$) {
    $_[0]->{'INFORM_USER'}->{$_[1]} = $_[1];
}
sub add_failed_node_inform_tbopswarn($$) {
    $_[0]->{'INFORM_TBOPSWARN'}->{$_[1]} = $_[1];
}
sub add_failed_node_inform_tbopsfatal($$) {
    $_[0]->{'INFORM_TBOPSFATAL'}->{$_[1]} = $_[1];
}
sub ClearInformLists($)
{
    $_[0]->{'INFORM_USER'}       = {};
    $_[0]->{'INFORM_TBOPSWARN'}  = {};
    $_[0]->{'INFORM_TBOPSFATAL'} = {};
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
#
# Collect up log files and create a list of urls.
#
sub CollectBootLogFiles($@)
{
    my ($self, @node_ids) = @_;
    my $logmsg = "";

    foreach my $node_id (@node_ids) {
	my $node = $self->node($node_id);
	next
	    if (!defined($node) || !defined($node->_bootlog()));

	$logmsg .= sprintf("%-15s : %s\n",
			   $node->node_id(), $node->_bootlog()->URL());
    }
    return $logmsg;
}

317 318 319 320 321 322 323 324 325 326 327 328
#
# Spam time! Send mail to the user and testbed-ops about failures.
#
sub InformUser($)
{
    my ($self) = @_;
    my $pid    = $self->experiment()->pid();
    my $eid    = $self->experiment()->eid();
    my $name   = $self->user()->name();
    my $email  = $self->user()->email();

    my @nodelist = keys(%{ $self->{'INFORM_USER'} });
329
    my $logmsg   = $self->CollectBootLogFiles(@nodelist);
330 331 332 333 334 335 336 337 338 339 340 341
    my $count    = scalar(@nodelist);
    if ($count > 0) {
	SENDMAIL("$name <$email>", "$count nodes are down",
		 "Nodes:\n".
		 "  " . join(" ", @nodelist) . "\n".
		 "in pid/eid $pid/$eid appear to be dead.\n\n".
		 "Your experiment will continue to run since these failures\n".
		 "are nonfatal, although you might encounter other problems\n".
		 "if your experiment depends explicitly on these nodes.\n".
		 "You should terminate this experiment if it cannot ".
		 "tolerate these failures.\n\n".
		 "Testbed Operations has also been notified.\n\n".
342 343
		 "$logmsg\n",
		 undef,
344 345 346 347 348 349 350 351 352 353 354 355 356
		 "Cc: $TBOPS");
    }
}

sub InformTBopsFatal($)
{
    my ($self) = @_;
    my $pid    = $self->experiment()->pid();
    my $eid    = $self->experiment()->eid();
    my $name   = $self->user()->name();
    my $email  = $self->user()->email();

    my @nodelist = keys(%{ $self->{'INFORM_TBOPSFATAL'} });
357
    my $logmsg   = $self->CollectBootLogFiles(@nodelist);
358 359 360 361 362 363
    my $count    = scalar(@nodelist);
    if ($count > 0) {
	SENDMAIL($TBOPS, "$count nodes are down",
		 "Nodes:\n".
		 "  " . join(" ", @nodelist) . "\n".
		 "in pid/eid $pid/$eid appear to be dead.\n\n".
364
		 "The nodes have been moved into hardware checkup.\n\n$logmsg",
365 366 367 368 369 370 371 372 373 374 375 376 377
		 "$name <$email>");
    }
}

sub InformTBopsWarn($)
{
    my ($self) = @_;
    my $pid    = $self->experiment()->pid();
    my $eid    = $self->experiment()->eid();
    my $name   = $self->user()->name();
    my $email  = $self->user()->email();

    my @nodelist = keys(%{ $self->{'INFORM_TBOPSWARN'} });
378
    my $logmsg   = $self->CollectBootLogFiles(@nodelist);
379 380 381 382 383 384
    my $count    = scalar(@nodelist);
    if ($count > 0) {
	SENDMAIL($TBOPS, "$count nodes are down",
		 "Nodes:\n".
		 "  " . join(" ", @nodelist) . "\n".
		 "in pid/eid $pid/$eid failed to boot after loading OS.\n\n".
385
		 "The nodes have been freed.\n\n$logmsg",
386 387 388 389 390 391 392 393 394 395 396 397 398
		 "$name <$email>");
    }
}

#
# Set the OS that needs to boot on a node, and mark it.
#
sub SetOS($$)
{
    my ($self, $node) = @_;
    my $node_id       = $node->node_id();
    my $imageable     = $node->imageable();
    my $osid          = $node->def_boot_osid();
399 400
    my $osid_vers     = $node->def_boot_osid_vers();
    my $osinfo        = OSinfo->Lookup($osid, $osid_vers);
401
    my $loadlist      = $node->loadlist();
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    my $experiment    = $self->experiment();
    my $pid           = $experiment->pid();

    die_noretry("Could not map $osid to its object!")
	if (!defined($osinfo));

    # Lets remember the osinfo for later.
    $self->osmap($osid, $osinfo);

    # Set the canfail bit.
    $node->_canfail($node->failureaction() eq NODEFAILMODE_FATAL() ? 0 : 1);

    #
    # If a virtnode is running a subOS, we set $imageable because it
    # really is going to be reloaded... even though virtnode types are
    # not typically imageable.
    #
    if ($node->isvirtnode() && $osinfo->def_parentosid()) {
420 421 422 423 424 425 426
	#
	# But, if the subos is static (like OPENVZ-STD), there is
	# no reload, cause it is already on the parent image. Lets use 
	# the fact that no image link exists, to distinguish this case.
	#
	$imageable = 1
	    if (defined($osinfo->MapToImage($node->type())));
427 428
    }

429
    if ($imageable) {
430 431 432 433 434 435 436
	#
	# These checks are not necessary if the front end and web page
	# are doing the right thing, but lets be careful anyway.
	#
	if (! $osinfo) {
	    die_noretry("$node has no bootpath and no def_boot_osid set!");
	}
437
	$self->SetOSAux($node, $osinfo, 0, $loadlist);
438

439
	#
440
	# Now look for additional loads. 
441
	#
442 443 444 445 446 447 448 449 450 451
	if ($loadlist) {
	    my @osnamelist = split(",", $loadlist);

	    foreach my $osname (@osnamelist) {
		my $osinfo = OSinfo->Lookup($pid, $osname) ||
		    OSinfo->LookupByName($osname);

		die_noretry("Could not map $osname to its object!")
		    if (!defined($osinfo));

452
		$self->SetOSAux($node, $osinfo, 1, 1);
453 454 455 456 457 458 459 460 461 462 463 464 465
	    }
	}
    }
    # Remember this for later. 
    $node->_bootosinfo($osinfo);
    
    print STDERR "$node_id - $osinfo\n"
	if ($self->debug());
}

#
# Helper for above.
#
466
sub SetOSAux($$$$$)
467
{
468
    my ($self, $node, $osinfo, $auxload, $multiple) = @_;
469 470 471 472 473 474 475 476 477
    my $node_id       = $node->node_id();
    my $experiment    = $self->experiment();
    my $pid           = $experiment->pid();
    
    #
    # If there is an actual path, its an OSKit kernel not an image.
    #
    if (! defined($osinfo->path()) || $osinfo->path() eq "") {
	my $nextosinfo;
478
	    
479 480 481 482
	#
	# Not an OSKit kernel.
	#
	if ($osinfo->IsGeneric()) {
483
	    #
484
	    # Map generic OSID to the specific one.
485
	    #
486 487 488
	    $nextosinfo = $osinfo->ResolveNextOSID($experiment);
	    if (!defined($nextosinfo)) {
		die_noretry("No next mapping for $osinfo on $node!\n");
489
	    }
490 491 492 493
	    print "Mapping $osinfo on $node_id to $nextosinfo\n";
	    $osinfo = $nextosinfo;
	}
	#
494 495 496 497 498 499
	# Have the node inherit any taint states set for the OS. This
	# call retains any taint states the node had set previously.
	#
	$node->InheritTaintStates($osinfo) == 0
	    or die_noretry("$node could not inherit taint states from $osinfo");
	#
500 501 502 503 504 505 506
	# Make sure this OSID is actually loaded on the machine.
	#
	my $isloaded = $node->IsOSLoaded($osinfo);
	if ($isloaded < 0) {
	    die_noretry("Error determining if $osinfo is loaded on $node\n");
	}
	if ($isloaded) {
507 508 509 510 511 512 513 514 515
	    #
	    # XXX the primary (boot) OSID is already loaded, but we are loading
	    # multiple images. In this case we reload the primary OS anyway so that
	    # the logic in osload continues to work (it assumes that it is responsible
	    # for setting the def_boot_osid based on the last image specified).
	    #
	    if (!$auxload && $multiple) {
		$self->SetupReload($node, $osinfo, $auxload);
	    }
516
	    #
517
	    # OSID is loaded, but might need to be cleaned.
518
	    #
519
	    elsif ($self->dolastload() &&
520 521 522
		defined($node->last_reservation()) &&
		$node->last_reservation() ne $pid) {
		$self->SetupReload($node, $osinfo, $auxload);
523
	    }
524
	    elsif ($nextosinfo && !$auxload) {
525
		#
526 527 528
		# Seems like a bad place for this; if the OS was
		# mapped to something else that is already on the
		# disk, need to reset def_boot_osid.
529
		#
530 531 532
		if ($self->impotent()) {
		    print STDERR "$node_id:def_boot_osid --> $osinfo\n"
			if ($self->debug());
533
		}
534 535
		elsif ($node->OSSelect($osinfo, "def_boot_osid", 0)) {
		    die_noretry("Could not set boot OS to $osinfo for $node");
536 537
		}
	    }
538 539 540 541 542 543
	}
	else {
	    #
	    # OS not loaded.
	    #
	    $self->SetupReload($node, $osinfo, $auxload);
544 545 546 547 548 549 550 551
	}
    }
}

#
# Take a list of nodes and fire off the required reloads/reboots/reconfigs
# in parallel, then wait for finish.
#
552 553 554
# This is called in a child (fork) so all state has to be communicated via
# the DB, back to the parent. See the WaitForNodes() function. 
#
555 556 557 558
sub LightUpNodes($@)
{
    my ($self, @nodelist) = @_;
    my @children = ();
Leigh B Stoller's avatar
Leigh B Stoller committed
559
    my @failed   = ();
560 561 562 563 564 565 566 567 568 569 570 571 572 573

    #
    # 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;

574 575 576 577
	# Nothing to do.
	next
	    if ($op == $NOSTATE);

Leigh B Stoller's avatar
Leigh B Stoller committed
578
	if ($op == $RELOAD) {
579 580
	    my $image    = $node->_loadimage();
	    my @images   = ($image);
581
	    my $imageids = $image->versid();
582 583 584 585 586 587 588 589
	    my $auxloads = $node->_loadimages();

	    #
	    # Watch for multiple loads, but remember that the boot image
	    # has to be last since libosload assumes that is the one to boot.
	    #
	    if (defined($auxloads)) {
		foreach my $aux (@{ $auxloads }) {
590
		    my $auxid = $aux->versid();
591 592 593 594 595 596 597
		    unshift(@images, $aux);
		    $imageids = "${auxid},${imageids}";
		}
	    }
	    
	    if (!exists($reloads{"$imageids"})) {
		$reloads{"$imageids"} = [];
598
	    }
599 600
	    push(@{ $reloads{"$imageids"} }, $node);
	    $action = "reloaded with @images";
601 602 603 604 605 606 607 608 609
	}
	elsif ($op == $REBOOT) {
	    $reboots{$node_id} = $node;
	    $action = "rebooted";
	}
	elsif ($op == $RECONFIG) {
	    $reconfigs{$node_id} = $node;
	    $action = "reconfiged";
	}
610
	print STDERR "$node_id will be $action\n";
611 612 613
    }

    # XXX Caller wants a list. 
614
    return 0
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
	if ($self->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);
	    
	    #
	    # vnodes only get rebooted if this is a modify and we need
	    # to reload them (otherwise they will get rebooted because
	    # of presence in %reboots).
	    #
	    if ($node->isvirtnode()) {
		if (defined($node->allocstate()) &&
		    $node->allocstate() eq TBDB_ALLOCSTATE_RES_INIT_CLEAN()) {
		    $nodeflags{$node_id}{'noreboot'} = 1;
		}
	    }

	    #
	    # osload should not wait for shared vnodes.  We need
	    # vnode_setup to boot/reboot them since the underlying pnode
Leigh B Stoller's avatar
Leigh B Stoller committed
645
	    # will not be booting.  So for them, osload just sets up the
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	    # reload and finishes.
	    #
	    if ($node->isvirtnode() && $node->_onsharednode()) {
		$nodeflags{$node_id}{'noreboot'} = 1;
		$nodeflags{$node_id}{'nowait'} = 1;
	    }

	    $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;
	}

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

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

669 670 671 672 673 674
	my $pid;
	my $coderef;
	if (EmulabFeatures->FeatureEnabled("NewOsload",$self->user(),
					   $self->group(),
					   $self->experiment())) {
	    ($self->loadobj())->debug($self->debug());
675 676 677 678 679 680

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

681 682 683 684 685 686 687 688 689 690 691
	    $pid = ($self->loadobj())->osload(\%reload_args, $reload_failures);
	    $coderef = sub {
		my $childpid = shift;
		return ($self->loadobj())->osload_wait($childpid);
	    };
	}
	else {
	    $pid = osload(\%reload_args, $reload_failures);
	    $coderef = \&osload_wait;
	}
	push(@children, [ $pid, $coderef,			  ,
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
			  [ @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'}     = $self->debug();
	$reboot_args{'waitmode'}  = 0;
	$reboot_args{'asyncmode'} = 1;
721
	$reboot_args{'rebootmode'} = $self->realreboot();
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
	$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 %reboot_args     = ();
	my $reboot_failures = {};

	$reboot_args{'debug'}     = $self->debug();
	$reboot_args{'waitmode'}  = 0;
	$reboot_args{'asyncmode'} = 1;
741
	$reboot_args{'rebootmode'} = $self->realreboot();
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
	$reboot_args{'reconfig'}  = 1;
	$reboot_args{'nodelist'}  = [ @list ];

	my $pid = nodereboot(\%reboot_args, $reboot_failures);
	push(@children, [ $pid, \&nodereboot_wait,
			  [ @list ], $reboot_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});
	    }
	}

	#
Leigh B Stoller's avatar
Leigh B Stoller committed
786 787 788
	# Mark all of the failed nodes so that the caller knows there
	# was a failure. 
	# 
789 790
	foreach my $node_id (@nlist) {
	    my $node = $self->node($node_id);
Leigh B Stoller's avatar
Leigh B Stoller committed
791

792 793 794
	    $node->SetAllocState(TBDB_ALLOCSTATE_DOWN());
	}
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
795 796 797 798 799 800 801 802 803 804 805

    return scalar(@failed);
}

#
# Generic waiting function for a list of nodes.
#
sub WaitForNodes($@)
{
    my ($self, @nodelist) = @_;
    my %nodes = ();
806 807 808
    my @waitstates = (TBDB_NODESTATE_TBFAILED,
		      TBDB_NODESTATE_RELOADFAILED,
		      TBDB_NODESTATE_ISUP);
Leigh B Stoller's avatar
Leigh B Stoller committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826

    # Maybe all nodes failed to light up?
    return 0
	if (! @nodelist);

    # Copy of the array. 
    foreach my $node (@nodelist) {
	$nodes{$node->node_id()} = $node;
	$node->_waitstart(time());
	$node->Refresh();
    }

    #
    # See if the previous phase (lighting up the nodes) failed.
    # We use the bogus allocstate for this.
    #
    foreach my $node (@nodelist) {
	my $node_id = $node->node_id();
827 828
	my $typehandler = $node->_typehandler();
	
Leigh B Stoller's avatar
Leigh B Stoller committed
829 830 831 832 833 834 835 836 837 838 839
	next
	    if (! ($node->allocstate() eq TBDB_ALLOCSTATE_DOWN() ||
		   $node->allocstate() eq TBDB_ALLOCSTATE_DEAD()));

	#
	# These are unusual errors. Lets turn off retry.
	#
	$self->noretry(1);

	tbnotice("Not waiting for $node_id since reload/reboot failed!\n");
	delete($nodes{$node_id});
840 841
	$node->_setupstatus($RELOAD_FAILED);
	$typehandler->WaitDone($node);
Leigh B Stoller's avatar
Leigh B Stoller committed
842 843 844 845 846 847 848 849
    }

    #
    # Start a counter going, relative to the time we rebooted the first
    # node.
    #
    my $waittime  = 0;
    my $minutes   = 0;
850
    my $canceled  = $self->canceled();
Leigh B Stoller's avatar
Leigh B Stoller committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890

    #
    # Wait for the nodes to finish booting, as recorded in database
    #
    while (keys(%nodes)) {
	#
	# Check for cancelation. Do not want to retry if swap was canceled.
	#
	if (!$canceled) {
	    $canceled = $self->experiment()->canceled();
	    if ($canceled) {
		# So everyone knows.
		$self->canceled($canceled);
		tbnotice({cause => 'canceled', severity => SEV_IMMEDIATE,
			  error => ['cancel_flag']},
			 "Swap canceled; will terminate os_setup early!");
	    }
	}
	
	foreach my $node (values(%nodes)) {
	    my $node_id = $node->node_id();
	    my $typehandler = $node->_typehandler();
	    my $state;

	    #
	    # Call typehandler specific wait function; 
	    #
	    my $retval = $typehandler->WaitForNode($node);
	    #
	    # Zero means the handler had nothing to say.
	    #
	    if ($retval) {
		# The handler has to set the node state to something useful.
		goto node_error
		    if ($retval < 0);
		goto node_done
		    if ($retval > 0);
	    }
	    if ($node->GetEventState(\$state)) {
		print STDERR "*** Error getting event state for $node_id.\n";
891
		$node->_setupstatus($SETUP_FAILED);
Leigh B Stoller's avatar
Leigh B Stoller committed
892 893 894 895 896 897 898
	      node_error:
		delete($nodes{$node_id});
		$typehandler->WaitDone($node);
		next;
	    }
	    if (grep {$_ eq $state} @waitstates) {
		print "$node_id has reported state $state\n";
899
		$node->_setupstatus($SETUP_OKAY);
Leigh B Stoller's avatar
Leigh B Stoller committed
900 901 902 903 904 905 906 907 908 909 910 911 912
	      node_done:
		delete($nodes{$node_id});
		$typehandler->WaitDone($node);
		next;
	    }
	    $waittime = time() - $node->_waitstart();
	    if ($waittime > $node->_maxwait()) {
		$minutes = int($waittime / 60);
		if ($canceled ||
		    $self->noretry() || !$node->_retrycount()) {
		    tbnotice("*** Giving up on $node_id ($state) - ".
			     "it's been $minutes minute(s).\n");

913
		    $node->_setupstatus($SETUP_FAILED);
Leigh B Stoller's avatar
Leigh B Stoller committed
914 915 916 917 918
		    delete($nodes{$node_id});
		    $typehandler->WaitDone($node);
		}
		else {
		    if ($typehandler->Retry($node) != 0) {
919
			$node->_setupstatus($SETUP_OKAY);
Leigh B Stoller's avatar
Leigh B Stoller committed
920 921 922 923 924 925 926 927 928 929 930
			delete($nodes{$node_id});
			$typehandler->WaitDone($node);
		    }
		    else {
			$node->_retrycount($node->_retrycount() - 1);
			$node->_waitstart(time());
		    }
		}
		next;
	    }
	    if (int($waittime / 60) > $minutes) {
931 932
		# Changing minutes is why we get this print for just
		# a single node each time. 
Leigh B Stoller's avatar
Leigh B Stoller committed
933 934 935 936 937 938 939 940
		$minutes = int($waittime / 60);
		tbnotice("Still waiting for $node_id ($state) - ".
			 "it's been $minutes minute(s).\n");
	    }
	}
	sleep(5);
    }
    return 0;
941 942 943 944 945 946 947 948
}

#
# Create a type (or class) specific object to handle those nodes.
#
sub NewType($$)
{
    my ($self, $type) = @_;
949 950 951

    #
    # These special cases will eventually be encoded in the DB.
952
    # Note that type might be a class, and so Lookup() returns nothing.
953
    #
954 955 956 957 958 959 960 961
    my $typeinfo = NodeType->Lookup($type);

    if ($type eq "pcfedphys" || $type eq "pcfed") {
	$type = "protogeni";
    }
    elsif (defined($typeinfo) && $typeinfo->issubnode()) {
	$type = "subnode";
    }
962

963 964 965 966
    my $packname = "libossetup_${type}";
    my $newtype  = eval { $packname->New($self); };
    # Not loaded?
    if ($@) {
967
	#print STDERR "module load failed: " . $@ . "\n";
968
	eval "require libossetup_" . $type;
969 970
	$newtype  = eval { $packname->New($self); };
	if ($@) {
971 972
#	    print STDERR "$self NewType($type): module load failed: " . $@ . "\n"
#		if ($self->debug());
973 974
	    return undef;
	}
975 976 977
    }
    $self->{'TYPECACHE'}->{$type} = $newtype;
    
978 979
#    print STDERR "Created type object $type with parent $self\n"
#	if ($self->debug());
980 981 982 983 984 985
    
    return $newtype;
}
#
# Return the cached type object.
#
986
sub TypeLookup($$)
987 988 989
{
    my ($self, $node) = @_;

990 991 992 993 994 995
    my $type  = $node->type();
    my $class = $node->class();

    #
    # These special cases will eventually be encoded in the DB.
    #
996 997 998 999 1000 1001
    if ($type eq "pcfedphys" || $type eq "pcfed") {
	$type = "protogeni";
    }
    elsif ($node->issubnode()) {
	$type = "subnode";
    }
1002

1003 1004 1005 1006 1007
    return $self->{'TYPECACHE'}->{$type}
        if (exists($self->{'TYPECACHE'}->{$type}));

    return $self->{'TYPECACHE'}->{$class}
        if (exists($self->{'TYPECACHE'}->{$class}));
1008 1009 1010 1011 1012 1013 1014 1015

    return undef;
}

#
# Wrapper class for the type/class specific packages below.
#
package libossetup_handler;
1016
use base qw(libossetup);
1017 1018 1019 1020 1021 1022 1023 1024

use libdb;
use libtestbed;
use libossetup;
use libtblog;
use Node;
use English;
use Data::Dumper;
1025
use overload ('""' => 'Stringify');
1026

1027 1028
use vars qw($AUTOLOAD);

1029 1030 1031 1032
sub New($$$)
{
    my ($class, $type, $parent) = @_;

Leigh B Stoller's avatar
Leigh B Stoller committed
1033 1034 1035 1036 1037 1038
    my $self             = {};
    $self->{'TYPE'}      = $type;
    $self->{'NODES'}     = {};
    $self->{'PARENT'}    = $parent;
    $self->{'FAILCOUNT'} = 0;
    $self->{'TODOLIST'}  = {};
1039 1040 1041 1042 1043 1044 1045

    bless($self, $class);
    return $self;
}
# Access methods for the objects defined below, which are 
sub type($)		{ return $_[0]->{'TYPE'}; }
sub parent($)		{ return $_[0]->{'PARENT'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
1046
sub failcount($)	{ return $_[0]->{'FAILCOUNT'}; }
1047
sub nodelist($)         { return values(%{ $_[0]->{'NODES'} }); }
Leigh B Stoller's avatar
Leigh B Stoller committed
1048 1049 1050
sub todo($)		{ return $_[0]->{'TODOLIST'}; }
sub todolist($)		{ return values(%{ $_[0]->{'TODOLIST'} }); }
sub IncrFailCount($)    { $_[0]->{'FAILCOUNT'}++ }
1051

1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
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 "$self: tried to access unknown slot $name\n";
    return undef;
}

1069
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1070
# Add a node to the list.
1071
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1072
sub AddNode($$)
1073
{
Leigh B Stoller's avatar
Leigh B Stoller committed
1074 1075 1076 1077 1078 1079 1080
    my ($self, $node) = @_;
    my $node_id = $node->node_id();

    $self->{'NODES'}->{$node_id}    = $node;
    $self->{'TODOLIST'}->{$node_id} = $node;
    $node->_typehandler($self);
    $node->_setupoperation($libossetup::NOSTATE);
1081 1082

    #
Leigh B Stoller's avatar
Leigh B Stoller committed
1083 1084
    # This sets the OS that should boot, as well as any reloads, reboots
    # and reconfigs that are needed.
1085
    #
Leigh B Stoller's avatar
Leigh B Stoller committed
1086 1087
    $self->parent()->SetOS($node);
    return 0;
1088 1089 1090
}

#
Leigh B Stoller's avatar
Leigh B Stoller committed
1091 1092
# Stub function to light up a set of nodes; calls the big version in the
# parent.
1093
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1094
sub LightUpNodes($@)
1095
{
Leigh B Stoller's avatar
Leigh B Stoller committed
1096
    my ($self, @nodelist)  = @_;
1097 1098
    my $parent = $self->parent();

Leigh B Stoller's avatar
Leigh B Stoller committed
1099 1100
    return $parent->LightUpNodes(@nodelist);
}
1101

Leigh B Stoller's avatar
Leigh B Stoller committed
1102 1103 1104 1105 1106 1107
#
# Return the set of nodes ready to go.
#
sub Volunteers($)
{
    my ($self) = @_;
1108

Leigh B Stoller's avatar
Leigh B Stoller committed
1109 1110 1111 1112 1113 1114 1115 1116 1117
    return $self->todolist();
}

#
# Stub function to retry a node, called by the wait function. 
#
sub Retry($$)
{
    my ($self, $node) = @_;
1118 1119

    #
Leigh B Stoller's avatar
Leigh B Stoller committed
1120 1121
    # Return an error since this should not be called; it
    # should be overridden or the retry count always set to zero.
1122
    #
Leigh B Stoller's avatar
Leigh B Stoller committed
1123 1124
    return -1;
}
1125

Leigh B Stoller's avatar
Leigh B Stoller committed
1126 1127 1128 1129 1130 1131
#
# Generic wait for a single function.
#
sub WaitForNode($$)
{
    my ($self, $node) = @_;
1132

Leigh B Stoller's avatar
Leigh B Stoller committed
1133 1134 1135
    # Tell caller I have nothing to offer. 
    return 0;
}
1136

Leigh B Stoller's avatar
Leigh B Stoller committed
1137 1138 1139 1140 1141 1142
#
# Generic waitdone function.
#
sub WaitDone($@)
{
    my ($self, @nodelist) = @_;
1143 1144
    my $parent     = $self->parent();
    my $experiment = $parent->experiment();
Leigh B Stoller's avatar
Leigh B Stoller committed
1145 1146 1147 1148 1149 1150 1151 1152 1153

    foreach my $node (@nodelist) {
	my $node_id = $node->node_id();
	
    	#
	# At this point, we are done with this node, so remove
	# it from the todo list. Might do something fancy later.
	#
	delete($self->todo()->{$node_id});
1154 1155 1156 1157
    }
    return 0;
}

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $type = $self->type();

    return "[$type]";
}

1170 1171 1172 1173 1174 1175
#####################################################################
#
# Generic handler for local cluster nodes that do not require much
# special handling.
#
package libossetup_pc;
1176
use base qw(libossetup_handler);
1177 1178 1179 1180 1181 1182 1183 1184

use libdb;
use libtestbed;
use libossetup;
use libtblog;
use Node;
use English;
use Data::Dumper;
1185
use overload ('""' => 'Stringify');
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

#
# A constructor for an object to handle all nodes of this type.
#
sub New($$) {
    my ($class, $parent) = @_;

    my $self = $class->SUPER::New("pc", $parent);
    bless($self, $class);
    return $self;
}

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

    #
    # XXX Inner elab nodes should never report in to us.
    # If they do, make sure they wind up in PXEWAIT.
    #
    if ($node->_iseinenode()) {
	print "Will skip reload/reboot of inner elab node: $node_id.\n";
	$node->ClearOsids();
1210 1211
	# Need to initialize this for the stats code.
	$node->_bootosinfo(undef);
1212 1213 1214
	return 0;
    }

Leigh B Stoller's avatar
Leigh B Stoller committed
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
    $self->SUPER::AddNode($node);

    #
    # Skip this if an OS needs to be reloaded.
    #
    if ($node->_setupoperation() != $libossetup::RELOAD) {
	if ($node->allocstate() eq TBDB_ALLOCSTATE_RES_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)
	    $node->_setupoperation($libossetup::REBOOT);
	}
1229
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241

    #
    # Set up the retry count and the waittime.
    #
    my $waittime = (60 * 7);	# The default.
    my $osinfo   = $node->_bootosinfo();

    # Compute actual waittime.
    if (defined($node->bios_waittime()) &&
	defined($osinfo->reboot_waittime())) {
	$waittime = ($node->bios_waittime() +
		     $osinfo->reboot_waittime()) * 2;
1242
    }
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
    # Adjust for image backed datasets which take more time to load.
    my @blockstores =
	$self->parent()->experiment()->LookupBlockstoresForNode($node->vname());
    if (@blockstores) {
	require Blockstore;

	foreach my $blockstore (@blockstores) {
	    next
		if ($blockstore->type() ne "imdataset");
	    my $bsname = $blockstore->vname();
	    my $extra  = Blockstore::LoadEstimate($blockstore);

	    if ($extra < 0) {
		print STDERR
		    "*** Could not compute load estimate for $bsname\n";
		next;
	    }
	    print "Adding $extra seconds for blockstore $bsname\n";
	    $waittime += $extra;
	}
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
1264 1265
    $node->_retrycount(1);
    $node->_maxwait($waittime);
1266
    $node->_setupstatus($libossetup::SETUP_OKAY);
1267 1268 1269 1270

    return 0;
}

1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
#
# Return the set of nodes ready to go.
#
sub Volunteers($)
{
    my ($self)     = @_;
    my @nodelist   = $self->todolist();
    my $parent     = $self->parent();
    my $experiment = $parent->experiment();
    my $firewall;
    my $firewalled = $experiment->IsFirewalled(\$firewall);
    my $elabinelab = $experiment->elabinelab();

    #
    # Setup the firewall first.  Once it is up we can continue with the
    # remaining nodes.
    #
    # There is very little point in setting up the other nodes at the same time
    # as they will not be able to PXE boot until the firewall is up.  We
    # could fire them off a little early in hopes of overlapping any BIOS
    # boot time with the last stages of the firewall setup, but it probably
    # isn't worth the complexity (and would not work with nodes for which
    # "reboot" means "fall out of PXEWAIT and boot".
    #
    # Note that we formerly did just do them all at once and let the nodes
    # continually PXE-timeout and reboot until the firewall came up.  But
    # that can actually take longer than what we do now, if a node happened
    # to timeout and reboot just as the firewall came up (i.e., we would
    # have to wait an extra BIOS-reboot cycle, which can be 90 seconds or
    # more.
    #
    if ($firewalled) {
	my $fwnode = $self->node($firewall);
	#
	# If the firewall fails, we abort and never come back.
	#
	if ($fwnode->allocstate() ne TBDB_ALLOCSTATE_RES_READY()) {
	    tbnotice("Setting up the firewall node first ... \n");
	    return ($fwnode);
	}
	# Remove firewall from the node list.
	@nodelist = grep {$_->node_id() ne $firewall} @nodelist;
    }
    return @nodelist;
}

1317
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1318
# Wait function signals some local cluster nodes are done waiting.
1319
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1320
sub WaitDone($@)
1321
{
Leigh B Stoller's avatar
Leigh B Stoller committed
1322
    my ($self, @nodelist) = @_;
1323 1324 1325 1326
    my $parent     = $self->parent();
    my $experiment = $parent->experiment();
    my $pid        = $experiment->pid();
    my $eid        = $experiment->eid();
1327 1328 1329
    my $firewall;
    my $firewalled = $experiment->IsFirewalled(\$firewall);
    my $elabinelab = $experiment->elabinelab();
Leigh B Stoller's avatar
Leigh B Stoller committed
1330

1331
    #
Leigh B Stoller's avatar
Leigh B Stoller committed
1332
    # Must call the generic WaitDone handler too.
1333
    #
Leigh B Stoller's avatar
Leigh B Stoller committed
1334
    $self->SUPER::WaitDone(@nodelist);
1335

1336 1337 1338 1339 1340 1341 1342 1343 1344
    #
    # See if we allow failed osloads to continue on failureaction==nonfatal.
    # Since this is at its finest per-experiment, we check outside the loop.
    #
    my $osloadfailok = EmulabFeatures->FeatureEnabled("OsloadFailNonfatal",
						      $parent->user(),
						      $parent->group(),
						      $experiment);

1345
    #
Leigh B Stoller's avatar
Leigh B Stoller committed
1346
    # Then per node processing.
1347 1348
    #
    foreach my $node (@nodelist) {
1349 1350 1351
	my $node_id     = $node->node_id();
	my $setupstatus = $node->_setupstatus();
	my $eventstate  = $node->eventstate();
1352

1353
	if ($eventstate eq TBDB_NODESTATE_ISUP()) {
1354 1355 1356
	    print "$node_id is alive and well\n";
	    $node->SetBootStatus(NODEBOOTSTATUS_OKAY);
	    $node->SetAllocState(TBDB_ALLOCSTATE_RES_READY());
1357 1358 1359 1360
	    # 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);
1361

1362
	    if ($firewalled && $node_id eq $firewall) {
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
		#
		# Firewall has booted, perform any final actions.
		#
		# The only case that currently matters is if the experiment is
		# elabinelab. In this case we want to turn off the firewall
		# so the nodes can boot/reload normally. Later, after we
		# set up the inner elab, we turn the firewall back on.
		#
		if ($elabinelab) {
		    #
		    # We use the elabinelab program to do this, since it
		    # knows what it might want to do (and helpfully, is
		    # setuid so it can ssh over).
		    #
		    system("$ELABINELAB -f $pid $eid");
		    if ($?) {
			tbnotice("Firewall Boot Setup failed!\n");
			$parent->aborted(1);
		    }
		}
	    }
1384 1385 1386
	    next;
	}
	# Fall through on failure.	
1387
	if ($eventstate eq TBDB_NODESTATE_TBFAILED()) {
1388 1389 1390 1391 1392 1393 1394
	    tbwarn("$node_id reported a TBFAILED event\n");
	}
	else {
	    tbwarn("$node_id failed to boot\n");
	}
	$node->SetBootStatus(NODEBOOTSTATUS_FAILED);

1395 1396 1397
	#
	# If the firewall fails to boot, we are hosed. Quit right away.
	#
1398
	if ($firewalled && $node_id eq $firewall) {
1399 1400 1401 1402
	    tbnotice("Firewall failed to boot!\n");
	    $parent->aborted(1);
	}

1403
	#
1404
	# Handle non-fatal failures (if we have not been canceled or aborted).
1405
	#
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
	if ($node->_canfail() &&
	    !$experiment->canceled() && !$parent->aborted()) {
	    #
	    # Non-fatal reload failures require special handling.
	    # (Note: no check for noretry() since it will always be set.)
	    #
	    # XXX it is not clear what the right strategy is here since
	    # the node might not boot, might boot into the wrong OS, or
	    # it might just sit there trying to boot over and over again.
	    # We could just power the machine off and tell the user.
	    # We could boot the node into the admin MFS and tell the user.
	    # We could park the node at PXEWAIT whenever it boots.
	    # Or we could just let it go and tell the user.
	    #
	    # Right now we park it in PXEWAIT on reboot. If the node is
	    # hung, the user will surely notice. If the node does reboot
	    # it will stop in PXEWAIT and the user will surely notice.
	    #
	    if ($setupstatus == $libossetup::RELOAD_FAILED) {
		if ($osloadfailok) {
		    # clear all boot osids causing node to go into PXEWAIT
		    if ($node->OSSelect(undef, undef, 0)) {
			tbnotice("could not force into PXEWAIT; failing\n");
			goto bad;
		    }
		    # clear the reload info
		    $node->ClearCurrentReload();
		    # and partitions
		    $node->ClearPartitions();

		    # XXX this will force node to reboot
		    $node->SetEventState(TBDB_NODESTATE_TBFAILED());

		    $parent->add_failed_node_inform_user($node_id);
		    $parent->add_failed_node_nonfatal($node_id);
		    tbwarn("$node_id will stop in PXEWAIT state on reboot\n");
		    tbnotice("Continuing with experiment setup anyway ...\n");

		    # XXX otherwise, os_setup will still fail
		    $parent->noretry(0);
		    # XXX so subsequent swapmods don't free the node
		    $node->SetAllocState(TBDB_ALLOCSTATE_RES_READY());
		    next;
		}
	    }
	    elsif (!$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;
	    }
1457 1458
	}

1459
      bad:
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
	#
	# 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 (! 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);
	}
	$node->SetAllocState(TBDB_ALLOCSTATE_DOWN());
Leigh B Stoller's avatar
Leigh B Stoller committed