tbswap.in 16.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/perl -w

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
# All rights reserved.
#

use English;

11 12 13 14 15
# 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.
16 17 18

sub usage()
{
19
    print STDERR "Usage: $0 { in | out [-force] | update [-restart] } pid eid\n";
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    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.
#
45 46 47 48 49 50 51
sub doSwapout($);
sub doSwapin($);

sub REAL()    { return 4; }
sub CLEANUP() { return 3; }
sub RETRY()   { return 2; }
sub UPDATE()  { return 1; }
52 53 54 55 56 57 58

#
# Turn off line buffering on output
#

$| = 1;

59
my $restart = 0;
60 61
my $force  = 0;
my $errors = 0;
62

63
my $retry  = 0;
64

65 66 67 68 69 70
my $state;

my $os_setup_pid;
my $cleanvlans;

#
71
# First argument is either "in", "out", or "update";
72 73 74 75 76
# this value goes into $swapop.
#

my $swapop = shift;	

77 78 79 80
if (!$swapop || 
    (($swapop ne "in") && 
     ($swapop ne "out") &&
     ($swapop ne "update"))) {
81 82 83 84 85 86 87 88 89 90 91
    usage();
}

#
# Get other arguments.
#

while ($#ARGV > 1) {
    $arg = shift;
    if ($arg eq "-force") {
	$force = 1;
92 93
    } elsif ($arg eq "-restart") {
	$restart = 1;
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    } else {
	usage();
    }
}
if ($#ARGV < 1) {
    usage();
}
my ($pid,$eid) = @ARGV;

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))) {
    print STDERR "*** No such experiment $pid/$eid\n";
    $errors = 1;
}

#
# Figure out which state we're looking for 
# and which state we're going to set according to the operation.
#

my $desiredState;
my $nextState;
my $suggestion;
my $suggestUseOfForce;

if ($swapop eq "in") {
    $desiredState = EXPTSTATE_SWAPPED;
    $nextState    = EXPTSTATE_ACTIVATING;
    $suggestion   = "Must be swapped out.";
    $suggestUseOfForce = 0;
136
} elsif ($swapop eq "out") {
137 138 139 140 141 142 143 144 145
    if (! $TESTMODE) {
	$desiredState = EXPTSTATE_ACTIVE;
	$suggestion   = "Must be running.";
    } else {
	$desiredState = EXPTSTATE_TESTING;
	$suggestion   = "Must be in testing state.";
    }
    $nextState    = EXPTSTATE_SWAPPING;
    $suggestUseOfForce = 1;
146 147 148 149 150
} elsif ($swapop eq "update") {
    $desiredState = EXPTSTATE_ACTIVE;
    $nextState    = EXPTSTATE_ACTIVATING; # XXX add an updating state?
    $suggestion   = "Must be running.";
    $suggestUseOfForce = 0;
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
}

#
# Check that experiment is in correct state.
# If it is not, print appropriate error message.
#

if (! $errors) {
    if ($state ne $desiredState) {
	if (! $force) {
	    if ($state eq EXPTSTATE_SWAPPED) {
		print STDERR "*** Experiment is already swapped out. $suggestion\n";
	    } elsif ($state eq EXPTSTATE_ACTIVE) {
		print STDERR "*** Experiment is already running. $suggestion\n";
	    } else {
		print STDERR "*** Experiment is in the wrong state: $state. $suggestion\n";
	    }
	    if ($suggestUseOfForce) {
		print STDERR "    Try using -force to ignore improper state.\n";
	    }
	    $errors = 1;
	} else {
	    print STDERR "*** WARNING: Ignoring improper state: $state.\n";
	}
    }
}

#
# Set intermediate experiment state.
#

if (! $errors) {
    if (! SetExpState($pid, $eid, $nextState)) {
	print STDERR "*** Failed to set intermediate experiment state.\n";
	$errors = 1;
    }
}

#
# Do actual swapping
#

193
undef $nextState;
194 195

if (! $errors) {
196
    if ($swapop eq "out") {
197
	#
198
	# Swap out
199
	#
200 201 202 203 204 205 206
	$errors = doSwapout(REAL);
	if (! $errors) {
	    $nextState = EXPTSTATE_SWAPPED;
	} else {
	    # leave $nextState undefined
	    # experiment will stay in SWAPPING.
	}
207 208

	#
209 210 211 212 213
	# Update Accounting Information
	#
	TBSetExpSwapTime($pid, $eid);
    }
}
214

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
if (! $errors) {
    if ($swapop eq "update") {
	$errors = doSwapout(UPDATE);
	if (! $errors) {
	    # leave $nextState undefined. 
	    # (this will get defined below.)
	} else {
	    # Error; Clean up and put experiment in SWAPPED.
	    print STDERR "Cleaning up after errors.\n";
	    doSwapout(CLEANUP);
	    $nextState = EXPTSTATE_SWAPPED;
	}
	
	#
	# Update Accounting Information
	#
	TBSetExpSwapTime($pid, $eid);
    }
}

if (! $errors) {
    if ($swapop eq "in" || $swapop eq "update") {
	#
	# Swap in
	#

	if ($swapop eq "in") {
	    $errors = doSwapin(REAL);
	} else {
	    $errors = doSwapin(UPDATE);
	}
246

247
	my $retries = 2;
248 249 250 251 252 253

	#
	# Attempt a retry if: 
	#   a) there were errors, 
	#   b) doswapin() indicated (via $retry) a retry is appropriate,
	#   c) we haven't tried too many times already.
254
	#   d) (XXX temporary) We're not doing an update.
255
	#
256 257 258
	# NOTE that if $TESTMODE == 1, retries are impossible, since
        # $retry will never get set to 1.
	#
259
	while ($errors && $retry && $retries && $swapop eq "in") {
260 261 262
	    $retries--;

	    print STDERR "Cleaning up after errors; will try again.\n";
263
	    doSwapout(RETRY);
264 265

	    print STDERR "Trying again...\n";
266
	    $errors = doSwapin(RETRY);
267 268 269 270 271 272 273 274 275 276
	}
	
	if (! $errors) {
	    #
	    # Update Accounting Information
	    #
	    TBSetExpSwapTime($pid, $eid);

	    #
	    # Swapin worked; exp is now ACTIVE.
277
	    # (or TESTING) 
278
	    #
279 280 281 282 283
	    if ($TESTMODE) {
		$nextState = EXPTSTATE_TESTING;
	    } else {
		$nextState = EXPTSTATE_ACTIVE;
	    }
284 285
	} else {	    
	    print STDERR "Cleaning up after errors.\n";
286

287
	    doSwapout(CLEANUP);
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338

	    #
	    # Regardless of how well cleanup swapout worked, 
	    # send exp to SWAPPED.
	    #
	    $nextState = EXPTSTATE_SWAPPED; 
	}
    }
}

if (defined $nextState) {
    if (! SetExpState($pid, $eid, $nextState)) {
	print STDERR "*** Failed to set experiment state.\n";
	#
	# Don't obliterate a meaningful error code if this happens.
	#
	if (! $errors) { $errors = 1; }
    }
}

#
# Write appropriate message and exit.
#

if ($errors) {
    print "Failingly finished swap-$swapop for $pid/$eid. " . TBTimeStamp() . "\n";
    TBDebugTimeStamp("tbswap $swapop finished (failed)");
} else {
    print "Successfully finished swap-$swapop for $pid/$eid. " . TBTimeStamp() . "\n";
    TBDebugTimeStamp("tbswap $swapop finished (succeeded)");
}

exit($errors);

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

##
# 
# doSwapout - Swaps experiment out.
#
#             If the global $retry flag is 0,
#             this function will free all nodes for the 
#             experiment.
#
#             If the global $retry flag is 1,
#             only nodes not in RES_READY will be freed.
#
#             Returns 0 on success, >0 on failure.
#
##

339 340
sub doSwapout($) {
    my $type = shift; # REAL==4, CLEANUP==3, RETRY==2, UPDATE==1.
341 342 343 344 345 346 347 348 349 350 351 352 353 354
    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;
    }
    
    if (! $TESTMODE) { 
	if (! $DISABLE_EVENTS) {
355 356 357 358 359 360
	    if ($type >= RETRY) {
		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;
		}
361 362 363 364
	    }
	}
	
	#
365
	# If this is an actual swapout, or an update/retry, always clean up VLANs
366 367
	# Otherwise, only do it if swapin previously messed them up.
	#
368 369
	# XXX Is doing this all the time cromulent?
	if ($type != CLEANUP || $cleanvlans) {
370 371 372 373 374 375 376 377 378 379 380 381 382 383
	    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;
	    }
	    TBDebugTimeStamp("snmpit finished");
	}
	
	#
	# This is a hack. We need a more general os_teardown, but for now
	# we just kill off the vnode stuff. 
384
	# (don't kill off vnodes when UPDATEing.)
385
	#
386 387 388 389 390 391 392 393
	if ($type >= RETRY) {
	    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");
394 395 396
	}
    }

397
    if ($type >= CLEANUP) {
398 399 400
	#
	# We're not attempting a retry; 
	# remove all nodes from the experiment.
Chad Barb's avatar
Chad Barb committed
401
	# (nfree will send them to RES_FREE_DIRTY)
402 403 404 405 406 407 408 409
	#
	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");
410 411 412 413 414

	#
	# Since this is an actual swapout, 
	# reset our count of swap out nag emails sent.
	#
415 416
	DBQueryWarn("update experiments set swap_requests='' ".
		    "where eid='$eid' and pid='$pid'");
417 418
    } else {
	#
419
	# $type == RETRY or $type == UPDATE.
420 421
	# 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
422
	# (nfree will send deallocated nodes to RES_FREE_DIRTY)
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
	#

	my @failedNodes = ();
	print STDERR "Freeing failed nodes.\n";
	$db_result =
	    DBQueryFatal("select rv.node_id, n.allocstate ".
                         "from reserved as rv ".
			 "left join nodes as n on ".
			 "n.node_id = rv.node_id ".
			 "where rv.pid='$pid' and rv.eid='$eid'");

	while (($node,$allocstate) = $db_result->fetchrow_array) {
	    if ($allocstate ne TBDB_ALLOCSTATE_RES_READY()) {
		push(@failedNodes, $node);
	    }
	}

440 441 442
	# XXX reboot nodes [e.g., put them in RES_DIRTY()] 
	# if doing an update -restart.

443 444
	if (@failedNodes > 0) {
	    TBDebugTimeStamp("nfree started");
Chad Barb's avatar
Chad Barb committed
445 446 447 448 449 450
	    #
	    # 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.
	    #
	    if (system("nfree -x $pid $eid " . join(" ", @failedNodes))) {
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
		print STDERR "*** Could not free nodes.\n";
		$swapout_errors = 1;
	    }
	    TBDebugTimeStamp("nfree finished");
	}
    }

    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
466
	# not a failed swapin(cleanup), update, or retry.
467
	#
468
	if ($type == REAL) {
469 470 471 472 473 474 475 476
	    print "Resetting mountpoints.\n";
	    TBDebugTimeStamp("exports started");
	    if (system("exports_setup")) {
		print STDERR "*** Failed to reset mountpoints.\n";
	    }
	    TBDebugTimeStamp("exports finished");
	}
	
477 478 479 480
	#
	# Resetting named maps and email lists is fast and idempotent,
	# so whatever.
	#
481 482 483 484 485 486 487 488 489 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");
	
	print "Resetting email lists.\n";
	TBDebugTimeStamp("genelists started");
	if (system("genelists")) {
	    print "*** WARNING: Failed to reset email lists.\n";
	}
	TBDebugTimeStamp("genelists finished");
    }

496 497 498 499
    #
    # Wipe the DB clean.
    #
    
500
    print STDERR "Resetting DB.\n";
501
    TBExptRemovePhysicalState( $pid, $eid );
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516

    return $swapout_errors;
}

##
# 
# doSwapin - Swaps experiment in.
#
#            Returns 0 on success, 1 on failure,
#            assign_wrapper's exitcode if that failed.
#
#            Will set $retry = 1 if os_setup failed.
#
##

517 518 519 520 521 522 523
sub doSwapin($) {
    my $type = shift; # REAL==4, RETRY==2, UPDATE==1. (CLEANUP is nonsense.)
    #
    # By default, errors are not tagged as 'retry-suitable.'
    #
    $retry = 0;

524 525 526 527 528 529 530
    #
    # assign_wrapper does all the virtual to physical mapping 
    # and updating the DB state.
    #
    print "Mapping to physical reality ...\n";
    TBDebugTimeStamp("assign_wrapper started");

Chad Barb's avatar
Chad Barb committed
531 532 533 534 535
    #
    # Pass the -u (update) switch into assign_wrapper,
    # So any nodes already in the experiment are
    # fixed.
    #
536
    my $exitcode;
Chad Barb's avatar
Chad Barb committed
537
    if ($exitcode = system("assign_wrapper -u $pid $eid")) {
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
	print STDERR "*** Failed to map to reality.\n";
	# Pass exit code through
	return $exitcode >> 8;
    }
    TBDebugTimeStamp("assign_wrapper finished");

    print "Mapped to physical reality!\n";

    # Exit here if we are testing.
    if ($TESTMODE) {
	print "Testing run - Stopping here.\n";
	return 0;
    }

    #
    # 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");
    
    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.
	# 
    }
    TBDebugTimeStamp("named finished");
    
    #
    # 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;
    }
    
    #
    # 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.)
    #

    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");
    
    #
    # An error now means that the VLANS need to be cleaned up.
    #
    $cleanvlans = 1;
    
    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");
    
619 620 621 622 623 624 625 626 627 628 629 630 631 632
    #
    # 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");
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    }
    
    #
    # 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";
	    #
	    # Set global $retry flag to indicate to caller
	    # that it may be beneficial to attempt
	    # a doSwapin() again.
	    #

Chad Barb's avatar
Chad Barb committed
649
	    $retry = 1;
650 651 652 653 654 655 656 657 658 659 660 661 662 663

	    return 1;
	}
    } else {
	undef $os_setup_pid;
	print STDERR "*** Error waiting for os_setup to finish.\n";
	return 1;
    }
    TBDebugTimeStamp("os_setup finished");
    
    #
    # 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.
664 665
    # Don't do this during an update, since we didn't kill the 
    # event system previously, so starting it again will fail!
666 667
    # 
    if (! $DISABLE_EVENTS) {
668 669 670 671 672 673 674 675
	if ($type != UPDATE) {
	    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");
676 677 678 679 680 681
	}
    }
    
    return 0;
}