tbswap.in 21.7 KB
Newer Older
Chad Barb's avatar
 
Chad Barb committed
1 2 3 4 5 6 7 8 9
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
# 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    exit(-1);
}

#
# Configure variables
#
my $TBROOT         = "@prefix@";
my $TESTMODE       = @TESTMODE@;
my $DISABLE_EVENTS = "@DISABLE_EVENT_SCHED@";

# 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;
require exitonwarn; # exitonwarn isn't really a module, so just require it

#
# Actual swap-in and swap-out functions, defined below.
#
Chad Barb's avatar
 
Chad Barb committed
53 54 55 56 57 58 59
sub doSwapout($);
sub doSwapin($);

sub REAL()    { return 4; }
sub CLEANUP() { return 3; }
sub RETRY()   { return 2; }
sub UPDATE()  { return 1; }
Chad Barb's avatar
Chad Barb committed
60
sub UPDATE_RECOVER()  { return 0; }
Chad Barb's avatar
 
Chad Barb committed
61 62 63 64 65 66 67

#
# Turn off line buffering on output
#

$| = 1;

Chad Barb's avatar
Chad Barb committed
68
my $updateReboot = 0;
69
my $updateEventsys_restart = 0;
Chad Barb's avatar
 
Chad Barb committed
70 71
my $force  = 0;
my $errors = 0;
Chad Barb's avatar
Chad Barb committed
72
my $assignWrapperErrorCode = 0;
73
my $updatehosed = 0;
Chad Barb's avatar
 
Chad Barb committed
74
my $state;
75
my $canceled;
Chad Barb's avatar
 
Chad Barb committed
76 77
my $os_setup_pid;
my $cleanvlans;
78
my $nextState;
Chad Barb's avatar
 
Chad Barb committed
79 80

#
Chad Barb's avatar
 
Chad Barb committed
81
# First argument is either "in", "out", or "update";
Chad Barb's avatar
 
Chad Barb committed
82 83 84 85 86
# this value goes into $swapop.
#

my $swapop = shift;	

Chad Barb's avatar
 
Chad Barb committed
87 88 89 90
if (!$swapop || 
    (($swapop ne "in") && 
     ($swapop ne "out") &&
     ($swapop ne "update"))) {
Chad Barb's avatar
 
Chad Barb committed
91 92 93 94 95 96 97 98 99 100 101
    usage();
}

#
# Get other arguments.
#

while ($#ARGV > 1) {
    $arg = shift;
    if ($arg eq "-force") {
	$force = 1;
Chad Barb's avatar
Chad Barb committed
102 103
    } elsif ($arg eq "-reboot") {
	$updateReboot = 1;
104 105
    } elsif ($arg eq "-eventsys_restart" && $swapop eq "update") {
	$update_Eventsys_restart = 1;
Chad Barb's avatar
 
Chad Barb committed
106 107 108 109 110 111 112 113 114
    } else {
	usage();
    }
}
if ($#ARGV < 1) {
    usage();
}
my ($pid,$eid) = @ARGV;

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
#
# 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");
}

Chad Barb's avatar
 
Chad Barb committed
131 132 133 134 135 136 137 138 139 140 141 142 143
TBDebugTimeStampsOn();

#
# Print starting message.
#

print "Beginning swap-$swapop for $pid/$eid. " . TBTimeStamp() . "\n";
TBDebugTimeStamp("tbswap $swapop started");

#
# Get experiment state; verify that experiment exists.
#
if (! ($state = ExpState($pid, $eid))) {
144 145
    die("*** $0:\n".
	"    No such experiment $pid/$eid\n");
Chad Barb's avatar
 
Chad Barb committed
146
}
147 148 149 150 151 152
# Sanity check the current state. 
if (!$force) {
    if ($swapop eq "in") {
	die("*** $0:\n".
	    "    Experiment should be ACTIVATING. Currently $state.\n")
	    if ($state ne EXPTSTATE_ACTIVATING);
Chad Barb's avatar
 
Chad Barb committed
153
    }
154 155 156 157
    elsif ($swapop eq "out") {
	die("*** $0:\n".
	    "    Experiment should be SWAPPING. Currently $state.\n")
	    if ($state ne EXPTSTATE_SWAPPING);
Chad Barb's avatar
 
Chad Barb committed
158
    }
159 160 161 162
    elsif ($swapop eq "update") {
	die("*** $0:\n".
	    "    Experiment should be MODIFY_RESWAP. Currently $state.\n")
	    if ($state ne EXPTSTATE_MODIFY_RESWAP);
Chad Barb's avatar
 
Chad Barb committed
163 164 165 166 167 168
    }
}

#
# Do actual swapping
#
169 170 171 172 173
if ($swapop eq "out") {
    #
    # Swap out
    #
    $errors = doSwapout(REAL);
Chad Barb's avatar
 
Chad Barb committed
174
}
175 176 177 178 179 180 181 182
elsif ($swapop eq "update") {
    #
    # Update.
    #
    # Phase One -- swap experiment partially out.
    #
    print STDERR "Backing up physical state...\n";
    TBExptBackupPhysicalState($pid,$eid);
Chad Barb's avatar
 
Chad Barb committed
183

184
    $errors = doSwapout(UPDATE);
Chad Barb's avatar
Chad Barb committed
185

186
    if ($errors) {
Chad Barb's avatar
 
Chad Barb committed
187
	#
188 189 190 191 192
	# 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
193
    }
194
    else {
Chad Barb's avatar
 
Chad Barb committed
195
	#
196
	# Phase Two -- swap experiment back in.
Chad Barb's avatar
 
Chad Barb committed
197
	#
Chad Barb's avatar
Chad Barb committed
198 199
	$errors = doSwapin(UPDATE);

200
	if ($errors) {
Chad Barb's avatar
Chad Barb committed
201 202 203
	    #
	    # There were errors; see if we can recover.
	    #
204
	    my $NoRecover = 0;
Chad Barb's avatar
Chad Barb committed
205 206 207

	    if ($errors != 7) {
		print STDERR "Update failure occurred _after_ assign phase; ";
208
		$NoRecover = 1;
Chad Barb's avatar
Chad Barb committed
209 210
	    }

211 212 213
	    if (! $NoRecover) {
		print STDERR "Recovering physical state.\n";
		if (($NoRecover = TBExptRestorePhysicalState($pid,$eid))) {
Chad Barb's avatar
Chad Barb committed
214 215 216
		    print STDERR "Could not restore backed-up physical state; ";
		}
	    }
217 218 219
	    if (! $NoRecover) {
		print STDERR "Doing a recovery swap-in of old state.\n";
		if (($NoRecover = doSwapin(UPDATE_RECOVER))) {
Chad Barb's avatar
Chad Barb committed
220 221 222
		    print STDERR "Could not swap in old physical state; ";
		}
	    }
223 224
	    if ($NoRecover) {
		print STDERR "Recovery aborted! Swapping experiment out.\n";
Chad Barb's avatar
Chad Barb committed
225
		doSwapout(CLEANUP);
226 227 228 229
		$updatehosed = 1;
	    }
	    else {
		print STDERR "Update recovery successful.\n";
Chad Barb's avatar
Chad Barb committed
230
	    }
Chad Barb's avatar
 
Chad Barb committed
231
	}
Chad Barb's avatar
Chad Barb committed
232 233
    }
}
234 235 236 237 238 239 240
elsif ($swapop eq "in") {
    #
    # Swap in
    #
    my $retries = 2;
    
    $errors = doSwapin(REAL);
Chad Barb's avatar
Chad Barb committed
241

242 243 244 245 246 247 248 249 250 251
    #
    # 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
252

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

256 257
	print STDERR "Trying again...\n";
	$errors = doSwapin(RETRY);
Chad Barb's avatar
 
Chad Barb committed
258
    }
259 260 261 262
    if ($errors || $canceled) {
	print STDERR "Cleaning up after " .
	    ($errors ? "errors" : "cancelation") . ".\n";
	doSwapout(CLEANUP);
Chad Barb's avatar
 
Chad Barb committed
263 264 265 266 267 268 269
    }
}

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

Chad Barb's avatar
 
Chad Barb committed
273 274 275
    # pass assign wrapper info along.
    # other codes in 'errors' (3 or 7) are meaningless and
    # should just be reported as 1's.
276
    exit(1 | $assignWrapperErrorCode | ($updatehosed ? 0x40 : 0));
Chad Barb's avatar
 
Chad Barb committed
277
}
278 279 280
print "Successfully finished swap-$swapop for $pid/$eid. " . TBTimeStamp() ."\n";
TBDebugTimeStamp("tbswap $swapop finished (succeeded)");
exit(0);
Chad Barb's avatar
 
Chad Barb committed
281 282 283 284

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

##
Chad Barb's avatar
Chad Barb committed
285
#
Chad Barb's avatar
 
Chad Barb committed
286 287
# doSwapout - Swaps experiment out.
#
Chad Barb's avatar
Chad Barb committed
288
#             If in REAL or CLEANUP,
Chad Barb's avatar
 
Chad Barb committed
289 290 291
#             this function will free all nodes for the 
#             experiment.
#
Chad Barb's avatar
Chad Barb committed
292
#             If in RETRY or UDPATE,
Chad Barb's avatar
 
Chad Barb committed
293 294 295 296 297 298
#             only nodes not in RES_READY will be freed.
#
#             Returns 0 on success, >0 on failure.
#
##

Chad Barb's avatar
 
Chad Barb committed
299 300
sub doSwapout($) {
    my $type = shift; # REAL==4, CLEANUP==3, RETRY==2, UPDATE==1.
Chad Barb's avatar
 
Chad Barb committed
301 302 303 304 305 306 307 308 309 310 311
    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
312

Chad Barb's avatar
 
Chad Barb committed
313 314
    if (! $TESTMODE) { 
	if (! $DISABLE_EVENTS) {
315
	    if ($type >= RETRY || ($update_Eventsys_restart && $type == UPDATE) ) {
Chad Barb's avatar
 
Chad Barb committed
316 317 318 319 320
		print "Stopping the event system\n";
		if (system("eventsys_control stop $pid $eid")) {
		    print STDERR "*** Failed to stop the event system.\n";
		    $swapout_errors = 1;
		}
Chad Barb's avatar
 
Chad Barb committed
321 322
	    }
	}
Chad Barb's avatar
Chad Barb committed
323

Chad Barb's avatar
 
Chad Barb committed
324
	#
Chad Barb's avatar
Chad Barb committed
325
	# Clean up any VLANs in experiment.
Chad Barb's avatar
 
Chad Barb committed
326
	#
Chad Barb's avatar
Chad Barb committed
327 328 329 330 331 332 333
	TBDebugTimeStamp("snmpit started");
	print STDERR "Removing VLANs.\n";
	if (system("snmpit -r $pid $eid")) {
	    print STDERR "*** Failed to reset VLANs\n";
	    $swapout_errors = 1;
	} else {
	    $cleanvlans = 0;
Chad Barb's avatar
 
Chad Barb committed
334
	}
Chad Barb's avatar
Chad Barb committed
335
	TBDebugTimeStamp("snmpit finished");
336
    }
Chad Barb's avatar
Chad Barb committed
337

338 339 340
    if ($type >= CLEANUP) {
	#
	# We're not attempting a retry;
Chad Barb's avatar
 
Chad Barb committed
341
	#
342
	# Stop all of the vnodes.
Chad Barb's avatar
 
Chad Barb committed
343
	#
344
	if (! $TESTMODE) { 	
Chad Barb's avatar
 
Chad Barb committed
345 346 347 348 349 350 351
	    print "Tearing down virtual nodes.\n";
	    TBDebugTimeStamp("vnode_setup -k started");
	    if (system("vnode_setup -d -k $pid $eid")) {
		print STDERR "*** Failed to tear down vnodes.\n";
		$swapout_errors = 1;
	    }
	    TBDebugTimeStamp("vnode_setup finished");
Chad Barb's avatar
 
Chad Barb committed
352 353 354 355
	}

	#
	# remove all nodes from the experiment.
Chad Barb's avatar
 
Chad Barb committed
356
	# (nfree will send them to RES_FREE_DIRTY)
Chad Barb's avatar
 
Chad Barb committed
357 358 359 360 361 362 363 364
	#
	print STDERR "Freeing nodes.\n";
	TBDebugTimeStamp("nfree started");
	if (system("nfree $pid $eid")) {
	    print STDERR "*** Could not free nodes.\n";
	    $swapout_errors = 1;
	}
	TBDebugTimeStamp("nfree finished");
Chad Barb's avatar
 
Chad Barb committed
365 366 367 368 369

	#
	# Since this is an actual swapout, 
	# reset our count of swap out nag emails sent.
	#
370 371
	DBQueryWarn("update experiments set swap_requests='' ".
		    "where eid='$eid' and pid='$pid'");
Chad Barb's avatar
 
Chad Barb committed
372 373
    } else {
	#
Chad Barb's avatar
 
Chad Barb committed
374
	# $type == RETRY or $type == UPDATE.
Chad Barb's avatar
 
Chad Barb committed
375 376
	# 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
377
	# (nfree will send deallocated nodes to RES_FREE_DIRTY)
Chad Barb's avatar
 
Chad Barb committed
378
	#
379 380 381 382 383 384 385 386
	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
387 388
			 "where rv.pid='$pid' and rv.eid='$eid'");

389
	while (my ($node,$allocstate,$isvirt) = $db_result->fetchrow_array) {
Chad Barb's avatar
 
Chad Barb committed
390
	    if ($allocstate ne TBDB_ALLOCSTATE_RES_READY()) {
391 392 393 394 395 396
		if ($isvirt) {
		    push(@failedvnodes, $node);
		}
		else {
		    push(@failedpnodes, $node);
		}
Chad Barb's avatar
 
Chad Barb committed
397 398 399
	    }
	}

400 401 402 403 404 405 406 407 408 409 410 411
	#
	# 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")) {
		print STDERR "*** Failed to tear down vnodes.\n";
		$swapout_errors = 1;
	    }
	    TBDebugTimeStamp("vnode_setup -k finished");
	}
Chad Barb's avatar
 
Chad Barb committed
412

413 414 415 416
	#
	# Release all failed nodes.
	# 
	if (@failedpnodes > 0 || @failedvnodes > 0) {
417 418
	    print STDERR "Freeing failed nodes.\n";
	    
Chad Barb's avatar
 
Chad Barb committed
419
	    TBDebugTimeStamp("nfree started");
Chad Barb's avatar
 
Chad Barb committed
420 421 422 423 424
	    #
	    # 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.
	    #
425 426
	    if (system("nfree -x $pid $eid " .
		       join(" ", (@failedpnodes, @failedvnodes)))) {
Chad Barb's avatar
 
Chad Barb committed
427 428 429 430 431 432 433
		print STDERR "*** Could not free nodes.\n";
		$swapout_errors = 1;
	    }
	    TBDebugTimeStamp("nfree finished");
	}
    }

434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
    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");
458
		if (system("plabslice destroy $pid $eid")) {
459 460 461 462 463 464 465 466
		    print STDERR "*** Failed to destroy Plab dslice\n";
		    $swapout_errors = 1;
		}
		TBDebugTimeStamp("plabslice destroy finished");
	    }
	}
    }

Chad Barb's avatar
 
Chad Barb committed
467 468 469 470 471 472 473 474
    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
475
	# not a failed swapin(cleanup), update, or retry.
Chad Barb's avatar
 
Chad Barb committed
476
	#
Chad Barb's avatar
 
Chad Barb committed
477
	if ($type == REAL) {
Chad Barb's avatar
 
Chad Barb committed
478 479 480 481 482 483 484
	    print "Resetting mountpoints.\n";
	    TBDebugTimeStamp("exports started");
	    if (system("exports_setup")) {
		print STDERR "*** Failed to reset mountpoints.\n";
	    }
	    TBDebugTimeStamp("exports finished");
	}
Chad Barb's avatar
Chad Barb committed
485

Chad Barb's avatar
 
Chad Barb committed
486 487 488 489
	#
	# Resetting named maps and email lists is fast and idempotent,
	# so whatever.
	#
Chad Barb's avatar
 
Chad Barb committed
490 491 492 493 494 495
	print "Resetting named maps.\n";
	TBDebugTimeStamp("named started");
	if (system("named_setup")) {
	    print "*** WARNING: Failed to reset named map.\n";
	}
	TBDebugTimeStamp("named finished");
Chad Barb's avatar
Chad Barb committed
496

Chad Barb's avatar
 
Chad Barb committed
497 498 499 500 501 502 503 504
	print "Resetting email lists.\n";
	TBDebugTimeStamp("genelists started");
	if (system("genelists")) {
	    print "*** WARNING: Failed to reset email lists.\n";
	}
	TBDebugTimeStamp("genelists finished");
    }

Chad Barb's avatar
 
Chad Barb committed
505 506 507
    #
    # Wipe the DB clean.
    #
Chad Barb's avatar
Chad Barb committed
508

Chad Barb's avatar
 
Chad Barb committed
509
    print STDERR "Resetting DB.\n";
Chad Barb's avatar
 
Chad Barb committed
510
    TBExptRemovePhysicalState( $pid, $eid );
Chad Barb's avatar
 
Chad Barb committed
511 512 513 514 515

    return $swapout_errors;
}

##
Chad Barb's avatar
Chad Barb committed
516
#
Chad Barb's avatar
 
Chad Barb committed
517 518
# doSwapin - Swaps experiment in.
#
Chad Barb's avatar
Chad Barb committed
519 520 521 522 523
#            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
524 525
##

Chad Barb's avatar
 
Chad Barb committed
526
sub doSwapin($) {
Chad Barb's avatar
Chad Barb committed
527
    my $type = shift; # REAL==4, RETRY==2, UPDATE==1, UPDATE_RECOVER=0. 
Chad Barb's avatar
 
Chad Barb committed
528

Chad Barb's avatar
 
Chad Barb committed
529 530 531 532 533
    #
    # assign_wrapper does all the virtual to physical mapping 
    # and updating the DB state.
    #

Chad Barb's avatar
Chad Barb committed
534 535 536 537 538 539 540 541 542 543
    if ($type > UPDATE_RECOVER) {
	print "Mapping to physical reality ...\n";
	TBDebugTimeStamp("assign_wrapper started");

	#
	# Pass the -u (update) switch into assign_wrapper,
	# So any nodes already in the experiment are
	# fixed.
	#
	my $exitcode;
544
	my $wrapper = "assign_wrapper";
545
	
546
	if (system("$wrapper -u $pid $eid")) {
Chad Barb's avatar
Chad Barb committed
547 548
	    #
	    # save this off so it will get passed back later.
549
	    # Note that -1 is an uncontrolled error. No recovery.
Chad Barb's avatar
Chad Barb committed
550
	    #
551
	    $assignWrapperErrorCode = $? >> 8;
552 553 554 555 556
	    $exitcode = $? >> 8;

	    print STDERR "*** Failed ($exitcode) to map to reality.\n";

	    if (($exitcode & 64) && ($exitcode != 255)) {
Chad Barb's avatar
 
Chad Barb committed
557
		# so batchexp doesn't choke.
558 559 560
		$assignWrapperErrorCode -= 64
		    if ($exitcode != 255);
		# We can recover. 
Chad Barb's avatar
Chad Barb committed
561
		return 7;
562 563 564
	    }
	    else {
		# No recovery.
Chad Barb's avatar
Chad Barb committed
565 566 567 568
		return 1;
	    }
	}
	TBDebugTimeStamp("assign_wrapper finished");
Chad Barb's avatar
 
Chad Barb committed
569

Chad Barb's avatar
Chad Barb committed
570 571
	print "Mapped to physical reality!\n";
    }
Chad Barb's avatar
 
Chad Barb committed
572

573
    # Check cancel flag before continuing. No retry, 
574
    TBGetCancelFlag($pid, $eid, \$canceled);
575 576 577
    return 1
	if ($canceled);

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
    #
    # 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);
		}
	    }
	    if (@virtnodes) {
		TBDebugTimeStamp("vnode_setup started");
		
616
		if (system("vnode_setup -k $pid $eid @virtnodes")) {
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
		    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) {
		if (system("nfree $pid $eid @physnodes")) {
		    print "Failed to nfree unused physnodes!\n";
		    return 1;
		}
	    }
	}
    }

Chad Barb's avatar
 
Chad Barb committed
636 637 638 639 640 641
    # Exit here if we are testing.
    if ($TESTMODE) {
	print "Testing run - Stopping here.\n";
	return 0;
    }

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
    #
    # 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?
	#
	print STDERR "*** Failed to set up tarballs.\n";
	return 1;
    }
    TBDebugTimeStamp("tarfiles_setup finished");

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
    #
    # 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");
686
		if (system("plabslice create $pid $eid")) {
687 688 689 690 691 692 693 694
		    print STDERR "*** Failed to create Plab dslice\n";
		    return 3;
		}
		TBDebugTimeStamp("plabslice alloc finished");
	    }
	}
    }

695
    # Check cancel flag before continuing. No retry, 
696
    TBGetCancelFlag($pid, $eid, \$canceled);
697 698 699
    return 1
	if ($canceled);

Chad Barb's avatar
 
Chad Barb committed
700 701 702 703 704 705 706 707 708 709 710 711 712
    #
    # 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")) {
	print STDERR "*** Failed to setup mountpoints.\n";
	return 1;
    }
    TBDebugTimeStamp("mountpoints finished");
Chad Barb's avatar
Chad Barb committed
713

Chad Barb's avatar
 
Chad Barb committed
714 715 716 717 718 719
    TBDebugTimeStamp("named started");
    print "Setting up named maps.\n";
    if (system("named_setup")) {
	print STDERR "*** WARNING: Failed to add node names to named map.\n";
	#
	# This is a non-fatal error.
Chad Barb's avatar
Chad Barb committed
720
	#
Chad Barb's avatar
 
Chad Barb committed
721 722
    }
    TBDebugTimeStamp("named finished");
Chad Barb's avatar
Chad Barb committed
723

724
    # Check cancel flag before continuing. No retry, 
725
    TBGetCancelFlag($pid, $eid, \$canceled);
726 727
    return 1
	if ($canceled);
Chad Barb's avatar
Chad Barb committed
728 729 730

    #
    # If user specified -reboot to update,
Chad Barb's avatar
 
Chad Barb committed
731
    # and we are successfully performing the update,
732
    # then mark all nodes in experiment so os_setup will reboot them.
Chad Barb's avatar
Chad Barb committed
733
    #
Chad Barb's avatar
 
Chad Barb committed
734
    if (($type == UPDATE) && $updateReboot) {
Chad Barb's avatar
Chad Barb committed
735 736
	print STDERR "Marking nodes for reboot.\n";
	$db_result =
737 738 739
	    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
740

741 742 743 744 745 746 747 748 749 750
	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()) {
		TBSetNodeAllocState($node, TBDB_ALLOCSTATE_RES_INIT_DIRTY());
	    }
Chad Barb's avatar
Chad Barb committed
751 752 753
	}
    }

Chad Barb's avatar
 
Chad Barb committed
754 755 756 757 758 759 760 761 762 763 764 765
    #
    # 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";
    TBDebugTimeStamp("os_setup started");
    if (!($os_setup_pid = fork())) { 
	exec("os_setup $pid $eid") or return 1;
    } elsif ($os_setup_pid == -1) {
	print STDERR "*** Fork failed.\n";
	return 1;
    }
Chad Barb's avatar
Chad Barb committed
766

Chad Barb's avatar
 
Chad Barb committed
767
    #
768 769 770 771 772
    # 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
773 774 775 776 777 778 779 780 781
    #

    print "Setting up VLANs.\n";
    TBDebugTimeStamp("snmpit started");
    if (system("snmpit -t $pid $eid")) {
	print STDERR "*** Failed to set up VLANs.\n";
	return 1;
    }
    TBDebugTimeStamp("snmpit finished");
Chad Barb's avatar
Chad Barb committed
782

Chad Barb's avatar
 
Chad Barb committed
783 784 785 786
    #
    # An error now means that the VLANS need to be cleaned up.
    #
    $cleanvlans = 1;
Chad Barb's avatar
Chad Barb committed
787

Chad Barb's avatar
 
Chad Barb committed
788 789 790 791 792 793 794 795 796
    print "Setting up email lists.\n";
    TBDebugTimeStamp("genelists started");
    if (system("genelists")) {
	print STDERR "*** WARNING: Failed to update email lists.\n";
	#
	# This is a non-fatal error.
	# 
    }
    TBDebugTimeStamp("genelists finished");
Chad Barb's avatar
Chad Barb committed
797

Chad Barb's avatar
 
Chad Barb committed
798 799 800 801 802 803 804 805 806 807 808 809 810 811
    #
    # 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")) {
	    print STDERR "*** WARNING: Failed to clear port counters.\n";
	    #
	    # This is a non-fatal error.
	    # 
	}
	TBDebugTimeStamp("portstats finished");
Chad Barb's avatar
 
Chad Barb committed
812
    }
Chad Barb's avatar
Chad Barb committed
813

Chad Barb's avatar
 
Chad Barb committed
814 815 816 817 818 819 820 821 822
    #
    # 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) {
	    print STDERR "*** Failed to reset OS and reboot nodes.\n";
	    #
Chad Barb's avatar
 
Chad Barb committed
823 824
	    # Use returncode from os_setup process to
	    # set global $retry flag, indicating to caller
Chad Barb's avatar
 
Chad Barb committed
825 826 827
	    # that it may be beneficial to attempt
	    # a doSwapin() again.
	    #
Chad Barb's avatar
 
Chad Barb committed
828
            if (($CHILD_ERROR >> 8) == 1) {
Chad Barb's avatar
Chad Barb committed
829
		return 3;
Chad Barb's avatar
 
Chad Barb committed
830 831
	    } else {
		print STDERR "Not retrying due to error type.\n";
Chad Barb's avatar
Chad Barb committed
832
		return 1;
Chad Barb's avatar
 
Chad Barb committed
833
	    }
Chad Barb's avatar
 
Chad Barb committed
834 835 836 837 838 839 840
	}
    } else {
	undef $os_setup_pid;
	print STDERR "*** Error waiting for os_setup to finish.\n";
	return 1;
    }
    TBDebugTimeStamp("os_setup finished");
Chad Barb's avatar
Chad Barb committed
841

Chad Barb's avatar
 
Chad Barb committed
842 843 844 845
    #
    # 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
846 847
    # 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
848 849
    # 
    if (! $DISABLE_EVENTS) {
850 851
	if ( $update_Eventsys_restart || 
	    ($type != UPDATE && $type != UPDATE_RECOVER) ) {
Chad Barb's avatar
 
Chad Barb committed
852 853 854 855 856 857 858
	    print "Starting the event system.\n";
	    TBDebugTimeStamp("eventsys_control started");
	    if (system("eventsys_control start $pid $eid")) {
		print STDERR "*** Failed to start the event system.\n";
		return 1;
	    }
	    TBDebugTimeStamp("eventsys_control finished");
Chad Barb's avatar
 
Chad Barb committed
859 860
	}
    }
Chad Barb's avatar
Chad Barb committed
861

Chad Barb's avatar
 
Chad Barb committed
862 863
    return 0;
}