All new accounts created on Gitlab now require administrator approval. If you invite any collaborators, please let Flux staff know so they can approve the accounts.

swapexp.in 8.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#!/usr/bin/perl -wT
use English;
use Getopt::Std;

#
# This gets invoked from the Web interface. Swap an experiment in or out.
#
# Note about exit value. -1 means error. 0 means backgrounded. 1 means
# somthing else. The web page uses this to decide what kind of message
# to give the user. 
#
sub usage()
{
    print STDOUT "Usage: swapexp [-b] <-s in | out> <pid> <eid>\n";
    exit(-1);
}
my  $optlist = "bs:";

#
# Configure variables
#
my $TB     = "@prefix@";
my $DBNAME = "@TBDBNAME@";
my $TBOPS  = "@TBOPSEMAIL@";
my $TBLOGS = "@TBLOGSEMAIL@";

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

my $tbdir    = "$TB/bin/";
35
my $tbdata   = "tbdata";
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
my $batch    = 0;
my $inout;
my $logname;
my $dbuid;
my $user_name;
my $user_email;
my @row;
    
#
# Untaint the path
# 
$ENV{'PATH'} = '/bin:/usr/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

55 56 57 58 59 60 61
#
# 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);

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 2) {
    usage();
}
my $pid   = $ARGV[0];
my $eid   = $ARGV[1];
if (defined($options{"b"})) {
    $batch = $options{"b"};
}
if (defined($options{"s"})) {
    $inout = $options{"s"};

    if ($inout ne "out" && $inout ne "in") {
	usage();
    }
}
else {
    usage();
}

#
# 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");
}
104
my $repfile = "$eid.report";
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

#
# 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");
}

#
# Verify that this person is allowed to end the experiment.
# 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".
	"    You do not have permission to end this experiment!\n");
}

#
# 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'};

155 156 157 158 159
if (! chdir("$expt_path/$tbdata")) {
    die("*** $0:\n".
	"    Could not chdir to $expt_path/$tbdata: $!\n");
}

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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 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
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".
	"    Batch experiments cannot be swapped yet!");
}

#
# Okay, check state. We do not allow swapping to start when the
# 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!");
}

#
# 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.
#

#
# 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) {
    $logname = `mktemp /tmp/swap-$pid-$eid.XXXXXX`;

    if ($logname =~ /^([-\@\w.\/]+)$/) {
	$logname = $1;
    }
    else {
	die("Bad data in logfile name: $logname");
    }
240
    TBExptSetLogFile($pid, $eid, $logname);
241 242 243 244 245 246 247 248 249 250 251 252 253
    
    if (TBBackGround($logname)) {
	#
	# Parent exits normally
	#
	print STDOUT
	    "Experiment $pid/$eid is now swapping $inout.\n".
	    "You will be notified via email when the experiment has ".
	    "finished swapping.\n";
	exit(0);
    }
}

254 255 256 257 258 259 260
#
# Remove old report file since its contents are going to be invalid.
#
if (-e $repfile) {
    unlink("$repfile");
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
#
# Sanity check states in case someone changes something.
#
if ($inout eq "out") {
    print STDOUT "Running tbswapout with arguments: $pid $eid\n";
    if (system("$tbdir/tbswapout $pid $eid") != 0) {
	fatal("tbswapout failed!\n");
    }
    
    $estate = ExpState($pid,$eid);
    if ($estate ne EXPTSTATE_SWAPPED) {
	fatal("Experiment is in the wrong state: $estate\n");
    }
}
else {
    print STDOUT "Running tbswapin with arguments: $pid $eid\n";
    if (system("$tbdir/tbswapin $pid $eid") != 0) {
	fatal("tbswapin failed!\n");
    }
    
    $estate = ExpState($pid,$eid);
    if ($estate ne EXPTSTATE_ACTIVE) {
	fatal("Experiment is in the wrong state: $estate\n");
    }
285

286
    system("$tbdir/tbreport -b $pid $eid 2>&1 > $repfile");
287 288 289 290
}

#
# Must unlock before exit.
291 292
#
TBUnLockExp($pid, $eid);
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

print "Swap Success\n";

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

#
# Send email notification to user.
#
my $message =
    "Experiment `$eid' in project `$pid' has been swapped $inout.\n\n" .
    "Appended below is the output of the experiment swap${inout}. If you\n" .
    "have any questions or comments, please include the output below\n" .
    "in your message to $TBOPS\n";

SENDMAIL("$user_name <$user_email>",
313
	 "Experiment $pid/$eid Swapped $inout",
314 315 316 317
	 $message,
	 "$user_name <$user_email>",
	 "Cc:  $expt_head_name <$expt_head_email>\n".
	 "Bcc: $TBLOGS",
318
	 ($repfile, $logname));
319

320
TBExptClearLogFile($pid, $eid);
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
if (defined($logname)) {
    unlink("$logname");
}
exit 0;

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

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

    #
    # In batch mode, exit without sending the email. 
    #
    if ($batch) {
342 343
	TBExptClearLogFile($pid, $eid);
	TBUnLockExp($pid, $eid);
344 345 346 347 348 349 350
	exit(-1);
    }

    #
    # Send a message to the testbed list. Append the logfile.
    #
    SENDMAIL("$user_name <$user_email>",
351
	     "Swap${inout} Failure: $pid/$eid",
352 353 354
	     $mesg,
	     "$user_name <$user_email>",
	     "Cc:  $expt_head_name <$expt_head_email>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
355
	     "Cc:  $TBOPS",
356 357
	     ($logname));

358
    TBExptClearLogFile($pid, $eid);
359 360 361 362 363 364
    if (defined($logname)) {
	unlink("$logname");
    }
    exit(-1);
}