libossetup.pm.in 40.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2010 University of Utah and the Flux Group.
# All rights reserved.
#
package libossetup;

use strict;
use Exporter;
11 12 13 14 15
use vars qw(@EXPORT $AUTOLOAD
	    $NOSTATE $RELOAD $RECONFIG $REBOOT
	    $RELOAD_FAILED $RECONFIG_FAILED $REBOOT_FAILED $SETUP_FAILED
	    $SETUP_OKAY);
use base qw( Exporter );
16

17 18 19 20
@EXPORT = qw(&die_noretry
	     $NOSTATE $RELOAD $RECONFIG $REBOOT
	     $RELOAD_FAILED $RECONFIG_FAILED $REBOOT_FAILED $SETUP_FAILED
	     $SETUP_OKAY);
21 22 23 24 25 26

use libdb;
use libtestbed;
use libreboot;
use libosload;
use Node;
27
use NodeType;
28 29 30 31 32 33 34 35
use libtblog;
use English;
use Data::Dumper;

# Configure variables
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $NODEREBOOT  = "$TB/bin/node_reboot";
Leigh B Stoller's avatar
Leigh B Stoller committed
36
my $VNODESETUP  = "$TB/sbin/vnode_setup";
37

38
# Setup Operation Flags
Leigh B Stoller's avatar
Leigh B Stoller committed
39
$NOSTATE    = 0x00;
40
$RELOAD     = 0x01;
41 42 43
$RECONFIG   = 0x02;
$REBOOT     = 0x04;

44 45 46 47 48 49 50
# Setup Result Flags
$SETUP_OKAY      = 0x00;
$RELOAD_FAILED   = 0x01;
$RECONFIG_FAILED = 0x02;
$REBOOT_FAILED   = 0x04;
$SETUP_FAILED    = 0x08;

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#
# 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;
    $self->{'HASH'}       = {};
    $self->{'NODES'}      = {};
Leigh B Stoller's avatar
Leigh B Stoller committed
78
    $self->{'PHYSNODES'}  = {};
79
    $self->{'FAILED'}     = {};
80
    $self->{'FAILCOUNT'}  = 0;
81 82
    $self->{'OSMAP'}      = {};
    $self->{'TYPECACHE'}  = {};
Leigh B Stoller's avatar
Leigh B Stoller committed
83
    $self->{'OPLIST'}     = {};
84 85 86 87 88 89 90 91

    # 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
92 93 94

	# Init some per-node stuff.
	$node->_rebooted(0);
95
	$node->_vnodecount(0);
96 97 98 99 100
    }

    bless($self, $class);
    # XXX
    $self->noretry(0);
Leigh B Stoller's avatar
Leigh B Stoller committed
101
    $self->canceled(0);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    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
118
sub oplist($)		{ return $_[0]->{'OPLIST'}; }
119 120
sub user($)		{ return $_[0]->{'USER'}; }
sub experiment($)	{ return $_[0]->{'EXPT'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
121
sub IncrFailCount($)    { $_[0]->{'FAILCOUNT'}++ }
122
sub failed($)           { return $_[0]->{'FAILCOUNT'}; }
123 124 125 126 127 128

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

Leigh B Stoller's avatar
Leigh B Stoller committed
129 130 131 132 133 134 135
    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};
    }
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
    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;
}

# To avoid writting out all the methods.
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;
}

#
#
Leigh B Stoller's avatar
Leigh B Stoller committed
173
sub OperationList($)
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
{
    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.
#
sub SetupReload($$$)
{
    my ($self, $node, $osinfo) = @_;
    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))) {
	# Mark this node as needing this image. 
	$node->_loadimage($image);
Leigh B Stoller's avatar
Leigh B Stoller committed
204
	$node->_setupoperation($RELOAD);
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 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
    }
    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'} = {};
}

#
# 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'} });
    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".
		 "Thanks\n".
		 "Testbed Operations\n",
		 0,
		 "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'} });
    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".
		 "The nodes have been moved into hardware checkup.\n",
		 "$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'} });
    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".
		 "The nodes have been freed.\n",
		 "$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();
    my $osinfo        = OSinfo->Lookup($osid);
    my $experiment    = $self->experiment();
    my $pid           = $experiment->pid();
    # Do we use def_boot_path anymore?
Leigh B Stoller's avatar
Leigh B Stoller committed
329
    my $bootpath      = defined($node->def_boot_path()) && 
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 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 417 418
	$node->def_boot_path() ne "";

    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()) {
	$imageable = 1;
    }

    if (!$bootpath && $imageable) {
	#
	# 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!");
	}
	#
	# If there is an actual path, its an OSKit kernel not an image.
	#
	if (! defined($osinfo->path()) || $osinfo->path() eq "") {
	    my $nextosinfo;
	    
	    #
	    # Not an OSKit kernel.
	    #
	    if ($osinfo->IsGeneric()) {
		#
		# Map generic OSID to the specific one.
		#
		$nextosinfo = $osinfo->ResolveNextOSID($experiment);
		if (!defined($nextosinfo)) {
		    die_noretry("No next mapping for $osinfo on $node!\n");
		}
		print "Mapping $osinfo on $node_id to $nextosinfo\n";
		$osinfo = $nextosinfo;
	    }
	    #
	    # 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) {
		#
		# OSID is loaded, but might need to be cleaned.
		#
		if ($self->dolastload() &&
		    defined($node->last_reservation()) &&
		    $node->last_reservation() ne $pid) {
		    $self->SetupReload($node, $osinfo);
		}
		elsif ($nextosinfo) {
		    #
		    # 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.
		    #
		    if ($self->impotent()) {
			print STDERR "$node_id:def_boot_osid --> $osinfo\n"
			    if ($self->debug());
		    }
		    elsif ($node->OSSelect($osinfo, "def_boot_osid", 0)) {
			die_noretry("Could not set boot OS to ".
				    "$osinfo for $node");
		    }
		}
	    }
	    else {
		#
		# OS not loaded.
		#
		$self->SetupReload($node, $osinfo);
	    }
	}
    }
419 420 421
    # Remember this for later. 
    $node->_bootosinfo($osinfo);
    
422 423 424 425 426 427 428 429
    print STDERR "$node_id - $osinfo\n"
	if ($self->debug());
}

#
# Take a list of nodes and fire off the required reloads/reboots/reconfigs
# in parallel, then wait for finish.
#
430 431 432
# 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. 
#
433 434 435 436
sub LightUpNodes($@)
{
    my ($self, @nodelist) = @_;
    my @children = ();
Leigh B Stoller's avatar
Leigh B Stoller committed
437
    my @failed   = ();
438 439 440 441 442 443 444 445 446 447 448 449 450 451

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

Leigh B Stoller's avatar
Leigh B Stoller committed
452
	if ($op == $RELOAD) {
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
	    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";
	}
469
	print STDERR "$node_id will be $action\n";
470 471 472
    }

    # XXX Caller wants a list. 
473
    return 0
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
	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
504
	    # will not be booting.  So for them, osload just sets up the
505 506 507 508 509 510 511 512 513 514 515 516 517 518 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 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
	    # 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;

	my $pid = osload(\%reload_args, $reload_failures);
	push(@children, [ $pid, \&osload_wait,
			  [ @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;
	$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;
	$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
622 623 624
	# Mark all of the failed nodes so that the caller knows there
	# was a failure. 
	# 
625 626
	foreach my $node_id (@nlist) {
	    my $node = $self->node($node_id);
Leigh B Stoller's avatar
Leigh B Stoller committed
627

628 629 630
	    $node->SetAllocState(TBDB_ALLOCSTATE_DOWN());
	}
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

    return scalar(@failed);
}

#
# Generic waiting function for a list of nodes.
#
sub WaitForNodes($@)
{
    my ($self, @nodelist) = @_;
    my %nodes = ();
    my @waitstates = (TBDB_NODESTATE_TBFAILED, TBDB_NODESTATE_ISUP);

    # 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();
661 662
	my $typehandler = $node->_typehandler();
	
Leigh B Stoller's avatar
Leigh B Stoller committed
663 664 665 666 667 668 669 670 671 672 673
	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});
674 675
	$node->_setupstatus($RELOAD_FAILED);
	$typehandler->WaitDone($node);
Leigh B Stoller's avatar
Leigh B Stoller committed
676 677 678 679 680 681 682 683
    }

    #
    # Start a counter going, relative to the time we rebooted the first
    # node.
    #
    my $waittime  = 0;
    my $minutes   = 0;
684
    my $canceled  = $self->canceled();
Leigh B Stoller's avatar
Leigh B Stoller committed
685 686 687 688 689 690 691 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 721 722 723 724

    #
    # 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";
725
		$node->_setupstatus($SETUP_FAILED);
Leigh B Stoller's avatar
Leigh B Stoller committed
726 727 728 729 730 731 732
	      node_error:
		delete($nodes{$node_id});
		$typehandler->WaitDone($node);
		next;
	    }
	    if (grep {$_ eq $state} @waitstates) {
		print "$node_id has reported state $state\n";
733
		$node->_setupstatus($SETUP_OKAY);
Leigh B Stoller's avatar
Leigh B Stoller committed
734 735 736 737 738 739 740 741 742 743 744 745 746
	      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");

747
		    $node->_setupstatus($SETUP_FAILED);
Leigh B Stoller's avatar
Leigh B Stoller committed
748 749 750 751 752
		    delete($nodes{$node_id});
		    $typehandler->WaitDone($node);
		}
		else {
		    if ($typehandler->Retry($node) != 0) {
753
			$node->_setupstatus($SETUP_OKAY);
Leigh B Stoller's avatar
Leigh B Stoller committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
			delete($nodes{$node_id});
			$typehandler->WaitDone($node);
		    }
		    else {
			$node->_retrycount($node->_retrycount() - 1);
			$node->_waitstart(time());
		    }
		}
		next;
	    }
	    if (int($waittime / 60) > $minutes) {
		$minutes = int($waittime / 60);
		tbnotice("Still waiting for $node_id ($state) - ".
			 "it's been $minutes minute(s).\n");
	    }
	}
	sleep(5);
    }
    return 0;
773 774 775 776 777 778 779 780
}

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

    #
    # These special cases will eventually be encoded in the DB.
784
    # Note that type might be a class, and so Lookup() returns nothing.
785
    #
786 787 788 789 790 791 792 793
    my $typeinfo = NodeType->Lookup($type);

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

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
    my $packname = "libossetup_${type}";
    my $newtype  = eval { $packname->New($self); };
    # Not loaded?
    if ($@) {
	#print STDERR $@;
	return undef;
    }
    $self->{'TYPECACHE'}->{$type} = $newtype;
    
    print STDERR "Created type object $type\n"
	if ($self->debug());
    
    return $newtype;
}
#
# Return the cached type object.
#
812
sub TypeLookup($$)
813 814 815
{
    my ($self, $node) = @_;

816 817 818 819 820 821
    my $type  = $node->type();
    my $class = $node->class();

    #
    # These special cases will eventually be encoded in the DB.
    #
822 823 824 825 826 827
    if ($type eq "pcfedphys" || $type eq "pcfed") {
	$type = "protogeni";
    }
    elsif ($node->issubnode()) {
	$type = "subnode";
    }
828

829 830 831 832 833
    return $self->{'TYPECACHE'}->{$type}
        if (exists($self->{'TYPECACHE'}->{$type}));

    return $self->{'TYPECACHE'}->{$class}
        if (exists($self->{'TYPECACHE'}->{$class}));
834 835 836 837 838 839 840 841

    return undef;
}

#
# Wrapper class for the type/class specific packages below.
#
package libossetup_handler;
842
use base qw(libossetup);
843 844 845 846 847 848 849 850

use libdb;
use libtestbed;
use libossetup;
use libtblog;
use Node;
use English;
use Data::Dumper;
851
use overload ('""' => 'Stringify');
852 853 854 855 856

sub New($$$)
{
    my ($class, $type, $parent) = @_;

Leigh B Stoller's avatar
Leigh B Stoller committed
857 858 859 860 861 862
    my $self             = {};
    $self->{'TYPE'}      = $type;
    $self->{'NODES'}     = {};
    $self->{'PARENT'}    = $parent;
    $self->{'FAILCOUNT'} = 0;
    $self->{'TODOLIST'}  = {};
863 864 865 866 867 868 869

    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
870
sub failcount($)	{ return $_[0]->{'FAILCOUNT'}; }
871
sub nodelist($)         { return values(%{ $_[0]->{'NODES'} }); }
Leigh B Stoller's avatar
Leigh B Stoller committed
872 873 874
sub todo($)		{ return $_[0]->{'TODOLIST'}; }
sub todolist($)		{ return values(%{ $_[0]->{'TODOLIST'} }); }
sub IncrFailCount($)    { $_[0]->{'FAILCOUNT'}++ }
875 876

#
Leigh B Stoller's avatar
Leigh B Stoller committed
877
# Add a node to the list.
878
#
Leigh B Stoller's avatar
Leigh B Stoller committed
879
sub AddNode($$)
880
{
Leigh B Stoller's avatar
Leigh B Stoller committed
881 882 883 884 885 886 887
    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);
888 889

    #
Leigh B Stoller's avatar
Leigh B Stoller committed
890 891
    # This sets the OS that should boot, as well as any reloads, reboots
    # and reconfigs that are needed.
892
    #
Leigh B Stoller's avatar
Leigh B Stoller committed
893 894
    $self->parent()->SetOS($node);
    return 0;
895 896 897
}

#
Leigh B Stoller's avatar
Leigh B Stoller committed
898 899
# Stub function to light up a set of nodes; calls the big version in the
# parent.
900
#
Leigh B Stoller's avatar
Leigh B Stoller committed
901
sub LightUpNodes($@)
902
{
Leigh B Stoller's avatar
Leigh B Stoller committed
903
    my ($self, @nodelist)  = @_;
904 905
    my $parent = $self->parent();

Leigh B Stoller's avatar
Leigh B Stoller committed
906 907
    return $parent->LightUpNodes(@nodelist);
}
908

Leigh B Stoller's avatar
Leigh B Stoller committed
909 910 911 912 913 914
#
# Return the set of nodes ready to go.
#
sub Volunteers($)
{
    my ($self) = @_;
915

Leigh B Stoller's avatar
Leigh B Stoller committed
916 917 918 919 920 921 922 923 924
    return $self->todolist();
}

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

    #
Leigh B Stoller's avatar
Leigh B Stoller committed
927 928
    # Return an error since this should not be called; it
    # should be overridden or the retry count always set to zero.
929
    #
Leigh B Stoller's avatar
Leigh B Stoller committed
930 931
    return -1;
}
932

Leigh B Stoller's avatar
Leigh B Stoller committed
933 934 935 936 937 938
#
# Generic wait for a single function.
#
sub WaitForNode($$)
{
    my ($self, $node) = @_;
939

Leigh B Stoller's avatar
Leigh B Stoller committed
940 941 942
    # Tell caller I have nothing to offer. 
    return 0;
}
943

Leigh B Stoller's avatar
Leigh B Stoller committed
944 945 946 947 948 949
#
# Generic waitdone function.
#
sub WaitDone($@)
{
    my ($self, @nodelist) = @_;
950 951
    my $parent     = $self->parent();
    my $experiment = $parent->experiment();
Leigh B Stoller's avatar
Leigh B Stoller committed
952 953 954 955 956 957 958 959 960

    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});
961 962 963 964
    }
    return 0;
}

965 966 967 968 969 970 971 972 973 974 975 976
#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $type = $self->type();

    return "[$type]";
}

977 978 979 980 981 982
#####################################################################
#
# Generic handler for local cluster nodes that do not require much
# special handling.
#
package libossetup_pc;
983
use base qw(libossetup_handler);
984 985 986 987 988 989 990 991

use libdb;
use libtestbed;
use libossetup;
use libtblog;
use Node;
use English;
use Data::Dumper;
992
use overload ('""' => 'Stringify');
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019

#
# 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();
	return 0;
    }

Leigh B Stoller's avatar
Leigh B Stoller committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
    $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);
	}
1034
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

    #
    # 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;
1047
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
1048 1049
    $node->_retrycount(1);
    $node->_maxwait($waittime);
1050
    $node->_setupstatus($libossetup::SETUP_OKAY);
1051 1052 1053 1054 1055

    return 0;
}

#
Leigh B Stoller's avatar
Leigh B Stoller committed
1056
# Wait function signals some local cluster nodes are done waiting.
1057
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1058
sub WaitDone($@)
1059
{
Leigh B Stoller's avatar
Leigh B Stoller committed
1060
    my ($self, @nodelist) = @_;
1061 1062 1063 1064
    my $parent     = $self->parent();
    my $experiment = $parent->experiment();
    my $pid        = $experiment->pid();
    my $eid        = $experiment->eid();
Leigh B Stoller's avatar
Leigh B Stoller committed
1065

1066
    #
Leigh B Stoller's avatar
Leigh B Stoller committed
1067
    # Must call the generic WaitDone handler too.
1068
    #
Leigh B Stoller's avatar
Leigh B Stoller committed
1069
    $self->SUPER::WaitDone(@nodelist);
1070 1071

    #
Leigh B Stoller's avatar
Leigh B Stoller committed
1072
    # Then per node processing.
1073 1074
    #
    foreach my $node (@nodelist) {
1075 1076 1077
	my $node_id     = $node->node_id();
	my $setupstatus = $node->_setupstatus();
	my $eventstate  = $node->eventstate();
1078

1079
	if ($eventstate eq TBDB_NODESTATE_ISUP()) {
1080 1081 1082
	    print "$node_id is alive and well\n";
	    $node->SetBootStatus(NODEBOOTSTATUS_OKAY);
	    $node->SetAllocState(TBDB_ALLOCSTATE_RES_READY());
1083 1084 1085 1086
	    # 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);
1087 1088 1089
	    next;
	}
	# Fall through on failure.	
1090
	if ($eventstate eq TBDB_NODESTATE_TBFAILED()) {
1091 1092 1093 1094 1095 1096 1097
	    tbwarn("$node_id reported a TBFAILED event\n");
	}
	else {
	    tbwarn("$node_id failed to boot\n");
	}
	$node->SetBootStatus(NODEBOOTSTATUS_FAILED);

1098 1099 1100 1101
	#
	# Reload failures are terminal.
	#
	if ($node->_canfail() && $setupstatus != $libossetup::RELOAD_FAILED &&
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
	    !($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;
	}

	#
	# 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
1132 1133
	$self->IncrFailCount();
	$parent->IncrFailCount();
1134 1135 1136 1137 1138 1139
	if ($setupstatus == $libossetup::RELOAD_FAILED) {
	    $parent->add_failed_node_reload($node_id);
	}
	else {
	    $parent->add_failed_node_fatal($node_id);
	}
1140
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
    return 0;
}

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

    tbnotice("Rebooting $node_id and waiting again ...\n");

    system("$NODEREBOOT $node_id");
    return $?;
1156 1157
}

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
#####################################################################
package libossetup_pcRemote;
use base qw(libossetup_pc);

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

1171 1172
#####################################################################
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1173 1174
# This is a wrapper for virtnodes; hopefully most virtnodes can share
# stuff.
1175
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1176
package libossetup_virtnode;
1177
use base qw(libossetup_handler);
1178 1179 1180 1181

use libdb;
use libtestbed;
use libossetup;
1182
use libtblog;
1183 1184 1185
use Node;
use English;
use Data::Dumper;
1186
use overload ('""' => 'Stringify');
1187

Leigh B Stoller's avatar
Leigh B Stoller committed
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
#
# A constructor for an object to handle all nodes of this type.
#
sub New($$$) {
    my ($class, $type, $parent) = @_;

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

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

    #
    # The old code could deal with just jailed, plab, and remote nodes.
    # We are dropping plab support for now ...
    #
    return 0
	if (! ($node->jailflag() || $node->isremote()));

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

    #
    # We need the physical node, although it if the physnode is shared,
    # it will not be allocated to this experiment.
    #
    my $pnode_id = $node->phys_nodeid();
    my $pnode    = $parent->node($pnode_id);
    if (!defined($pnode)) {
	$pnode    = Node->Lookup($pnode_id); 
	if (!defined($pnode)) {
	    die_noretry("Cannot lookup object for $pnode_id!");
	}
	# Put it in the parent node list cache to avoid lookup.
	$parent->{'PHYSNODES'}->{$pnode_id} = $pnode;
	$pnode->_vnodecount(0);
    }
    
    #
    # Count up the number of VMs on this pnode. We use this for
    # determining how long to wait for a particular vnode.
    #
    $pnode->_vnodecount($pnode->_vnodecount() + 1);
    # and stash the pnode in case we need it later.
    $node->_pnode($pnode);

    #
    # See if a "reload" is required; some virtual nodes can get
    # custom guest OSs, which are loaded on the client side with
    # a sorta standard frisbee approach. We reach into the libosload
    # library to set up the necessary state, but otherwise the node
1243
    # is rebooted normally (or starts when its physical node starts).
Leigh B Stoller's avatar
Leigh B Stoller committed
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
    #
    if ($node->_setupoperation() eq $libossetup::RELOAD) {
	my $image = $node->_loadimage();

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

	$reload_args{'debug'}     = $parent->debug();
	$reload_args{'asyncmode'} = 0;
	$reload_args{'waitmode'}  = 0;
	$reload_args{'noreboot'}  = 1;
	$reload_args{'imageid'}   = $image->imageid();
	$reload_args{'nodelist'}  = [ $node_id ];

	osload(\%reload_args, $reload_failures);

1260
	# Reset this. Updated in Volunteers() below. 
Leigh B Stoller's avatar
Leigh B Stoller committed
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
	$node->_setupoperation($libossetup::NOSTATE);
    }
    return 0;
}

#
# VMs are done with a different program. 
#
sub LightUpNodes($@)
{
    my ($self, @nodelist)  = @_;
    my $parent     = $self->parent();
    my $experiment = $parent->experiment();
    my $pid        = $experiment->pid();
    my $eid        = $experiment->eid();
1276
    my @nodeids    = map { $_->node_id() } @nodelist;
Leigh B Stoller's avatar
Leigh B Stoller committed
1277 1278 1279

    tbnotice("Setting up virtual testbed nodes ...\n");
    TBDebugTimeStamp("vnode_setup starting");
1280
    system("$VNODESETUP -j $pid $eid @nodeids");
Leigh B Stoller's avatar
Leigh B Stoller committed
1281 1282 1283 1284
    my $exitval = $?;
    TBDebugTimeStamp("vnode_setup done");

    if ($exitval) {
1285 1286
	# This is very unusual.
	$parent->noretry(1);
Leigh B Stoller's avatar
Leigh B Stoller committed
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
	return -1;
    }
    return 0;
}

sub Volunteers($)
{
    my ($self)     = @_;
    my @nodelist   = ();
    my @failed     = ();
    my $parent     = $self->parent();
    my $experiment = $parent->experiment();

    #
    # Look for nodes that we can do on this pass. Some nodes have
    # dependencies.
    #
1304
    foreach my $node ($self->todolist()) {
Leigh B Stoller's avatar
Leigh B Stoller committed
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
	my $node_id     = $node->node_id();
	my $pnode       = $node->_pnode();
	my $pnode_id    = $pnode->node_id();
	my $typehandler = $node->_typehandler();
	
	#
	# Make sure we get fresh state after vnode_setup ran.
	#
	$node->Refresh();
	$pnode->Refresh();

	# No retry.
	$node->_retrycount(0);

	#
	# If the physnode is not ours, the vnode always has to do the
	# setup, since the physnode does not reboot.
	#
        my $reservation = $pnode->Reservation();
	if (!defined($reservation)) {
	    die_noretry("Cannot lookup object for $pnode_id!");
	}

	#
	# If the physnode is not in this experiment, there is no
	# dependency, and we can do it now. Otherwise, we have to
	# wait to a later pass, after the physnode.
	#
1333
	if ($reservation->SameExperiment($parent->experiment())) {
Leigh B Stoller's avatar
Leigh B Stoller committed
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
	    #
	    # If the node was rebooted, then we can determine if
	    # the vnode is dead, or worth waiting for.
	    #
	    if ($pnode->_rebooted()) {
		#
		# Virtnodes automatically boot up, but we still have
		# to wait for them. 
		#
		if ($pnode->allocstate() eq TBDB_ALLOCSTATE_RES_READY()) {
		    #
		    # Set the allocstate for the local vnodes that were
		    # sucessfully rebooted and came to ISUP above. These do
		    # not need to be setup again. We move them to
		    # RES_READY, so vnode_setup will ignore them. If they
		    # fail to hit ISUP, we will move them to DOWN so that
		    # vnode_setup will ignore them again, in the teardown
		    # phase.
		    #
		    $node->SetAllocState(TBDB_ALLOCSTATE_RES_READY());
		    print "$node_id will boot up with $pnode_id\n"
			if ($parent->debug());
		}
		else {
		    #
		    # Something went wrong with the physnode reboot, so the
		    # virtnodes are DOA.
		    #
Leigh B Stoller's avatar
Leigh B Stoller committed
1362
		    $node->_setupstatus($libossetup::SETUP_FAILED);
Leigh B Stoller's avatar
Leigh B Stoller committed
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
		    $typehandler->WaitDone($node);
		    next;
		}
	    }
	    else {
		#
		# If the physnode was not rebooted, then the node is either
		# new and not processed yet, or it was an existing node and
		# swapmod was called with the no-reboot/reconfig option, in
		# which case we have to send the node through vnode_setup.
		#
		if ($pnode->allocstate() eq TBDB_ALLOCSTATE_RES_READY()) {
		    #
		    # Node is already part of the experiment and not
		    # going to be rebooted or reconfiged, so must go
		    # through vnode_setup, and it can happen now since the
		    # physnode is ready. 
		    #
		    print "$node_id needs vnode_setup\n"
			if ($parent->debug());

		    $node->_setupoperation($libossetup::REBOOT);
		}
		else {
		    print "Skipping $node_id this time around\n"
			if ($parent->debug());
1389
		    next;
Leigh B Stoller's avatar
Leigh B Stoller committed
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
		}
	    }
	}
	else {
	    $node->_setupoperation($libossetup::REBOOT);
	}
	push(@nodelist, $node);
	
        #
        # Base the maxwait for vnodes on the reboot_waittime field for
        # their respective OSIDs, with some slop time that scales up
        # as a function of the number of vnodes on the parent pnode.
        #
	my $osinfo      = $node->_bootosinfo();
	my $reboot_time = $osinfo->reboot_waittime();

	#
	# The wait times are totally bogus! Need a better way to do this.
	#
	if ($node->isremotenode()) {
	    $node->_maxwait($reboot_time + (60 * $pnode->_vnodecount()));
	}
	else {
	    $node->_maxwait($reboot_time + (40 * $pnode->_vnodecount()));
	}
1415
	$node->_seteupstatus($libossetup::SETUP_OKAY);
Leigh B Stoller's avatar
Leigh B Stoller committed
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
    }
    return @nodelist;
}

#
# Look for unusual errors when waiting for virt nodes.
#
sub WaitForNode($$)
{
    my ($self, $node) = @_;

    return -1
	if ($node->allocstate() eq TBDB_ALLOCSTATE_DOWN() ||
	    $node->allocstate() eq TBDB_ALLOCSTATE_DEAD());

    return 0;
}

#
# VMs have signaled.
#
sub WaitDone($@)
{
    my ($self, @nodelist) = @_;
    my $parent     = $self->parent();
    my $experiment = $parent->experiment();
    my @failed     = ();
    
1444 1445 1446 1447 1448
    #
    # Must call the generic WaitDone handler too.
    #
    $self->SUPER::WaitDone(@nodelist);

Leigh B Stoller's avatar
Leigh B Stoller committed
1449 1450 1451 1452
    #
    # See who booted okay; save failures for next loop.
    #
    foreach my $node (@nodelist) {
1453 1454
	my $node_id     = $node->node_id();
	my $eventstate  = $node->eventstate();
Leigh B Stoller's avatar
Leigh B Stoller committed
1455

1456
	if ($eventstate eq TBDB_NODESTATE_ISUP()) {
Leigh B Stoller's avatar
Leigh B Stoller committed
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
	    print "$node_id is alive and well\n";
	    $node->SetBootStatus(NODEBOOTSTATUS_OKAY);
	    $node->SetAllocState(TBDB_ALLOCSTATE_RES_READY());
	    next;
	}
	$node->SetBootStatus(NODEBOOTSTATUS_FAILED);
	$node->SetAllocState(TBDB_ALLOCSTATE_DOWN());
	push(@failed, $node);
    }
    foreach my $node (@failed) {
1467 1468 1469
	my $node_id     = $node->node_id();
	my $eventstate  = $node->eventstate();
	my $setupstatus = $node->_setupstatus();
Leigh B Stoller's avatar
Leigh B Stoller committed
1470 1471

	# Fall through on failure.	
1472
	if ($eventstate eq TBDB_NODESTATE_TBFAILED()) {
Leigh B Stoller's avatar
Leigh B Stoller committed
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
	    tbwarn("$node_id reported a TBFAILED event\n");
	}
	else {
	    tbwarn("$node_id failed to boot\n");
	}
	if ($node->_canfail() &&
	    !($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;
	}
	$self->IncrFailCount();
	$parent->IncrFailCount();
	$parent->add_failed_node_fatal($node_id);
    }
    return 0;
}

1493 1494
#####################################################################
#
1495 1496 1497
# Local virtual nodes.
#
# Use libossetup_virtnode class above.
1498
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1499
package libossetup_pcvm;
1500
use base qw(libossetup_virtnode);
1501 1502 1503 1504

use libdb;
use libtestbed;
use libossetup;
1505
use libtblog;
1506 1507 1508
use Node;
use English;
use Data::Dumper;
1509
use overload ('""' => 'Stringify');
1510

Leigh B Stoller's avatar
Leigh B Stoller committed
1511 1512 1513
#
# A constructor for an object to handle all nodes of this type.
#
1514 1515
sub New($$) {
    my ($class, $parent) = @_;
Leigh B Stoller's avatar
Leigh B Stoller committed
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528

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

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

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

1529 1530 1531 1532 1533
#####################################################################
#
# This is a catchall for subnodes that do not have a type handler.
#
package libossetup_subnode;
1534
use base qw(libossetup_handler);
1535 1536 1537 1538

use libdb;
use libtestbed;
use libossetup;
1539
use libtblog;
1540 1541 1542
use Node;
use English;
use Data::Dumper;
1543
use overload ('""' => 'Stringify');
1544

1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
#
# A constructor for an object to handle all nodes of this type.
#
sub New($$) {
    my ($class, $parent) = @_;

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

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

    print "Will skip subnode $node ISUP wait.\n";

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

#
# We do not currently do anything with subnodes; no waiting for ISUP. 
#
sub Volunteers($)
{
    my ($self) = @_;

    return ();
}

1575 1576
#####################################################################
#
1577 1578 1579 1580
# All protogeni nodes.
#
package libossetup_protogeni;
use base qw(libossetup_handler);
1581

1582 1583
my $PGENISUPPORT= @PROTOGENI_SUPPORT@;

1584 1585 1586
use libdb;
use libtestbed;
use libossetup;
1587
use libtblog;
1588 1589 1590
use Node;
use English;
use Data::Dumper;
1591
use overload ('""' => 'Stringify');
1592

1593 1594 1595 1596
if ($PGENISUPPORT) {
    require libGeni;
}

1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632
sub New($$) {
    my ($class, $parent) = @_;

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

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

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

    #
    # 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;
    }
    $node->_retrycount(0);
    $node->_maxwait($waittime);
    $node->_setupstatus($libossetup::SETUP_OKAY);
    return 0;
}

#
# Protogeni are special.
#
# The problem with the current approach, is that this can happen in
1633 1634
# parallel with all of os_setup; there are no dependencies. Maybe we
# need pre/post hooks of some kind?
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
#
sub LightUpNodes($@)
{
    my ($self, @nodelist)  = @_;
    my $parent     = $self->parent();
    my $experiment = $parent->experiment();
    my $pid        = $experiment->pid();
    my $eid        = $experiment->eid();

    TBDebugTimeStamp("Starting Geni setup.");
    if (libGeni::StartSlivers($experiment,
			      $parent->user(), 0, $parent->debug())) {
	print STDERR "*** Could not start Geni slivers\n";
	return 1;
    }
    TBDebugTimeStamp("Geni slivers have been started.");
    return 0;
}

1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
#
# Look for unusual errors when waiting for virt nodes.
#
sub WaitForNode($$)
{
    my ($self, $node) = @_;
    my $parent = $self->parent();

    #
    # No need to wait for protogeni nodes after cancel.
    #
    if ($parent->canceled()) {
	$node->_maxwait(0);
    }
    return $self->SUPER::WaitForNode($node);
}

1671 1672
#
# Nodes have signaled.
1673
#
1674 1675 1676 1677 1678
sub WaitDone($@)
{
    my ($self, @nodelist) = @_;
    my $parent     = $self->parent();
    my $experiment = $parent->experiment();
1679

1680 1681 1682 1683 1684 1685 1686
    #
    # I think this will work; just use the virtnode waitdone function.
    #
    return libossetup_virtnode::WaitDone($self, @nodelist);
}

1;