swapexp.in 12.3 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 14
# This gets invoked from the Web interface. 
# Swap an experiment in, swap it out, restart or modify.
15 16 17
#
sub usage()
{
Chad Barb's avatar
 
Chad Barb committed
18
    print STDOUT "Usage: swapexp [-i] [-r] <-s in | out | restart | modify> ". 
Chad Barb's avatar
 
Chad Barb committed
19
	         "<pid> <eid> [<nsfile>]\n";
20 21
    exit(-1);
}
Chad Barb's avatar
 
Chad Barb committed
22
my  $optlist = "s:ir";
23 24 25 26 27 28 29

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

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

my $tbdir    = "$TB/bin/";
40
my $tbdata   = "tbdata";
41
my $batch    = 0;
42
my $idleswap = 0;
Chad Barb's avatar
 
Chad Barb committed
43 44
my $reboot   = 0;    

45 46 47 48 49 50
my $inout;
my $logname;
my $dbuid;
my $user_name;
my $user_email;
my @row;
51
my $action;
Chad Barb's avatar
 
Chad Barb committed
52

53 54 55 56 57 58 59 60 61 62 63
#
# Untaint the path
# 
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

64 65 66 67 68 69 70
#
# 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);

71 72 73 74 75 76 77 78
#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
79 80 81
if (defined($options{"i"})) {
    $idleswap = 1;
}
Chad Barb's avatar
 
Chad Barb committed
82 83 84
if (defined($options{"r"})) {
    $reboot = 1;
}
85 86 87
if (defined($options{"s"})) {
    $inout = $options{"s"};

Chad Barb's avatar
 
Chad Barb committed
88 89 90 91
    if ($inout ne "out" && 
	$inout ne "in" && 
	$inout ne "restart" && 
	$inout ne "modify") {
92 93 94 95 96 97 98
	usage();
    }
}
else {
    usage();
}

Chad Barb's avatar
 
Chad Barb committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
if (@ARGV != (($inout eq "modify") ? 3 : 2)) {
    usage();
}
my $pid   = $ARGV[0];
my $eid   = $ARGV[1];

my $nsfile;

if ($inout eq "modify") { 
    $nsfile = $ARGV[2];

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

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
#
# 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");
}
135
my $repfile = "$eid.report";
136 137
my $workdir = TBExptWorkDir($pid, $eid);
my $userdir = TBExptUserDir($pid, $eid);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

#
# 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
156
# Verify that this person can muck with the experiment.
157 158 159 160 161
# 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
162
	"    You do not have permission to swap or modify this experiment!\n");
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
}

#
# 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
# the tb scripts. 
#
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'};

188
if (! chdir($workdir)) {
189
    die("*** $0:\n".
190
	"    Could not chdir to $workdir: $!\n");
191 192
}

193 194 195 196 197 198 199 200 201 202 203 204 205 206
if (defined($hashrow{'expt_locked'})) {
    $val = $hashrow{'expt_locked'};
    
    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");
}

#
# Disallow batch experiment swaps for now.
#
if ($isbatchexpt) {
    die("*** $0:\n".
Chad Barb's avatar
 
Chad Barb committed
207
	"    Batch experiments cannot be swapped or modified yet!");
208 209 210
}

#
Chad Barb's avatar
 
Chad Barb committed
211
# Okay, check state. We do not allow modification or swapping to start when the
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
# 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!
#
if ($estate ne EXPTSTATE_ACTIVE && $estate ne EXPTSTATE_SWAPPED) {
    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!");
}
232 233 234 235
if ($inout eq "restart" && $estate ne EXPTSTATE_ACTIVE) {
    die("*** $0:\n".
	"    It appears that experiment $pid/$eid is not active!");
}
236

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

239 240 241 242 243 244 245 246 247 248 249 250 251
#
# Set the timestamp now, and unlock the experiments table.
#
DBQueryFatal("UPDATE experiments SET expt_locked=now() ".
	     "WHERE eid='$eid' and pid='$pid'");

DBQueryFatal("unlock tables");

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

252 253 254 255 256 257 258 259 260
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
261 262 263
if ($inout eq "modify") {
    $action = "modified";
}
264

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
#
# 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) {
284
    $logname = TBExptCreateLogFile($pid, $eid, "swapexp");
285
    TBExptSetLogFile($pid, $eid, $logname);
286
    TBExptOpenLogFile($pid, $eid);
287 288 289 290 291
    
    if (TBBackGround($logname)) {
	#
	# Parent exits normally
	#
292 293
	print "Experiment $pid/$eid is now being $action.\n".
	    "You will be notified via email when the this is done.\n";
294 295 296 297
	exit(0);
    }
}

298 299 300
#
# Remove old report file since its contents are going to be invalid.
#
301
if ($inout ne "restart" && -e $repfile) {
302 303 304
    unlink("$repfile");
}

Chad Barb's avatar
 
Chad Barb committed
305 306 307 308 309 310 311 312 313
#
# Rerun tbprerun if modifying.
#
if ($inout eq "modify") {
    if (system("$tbdir/tbprerun -m $pid $eid $nsfile") != 0) {
	fatal("tbprerun failed!\n"); 
    }
}

314 315 316 317
#
# Sanity check states in case someone changes something.
#
if ($inout eq "out") {
Chad Barb's avatar
 
Chad Barb committed
318 319 320
    print STDOUT "Running 'tbswap out' with arguments: $pid $eid\n";
    if (system("$tbdir/tbswap out $pid $eid") != 0) {
	fatal("tbswap out failed!\n");
321 322 323 324 325 326 327
    }
    
    $estate = ExpState($pid,$eid);
    if ($estate ne EXPTSTATE_SWAPPED) {
	fatal("Experiment is in the wrong state: $estate\n");
    }
}
328
elsif ($inout eq "in") {
Chad Barb's avatar
 
Chad Barb committed
329 330 331
    print STDOUT "Running 'tbswap in' with arguments: $pid $eid\n";
    if (system("$tbdir/tbswap in $pid $eid") != 0) {
	fatal("tbswap in failed!\n");
332 333 334 335 336 337
    }
    
    $estate = ExpState($pid,$eid);
    if ($estate ne EXPTSTATE_ACTIVE) {
	fatal("Experiment is in the wrong state: $estate\n");
    }
338

339
    system("$tbdir/tbreport -b $pid $eid 2>&1 > $repfile");
Chad Barb's avatar
 
Chad Barb committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
} 
elsif ($inout eq "modify") {
    #
    # If experiment is currently swapped out, no need to do an update 
    # after modifying it.
    #
    if ($estate eq EXPTSTATE_ACTIVE) {
	print STDOUT "Running 'tbswap update' with arguments: $pid $eid\n";
	if (system("$tbdir/tbswap update $pid $eid") != 0) {
	    fatal("tbswap update failed!\n");
	}

	$estate = ExpState($pid, $eid);
	if ($estate ne EXPTSTATE_ACTIVE) {
	    fatal("Experiment is in the wrong state: $estate\n");
	}
	
	system("$tbdir/tbreport -b $pid $eid 2>&1 > $repfile");
Chad Barb's avatar
 
Chad Barb committed
358 359 360 361 362 363

	if ($reboot) {
	    if (system("$tbdir/node_reboot -e $pid,$eid") != 0) {
		fatal("node reboot failed!\n");
	    }
	}
Chad Barb's avatar
 
Chad Barb committed
364
    }
365
}
Chad Barb's avatar
 
Chad Barb committed
366
else { # $inout eq "restart" assumed.
367 368 369 370 371
    print STDOUT "Running tbrestart with arguments: $pid $eid\n";
    if (system("$tbdir/tbrestart $pid $eid") != 0) {
	fatal("tbrestart failed!\n");
    }
}
372

373 374 375 376 377 378 379 380 381 382
#
# 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.
#
383
system("cp -Rfp $workdir/ $userdir/tbdata/");
384

385 386
#
# Must unlock before exit.
387 388
#
TBUnLockExp($pid, $eid);
389

390
print "Done!\n";
391 392 393 394 395 396 397 398

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

Chad Barb's avatar
 
Chad Barb committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
#
# 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";
    }
}

416 417 418 419 420 421 422
#
# Clear the log file so the web page stops spewing. 
#
if (defined($logname)) {
    TBExptCloseLogFile($pid, $eid);
}

423 424 425 426
#
# Send email notification to user.
#
my $message =
427 428 429 430 431 432 433 434 435 436 437 438
    "Experiment $eid in project $pid has been ";

if ($idleswap) {
    $message .= "forcibly swapped out by\n".
	"Testbed Operations because it was idle for too long.\n";
}
else {
    $message .= "$action.\n";
}

$message .=
    "\n".
439 440
    "Appended below is the output. If you have any questions or comments,\n" .
    "please include the output in your message to $TBOPS\n";
441 442

SENDMAIL("$user_name <$user_email>",
443
	 "Experiment $pid/$eid $action",
444
	 $message,
445
	 ($idleswap ? $TBOPS : "$user_name <$user_email>"),
446 447
	 "Cc:  $expt_head_name <$expt_head_email>\n".
	 "Bcc: $TBLOGS",
448
	 (($inout eq "restart") ? ($logname) : ($repfile, $logname)));
449 450 451 452 453 454 455 456 457

exit 0;

sub fatal($)
{
    my($mesg) = $_[0];
    
    print STDOUT $mesg;

Chad Barb's avatar
 
Chad Barb committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
    #
    # 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;
    }

    if ($hosed) { 
	#
	# 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";
	    }
	}
	
	print "Running tbend with arguments: -force $pid $eid\n";
	if (system("$tbdir/tbend -force $pid $eid") != 0) {
	    print "tbend failed!\n";
	}
    }

488 489 490 491 492 493
    #
    # Kill this for convenience later.
    # 
    DBQueryWarn("update experiments set expt_locked=NULL ".
		"WHERE eid='$eid' and pid='$pid'");

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

497 498 499 500
    #
    # In batch mode, exit without sending the email. 
    #
    if ($batch) {
501
	TBUnLockExp($pid, $eid);
502 503 504
	exit(-1);
    }

505 506 507 508 509 510 511
    #
    # Clear the log file so the web page stops spewing. 
    #
    if (defined($logname)) {
	TBExptCloseLogFile($pid, $eid);
    }

512 513 514 515
    #
    # Send a message to the testbed list. Append the logfile.
    #
    SENDMAIL("$user_name <$user_email>",
516
	     "Swap ${inout} Failure: $pid/$eid",
517
	     $mesg,
518
	     ($idleswap ? $TBOPS : "$user_name <$user_email>"),
519
	     "Cc:  $expt_head_name <$expt_head_email>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
520
	     "Cc:  $TBOPS",
521 522
	     ($logname));

Chad Barb's avatar
 
Chad Barb committed
523 524 525 526 527 528 529 530 531 532 533 534 535
    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");
	TBExptDestroy($pid, $eid);        
    }

536 537 538
    exit(-1);
}