tbswap.in 43.9 KB
Newer Older
Chad Barb's avatar
 
Chad Barb committed
1 2 3 4
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2005 University of Utah and the Flux Group.
Chad Barb's avatar
 
Chad Barb committed
6 7 8 9
# All rights reserved.
#
use English;

Chad Barb's avatar
 
Chad Barb committed
10 11 12
# Returns 0 on success.
# Returns 1 on non-assign_wrapper failure.
# Returns (1 | assign_wrapper's errorcode) on assign_wrapper failure.
13 14
# Returns |0x40 if update caused a swapout. Icky.
# Returns -1 on uncontrolled error (die called).
Chad Barb's avatar
 
Chad Barb committed
15

Chad Barb's avatar
 
Chad Barb committed
16 17 18 19 20
# XXX: handle error cases for update? (backup the db?)
# XXX: Shouldn't do idempotent stuff twice for update.
# XXX: repush/calc routing for update??? (tbprerun)
# XXX: previz for update???              (tbprerun)
# XXX: make snmpit faster for update.
Chad Barb's avatar
Chad Barb committed
21 22 23
#
# XXX: for update, expt is swapped out on os_setup fail.
#      (we only recover if assign fails)
Chad Barb's avatar
 
Chad Barb committed
24 25 26

sub usage()
{
27
    print STDERR "Usage: $0 -force { in | out | update [-reboot] [-eventsys_restart] } pid eid\n";
Chad Barb's avatar
 
Chad Barb committed
28 29 30 31 32 33 34
    exit(-1);
}

#
# Configure variables
#
my $TBROOT         = "@prefix@";
Mike Hibler's avatar
Mike Hibler committed
35
my $TBOPS          = "@TBOPSEMAIL@";
Timothy Stack's avatar
Timothy Stack committed
36
my $TBLOGS         = "@TBLOGSEMAIL@";
37
my $THISHOMEBASE   = "@THISHOMEBASE@";
Chad Barb's avatar
 
Chad Barb committed
38 39
my $TESTMODE       = @TESTMODE@;
my $DISABLE_EVENTS = "@DISABLE_EVENT_SCHED@";
40
my $piper          = "$TBROOT/sbin/locpiper";
Chad Barb's avatar
 
Chad Barb committed
41 42 43 44 45 46 47 48 49 50 51

# Untaint the path
$ENV{'PATH'} = "/usr/bin:$TBROOT/libexec:$TBROOT/libexec/ns2ir" . 
    ":$TBROOT/sbin:$TBROOT/bin";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
52
use libadminctrl;
Mike Hibler's avatar
Mike Hibler committed
53
use libadminmfs;
Kevin Atkinson's avatar
 
Kevin Atkinson committed
54 55
use libtblog;

56
#require exitonwarn; # exitonwarn isn't really a module, so just require it
Chad Barb's avatar
 
Chad Barb committed
57 58 59 60

#
# Actual swap-in and swap-out functions, defined below.
#
Chad Barb's avatar
 
Chad Barb committed
61 62 63
sub doSwapout($);
sub doSwapin($);

64 65 66 67
#
# Firewall stuff
# XXX maybe should be elsewhere
#
68 69 70 71 72
sub FWSETUP()     { return 1; }
sub FWADDNODES()  { return 2; }
sub FWDELNODES()  { return 3; }
sub FWTEARDOWN()  { return 4; }
sub doFW($$$$);
73 74

# XXX fixme: should not be hardwired!
75
my $cnetstack = "-S Control";
76 77 78
my $cnetvlanname = "Control";


Chad Barb's avatar
 
Chad Barb committed
79 80 81 82
sub REAL()    { return 4; }
sub CLEANUP() { return 3; }
sub RETRY()   { return 2; }
sub UPDATE()  { return 1; }
Chad Barb's avatar
Chad Barb committed
83
sub UPDATE_RECOVER()  { return 0; }
Chad Barb's avatar
 
Chad Barb committed
84

85 86 87
#
# Grab global enable of linkdelays.
#
Kirk Webb's avatar
 
Kirk Webb committed
88
my $enablelinkdelays = TBGetSiteVar("general/linux_endnodeshaping");
89

Chad Barb's avatar
 
Chad Barb committed
90 91 92 93 94 95
#
# Turn off line buffering on output
#

$| = 1;

96 97
my $updateReboot   = 0;
my $updateReconfig = 1;
98 99
my $update_Eventsys_restart = 0;
my $elabinelab     = 0;
Chad Barb's avatar
 
Chad Barb committed
100 101
my $force  = 0;
my $errors = 0;
102
my $updatehosed = 0;
Chad Barb's avatar
 
Chad Barb committed
103
my $state;
104
my $canceled;
Chad Barb's avatar
 
Chad Barb committed
105 106
my $os_setup_pid;
my $cleanvlans;
107
my $nextState;
Chad Barb's avatar
 
Chad Barb committed
108 109

#
Chad Barb's avatar
 
Chad Barb committed
110
# First argument is either "in", "out", or "update";
Chad Barb's avatar
 
Chad Barb committed
111 112 113 114 115
# this value goes into $swapop.
#

my $swapop = shift;	

Chad Barb's avatar
 
Chad Barb committed
116 117 118 119
if (!$swapop || 
    (($swapop ne "in") && 
     ($swapop ne "out") &&
     ($swapop ne "update"))) {
Chad Barb's avatar
 
Chad Barb committed
120 121 122 123 124 125 126 127 128 129 130
    usage();
}

#
# Get other arguments.
#

while ($#ARGV > 1) {
    $arg = shift;
    if ($arg eq "-force") {
	$force = 1;
Chad Barb's avatar
Chad Barb committed
131 132
    } elsif ($arg eq "-reboot") {
	$updateReboot = 1;
133
	$updateReconfig = 0;
134 135 136
    } elsif ($arg eq "-noreconfig") {
	$updateReboot   = 0;
	$updateReconfig = 0;
137 138
    } elsif ($arg eq "-eventsys_restart" && $swapop eq "update") {
	$update_Eventsys_restart = 1;
Chad Barb's avatar
 
Chad Barb committed
139 140 141 142 143 144 145 146 147
    } else {
	usage();
    }
}
if ($#ARGV < 1) {
    usage();
}
my ($pid,$eid) = @ARGV;

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
#
# Untaint the arguments.
#
if ($pid =~ /^([-\@\w.]+)$/) {
    $pid = $1;
}
else {
    die("Tainted argument $pid!\n");
}
if ($eid =~ /^([-\@\w.]+)$/) {
    $eid = $1;
}
else {
    die("Tainted argument $eid!\n");
}

Kevin Atkinson's avatar
 
Kevin Atkinson committed
164 165 166 167 168 169
#
# Set Error reporting info
# 
tblog_set_info($pid,$eid,$UID);

#
Mike Hibler's avatar
Mike Hibler committed
170
# Turn on timestamps
Kevin Atkinson's avatar
 
Kevin Atkinson committed
171
#
Chad Barb's avatar
 
Chad Barb committed
172 173 174 175 176
TBDebugTimeStampsOn();

#
# Print starting message.
#
177 178
my $exptidx;
TBExptIDX($pid, $eid, \$exptidx);
Chad Barb's avatar
 
Chad Barb committed
179

180 181
print "Beginning swap-$swapop for $pid/$eid ($exptidx). " .
    TBTimeStampWithDate() . "\n";
Chad Barb's avatar
 
Chad Barb committed
182 183 184 185 186 187
TBDebugTimeStamp("tbswap $swapop started");

#
# Get experiment state; verify that experiment exists.
#
if (! ($state = ExpState($pid, $eid))) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
188
    tbdie "No such experiment $pid/$eid";
Chad Barb's avatar
 
Chad Barb committed
189
}
190 191 192
# Sanity check the current state. 
if (!$force) {
    if ($swapop eq "in") {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
193
	tbdie("Experiment should be ACTIVATING. Currently $state.")
194
	    if ($state ne EXPTSTATE_ACTIVATING);
Chad Barb's avatar
 
Chad Barb committed
195
    }
196
    elsif ($swapop eq "out") {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
197
	tbdie("Experiment should be SWAPPING. Currently $state.")
198
	    if ($state ne EXPTSTATE_SWAPPING);
Chad Barb's avatar
 
Chad Barb committed
199
    }
200
    elsif ($swapop eq "update") {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
201
	tbdie("Experiment should be MODIFY_RESWAP. Currently $state.")
202
	    if ($state ne EXPTSTATE_MODIFY_RESWAP);
Chad Barb's avatar
 
Chad Barb committed
203 204
    }
}
205 206
# Get elabinelab status. See below.
if (! TBExptIsElabInElab($pid, $eid, \$elabinelab)) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
207
    tbdie("Could not get elabinelab status for experiment $pid/$eid");
208
}
Chad Barb's avatar
 
Chad Barb committed
209

210 211 212 213 214
#
# See if the experiment is firewalled
#
my $firewalled = TBExptFirewall($pid, $eid);

Chad Barb's avatar
 
Chad Barb committed
215 216 217
#
# Do actual swapping
#
218 219 220 221 222
if ($swapop eq "out") {
    #
    # Swap out
    #
    $errors = doSwapout(REAL);
Chad Barb's avatar
 
Chad Barb committed
223
}
224
elsif ($swapop eq "update") {
225 226 227 228 229 230 231 232 233 234 235 236 237 238
    #
    # Before swapout, do cursory admission control to see if the
    # modified experiment will be swappable. assign_wrapper does a
    # more stringent check using assign.
    #
    print STDERR "Checking with Admission Control ...\n";
    if (! TBAdmissionControlCheck($UID, $pid, $eid, undef)) {
	print STDERR "Admission control failure!\n";
	print "Failingly finished swap-$swapop for $pid/$eid. " .
	    TBTimeStamp() . "\n";
	TBDebugTimeStamp("tbswap $swapop finished (failed)");
	exit(1);
    }
    
239 240 241 242 243 244 245
    #
    # Update.
    #
    # Phase One -- swap experiment partially out.
    #
    print STDERR "Backing up physical state...\n";
    TBExptBackupPhysicalState($pid,$eid);
Chad Barb's avatar
 
Chad Barb committed
246

247
    $errors = doSwapout(UPDATE);
Chad Barb's avatar
Chad Barb committed
248

249
    if ($errors) {
Chad Barb's avatar
 
Chad Barb committed
250
	#
251 252 253 254 255
	# Clean up the mess, leaving the experiment in the SWAPPED state,
	# 
	print STDERR "Cleaning up after errors.\n";
	doSwapout(CLEANUP);
	$updatehosed = 1;
Chad Barb's avatar
 
Chad Barb committed
256
    }
257
    else {
Chad Barb's avatar
 
Chad Barb committed
258
	#
259
	# Phase Two -- swap experiment back in.
Chad Barb's avatar
 
Chad Barb committed
260
	#
Chad Barb's avatar
Chad Barb committed
261 262
	$errors = doSwapin(UPDATE);

263
	if ($errors) {
Chad Barb's avatar
Chad Barb committed
264 265 266
	    #
	    # There were errors; see if we can recover.
	    #
267
	    my $CanRecover = 1;
Chad Barb's avatar
Chad Barb committed
268 269 270

	    if ($errors != 7) {
		print STDERR "Update failure occurred _after_ assign phase; ";
271
		$CanRecover = 0;
Chad Barb's avatar
Chad Barb committed
272 273
	    }

274 275 276 277 278 279 280 281
	    if ($CanRecover) {
		print STDERR "Recovering virtual and physical state.\n";

		if (TBExptRemoveVirtualState($pid, $eid) ||
		    TBExptRestoreVirtualState($pid, $eid) ||
		    TBExptRestorePhysicalState($pid,$eid)) {
		    print STDERR "Could not restore backed-up state; ";
		    $CanRecover = 0;
Chad Barb's avatar
Chad Barb committed
282
		}
283 284 285 286 287 288 289
		else {
		    print STDERR "Doing a recovery swap-in of old state.\n";

		    if (doSwapin(UPDATE_RECOVER)) {
			print STDERR "Could not swap in old physical state; ";
			$CanRecover = 0;
		    }
Chad Barb's avatar
Chad Barb committed
290 291
		}
	    }
292 293 294 295 296 297 298

	    #
	    # Some part of the recovery failed; must swap it out. swapexp
	    # (caller) will then have to do more clean up, hence the special
	    # exit status indicated by $updatehosed.
	    # 
	    if (! $CanRecover) {
299
		print STDERR "Recovery aborted! Swapping experiment out.\n";
Chad Barb's avatar
Chad Barb committed
300
		doSwapout(CLEANUP);
301 302 303 304
		$updatehosed = 1;
	    }
	    else {
		print STDERR "Update recovery successful.\n";
Chad Barb's avatar
Chad Barb committed
305
	    }
Chad Barb's avatar
 
Chad Barb committed
306
	}
Chad Barb's avatar
Chad Barb committed
307 308
    }
}
309 310 311 312 313
elsif ($swapop eq "in") {
    #
    # Swap in
    #
    my $retries = 2;
314 315 316 317 318 319 320 321 322 323 324 325 326

    #
    # Before real swapin, do cursory admission control. assign_wrapper does
    # a more stringent check using assign.
    #
    print STDERR "Checking with Admission Control ...\n";
    if (! TBAdmissionControlCheck($UID, $pid, $eid, undef)) {
	print STDERR "Admission control failure!\n";
	print "Failingly finished swap-$swapop for $pid/$eid. " .
	    TBTimeStamp() . "\n";
	TBDebugTimeStamp("tbswap $swapop finished (failed)");
	exit(1);
    }
327 328
    
    $errors = doSwapin(REAL);
Chad Barb's avatar
Chad Barb committed
329

330 331 332 333 334 335 336 337 338 339
    #
    # Attempt a retry if: 
    #   a) there were errors, 
    #   b) doswapin() indicated (via return code 3) a retry is appropriate,
    #   c) we haven't tried too many times already.
    #   d) The cancelflag has not been set.
    #   e) $TESTMODE == 0.
    #
    while ($errors == 3 && $retries && !$canceled && !$TESTMODE) {
	$retries--;
Chad Barb's avatar
 
Chad Barb committed
340

341 342
	print STDERR "Cleaning up after errors; will try again.\n";
	doSwapout(RETRY);
Chad Barb's avatar
 
Chad Barb committed
343

344 345
	print STDERR "Trying again...\n";
	$errors = doSwapin(RETRY);
Chad Barb's avatar
 
Chad Barb committed
346
    }
347 348
    if ($errors || $canceled) {
	print STDERR "Cleaning up after " .
349
	    ($canceled ? "cancelation" : "errors") . ".\n";
350
	doSwapout(CLEANUP);
Chad Barb's avatar
 
Chad Barb committed
351 352 353 354 355 356 357
    }
}

#
# Write appropriate message and exit.
#
if ($errors) {
358
    print "Failingly finished swap-$swapop for $pid/$eid. ".TBTimeStamp()."\n";
Chad Barb's avatar
 
Chad Barb committed
359
    TBDebugTimeStamp("tbswap $swapop finished (failed)");
Chad Barb's avatar
Chad Barb committed
360

361 362
    # Pass out magic value to indicate that update failed!
    exit(1 | ($updatehosed ? 0x40 : 0));
Chad Barb's avatar
 
Chad Barb committed
363
}
364
print "Successfully finished swap-$swapop for $pid/$eid. " .TBTimeStamp()."\n";
365 366
TBDebugTimeStamp("tbswap $swapop finished (succeeded)");
exit(0);
Chad Barb's avatar
 
Chad Barb committed
367 368 369 370

#################################

##
Chad Barb's avatar
Chad Barb committed
371
#
Chad Barb's avatar
 
Chad Barb committed
372 373
# doSwapout - Swaps experiment out.
#
Chad Barb's avatar
Chad Barb committed
374
#             If in REAL or CLEANUP,
Chad Barb's avatar
 
Chad Barb committed
375 376 377
#             this function will free all nodes for the 
#             experiment.
#
Chad Barb's avatar
Chad Barb committed
378
#             If in RETRY or UDPATE,
Chad Barb's avatar
 
Chad Barb committed
379 380 381 382 383 384
#             only nodes not in RES_READY will be freed.
#
#             Returns 0 on success, >0 on failure.
#
##

Chad Barb's avatar
 
Chad Barb committed
385 386
sub doSwapout($) {
    my $type = shift; # REAL==4, CLEANUP==3, RETRY==2, UPDATE==1.
Chad Barb's avatar
 
Chad Barb committed
387 388 389 390 391 392 393 394 395 396 397
    my $swapout_errors = 0;

    #
    # wait for os_setup;
    # this only applies if called after a failed doswapin.
    #
    if ($os_setup_pid) {
	print "Waiting for os_setup to finish\n";
	waitpid($os_setup_pid, 0);
	undef $os_setup_pid;
    }
Chad Barb's avatar
Chad Barb committed
398

399 400
    print "Getting files accessed via NFS.\n";
    TBDebugTimeStamp("nfstrace started");
401 402 403 404 405 406 407 408 409 410 411 412
    if (lc $THISHOMEBASE eq "emulab.net") {
	my ($dbuid, $uname, $umail);
	if (UNIX2DBUID($UID, \$dbuid) &&
	    UserDBInfo($dbuid, \$uname, \$umail)) {
	    SENDMAIL("$TBLOGS",
		     "Files accessed by $pid/$eid via NFS",
		     `nfstrace get $pid $eid`,
		     "$uname <$umail>");
	}
    }
    else {
	system("nfstrace get $pid $eid");
413 414 415
    }
    TBDebugTimeStamp("nfstrace finished");

Chad Barb's avatar
 
Chad Barb committed
416
    if (! $TESTMODE) { 
417 418 419
	if (! ($DISABLE_EVENTS || $elabinelab)) {
	    if ($type >= RETRY ||
		($update_Eventsys_restart && $type == UPDATE) ) {
Chad Barb's avatar
 
Chad Barb committed
420 421
		print "Stopping the event system\n";
		if (system("eventsys_control stop $pid $eid")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
422
		    tberror "Failed to stop the event system.";
Chad Barb's avatar
 
Chad Barb committed
423 424
		    $swapout_errors = 1;
		}
425 426 427 428 429 430 431

		#
		# Stop the location piper.
		#
		if (-x $piper) {
		    print "Stopping the location piper\n";
		    if (system("$piper -k $pid $eid")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
432
			tberror "Failed to stop location piper.";
433 434 435
			$swapout_errors = 1;
		    }
		}
Chad Barb's avatar
 
Chad Barb committed
436 437
	    }
	}
438 439 440 441 442 443
	
	#
	# Do teardown of inner elab. We must do this before we teardown the
	# vlans since the inner control network is a vlan, and we want that
	# active so inner boss can reboot the inner nodes (avoid power cycle).
	#
444
	if ($elabinelab && $type >= CLEANUP) {
445 446
	    print "Tearing down elabinelab. This could take a while.\n";
	    if (system("elabinelab -k $pid $eid")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
447
		tberror "Failed to teardown elabinelab!";
448 449 450 451
		$swapout_errors = 1;
	    }
	}

Chad Barb's avatar
 
Chad Barb committed
452
	#
Chad Barb's avatar
Chad Barb committed
453
	# Clean up any VLANs in experiment.
Chad Barb's avatar
 
Chad Barb committed
454
	#
455 456 457 458 459 460 461
	# When modifying an elabinelab experiment, leave the vlans intact
	# so that the inner networks are not suddenly disconnected!
	#
	if (! ($elabinelab && $type == UPDATE)) {
	    TBDebugTimeStamp("snmpit started");
	    print STDERR "Removing VLANs.\n";
	    if (system("snmpit -r $pid $eid")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
462
		tberror "Failed to reset VLANs";
463 464 465 466 467
		$swapout_errors = 1;
	    } else {
		$cleanvlans = 0;
	    }
	    TBDebugTimeStamp("snmpit finished");
Chad Barb's avatar
 
Chad Barb committed
468
	}
469
    }
Chad Barb's avatar
Chad Barb committed
470

471 472 473
    if ($type >= CLEANUP) {
	#
	# We're not attempting a retry;
Chad Barb's avatar
 
Chad Barb committed
474
	#
475
	# Stop all of the vnodes.
Chad Barb's avatar
 
Chad Barb committed
476
	#
477
	if (! $TESTMODE) { 	
Chad Barb's avatar
 
Chad Barb committed
478 479 480
	    print "Tearing down virtual nodes.\n";
	    TBDebugTimeStamp("vnode_setup -k started");
	    if (system("vnode_setup -d -k $pid $eid")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
481
		tberror "Failed to tear down vnodes.";
Chad Barb's avatar
 
Chad Barb committed
482 483 484
		$swapout_errors = 1;
	    }
	    TBDebugTimeStamp("vnode_setup finished");
Chad Barb's avatar
 
Chad Barb committed
485 486
	}

Mike Hibler's avatar
Mike Hibler committed
487 488 489 490 491 492 493
	#
	# Nodes behind a firewall are treated special.
	# See undoFWNodes for details.
	#
	if ($firewalled && undoFWNodes($pid, $eid)) {
	    return 1;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
494

Mike Hibler's avatar
Mike Hibler committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	#
	# Perform swapout time admin actions.  Right now there is at most
	# one of these.  It isn't really a general mechanism, just a hook
	# for state saving or data collection during swapout.
	# A couple of important "fer now" notes:
	#
	#	We don't do this for firewalled experiments.  We need a way
	#	to "tag" the saved disk state to ensure it doesn't get
	#	instantiated outside of a firewall.
	#
	#	We only do this on REAL swapouts, and not on CLEANUPs.
	#	There are some types of CLEANUPs where we may want to
	#	do this, in particular an invocation caused by a failed
	#	modify operation, where the admin action is to save the
	#	experiment state.  So we will need to revisit this.
	#
	my %soaction = ();
	if ($type == REAL && !$firewalled) {
	    TBExptGetSwapoutAction($pid, $eid, \%soaction);
	}
	if ($soaction{'command'}) {
	    my @nodes = ExpNodes($pid, $eid, 1);

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
	    if (@nodes > 0) {
		print STDERR "Performing swapout admin MFS actions.\n";
		TBDebugTimeStamp("Performing swapout actions");
		my @failed = ();
		my %myargs = ();
		$myargs{'name'} = "tbswap";
		$myargs{'command'} = $soaction{'command'};
		if (defined($soaction{'timeout'})) {
		    $myargs{'timeout'} = $soaction{'timeout'};
		}
		$myargs{'timestamp'} = 1;
		if (TBAdminMfsRunCmd(\%myargs, \@failed, @nodes)) {
		    if ($soaction{'isfatal'}) {
			tberror
			    "Failed to run '" . $soaction{'command'} .
				"' on @failed!";
			return 1;
		    }
		    tbwarn
Mike Hibler's avatar
Mike Hibler committed
537 538
			"Failed to run '" . $soaction{'command'} .
			    "' on @failed!";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
539 540
		}
	    }
541 542
	}

Chad Barb's avatar
 
Chad Barb committed
543 544
	#
	# remove all nodes from the experiment.
Chad Barb's avatar
 
Chad Barb committed
545
	# (nfree will send them to RES_FREE_DIRTY)
Chad Barb's avatar
 
Chad Barb committed
546 547 548 549
	#
	print STDERR "Freeing nodes.\n";
	TBDebugTimeStamp("nfree started");
	if (system("nfree $pid $eid")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
550
	    tberror "Could not free nodes.";
Chad Barb's avatar
 
Chad Barb committed
551 552 553
	    $swapout_errors = 1;
	}
	TBDebugTimeStamp("nfree finished");
Chad Barb's avatar
 
Chad Barb committed
554 555 556 557 558

	#
	# Since this is an actual swapout, 
	# reset our count of swap out nag emails sent.
	#
559
	DBQueryWarn("update experiments set swap_requests='',sim_reswap_count='0' ".
560
		    "where eid='$eid' and pid='$pid'");
Chad Barb's avatar
 
Chad Barb committed
561 562
    } else {
	#
Chad Barb's avatar
 
Chad Barb committed
563
	# $type == RETRY or $type == UPDATE.
Chad Barb's avatar
 
Chad Barb committed
564 565
	# Therefore, don't deallocate nodes which have been successfully
	# incorporated into the experiment (i.e., are RES_READY).
Chad Barb's avatar
 
Chad Barb committed
566
	# (nfree will send deallocated nodes to RES_FREE_DIRTY)
Chad Barb's avatar
 
Chad Barb committed
567
	#
568 569 570 571 572 573 574 575
	my @failedpnodes = ();
	my @failedvnodes = ();
	
	my $db_result =
	    DBQueryFatal("select rv.node_id,n.allocstate,nt.isvirtnode ".
                         "  from reserved as rv ".
			 "left join nodes as n on n.node_id = rv.node_id ".
			 "left join node_types as nt on nt.type=n.type ".
Chad Barb's avatar
 
Chad Barb committed
576 577
			 "where rv.pid='$pid' and rv.eid='$eid'");

578
	while (my ($node,$allocstate,$isvirt) = $db_result->fetchrow_array) {
Chad Barb's avatar
 
Chad Barb committed
579
	    if ($allocstate ne TBDB_ALLOCSTATE_RES_READY()) {
580 581 582 583 584 585
		if ($isvirt) {
		    push(@failedvnodes, $node);
		}
		else {
		    push(@failedpnodes, $node);
		}
Chad Barb's avatar
 
Chad Barb committed
586 587 588
	    }
	}

589 590 591 592 593 594 595
	#
	# Tear down failed vnodes. Perhaps not needed?
	# 
	if (!$TESTMODE && @failedvnodes > 0) {
	    print "Tearing down failed virtual nodes.\n";
	    TBDebugTimeStamp("vnode_setup -k started");
	    if (system("vnode_setup -d -k $pid $eid @failedvnodes")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
596
		tberror "Failed to tear down vnodes.";
597 598 599 600
		$swapout_errors = 1;
	    }
	    TBDebugTimeStamp("vnode_setup -k finished");
	}
Chad Barb's avatar
 
Chad Barb committed
601

602 603 604 605
	#
	# Release all failed nodes.
	# 
	if (@failedpnodes > 0 || @failedvnodes > 0) {
606 607
	    print STDERR "Freeing failed nodes.\n";
	    
Chad Barb's avatar
 
Chad Barb committed
608
	    TBDebugTimeStamp("nfree started");
Chad Barb's avatar
 
Chad Barb committed
609 610 611 612 613
	    #
	    # Specify -x switch so when a physical node gets freed,
	    # any virtual nodes (owned by this experiment)
	    # sitting on top of it are freed as well.
	    #
614 615
	    if (system("nfree -x $pid $eid " .
		       join(" ", (@failedpnodes, @failedvnodes)))) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
616
		tberror "Could not free nodes.";
Chad Barb's avatar
 
Chad Barb committed
617 618 619 620 621 622
		$swapout_errors = 1;
	    }
	    TBDebugTimeStamp("nfree finished");
	}
    }

623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
    if (! $TESTMODE) {
	#
	# If the experiment has no Plab dslice nodes left, but still has
	# a Plab slice, destroy the slice
	#

	# Does the slice exist?
	$db_result =
	    DBQueryFatal("select slicename from plab_slices ".
			 "where pid='$pid' and eid='$eid'");

	if ($db_result->numrows) {
	    # Are there any dslice nodes left?
	    $db_result =
		DBQueryFatal("select n.node_id from nodes as n ".
			     "left join node_types as nt on n.type = nt.type ".
			     "left join reserved as r ".
			     " on r.node_id = n.node_id ".
			     "where r.pid='$pid' and r.eid='$eid' ".
			     " and nt.isplabdslice = 1");

	    if (!$db_result->numrows) {
		print "Destroying Planetlab slice.\n";
		TBDebugTimeStamp("plabslice destroy started");
647
		if (system("plabslice destroy $pid $eid")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
648
		    tberror "Failed to destroy Plab dslice.";
649 650 651 652 653 654 655
		    $swapout_errors = 1;
		}
		TBDebugTimeStamp("plabslice destroy finished");
	    }
	}
    }

Chad Barb's avatar
 
Chad Barb committed
656 657 658 659 660 661 662 663
    if (! $TESTMODE) {
	#
	# All of these errors are non-fatal on swapout. We find out about them
	# via email sent from the individual scripts.
	#

	#
	# Only reset mountpoints if this is an actual swapout, and
Chad Barb's avatar
 
Chad Barb committed
664
	# not a failed swapin(cleanup), update, or retry.
Chad Barb's avatar
 
Chad Barb committed
665
	#
Chad Barb's avatar
 
Chad Barb committed
666
	if ($type == REAL) {
Chad Barb's avatar
 
Chad Barb committed
667 668 669
	    print "Resetting mountpoints.\n";
	    TBDebugTimeStamp("exports started");
	    if (system("exports_setup")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
670
		tberror "Failed to reset mountpoints.";
Chad Barb's avatar
 
Chad Barb committed
671 672 673
	    }
	    TBDebugTimeStamp("exports finished");
	}
Chad Barb's avatar
Chad Barb committed
674

Chad Barb's avatar
 
Chad Barb committed
675 676 677 678
	#
	# Resetting named maps and email lists is fast and idempotent,
	# so whatever.
	#
Chad Barb's avatar
 
Chad Barb committed
679 680 681
	print "Resetting named maps.\n";
	TBDebugTimeStamp("named started");
	if (system("named_setup")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
682
	    tbwarn "Failed to reset named map.";
Chad Barb's avatar
 
Chad Barb committed
683 684
	}
	TBDebugTimeStamp("named finished");
Chad Barb's avatar
Chad Barb committed
685

Chad Barb's avatar
 
Chad Barb committed
686 687
	print "Resetting email lists.\n";
	TBDebugTimeStamp("genelists started");
688
	if (system("genelists -t")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
689
	    tbwarn "Failed to reset email lists.";
Chad Barb's avatar
 
Chad Barb committed
690 691 692 693
	}
	TBDebugTimeStamp("genelists finished");
    }

Chad Barb's avatar
 
Chad Barb committed
694
    #
695 696 697
    # Wipe the DB clean except during UPDATE or RETRY. In those
    #    cases, assign_wrapper will reset the DB after reading
    #    the info.
Chad Barb's avatar
 
Chad Barb committed
698
    #
699 700 701 702
    if ( $type >= CLEANUP ) {
	print STDERR "Resetting DB.\n";
	TBExptRemovePhysicalState( $pid, $eid );
    }
Chad Barb's avatar
 
Chad Barb committed
703 704 705 706 707

    return $swapout_errors;
}

##
Chad Barb's avatar
Chad Barb committed
708
#
Chad Barb's avatar
 
Chad Barb committed
709 710
# doSwapin - Swaps experiment in.
#
Chad Barb's avatar
Chad Barb committed
711 712 713 714 715
#            Returns:
#              0 - successful swapin
#              1 - failed swapin; cleanup required.
#              3 - failed swapin; cleanup required; can retry.
#              7 - failed swapin; assign failed; no cleanup.
Chad Barb's avatar
 
Chad Barb committed
716 717
##

Chad Barb's avatar
 
Chad Barb committed
718
sub doSwapin($) {
719 720 721
    my $type = shift; # REAL==4, RETRY==2, UPDATE==1, UPDATE_RECOVER=0.
    # Just the physnodes ...
    my @deleted_pnodes = ();
Chad Barb's avatar
 
Chad Barb committed
722

Chad Barb's avatar
 
Chad Barb committed
723 724 725 726
    #
    # assign_wrapper does all the virtual to physical mapping 
    # and updating the DB state.
    #
727
    
Chad Barb's avatar
Chad Barb committed
728
    if ($type > UPDATE_RECOVER) {
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
        #
        # Hacky test to allow disabling of linkdelays if the node is going
        # to run Linux. See sitevar above.
        #
	if (! $enablelinkdelays) {
	    $db_result =
		DBQueryFatal("select distinct e.pid,e.eid,vl.vnode,vn.osname ".
			 "  from experiments as e ".
			 "left join virt_lans as vl on vl.pid=e.pid and ".
			 "     vl.eid=e.eid ".
			 "left join virt_nodes as vn on vn.pid=e.pid and ".
			 "     vn.eid=e.eid and vn.vname=vl.vnode ".
			 "left join os_info as o on o.osname=vn.osname and ".
			 "  (o.pid=vl.pid or o.pid='" . TBOPSPID() . "') ".
			 "where (vl.uselinkdelay!=0 or e.uselinkdelays!=0 or ".
			 "       e.forcelinkdelays!=0) and ".
			 "     (o.os is NULL or o.os='Linux') and ".
			 "     e.pid='$pid' and e.eid='$eid'");

	    if ($db_result->numrows) {
		print "*** Endnodeshaping is disabled on Linux Images!\n";
		print "*** You must modify your experiment to swap it in.\n";
		return 1;
	    }
	}
	
Chad Barb's avatar
Chad Barb committed
755 756 757 758
	print "Mapping to physical reality ...\n";
	TBDebugTimeStamp("assign_wrapper started");

	#
759 760 761 762
	# Pass the -u (update) switch into assign_wrapper, which turns on
	# update mode. When doing a retry, must also fix the current nodes
	# to avoid stuff jumping around when simply trying to replace a node
	# that did not boot.
Chad Barb's avatar
Chad Barb committed
763 764
	#
	my $exitcode;
765 766 767
	my $wrapper = "assign_wrapper -u";
	$wrapper .= " -f"
	    if ($type == RETRY);
768
	
769
	if (system("$wrapper $pid $eid")) {
770 771
	    $exitcode = $? >> 8;

Kevin Atkinson's avatar
 
Kevin Atkinson committed
772
	    tberror "Failed ($exitcode) to map to reality.";
773

774 775
	    # Wrapper sets this bit when recovery is possible.
	    if ($exitcode & 64) {
776
		# We can recover. 
Chad Barb's avatar
Chad Barb committed
777
		return 7;
778 779
	    }
	    else {
780
		# No recovery, no retry.
Chad Barb's avatar
Chad Barb committed
781 782 783 784
		return 1;
	    }
	}
	TBDebugTimeStamp("assign_wrapper finished");
Chad Barb's avatar
 
Chad Barb committed
785

Chad Barb's avatar
Chad Barb committed
786 787
	print "Mapped to physical reality!\n";
    }
Chad Barb's avatar
 
Chad Barb committed
788

789
    # Check cancel flag before continuing. No retry, 
790
    TBGetCancelFlag($pid, $eid, \$canceled);
791 792 793
    return 1
	if ($canceled);

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 824 825 826 827 828
    #
    # Look for any nodes in RES_TEARDOWN. These need to be released,
    # and if a virtnode, they need to be torn down. We cannot wait for
    # the virtnodes to go down with the physnode they are hosted on,
    # so teardown and release the virtnodes first, and then do the
    # physnodes.
    #
    # Errors are fatal; no recovery or retry.
    #
    if ($type == UPDATE) {
	my $allocstate = TBDB_ALLOCSTATE_RES_TEARDOWN();
	
	$db_result =
	    DBQueryFatal("select r.node_id,nt.isvirtnode,nt.isremotenode ".
			 "  from reserved as r ".
			 "left join nodes as n on n.node_id=r.node_id ".
			 "left join node_types as nt on nt.type=n.type ".
			 "where r.pid='$pid' and r.eid='$eid' and ".
			 "      n.allocstate='$allocstate'");

	if ($db_result->numrows) {
	    my @virtnodes = ();
	    my @physnodes = ();
	    
	    print "Tearing down and releasing unused nodes\n";

	    # First teardown/release virtnodes. 
	    while (my ($node,$isvirt,$isrem) = $db_result->fetchrow_array()) {
		if ($isvirt) {
		    push(@virtnodes, $node);
		}
		elsif (!$isrem) {
		    push(@physnodes, $node);
		}
	    }
829 830 831
	    # See below.
	    @deleted_pnodes = @physnodes;
	    
832 833 834
	    if (@virtnodes) {
		TBDebugTimeStamp("vnode_setup started");
		
835
		if (system("vnode_setup -k $pid $eid @virtnodes")) {
836 837 838 839 840 841 842 843 844 845 846
		    print "Failed to tear down unused virtnodes!\n";
		    return 1;
		}
		TBDebugTimeStamp("vnode_setup finished");
		
		if (system("nfree $pid $eid @virtnodes")) {
		    print "Failed to nfree unused virtnodes!\n";
		    return 1;
		}
	    }
	    if (@physnodes) {
847 848 849
		if ($elabinelab) {
		    print "Removing nodes from inner elab.\n";
		    if (system("elabinelab -r $pid $eid @physnodes")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
850
			tberror "Failed to remove inner nodes!";
851 852 853 854 855
			return 1;
		    }
		}

		#
Mike Hibler's avatar
Mike Hibler committed
856 857
		# If the experiment is firewalled, cleanup the nodes
		# we are releasing.
858
		# 
Mike Hibler's avatar
Mike Hibler committed
859
		if ($firewalled && undoFWNodes($pid, $eid, @deleted_pnodes)) {
860 861 862
		    return 1;
		}
		
863 864 865 866 867 868 869 870
		if (system("nfree $pid $eid @physnodes")) {
		    print "Failed to nfree unused physnodes!\n";
		    return 1;
		}
	    }
	}
    }

Chad Barb's avatar
 
Chad Barb committed
871 872 873 874 875 876
    # Exit here if we are testing.
    if ($TESTMODE) {
	print "Testing run - Stopping here.\n";
	return 0;
    }

877 878 879 880 881 882 883 884 885 886 887
    #
    # Handle tarballs - we might need to fetch some from URLs if the user
    # asked for that.
    #
    print "Fetching tarballs and RPMs (if any) ...\n";
    TBDebugTimeStamp("tarfiles_setup started");

    if (system("tarfiles_setup $pid $eid")) {
	#
	# No recovery for now - what would we do?
	#
Kevin Atkinson's avatar
 
Kevin Atkinson committed
888
	tberror "Failed to set up tarballs.";
889 890 891 892
	return 1;
    }
    TBDebugTimeStamp("tarfiles_setup finished");

893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
    #
    # If there are any Plab dslice nodes in the experiment, create the
    # dslice now
    #
    if ($type > UPDATE_RECOVER) {
	# Are there any Plab nodes?
	$db_result =
	    DBQueryFatal("select n.node_id from nodes as n ".
			 "left join node_types as nt on n.type = nt.type ".
			 "left join reserved as r on r.node_id = n.node_id ".
			 "where r.pid='$pid' and r.eid='$eid' ".
			 " and nt.isplabdslice = 1");

	if ($db_result->numrows) {
	    # Does slice already exist?
	    $db_result =
		DBQueryFatal("select slicename from plab_slices ".
			     "where pid='$pid' and eid='$eid'");

	    if (! $db_result->numrows) {
		my @plabnodes = ();
		
		while (my ($node) = $db_result->fetchrow_array()) {
		    push(@plabnodes, $node);
		}
		
		print "Creating Planetlab slice.\n";
		TBDebugTimeStamp("plabslice create started");
921
		if (system("plabslice create $pid $eid")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
922
		    tberror "Failed to create Plab dslice";
923 924 925 926 927 928 929
		    return 3;
		}
		TBDebugTimeStamp("plabslice alloc finished");
	    }
	}
    }

930
    # Check cancel flag before continuing. No retry, 
931
    TBGetCancelFlag($pid, $eid, \$canceled);
932 933 934
    return 1
	if ($canceled);

Chad Barb's avatar
 
Chad Barb committed
935 936 937 938 939 940 941 942 943
    #
    # These things need to get started before the nodes come up, so we'll
    # do them before the os_setup. Everything else can done in parallel with
    # os_setup. (Actually, these probably can too, since they should finish
    # long before the nodes reboot, but better safe than sorry)
    #
    print "Setting up mountpoints.\n";
    TBDebugTimeStamp("mountpoints started");
    if (system("exports_setup")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
944
	tberror "Failed to setup mountpoints.";
Chad Barb's avatar
 
Chad Barb committed
945 946 947
	return 1;
    }
    TBDebugTimeStamp("mountpoints finished");
Chad Barb's avatar
Chad Barb committed
948

Chad Barb's avatar
 
Chad Barb committed
949 950 951
    TBDebugTimeStamp("named started");
    print "Setting up named maps.\n";
    if (system("named_setup")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
952
	tbwarn "Failed to add node names to named map.";
Chad Barb's avatar
 
Chad Barb committed
953 954
	#
	# This is a non-fatal error.
Chad Barb's avatar
Chad Barb committed
955
	#
Chad Barb's avatar
 
Chad Barb committed
956 957
    }
    TBDebugTimeStamp("named finished");
Chad Barb's avatar
Chad Barb committed
958

959 960 961 962 963 964 965 966
    print "Cleaning NFS traces.\n";
    TBDebugTimeStamp("nfstrace gc started");
    if (system("nfstrace gc $pid $eid")) {
	tberror "Failed to setup nfstrace.";
	return 1;
    }
    TBDebugTimeStamp("nfstrace gc finished");

967
    # Check cancel flag before continuing. No retry, 
968
    TBGetCancelFlag($pid, $eid, \$canceled);
969 970
    return 1
	if ($canceled);
Chad Barb's avatar
Chad Barb committed
971

972 973 974 975
    #
    # Setup any control-net firewall.
    # This must be done before reloading and rebooting nodes.
    #
976 977
    if ($firewalled && ($type == REAL || $type == UPDATE) &&
	doFW($pid, $eid, (($type == UPDATE) ? FWADDNODES : FWSETUP), undef)) {
978 979 980
	return 1;
    }

Chad Barb's avatar
Chad Barb committed
981 982
    #
    # If user specified -reboot to update,
Chad Barb's avatar
 
Chad Barb committed
983
    # and we are successfully performing the update,
984
    # then mark all nodes in experiment so os_setup will reboot them.
Chad Barb's avatar
Chad Barb committed
985
    #
986 987 988
    if (($type == UPDATE) &&
	($updateReboot || $updateReconfig)) {
	print STDERR "Marking nodes for reboot/reconfig.\n";
Chad Barb's avatar
Chad Barb committed
989
	$db_result =
990 991 992
	    DBQueryFatal("select r.node_id,n.allocstate from reserved as r ".
			 "left join nodes as n on n.node_id=r.node_id ".
			 "where r.pid='$pid' and r.eid='$eid'");
Chad Barb's avatar
Chad Barb committed
993

994 995 996 997 998 999 1000 1001
	while (my ($node,$allocstate) = $db_result->fetchrow_array) {
	    #
	    # If the node is INIT_CLEAN, leave it alone. It will still get
	    # rebooted, but will not falsely be tagged as dirty. This is
	    # important for vnodes too, where INIT_CLEAN indicated the vnode
	    # does not even exist yet (plab nodes).
	    #
	    if ($allocstate ne TBDB_ALLOCSTATE_RES_INIT_CLEAN()) {
1002 1003 1004 1005
		TBSetNodeAllocState($node,
				    ($updateReboot ?
				     TBDB_ALLOCSTATE_RES_INIT_DIRTY() :
				     TBDB_ALLOCSTATE_RES_RECONFIG()));
1006
	    }
Chad Barb's avatar
Chad Barb committed
1007 1008 1009
	}
    }

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
    #
    # Lets run gentopofile again, so we get ltmap right. This will come out
    # later, most likely.
    #
    TBDebugTimeStamp("gentopofile started");
    print "Generating ltmap (again) ...\n";

    if (system("gentopofile $pid $eid")) {
        tberror("gentopofile failed!");
	return 1;
    }
    TBDebugTimeStamp("gentopofile finished");

Chad Barb's avatar
 
Chad Barb committed
1023 1024 1025 1026 1027
    #
    # Since it'll take a while for the nodes to reboot, we'll start now, and
    # wait for the os_setup to finish, down below
    #
    print "Resetting OS and rebooting.\n";
1028
    TBDebugTimeStamp("launching os_setup");
Chad Barb's avatar
 
Chad Barb committed
1029 1030 1031
    if (!($os_setup_pid = fork())) { 
	exec("os_setup $pid $eid") or return 1;
    } elsif ($os_setup_pid == -1) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
1032
	tberror "Fork failed.";
Chad Barb's avatar
 
Chad Barb committed
1033 1034
	return 1;
    }
Chad Barb's avatar
Chad Barb committed
1035

Chad Barb's avatar
 
Chad Barb committed
1036
    #
1037 1038 1039 1040 1041
    # XXX
    # Don't add any steps between here and the waitpid() call below
    # without verifying that 1) It's OK for nodes to come up before
    # the step has completed and 2) It's OK for the command to run in
    # parallel with os_setup (no DB dependencies, etc.)
Chad Barb's avatar
 
Chad Barb committed
1042 1043 1044 1045 1046
    #

    print "Setting up VLANs.\n";
    TBDebugTimeStamp("snmpit started");
    if (system("snmpit -t $pid $eid")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
1047
	tberror "Failed to set up VLANs.";
Chad Barb's avatar
 
Chad Barb committed
1048 1049 1050
	return 1;
    }
    TBDebugTimeStamp("snmpit finished");
Chad Barb's avatar
Chad Barb committed
1051

Chad Barb's avatar
 
Chad Barb committed
1052 1053 1054 1055
    #
    # An error now means that the VLANS need to be cleaned up.
    #
    $cleanvlans = 1;
Chad Barb's avatar
Chad Barb committed
1056

Chad Barb's avatar
 
Chad Barb committed
1057 1058
    print "Setting up email lists.\n";
    TBDebugTimeStamp("genelists started");
1059
    if (system("genelists -t")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
1060
	tbwarn "Failed to update email lists.";
Chad Barb's avatar
 
Chad Barb committed
1061 1062 1063 1064 1065
	#
	# This is a non-fatal error.
	# 
    }
    TBDebugTimeStamp("genelists finished");
Chad Barb's avatar
Chad Barb committed
1066

Chad Barb's avatar
 
Chad Barb committed
1067 1068 1069 1070 1071 1072 1073 1074
    #
    # Don't clear port counters on UPDATE.
    # (XXX should clear new nodes' port counters.)

    if ($type >= RETRY) {
	print "Clearing port counters.\n";
	TBDebugTimeStamp("portstats started");
	if (system("portstats -z -a -q $pid $eid")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
1075
	    tbwarn "Failed to clear port counters.";
Chad Barb's avatar
 
Chad Barb committed
1076 1077 1078 1079 1080
	    #
	    # This is a non-fatal error.
	    # 
	}
	TBDebugTimeStamp("portstats finished");
Chad Barb's avatar
 
Chad Barb committed
1081
    }
Chad Barb's avatar
Chad Barb committed
1082

Chad Barb's avatar
 
Chad Barb committed
1083 1084 1085 1086 1087 1088 1089
    #
    # OK, let's see how that os_setup did
    #
    $kid = waitpid($os_setup_pid,0);
    if ($kid == $os_setup_pid) {
	undef $os_setup_pid; # Make sure doswapout() doesn't wait for it.
	if ($CHILD_ERROR) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
1090
	    tberror "Failed to reset OS and reboot nodes.";
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
	    #
	    # If there is a firewall involved, it could be that the
	    # firewall rules are preventing essential communication,
	    # so don't retry.
	    #
	    # XXX should only do this if the user has specified additional
	    # rules.  But right now, I may screw up too!
	    #
	    if ($firewalled) {
		print STDERR "Not retrying, ".
		    "firewall may be preventing setup.\n";
		return 1;
	    }

Chad Barb's avatar
 
Chad Barb committed
1105
	    #
Chad Barb's avatar
 
Chad Barb committed
1106 1107
	    # Use returncode from os_setup process to
	    # set global $retry flag, indicating to caller
Chad Barb's avatar
 
Chad Barb committed
1108 1109 1110
	    # that it may be beneficial to attempt
	    # a doSwapin() again.
	    #
Chad Barb's avatar
 
Chad Barb committed
1111
            if (($CHILD_ERROR >> 8) == 1) {
Chad Barb's avatar
Chad Barb committed
1112
		return 3;
Chad Barb's avatar
 
Chad Barb committed
1113 1114
	    } else {
		print STDERR "Not retrying due to error type.\n";
Chad Barb's avatar
Chad Barb committed
1115
		return 1;
Chad Barb's avatar
 
Chad Barb committed
1116
	    }
Chad Barb's avatar
 
Chad Barb committed
1117 1118 1119
	}
    } else {
	undef $os_setup_pid;
Kevin Atkinson's avatar
 
Kevin Atkinson committed
1120
	tberror "Error waiting for os_setup to finish.";
Chad Barb's avatar
 
Chad Barb committed
1121 1122 1123
	return 1;
    }
    TBDebugTimeStamp("os_setup finished");
Chad Barb's avatar
Chad Barb committed
1124

Chad Barb's avatar
 
Chad Barb committed
1125 1126 1127 1128
    #
    # Okay, start the event system now that we know all the nodes have
    # rebooted (os_setup is done). This only takes a moment (puts itself
    # in the background), so its not enough of a delay to worry about.
Chad Barb's avatar
 
Chad Barb committed
1129 1130
    # Don't do this during an update, since we didn't kill the 
    # event system previously, so starting it again will fail!
Chad Barb's avatar
 
Chad Barb committed
1131
    # 
1132
    if (! ($DISABLE_EVENTS || $elabinelab)) {
1133 1134 1135 1136 1137 1138 1139
	#
	# For the robot testbed, start the location piper *before* the event
	# system.
	#
	if (-x $piper && ($type != UPDATE && $type != UPDATE_RECOVER)) {
	    print "Starting the location piper.\n";
	    if (system("$piper $pid $eid")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
1140
		tberror "Failed to start the location piper.";
1141 1142 1143 1144
		return 1;
	    }
	}
	
1145 1146
	if ( $update_Eventsys_restart || 
	    ($type != UPDATE && $type != UPDATE_RECOVER) ) {
Chad Barb's avatar
 
Chad Barb committed
1147 1148 1149
	    print "Starting the event system.\n";
	    TBDebugTimeStamp("eventsys_control started");
	    if (system("eventsys_control start $pid $eid")) {
Kevin Atkinson's avatar
 
Kevin Atkinson committed
1150
		tberror "Failed to start the event system.";
Chad Barb's avatar
 
Chad Barb committed
1151 1152 1153
		return 1;
	    }
	    TBDebugTimeStamp("eventsys_control finished");
Chad Barb's avatar
 
Chad Barb committed
1154 1155
	}
    }
Chad Barb's avatar
Chad Barb committed
1156

Leigh B. Stoller's avatar