swapexp.in 18.1 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7 8
# All rights reserved.
#

9 10 11 12
use English;
use Getopt::Std;

#
Chad Barb's avatar
Chad Barb committed
13
# This gets invoked from the Web interface.
Chad Barb's avatar
 
Chad Barb committed
14
# Swap an experiment in, swap it out, restart or modify.
15
#
Chad Barb's avatar
Chad Barb committed
16

17 18
sub usage()
{
19
    print STDOUT "Usage: swapexp [-b] [-i | -a | -f] [-r] ".
20
	"<-s in | out | restart | modify> <pid> <eid> [<nsfile>]\n";
21 22
    exit(-1);
}
23
my  $optlist = "biafrs:";
24 25 26 27 28 29 30

#
# Configure variables
#
my $TB     = "@prefix@";
my $TBOPS  = "@TBOPSEMAIL@";
my $TBLOGS = "@TBLOGSEMAIL@";
31
my $TBINFO = "$TB/expinfo";
32
my $TBDOCBASE = "@TBDOCBASE@";
33 34 35 36 37 38 39 40 41

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

my $tbdir    = "$TB/bin/";
42
my $tbdata   = "tbdata";
43
my $batch    = 0;
44
my $idleswap = 0;
45 46
my $autoswap = 0;
my $force    = 0;
Chad Barb's avatar
Chad Barb committed
47
my $reboot   = 0;
48
my $errorstat= -1;
Chad Barb's avatar
 
Chad Barb committed
49

50 51 52 53 54
my $inout;
my $logname;
my $dbuid;
my $user_name;
my $user_email;
55
my @allnodes;
56
my @row;
57
my $action;
Chad Barb's avatar
 
Chad Barb committed
58

59 60 61 62 63 64 65 66 67 68 69
#
# Untaint the path
# 
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1;

70 71 72 73 74 75 76
#
# Set umask for start/swap. We want other members in the project to be
# able to swap/end experiments, so the log and intermediate files need
# to be 664 since some are opened for append.
#
umask(0002);

77 78 79 80 81 82 83 84
#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
85 86 87
if (defined($options{"i"})) {
    $idleswap = 1;
}
88 89 90 91 92 93
if (defined($options{"a"})) {
    $autoswap = 1;
}
if (defined($options{"f"})) {
    $force = 1;
}
94 95 96
if (defined($options{"b"})) {
    $batch = 1;
}
Chad Barb's avatar
 
Chad Barb committed
97 98 99
if (defined($options{"r"})) {
    $reboot = 1;
}
100 101 102
if (defined($options{"s"})) {
    $inout = $options{"s"};

Chad Barb's avatar
Chad Barb committed
103 104 105
    if ($inout ne "out"     &&
	$inout ne "in"      &&
	$inout ne "restart" &&
Chad Barb's avatar
 
Chad Barb committed
106
	$inout ne "modify") {
107 108 109 110 111 112 113
	usage();
    }
}
else {
    usage();
}

Chad Barb's avatar
 
Chad Barb committed
114 115 116 117 118 119 120 121
if (@ARGV != (($inout eq "modify") ? 3 : 2)) {
    usage();
}
my $pid   = $ARGV[0];
my $eid   = $ARGV[1];

my $nsfile;

Chad Barb's avatar
Chad Barb committed
122
if ($inout eq "modify") {
Chad Barb's avatar
 
Chad Barb committed
123 124 125 126 127 128 129 130 131 132 133 134
    $nsfile = $ARGV[2];

    #
    # Untaint nsfile argument; Allow slash.
    #
    if ($nsfile =~ /^([-\w.\/]+)$/) {
	$nsfile = $1;
    } else {
	die("Tainted nsfile name: $nsfile");
    }
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#
# 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");
}
150
my $repfile = "$eid.report";
151 152
my $workdir = TBExptWorkDir($pid, $eid);
my $userdir = TBExptUserDir($pid, $eid);
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

#
# Verify user and get his DB uid.
#
if (! UNIX2DBUID($UID, \$dbuid)) {
    die("*** $0:\n".
	"    You do not exist in the Emulab Database.\n");
}

#
# Get email info for user.
#
if (! UserDBInfo($dbuid, \$user_name, \$user_email)) {
    die("*** $0:\n".
	"    Cannot determine your name and email address.\n");
}

#
Chad Barb's avatar
 
Chad Barb committed
171
# Verify that this person can muck with the experiment.
172 173 174 175 176
# Note that any script down the line has to do an admin check also. 
#
if ($UID && !TBAdmin($UID) &&
    !TBExptAccessCheck($dbuid, $pid, $eid, TB_EXPT_DESTROY)) {
    die("*** $0:\n".
Chad Barb's avatar
 
Chad Barb committed
177
	"    You do not have permission to swap or modify this experiment!\n");
178 179
}

180 181 182 183
# Must do this before lock tables!
# idleswap is in minutes, threshold is in hours
$idleswap_time = 60 * TBGetSiteVar("idle/threshold");

184 185 186 187 188
#
# We have to protect against trying to end an experiment that is currently
# in the process of being terminated. We use a "wrapper" state (actually
# a timestamp so we can say when termination was requested) since
# terminating consists of a couple of different experiment states down inside
Chad Barb's avatar
Chad Barb committed
189
# the tb scripts.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
#
DBQueryFatal("lock tables experiments write");

$query_result =
    DBQueryFatal("SELECT * FROM experiments WHERE eid='$eid' and pid='$pid'");

if (! $query_result->numrows) {
    die("*** $0:\n".
	"    No such experiment $pid/$eid exists!\n");
}
my %hashrow = $query_result->fetchhash();
my $expt_head_login = $hashrow{'expt_head_uid'};
my $estate          = $hashrow{'state'};
my $expt_path       = $hashrow{'path'};
my $isbatchexpt     = $hashrow{'batchmode'};
my $ebatchstate     = $hashrow{'batchstate'};
206 207 208 209 210 211 212 213 214 215
my $swappablebit= $hashrow{'swappable'};
my $idleswapbit = $hashrow{'idleswap'};
my $autoswapbit = $hashrow{'autoswap'};
my $swappablestr= ( $swappablebit ? "Yes" : "No" );
my $idleswapstr = ( $idleswapbit ? "Yes" : "No" );
my $autoswapstr = ( $autoswapbit ? "Yes" : "No" );
my $noswap      = $hashrow{'noswap_reason'};
my $noidleswap  = $hashrow{'noidleswap_reason'};
my $idleswaptime= $hashrow{'idleswap_timeout'} / 60.0;
my $autoswaptime= $hashrow{'autoswap_timeout'} / 60.0;
216

217 218
if ($inout ne "out") {
    # I'm going to update this below, so fix the value before I use it.
219
    $idleswap_time = min($idleswaptime * 60, $idleswap_time);
220 221 222
    $idleswaptime = $idleswap_time / 60.0;
}

223 224
my $swapsettings = 
 #"Name:        $expt_name\n" .
225 226
 #"Swappable:   $swappablestr".
 #($swappablebit ? "\n" : " (Reason: $noswap)\n").
227
  "Idle-Swap:   $idleswapstr".
228
  ($idleswapbit ? ", at $idleswaptime hours\n" : " (Reason: $noidleswap)\n").
229 230
  "Auto-Swap:   $autoswapstr".
  ($autoswapbit ? ", at $autoswaptime hours\n" : "\n");
231

232
if (! chdir($workdir)) {
233
    die("*** $0:\n".
234
	"    Could not chdir to $workdir: $!\n");
235 236
}

237 238
if (defined($hashrow{'expt_locked'})) {
    $val = $hashrow{'expt_locked'};
Chad Barb's avatar
Chad Barb committed
239

240 241 242 243 244 245 246
    die("*** $0:\n".
	"    It appears that $pid/$eid went into transition at $val.\n".
	"    You will be notified via email when experiment transition ".
	"    is complete.\n");
}

#
247
# Batchmode.
248
#
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
if ($isbatchexpt) {
    #
    # When coming from the daemon, sanity check the batch state.
    #
    if ($batch) {
	if ($inout eq "in") {
	    die("*** $0:\n".
		"    Batch experiment $pid/$eid is not in the proper state!\n".
		"    Currently $ebatchstate, but should be ACTIVATING\n")
		if ($ebatchstate ne BATCHSTATE_ACTIVATING);
	}
	elsif ($inout eq "out") {
	    die("*** $0:\n".
		"    Batch experiment $pid/$eid is not in the proper state!\n".
		"    Currently $ebatchstate, but should be TERMINATING\n")
		if ($ebatchstate ne BATCHSTATE_TERMINATING);
	}
	else {
	    die("*** $0:\n".
		"    Improper request from batch daemon for $pid/$eid!\n");
	}
    }
    else {
	#
	# User is requesting that a batch either be injected or paused.
	# Sanity check the state, but otherwise let the batch daemon
	# handle it.
	# 
	if ($inout eq "in") {
	    die("*** $0:\n".
		"    Batch experiment $pid/$eid is not in the proper state!\n".
		"    Currently $ebatchstate. Must be PAUSED to swap in.\n")
		if ($ebatchstate ne BATCHSTATE_PAUSED);
	    TBSetBatchState($pid, $eid, BATCHSTATE_POSTED);
	}
	elsif ($inout eq "out") {
	    die("*** $0:\n".
		"    Batch experiment $pid/$eid is not in the proper state!\n".
		"    Currently $ebatchstate. Must be RUNNING to swap out.\n")
		if ($ebatchstate ne BATCHSTATE_RUNNING);

	    my $flag = BATCHMODE_CANCELSWAP;
	
	    DBQueryFatal("UPDATE experiments set canceled=$flag ".
			 "WHERE eid='$eid' and pid='$pid'");
	}
295 296 297 298 299 300 301 302 303 304
	elsif ($inout eq "modify") {
	    die("*** $0:\n".
		"    Batch experiment $pid/$eid is not in the proper state!\n".
		"    Currently $ebatchstate. Must be PAUSED to modify.\n")
		if ($ebatchstate ne BATCHSTATE_PAUSED);
	    # Otherwise, proceed with the modify. The experiment will be
	    # locked below, and so it cannot be injected (posted) until
	    # that is finished cause of the expt_locked test above.
	    goto doit;
	}
305 306 307 308 309 310
	else {
	    die("*** $0:\n",
		"    Batch experiments can only be swapped in or out!\n");
	}
	exit(0);
    }
311
  doit:
312 313 314
}

#
Chad Barb's avatar
 
Chad Barb committed
315
# Okay, check state. We do not allow modification or swapping to start when the
316 317 318 319 320
# experiment is in transition. A future task would be to allow this,
# but for now the experiment must be in one of a few states to proceed
#
# Seems like too many states!
#
321
if ($estate ne EXPTSTATE_ACTIVE && $estate ne EXPTSTATE_SWAPPED && !$force) {
322 323 324 325 326 327 328 329 330 331 332 333 334 335
    die("*** $0:\n".
	"    It appears that experiment $pid/$eid is in transition.\n".
	"    The user that created the experiment will be notified via\n".
        "    email when the experiment is no longer in transition.\n");
}

if ($inout eq "in" && $estate eq EXPTSTATE_ACTIVE) {
    die("*** $0:\n".
	"    It appears that experiment $pid/$eid is already swapped in!");
}
if ($inout eq "out" && $estate eq EXPTSTATE_SWAPPED) {
    die("*** $0:\n".
	"    It appears that experiment $pid/$eid is already swapped out!");
}
336 337 338 339
if ($inout eq "restart" && $estate ne EXPTSTATE_ACTIVE) {
    die("*** $0:\n".
	"    It appears that experiment $pid/$eid is not active!");
}
340

Chad Barb's avatar
 
Chad Barb committed
341 342
# if $inout eq "modify", either EXPTSTATE_ACTIVE -or- EXPTSTATE_SWAPPED is ok.

343
# Set the timestamp now, and unlock the experiments table.
344 345 346 347 348
# Also update idleswap_timeout to whatever the current value is
if ($inout ne "out") {
    $idlestr = ", idleswap_timeout='$idleswap_time'";
} else { $idlestr = ""; }
DBQueryFatal("UPDATE experiments SET expt_locked=now() $idlestr ".
349 350 351 352 353 354 355 356 357
	     "WHERE eid='$eid' and pid='$pid'");

DBQueryFatal("unlock tables");

#
# XXX - At this point a failure is going to leave things in an
# inconsistent state.
#

358 359 360 361 362 363 364 365 366
if ($inout eq "in") {
    $action = "swapped in";
}
if ($inout eq "out") {
    $action = "swapped out";
}
if ($inout eq "restart") {
    $action = "restarted";
}
Chad Barb's avatar
 
Chad Barb committed
367 368 369
if ($inout eq "modify") {
    $action = "modified";
}
370

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
#
# Get email address of the experiment head, which may be different than
# the person who is actually terminating the experiment, since its polite
# to let the original creator know whats going on. 
#
my $expt_head_name;
my $expt_head_email;

if (! UserDBInfo($expt_head_login, \$expt_head_name, \$expt_head_email)) {
    print STDERR "*** WARNING: ".
	         "Could not determine name/email for $expt_head_login.\n";
    $expt_head_name  = "TBOPS";
    $expt_head_email = $TBOPS;
}

#
# If not in batch mode, go into the background. Parent exits.
#
if (! $batch) {
390
    $logname = TBExptCreateLogFile($pid, $eid, "swapexp");
391
    TBExptSetLogFile($pid, $eid, $logname);
392
    TBExptOpenLogFile($pid, $eid);
Chad Barb's avatar
Chad Barb committed
393

394 395 396 397
    if (TBBackGround($logname)) {
	#
	# Parent exits normally
	#
398 399
	print "Experiment $pid/$eid is now being $action.\n".
	    "You will be notified via email when the this is done.\n";
400 401 402 403
	exit(0);
    }
}

404 405 406
#
# Remove old report file since its contents are going to be invalid.
#
407
if ($inout ne "restart" && -e $repfile) {
408 409 410
    unlink("$repfile");
}

411 412 413 414
#
# Sanity check states in case someone changes something.
#
if ($inout eq "out") {
415
    if ($force || $idleswap) { $arg = "-force"; } else { $arg = ""; }
Chad Barb's avatar
 
Chad Barb committed
416
    print STDOUT "Running 'tbswap out' with arguments: $pid $eid\n";
417
    if (system("$tbdir/tbswap out $arg $pid $eid") != 0) {
418
	$errorstat = $? >> 8;
Chad Barb's avatar
 
Chad Barb committed
419
	fatal("tbswap out failed!\n");
420
    }
Chad Barb's avatar
Chad Barb committed
421

422 423 424 425 426
    $estate = ExpState($pid,$eid);
    if ($estate ne EXPTSTATE_SWAPPED) {
	fatal("Experiment is in the wrong state: $estate\n");
    }
}
427
elsif ($inout eq "in") {
Chad Barb's avatar
 
Chad Barb committed
428 429
    print STDOUT "Running 'tbswap in' with arguments: $pid $eid\n";
    if (system("$tbdir/tbswap in $pid $eid") != 0) {
430
	$errorstat = $? >> 8;
Chad Barb's avatar
 
Chad Barb committed
431
	fatal("tbswap in failed!\n");
432
    }
Chad Barb's avatar
Chad Barb committed
433

434 435 436 437
    $estate = ExpState($pid,$eid);
    if ($estate ne EXPTSTATE_ACTIVE) {
	fatal("Experiment is in the wrong state: $estate\n");
    }
438

439
    system("$tbdir/tbreport -b $pid $eid 2>&1 > $repfile");
Chad Barb's avatar
Chad Barb committed
440
}
Chad Barb's avatar
 
Chad Barb committed
441
elsif ($inout eq "modify") {
Chad Barb's avatar
Chad Barb committed
442 443
    my $modifyError = "";

444 445 446
    GatherSwapStats($pid, $eid, $dbuid,
		    TBDB_STATS_SWAPMODIFY, 0, TBDB_STATS_FLAGS_PREMODIFY);

Chad Barb's avatar
Chad Barb committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460
    print "Backing up old experiment state ... " . TBTimeStamp() . "\n";
    if (TBExptBackupVirtualState($pid, $eid, $$)) {
	fatal("*** $0:\n".
	      "    Could not backup experiment state; cannot safely continue!\n");
    }
    TBExptRemoveVirtualState($pid, $eid);

    #
    # Rerun tbprerun if modifying.
    #
    if (system("$tbdir/tbprerun -m $pid $eid $nsfile") != 0) {
	$modifyError = "tbprerun failed!";
    }

Chad Barb's avatar
 
Chad Barb committed
461 462 463 464
    #
    # If experiment is currently swapped out, no need to do an update 
    # after modifying it.
    #
Chad Barb's avatar
Chad Barb committed
465
    if (! $modifyError && $estate eq EXPTSTATE_ACTIVE) {
Chad Barb's avatar
 
Chad Barb committed
466
	print STDOUT "Running 'tbswap update' with arguments: $pid $eid\n";
Chad Barb's avatar
Chad Barb committed
467 468 469 470

	my $rebootSwitch = "";
	if ($reboot) {
	    $rebootSwitch = "-reboot";
Chad Barb's avatar
 
Chad Barb committed
471 472
	}

Chad Barb's avatar
Chad Barb committed
473 474 475
	if (system("$tbdir/tbswap update $rebootSwitch $pid $eid") != 0) {
	    $errorstat = $? >> 8;
	    $modifyError = "tbswap update failed!";
Chad Barb's avatar
 
Chad Barb committed
476
	}
Chad Barb's avatar
 
Chad Barb committed
477

Chad Barb's avatar
Chad Barb committed
478 479 480 481
	if (! $modifyError) {
	    $estate = ExpState($pid, $eid);
	    if ($estate ne EXPTSTATE_ACTIVE) {
		fatal("Experiment is in the wrong state: $estate!");
Chad Barb's avatar
 
Chad Barb committed
482 483
	    }
	}
Chad Barb's avatar
Chad Barb committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501

	if (! $modifyError) {
	    system("$tbdir/tbreport -b $pid $eid 2>&1 > $repfile");
	}
    }

    if ($modifyError) {
	print STDERR "*** $0:\n".
	             "    $modifyError\n";
	print STDOUT "Recovering experiment state...\n";
	TBExptRemoveVirtualState($pid, $eid );
	if (0 == TBExptRestoreVirtualState($pid, $eid, $$)) {
	    fatal("*** Update aborted; old state restored.\n");
	} else {
	    # Set state to NEW so experiment will get wiped.
	    SetExpState($pid, $eid, EXPTSTATE_NEW);
	    fatal("*** Experiment state could not be restored!\n");
	}
Chad Barb's avatar
 
Chad Barb committed
502
    }
503
}
Chad Barb's avatar
 
Chad Barb committed
504
else { # $inout eq "restart" assumed.
505 506 507 508 509
    print STDOUT "Running tbrestart with arguments: $pid $eid\n";
    if (system("$tbdir/tbrestart $pid $eid") != 0) {
	fatal("tbrestart failed!\n");
    }
}
510

511 512 513 514 515 516 517 518 519 520
#
# Try to copy off the files for testbed information gathering.
#
TBSaveExpLogFiles($pid, $eid);

#
# Make a copy of the work dir in the user visible space so the user
# can see the log files. This overwrites existing files of course,
# but thats okay.
#
521
system("cp -Rfp $workdir/ $userdir/tbdata/");
522

523 524 525 526
#
# Gather stats. 
#
if ($inout eq "in") {
527
    GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPIN, 0);
528 529
}
elsif ($inout eq "out") {
530
    GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPOUT, 0,
531
		    ($idleswap ? TBDB_STATS_FLAGS_IDLESWAP() : 0));
532 533
}
elsif ($inout eq "modify") {
534
    GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPMODIFY, 0);
535 536
}

537 538 539 540 541 542 543 544 545 546
#
# Set the swapper uid on success only, and *after* gathering swap stats!
#
TBExptSetSwapUID($pid, $eid, $dbuid);

#
# Must unlock before exit.
#
TBUnLockExp($pid, $eid);

547
print "Done!\n";
548 549 550 551 552 553 554 555

#
# In batch mode, just exit without sending email.
#
if ($batch) {
    exit(0);
}

Chad Barb's avatar
 
Chad Barb committed
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
#
# HACK! if successful, put new NS file in DB.
#

if ($inout eq "modify") {
    $nsdata_string = `cat $nsfile`;
    if (defined($nsdata_string)) {
	$nsdata_string = DBQuoteSpecial($nsdata_string);

	DBQueryWarn("delete from nsfiles WHERE eid='$eid' and pid='$pid'");
	DBQueryWarn("insert into nsfiles (pid, eid, nsfile) ".
		    "VALUES('$pid', '$eid', $nsdata_string)");
    } else {
	print "Warning!! Couldn't read nsfile '$nsfile'!\n";
    }
}

573 574 575 576 577 578 579
#
# Clear the log file so the web page stops spewing. 
#
if (defined($logname)) {
    TBExptCloseLogFile($pid, $eid);
}

580 581 582 583
#
# Send email notification to user.
#
my $message =
584 585
    "Experiment $eid in project $pid has been ";

586
if ($inout eq "out" && ($idleswap || $autoswap || $force) ) {
587
    $message .= "forcibly swapped out by\nEmulab";
588 589 590 591 592
    if ($idleswap) {
	$message .= " because it was idle for too long (Idle-Swap).\n".
	  "(See also the Idle-Swap info in \n".
	  "$TBDOCBASE/docwrapper.php3?docname=swapping.html )\n";
    } elsif ($autoswap) {
593 594
	$message .= " because it exceeded its Maximum Duration.\n".
	  "(See also the Max. Duration info in \n".
595 596 597 598 599
	  "$TBDOCBASE/docwrapper.php3?docname=swapping.html )\n";
    } elsif ($force) {
	$message .= ". (See also our Node Usage Policies in \n".
	  "$TBDOCBASE/docwrapper.php3?docname=swapping.html )\n";
    }
600 601 602 603 604
}
else {
    $message .= "$action.\n";
}

605 606 607 608 609
if ($inout eq "in") {
    # Add the swap settings...
    $message .="\nCurrent swap settings:\n$swapsettings";
}

610 611
$message .=
    "\n".
612 613
    "Appended below is the output. If you have any questions or comments,\n" .
    "please include the output in your message to $TBOPS\n";
614 615

SENDMAIL("$user_name <$user_email>",
616
	 "Experiment $pid/$eid \u$action",
617
	 $message,
618
	 ($idleswap ? $TBOPS : "$user_name <$user_email>"),
619 620
	 "Cc:  $expt_head_name <$expt_head_email>\n".
	 "Bcc: $TBLOGS",
621
	 (($inout eq "restart") ? ($logname) : ($repfile, $logname)));
622 623 624 625 626 627

exit 0;

sub fatal($)
{
    my($mesg) = $_[0];
Chad Barb's avatar
Chad Barb committed
628

629 630
    print STDOUT $mesg;

631 632 633 634 635 636 637 638 639 640 641 642 643
    #
    # Gather stats. 
    #
    if ($inout eq "in") {
	GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPIN, $errorstat);
    }
    elsif ($inout eq "out") {
	GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPOUT, $errorstat);
    }
    elsif ($inout eq "modify") {
	GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPMODIFY, $errorstat);
    }

Chad Barb's avatar
 
Chad Barb committed
644 645 646 647 648 649 650 651 652 653 654 655
    #
    # if $hosed == 1, we entirely terminate the experiment.
    #
    my $hosed = 0;

    # If we're doing a modify,
    # and tbprerun sent the experiment to "NEW",
    # we're hosed.
    if ($inout eq "modify" && ExpState($pid,$eid) eq EXPTSTATE_NEW) {
	$hosed = 1;
    }

Chad Barb's avatar
Chad Barb committed
656
    if ($hosed) {
Chad Barb's avatar
 
Chad Barb committed
657 658 659 660 661 662 663 664 665 666
	#
	# Note: $estate is still set to the state which the experiment was in
	# when we began.
	#
	if ($estate eq EXPTSTATE_ACTIVE) {
	    print "Running 'tbswap out' with arguments: $pid $eid\n";
	    if (system("$tbdir/tbswap out -force $pid $eid") != 0) {
		print "tbswap out failed!\n";
	    }
	}
Chad Barb's avatar
Chad Barb committed
667

Chad Barb's avatar
 
Chad Barb committed
668 669 670 671 672 673
	print "Running tbend with arguments: -force $pid $eid\n";
	if (system("$tbdir/tbend -force $pid $eid") != 0) {
	    print "tbend failed!\n";
	}
    }

674 675 676 677 678 679
    #
    # Kill this for convenience later.
    # 
    DBQueryWarn("update experiments set expt_locked=NULL ".
		"WHERE eid='$eid' and pid='$pid'");

680 681 682
    # Copy over the log files so the user can see them.
    system("/bin/cp -Rfp $workdir/ $userdir/tbdata");

683
    #
Chad Barb's avatar
Chad Barb committed
684
    # In batch mode, exit without sending the email.
685 686
    #
    if ($batch) {
687
	TBUnLockExp($pid, $eid);
688
	exit($errorstat);
689 690
    }

691
    #
Chad Barb's avatar
Chad Barb committed
692
    # Clear the log file so the web page stops spewing.
693 694 695 696 697
    #
    if (defined($logname)) {
	TBExptCloseLogFile($pid, $eid);
    }

698 699 700 701
    #
    # Send a message to the testbed list. Append the logfile.
    #
    SENDMAIL("$user_name <$user_email>",
702
	     "Swap ${inout} Failure: $pid/$eid",
703
	     $mesg,
704
	     ($idleswap ? $TBOPS : "$user_name <$user_email>"),
705
	     "Cc:  $expt_head_name <$expt_head_email>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
706
	     "Cc:  $TBOPS",
707 708
	     ($logname));

Chad Barb's avatar
 
Chad Barb committed
709 710 711 712 713 714 715 716 717 718
    if ($hosed) {
	#
	# Copy off the workdir to the user directory, Then back up both of
	# them for post-mortem debugging.
	#
	system("/bin/cp -Rfp $workdir/ $userdir/tbdata");
	system("/bin/rm -rf  ${workdir}-failed");
	system("/bin/mv -f   $workdir ${workdir}-failed");
	system("/bin/rm -rf  ${userdir}-failed");
	system("/bin/mv -f   $userdir ${userdir}-failed");
Chad Barb's avatar
Chad Barb committed
719
	TBExptDestroy($pid, $eid);
Chad Barb's avatar
 
Chad Barb committed
720 721
    }

722
    exit($errorstat);
723 724
}

Chad Barb's avatar
Chad Barb committed
725 726