libossetup.pm.in 55.5 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

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

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

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#
# 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
97
98
    $self->{'GROUP'}      = (defined($experiment) ?
			     $experiment->GetGroup() : undef);
99
100
    $self->{'HASH'}       = {};
    $self->{'NODES'}      = {};
Leigh B Stoller's avatar
Leigh B Stoller committed
101
    $self->{'PHYSNODES'}  = {};
102
    $self->{'FAILED'}     = {};
103
    $self->{'FAILCOUNT'}  = 0;
104
105
    $self->{'OSMAP'}      = {};
    $self->{'TYPECACHE'}  = {};
Leigh B Stoller's avatar
Leigh B Stoller committed
106
    $self->{'OPLIST'}     = {};
107
108
109
110
111
112
113
114

    # 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
115
116
117

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

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

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

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
    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
153
sub oplist($)		{ return $_[0]->{'OPLIST'}; }
154
155
sub user($)		{ return $_[0]->{'USER'}; }
sub experiment($)	{ return $_[0]->{'EXPT'}; }
156
sub group($)		{ return $_[0]->{'GROUP'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
157
sub IncrFailCount($)    { $_[0]->{'FAILCOUNT'}++ }
158
sub failed($)           { return $_[0]->{'FAILCOUNT'}; }
159
160
161
162
163
164

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

Leigh B Stoller's avatar
Leigh B Stoller committed
165
166
167
168
169
170
171
    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};
    }
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    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
189
# To avoid writing out all the methods.
190
191
sub AUTOLOAD {
    my $self = shift;
192
    my $type = ref($self) or die("$self is not an object");
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

    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
209
sub OperationList($)
210
211
212
213
214
215
216
217
218
219
{
    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.
#
220
sub SetupReload($$$$)
221
{
222
    my ($self, $node, $osinfo, $auxload) = @_;
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    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))) {
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
	#
	# 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
255
	$node->_setupoperation($RELOAD);
256
	$node->_reloaded(1);
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
    }
    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'} = {};
}

297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#
# 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;
}

316
317
318
319
320
321
322
323
324
325
326
327
#
# 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'} });
328
    my $logmsg   = $self->CollectBootLogFiles(@nodelist);
329
330
331
332
333
334
335
336
337
338
339
340
    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".
341
342
		 "$logmsg\n",
		 undef,
343
344
345
346
347
348
349
350
351
352
353
354
355
		 "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'} });
356
    my $logmsg   = $self->CollectBootLogFiles(@nodelist);
357
358
359
360
361
362
    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".
363
		 "The nodes have been moved into hardware checkup.\n\n$logmsg",
364
365
366
367
368
369
370
371
372
373
374
375
376
		 "$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'} });
377
    my $logmsg   = $self->CollectBootLogFiles(@nodelist);
378
379
380
381
382
383
    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".
384
		 "The nodes have been freed.\n\n$logmsg",
385
386
387
388
389
390
391
392
393
394
395
396
397
		 "$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();
398
399
    my $osid_vers     = $node->def_boot_osid_vers();
    my $osinfo        = OSinfo->Lookup($osid, $osid_vers);
400
    my $loadlist      = $node->loadlist();
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
    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()) {
419
420
421
422
423
424
425
	#
	# 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())));
426
427
    }

428
    if ($imageable) {
429
430
431
432
433
434
435
	#
	# 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!");
	}
436
	$self->SetOSAux($node, $osinfo, 0, $loadlist);
437

438
	#
439
	# Now look for additional loads. 
440
	#
441
442
443
444
445
446
447
448
449
450
	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));

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

#
# Helper for above.
#
465
sub SetOSAux($$$$$)
466
{
467
    my ($self, $node, $osinfo, $auxload, $multiple) = @_;
468
469
470
471
472
473
474
475
476
    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;
477
	    
478
479
480
481
	#
	# Not an OSKit kernel.
	#
	if ($osinfo->IsGeneric()) {
482
	    #
483
	    # Map generic OSID to the specific one.
484
	    #
485
486
487
	    $nextosinfo = $osinfo->ResolveNextOSID($experiment);
	    if (!defined($nextosinfo)) {
		die_noretry("No next mapping for $osinfo on $node!\n");
488
	    }
489
490
491
492
	    print "Mapping $osinfo on $node_id to $nextosinfo\n";
	    $osinfo = $nextosinfo;
	}
	#
493
494
495
496
497
498
	# 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");
	#
499
500
501
502
503
504
505
	# 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) {
506
507
508
509
510
511
512
513
514
	    #
	    # 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);
	    }
515
	    #
516
	    # OSID is loaded, but might need to be cleaned.
517
	    #
518
	    elsif ($self->dolastload() &&
519
520
521
		defined($node->last_reservation()) &&
		$node->last_reservation() ne $pid) {
		$self->SetupReload($node, $osinfo, $auxload);
522
	    }
523
	    elsif ($nextosinfo && !$auxload) {
524
		#
525
526
527
		# 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.
528
		#
529
530
531
		if ($self->impotent()) {
		    print STDERR "$node_id:def_boot_osid --> $osinfo\n"
			if ($self->debug());
532
		}
533
534
		elsif ($node->OSSelect($osinfo, "def_boot_osid", 0)) {
		    die_noretry("Could not set boot OS to $osinfo for $node");
535
536
		}
	    }
537
538
539
540
541
542
	}
	else {
	    #
	    # OS not loaded.
	    #
	    $self->SetupReload($node, $osinfo, $auxload);
543
544
545
546
547
548
549
550
	}
    }
}

#
# Take a list of nodes and fire off the required reloads/reboots/reconfigs
# in parallel, then wait for finish.
#
551
552
553
# 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. 
#
554
555
556
557
sub LightUpNodes($@)
{
    my ($self, @nodelist) = @_;
    my @children = ();
Leigh B Stoller's avatar
Leigh B Stoller committed
558
    my @failed   = ();
559
560
561
562
563
564
565
566
567
568
569
570
571
572

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

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

Leigh B Stoller's avatar
Leigh B Stoller committed
577
	if ($op == $RELOAD) {
578
579
	    my $image    = $node->_loadimage();
	    my @images   = ($image);
580
	    my $imageids = $image->versid();
581
582
583
584
585
586
587
588
	    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 }) {
589
		    my $auxid = $aux->versid();
590
591
592
593
594
595
596
		    unshift(@images, $aux);
		    $imageids = "${auxid},${imageids}";
		}
	    }
	    
	    if (!exists($reloads{"$imageids"})) {
		$reloads{"$imageids"} = [];
597
	    }
598
599
	    push(@{ $reloads{"$imageids"} }, $node);
	    $action = "reloaded with @images";
600
601
602
603
604
605
606
607
608
	}
	elsif ($op == $REBOOT) {
	    $reboots{$node_id} = $node;
	    $action = "rebooted";
	}
	elsif ($op == $RECONFIG) {
	    $reconfigs{$node_id} = $node;
	    $action = "reconfiged";
	}
609
	print STDERR "$node_id will be $action\n";
610
611
612
    }

    # XXX Caller wants a list. 
613
    return 0
614
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
	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
644
	    # will not be booting.  So for them, osload just sets up the
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
	    # 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;

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

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

680
681
682
683
684
685
686
687
688
689
690
	    $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,			  ,
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
			  [ @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;
720
	$reboot_args{'rebootmode'} = $self->realreboot();
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
	$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;
740
	$reboot_args{'rebootmode'} = $self->realreboot();
741
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
	$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
785
786
787
	# Mark all of the failed nodes so that the caller knows there
	# was a failure. 
	# 
788
789
	foreach my $node_id (@nlist) {
	    my $node = $self->node($node_id);
Leigh B Stoller's avatar
Leigh B Stoller committed
790

791
792
793
	    $node->SetAllocState(TBDB_ALLOCSTATE_DOWN());
	}
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823

    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();
824
825
	my $typehandler = $node->_typehandler();
	
Leigh B Stoller's avatar
Leigh B Stoller committed
826
827
828
829
830
831
832
833
834
835
836
	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});
837
838
	$node->_setupstatus($RELOAD_FAILED);
	$typehandler->WaitDone($node);
Leigh B Stoller's avatar
Leigh B Stoller committed
839
840
841
842
843
844
845
846
    }

    #
    # Start a counter going, relative to the time we rebooted the first
    # node.
    #
    my $waittime  = 0;
    my $minutes   = 0;
847
    my $canceled  = $self->canceled();
Leigh B Stoller's avatar
Leigh B Stoller committed
848
849
850
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

    #
    # 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";
888
		$node->_setupstatus($SETUP_FAILED);
Leigh B Stoller's avatar
Leigh B Stoller committed
889
890
891
892
893
894
895
	      node_error:
		delete($nodes{$node_id});
		$typehandler->WaitDone($node);
		next;
	    }
	    if (grep {$_ eq $state} @waitstates) {
		print "$node_id has reported state $state\n";
896
		$node->_setupstatus($SETUP_OKAY);
Leigh B Stoller's avatar
Leigh B Stoller committed
897
898
899
900
901
902
903
904
905
906
907
908
909
	      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");

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

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

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

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

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

987
988
989
990
991
992
    my $type  = $node->type();
    my $class = $node->class();

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

1000
1001
1002
1003
1004
    return $self->{'TYPECACHE'}->{$type}
        if (exists($self->{'TYPECACHE'}->{$type}));

    return $self->{'TYPECACHE'}->{$class}
        if (exists($self->{'TYPECACHE'}->{$class}));
1005
1006
1007
1008
1009
1010
1011
1012

    return undef;
}

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

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

1024
1025
use vars qw($AUTOLOAD);

1026
1027
1028
1029
sub New($$$)
{
    my ($class, $type, $parent) = @_;

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

    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
1043
sub failcount($)	{ return $_[0]->{'FAILCOUNT'}; }
1044
sub nodelist($)         { return values(%{ $_[0]->{'NODES'} }); }
Leigh B Stoller's avatar
Leigh B Stoller committed
1045
1046
1047
sub todo($)		{ return $_[0]->{'TODOLIST'}; }
sub todolist($)		{ return values(%{ $_[0]->{'TODOLIST'} }); }
sub IncrFailCount($)    { $_[0]->{'FAILCOUNT'}++ }
1048

1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
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;
}

1066
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1067
# Add a node to the list.
1068
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1069
sub AddNode($$)
1070
{
Leigh B Stoller's avatar
Leigh B Stoller committed
1071
1072
1073
1074
1075
1076
1077
    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);
1078
1079

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

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

Leigh B Stoller's avatar
Leigh B Stoller committed
1096
1097
    return $parent->LightUpNodes(@nodelist);
}
1098

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

Leigh B Stoller's avatar
Leigh B Stoller committed
1106
1107
1108
1109
1110
1111
1112
1113
1114
    return $self->todolist();
}

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

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

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

Leigh B Stoller's avatar
Leigh B Stoller committed
1130
1131
1132
    # Tell caller I have nothing to offer. 
    return 0;
}
1133

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

    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});
1151
1152
1153
1154
    }
    return 0;
}

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

    return "[$type]";
}

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

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

#
# 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();
1207
1208
	# Need to initialize this for the stats code.
	$node->_bootosinfo(undef);
1209
1210
1211
	return 0;
    }

Leigh B Stoller's avatar
Leigh B Stoller committed
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
    $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);
	}
1226
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238

    #
    # 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;
1239
    }
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
    # 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
1261
1262
    $node->_retrycount(1);
    $node->_maxwait($waittime);
1263
    $node->_setupstatus($libossetup::SETUP_OKAY);
1264
1265
1266
1267

    return 0;
}

1268
1269
1270
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
#
# 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;
}

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

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

1333
1334
1335
1336
1337
1338
1339
1340
1341
    #
    # 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);

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

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

1359
	    if ($firewalled && $node_id eq $firewall) {
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
		#
		# 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);
		    }
		}
	    }
1381
1382
1383
	    next;
	}
	# Fall through on failure.	
1384
	if ($eventstate eq TBDB_NODESTATE_TBFAILED()) {
1385
1386
1387
1388
1389
1390
1391
	    tbwarn("$node_id reported a TBFAILED event\n");
	}
	else {
	    tbwarn("$node_id failed to boot\n");
	}
	$node->SetBootStatus(NODEBOOTSTATUS_FAILED);

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

1400
	#
1401
	# Handle non-fatal failures (if we have not been canceled or aborted).
1402
	#
1403
1404
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
	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;
	    }
1454
1455
	}

1456
      bad:
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
	#
	# 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
1479
1480
	$self->IncrFailCount();
	$parent->IncrFailCount();
1481
1482
1483
1484
1485
1486
	if ($setupstatus == $libossetup::RELOAD_FAILED) {
	    $parent->add_failed_node_reload($node_id);
	}
	else {
	    $parent->add_failed_node_fatal($node_id);
	}
1487
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
    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 $?;
1503
1504
}

1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
#####################################################################
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');

1518
1519
#####################################################################
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1520
1521
# This is a wrapper for virtnodes; hopefully most virtnodes can share
# stuff.
1522
#
Leigh B Stoller's avatar
Leigh B Stoller committed
1523
package libossetup_virtnode;
1524
use base qw(libossetup_handler);
1525
1526
1527
1528

use libdb;
use libtestbed;
use libossetup;
Leigh B Stoller's avatar
Leigh B Stoller committed
1529
use libosload;
1530
use libtblog;
1531
1532
1533
use Node;
use English;
use Data::Dumper;
1534
use overload ('""' => 'Stringify');
1535

Leigh B Stoller's avatar
Leigh B Stoller committed
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
#
# 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
1558
	if (! ($node->jailflag() || $node->isremotenode()));
Leigh B Stoller's avatar
Leigh B Stoller committed
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584

    $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);
1585
1586
1587
1588
1589
    # Taint the pnode if this vnode was tainted by its OS
    # and is not on a shared host.  No user access already on
    # the latter.
    if (($node->IsTainted(TB_TAINTSTATE_USERONLY) ||
	 $node->IsTainted(TB_TAINTSTATE_BLACKBOX))
Kirk Webb's avatar
Kirk Webb committed
1590
	&& $pnode->erole() ne "sharedhost") {
1591
	$pnode->AddTaintState(TB_TAINTSTATE_BLACKBOX) == 0 
Kirk Webb's avatar
Kirk Webb committed
1592
	    or die_noretry("Could not add blackbox taint state to vnode host!\n");
1593
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
1594
1595
1596
1597
1598
1599

    #
    # 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
1600
    # is rebooted normally (or starts when its physical node starts).
Leigh B Stoller's avatar
Leigh B Stoller committed
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
    #
    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;
1612
	$reload_args{'imageid'}   = $image->versid();
Leigh B Stoller's avatar
Leigh B Stoller committed
1613
1614
	$reload_args{'nodelist'}  = [ $node_id ];

1615
1616
1617
1618
	if (EmulabFeatures->FeatureEnabled("NewOsload",$self->user(),
					   $self->group(),
					   $self->experiment())) {
	    ($parent->loadobj())->debug($self->debug());
1619
1620
1621
1622
1623
1624

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

1625
1626
1627
1628
1629
	    ($parent->loadobj())->osload(\%reload_args, $reload_failures);
	}
	else {
	    osload(\%reload_args, $reload_failures)
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
1630

1631
	# Reset this. Updated in Volunteers() below. 
Leigh B Stoller's avatar
Leigh B Stoller committed
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
	$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();
1647
    my @nodeids    = map { $_->node_id() } @nodelist;
Leigh B Stoller's avatar
Leigh B Stoller committed
1648
1649
1650

    tbnotice("Setting up virtual testbed nodes ...\n");
    TBDebugTimeStamp("vnode_setup starting");
1651
    system("$VNODESETUP -j $pid $eid @nodeids");
Leigh B Stoller's avatar
Leigh B Stoller committed
1652
1653
1654
1655
    my $exitval = $?;
    TBDebugTimeStamp("vnode_setup done");

    if ($exitval) {
1656
1657
	# This is very unusual.
	$parent->noretry(1);
Leigh B Stoller's avatar
Leigh B Stoller committed
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
	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.
    #
1675
    foreach my $node ($self->todolist()) {
Leigh B Stoller's avatar
Leigh B Stoller committed
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
	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.
	#
1704
	if ($reservation->SameExperiment($parent->experiment())) {
Leigh B Stoller's avatar
Leigh B Stoller committed
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
	    #
	    # 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
1733
		    $node->_setupstatus($libossetup::SETUP_FAILED);
Leigh B Stoller's avatar
Leigh B Stoller committed
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746