swapexp.in 24 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-2004 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] [-e] ".
20
	"<-s in | out | restart | modify | pause> <pid> <eid> [<nsfile>]\n";
21
22
    exit(-1);
}
23
my  $optlist = "biafres:";
24

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#
# Exit codes are important; they tell the web page what has happened so
# it can say something useful to the user. Fatal errors are mostly done
# with die(), but expected errors use this routine. At some point we will
# use the DB to communicate the actual error.
#
# $status < 0 - Fatal error. Something went wrong we did not expect.
# $status = 0 - Termination is proceeding in the background. Notified later.
# $status > 0 - Expected error. User not allowed for some reason. 
# 
sub ExitWithStatus($$)
{
    my ($status, $message) = @_;
    
    if ($status < 0) {
	die("*** $0:\n".
	    "    $message\n");
    }
    else {
	print STDERR "$message\n";
    }
    exit($status);
}

49
50
51
52
53
54
#
# Configure variables
#
my $TB     = "@prefix@";
my $TBOPS  = "@TBOPSEMAIL@";
my $TBLOGS = "@TBLOGSEMAIL@";
55
my $TBINFO = "$TB/expinfo";
56
my $TBDOCBASE = "@TBDOCBASE@";
57
58
59
60
61
62
63
64
65

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

my $tbdir    = "$TB/bin/";
66
my $tbdata   = "tbdata";
67
my $batch    = 0;
68
my $idleswap = 0;
69
70
my $autoswap = 0;
my $force    = 0;
Chad Barb's avatar
Chad Barb committed
71
my $reboot   = 0;
72
my $eventsys_restart   = 0;
73
my $errorstat= -1;
74
my $modifyHosed = 0;
Chad Barb's avatar
   
Chad Barb committed
75

76
77
78
79
80
my $inout;
my $logname;
my $dbuid;
my $user_name;
my $user_email;
81
my @allnodes;
82
my @row;
83
my $action;
84
my $nextswapstate;
85
my $termswapstate;
Chad Barb's avatar
   
Chad Barb committed
86

87
88
89
#
# Untaint the path
# 
90
$ENV{'PATH'} = "/bin:/usr/bin:$TB/libexec/vis";
91
92
93
94
95
96
97
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

98
99
100
101
102
103
104
#
# 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);

105
106
107
108
109
110
111
112
#
# Parse command arguments. Once we return from getopts, all that should
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
113
114
115
if (defined($options{"i"})) {
    $idleswap = 1;
}
116
117
118
119
120
121
if (defined($options{"a"})) {
    $autoswap = 1;
}
if (defined($options{"f"})) {
    $force = 1;
}
122
123
124
if (defined($options{"b"})) {
    $batch = 1;
}
Chad Barb's avatar
   
Chad Barb committed
125
126
127
if (defined($options{"r"})) {
    $reboot = 1;
}
128
129
130
if (defined($options{"e"})) {
    $eventsys_restart = 1;
}
131
132
133
if (defined($options{"s"})) {
    $inout = $options{"s"};

Chad Barb's avatar
Chad Barb committed
134
135
136
    if ($inout ne "out"     &&
	$inout ne "in"      &&
	$inout ne "restart" &&
137
	$inout ne "pause"   &&
Chad Barb's avatar
   
Chad Barb committed
138
	$inout ne "modify") {
139
140
141
142
143
144
145
	usage();
    }
}
else {
    usage();
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
146
if ($inout ne "modify" && @ARGV != 2) {
Chad Barb's avatar
   
Chad Barb committed
147
148
    usage();
}
149
150
151
152
153
if ($eventsys_restart && $inout ne "modify") {
    print STDOUT "Usage: swapexp: -e (eventsys_restart) can be used ".
                 "only with -s modify\n";
    usage();
}
Chad Barb's avatar
   
Chad Barb committed
154
155
156
my $pid   = $ARGV[0];
my $eid   = $ARGV[1];

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#
# 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");
}
172
my $repfile = "$eid.report";
173
174
my $workdir = TBExptWorkDir($pid, $eid);
my $userdir = TBExptUserDir($pid, $eid);
175
176
177
my $tempnsfile;
my $modnsfile;

Leigh B. Stoller's avatar
Leigh B. Stoller committed
178
if ($inout eq "modify" && @ARGV > 2) {
179
180
181
182
183
184
185
    $tempnsfile = $ARGV[2];

    #
    # Untaint nsfile argument; Allow slash.
    #
    if ($tempnsfile =~ /^([-\w.\/]+)$/) {
	$tempnsfile = $1;
186
187
    }
    else {
188
189
	die("Tainted nsfile name: $tempnsfile");
    }
190
191
192
193
    if (! -f $tempnsfile || -z $tempnsfile) {
	die("*** $0:\n".
	    "    $tempnsfile does not look like an NS file!\n");
    }
194
195
    $modnsfile = "$eid-modify.ns";
}
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213

#
# 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
214
# Verify that this person can muck with the experiment.
215
216
217
218
219
# 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
220
	"    You do not have permission to swap or modify this experiment!\n");
221
222
}

223
224
225
226
# Must do this before lock tables!
# idleswap is in minutes, threshold is in hours
$idleswap_time = 60 * TBGetSiteVar("idle/threshold");

227
228
229
230
231
#
# 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
232
# the tb scripts.
233
234
235
236
237
238
239
240
241
242
243
244
245
#
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'};
246
my $batchstate      = $hashrow{'batchstate'};
247
my $expt_path       = $hashrow{'path'};
248
my $expt_locked     = $hashrow{'expt_locked'};
249
my $isbatchexpt     = $hashrow{'batchmode'};
250
my $canceled        = $hashrow{'canceled'};
251
252
253
254
255
256
257
258
259
260
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;
261

262
263
if ($inout ne "out") {
    # I'm going to update this below, so fix the value before I use it.
264
    $idleswap_time = min($idleswaptime * 60, $idleswap_time);
265
266
267
    $idleswaptime = $idleswap_time / 60.0;
}

268
269
my $swapsettings = 
  "Idle-Swap:   $idleswapstr".
270
  ($idleswapbit ? ", at $idleswaptime hours\n" : " (Reason: $noidleswap)\n").
271
272
  "Auto-Swap:   $autoswapstr".
  ($autoswapbit ? ", at $autoswaptime hours\n" : "\n");
273

274
if (! chdir($workdir)) {
275
    die("*** $0:\n".
276
	"    Could not chdir to $workdir: $!\n");
277
278
}

279
#
280
281
282
# This script is called from the batch daemon.
# 
if ($batch) {
283
    #
284
285
286
    # Sanity Check. If called from the daemon, must already be locked,
    # must be a batch experiment, and must be in proper state for the
    # operation requested. 
287
    #
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    die("*** $0:\n".
	"    Experiment $pid/$eid is supposed to be a batch experiment!\n")
	if (!$isbatchexpt);
    
    die("*** $0:\n".
	"    Batch experiment $pid/$eid should be locked!\n")
	if (!defined($expt_locked) ||
	    $batchstate ne BATCHSTATE_LOCKED());

    if ($inout eq "in") {
	die("*** $0:\n".
	    "    Batch experiment $pid/$eid is not in the proper state!\n".
	    "    Currently $estate, but should be QUEUED.\n")
	    if ($estate ne EXPTSTATE_QUEUED);
	
	die("*** $0:\n".
	    "    Batch experiment $pid/$eid has been canceled! Aborting.\n")
	    if ($canceled);
    }
    elsif ($inout eq "out") {
	die("*** $0:\n".
	    "    Batch experiment $pid/$eid is not in the proper state!\n".
	    "    Currently $estate, but should be ACTIVE.\n")
	    if ($estate ne EXPTSTATE_ACTIVE);
312
313
    }
    else {
314
315
316
317
318
319
	die("*** $0:\n".
	    "    Improper request from batch daemon for $pid/$eid!\n");
    }
}
else {
    if ($isbatchexpt) {
320
321
322
323
	#
	# User is requesting that a batch either be injected or paused.
	# Sanity check the state, but otherwise let the batch daemon
	# handle it.
324
325
	#
	ExitWithStatus(1, "Batch experiment $pid/$eid is still canceling!")
326
	    if ($canceled);
327

328
	if ($inout eq "in") {
329
	    ExitWithStatus(1,
330
331
332
333
			   "Batch experiment $pid/$eid must be SWAPPED to\n".
			   "QUEUE. Currently $estate.")
		if ($estate ne EXPTSTATE_SWAPPED);
	    SetExpState($pid, $eid, EXPTSTATE_QUEUED);
334
335
	}
	elsif ($inout eq "out") {
336
	    ExitWithStatus(1,
337
338
339
340
			   "Batch experiment $pid/$eid must be ACTIVE or\n".
			   "ACTIVATING to swap out. Currently $estate.")
		if ($estate ne EXPTSTATE_ACTIVE &&
		    $estate ne EXPTSTATE_ACTIVATING);
341
342
343
344
345

	    #
	    # Since the batch daemon has control, all we can do is set
	    # the cancel bit.
	    # 
346
	    TBSetCancelFlag($pid, $eid, EXPTCANCEL_SWAP);
347
348
	}
	elsif ($inout eq "pause") {
349
	    ExitWithStatus(1,
350
351
352
			   "Batch experiment $pid/$eid must be QUEUED to\n".
			   "DEQUEUE. Currently $estate.")
		if ($estate ne EXPTSTATE_QUEUED);
353
354

	    #
355
356
357
358
	    # XXX. The batch daemon might already have the experiment, but
	    # not have shipped it off to startexp. Change the state
	    # anyway. The error will be noticed later when startexp dies,
	    # and the batch daemon gets the error back. This sucks.
359
	    #
360
	    SetExpState($pid, $eid, EXPTSTATE_SWAPPED);
361
	}
362
	elsif ($inout eq "modify") {
363
	    ExitWithStatus(1,
364
365
366
367
368
369
			   "Batch experiment $pid/$eid must be SWAPPED or\n".
			   "ACTIVE to modify. Currently $estate.")
		if (($estate ne EXPTSTATE_SWAPPED &&
		     $estate ne EXPTSTATE_ACTIVATING) ||
		    $batchstate != BATCHSTATE_UNLOCKED());

370
	    #
371
	    # Otherwise, proceed with the modify. The experiment will be
372
373
	    # locked below, and so it cannot be injected or otherwise messed
	    # with since its state is going to be changed before we unlock
374
375
376
377
	    # the experiments table. The batch daemon will leave it alone
	    # until the modify is done. If the modify fails and cannot recover
	    # it is going to get swapped out; that is okay since the batch
	    # daemon does not keep state internally. 
378
	    #
379
380
	    goto doit;
	}
381
382
	else {
	    die("*** $0:\n",
383
		"    Operation $inout not allowed on a batch experiment!\n");
384
	}
385
386
	ExitWithStatus(0, 
		       "Batch experiment $pid/$eid state has been changed.\n");
387
      doit:
388
    }
389
390
391
392
393
394
395
396
397
398
    else {
	#
	# If the cancel flag is set, then user must wait for that to
	# clear before we can do anything else.
	#
	ExitWithStatus(1,
		       "Experiment $pid/$eid has its cancel flag set!.\n".
		       "You must wait for that to clear before you can swap\n".
		       "or modify the experiment.\n")
	    if ($canceled);
399

400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
	#
	# Check the state for the various operations.
	#
	if (!$force) {
	  SWITCH: for ($inout) {
	      /^in$/i && do {
		  if ($estate ne EXPTSTATE_SWAPPED()) {
		      ExitWithStatus(1,
				     "Experiment $pid/$eid is not swapped out!");
		  }
		  last SWITCH;
	      };
	      /^out$/i && do {
		  if ($estate ne EXPTSTATE_ACTIVE() &&
		      $estate ne EXPTSTATE_ACTIVATING()) {
		      ExitWithStatus(1,
				     "Experiment $pid/$eid is not swapped in ".
				     "or activating!\n");
		  }
		  
		  if ($estate eq EXPTSTATE_ACTIVATING()) {
		      #
		      # All we can do is set the cancel flag and hope that
		      # it gets noticed. We do not wait. 
		      # 
		      TBSetCancelFlag($pid, $eid, EXPTCANCEL_SWAP);
		      
		      ExitWithStatus(0,
				     "Experiment $pid/$eid swapin has been  ".
				     "marked for cancelation.\n".
				     "You will receive email when the original ".
				     "swap request has finished.");
		  }
		  last SWITCH;
	      };
	      /^restart$/i && do {
		  if ($estate ne EXPTSTATE_ACTIVE()) {
		      ExitWithStatus(1,
				     "Experiment $pid/$eid is not swapped in!");
		  }
		  last SWITCH;
	      };
	      /^modify$/i && do {
		  if ($estate ne EXPTSTATE_ACTIVE() &&
		      $estate ne EXPTSTATE_SWAPPED()) {
		      ExitWithStatus(1,
				     "Experiment $pid/$eid must be ACTIVE or\n".
				     "SWAPPED to modify!\n");
		  }
		  last SWITCH;
	      };
	      die("*** $0:\n".
		  "    Missing state check for action: $action\n");
	  }
454
455
	}
    }
456
457
}

458
459
460
461
462
463
464
#
# Determine the temporary and next state for experiment. If the experiment
# is a batch experiment, then the next state is actually handled by the
# batch daemon, but we still have to deal with the temporary state. 
#
SWITCH: for ($inout) {
    /^in$/i && do {
465
	$nextswapstate = EXPTSTATE_ACTIVATING();
466
467
468
	last SWITCH;
    };
    /^out$/i && do {
469
	$nextswapstate = EXPTSTATE_SWAPPING();
470
471
472
	last SWITCH;
    };
    /^restart$/i && do {
473
	$nextswapstate = EXPTSTATE_RESTARTING();
474
475
476
	last SWITCH;
    };
    /^modify$/i && do {
477
478
	$nextswapstate = (($estate eq EXPTSTATE_SWAPPED()) ?
			  EXPTSTATE_MODIFY_PARSE() : EXPTSTATE_MODIFY_REPARSE());
479
480
	last SWITCH;
    };
481
    die("*** $0:\n".
482
	"    Missing state check for action: $action\n");
483
}
484
485
 
# Update idleswap_timeout to whatever the current value is.
486
if ($inout ne "out") {
487
488
489
    DBQueryFatal("update experiments set idleswap_timeout='$idleswap_time' ".
		 "where eid='$eid' and pid='$pid'");
}
490

491
492
493
494
495
496
497
#
# On a failure, we go back to this swapstate. Might be modified below.
# 
$termswapstate = $estate;

# Lock the record, set the nextstate, and unlock the table.
TBLockExp($pid, $eid, $nextswapstate);
498
499
500
501
DBQueryFatal("unlock tables");

#
# XXX - At this point a failure is going to leave things in an
502
503
504
505
# inconsistent state. Be sure to call fatal() only since we are
# going into the background, and we have to send email since no
# one is going to see printed error messages (output goes into the
# log file, which will be sent along in the email). 
506
507
#

508
509
510
511
512
513
514
515
516
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
517
518
519
if ($inout eq "modify") {
    $action = "modified";
}
520

521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
#
# 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;
}

536
537
538
#
# Before going to background, we have to copy out the NS file!
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
539
if ($inout eq "modify" && defined($modnsfile)) {
540
541
542
543
544
545
546
547
    unlink($modnsfile);
    if (system("/bin/cp", "$tempnsfile", "$modnsfile")) {
	die("*** $0:\n".
	    "    Could not copy $tempnsfile to $modnsfile");
    }
    chmod(0664, "$modnsfile");
}

548
549
550
551
#
# If not in batch mode, go into the background. Parent exits.
#
if (! $batch) {
552
    $logname = TBExptCreateLogFile($pid, $eid, "swapexp");
553
    TBExptSetLogFile($pid, $eid, $logname);
554
    TBExptOpenLogFile($pid, $eid);
Chad Barb's avatar
Chad Barb committed
555

556
557
558
559
    if (TBBackGround($logname)) {
	#
	# Parent exits normally
	#
560
561
	print "Experiment $pid/$eid is now being $action.\n".
	    "You will be notified via email when the this is done.\n";
562
563
564
565
	exit(0);
    }
}

566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#
# Gather stats; start clock ticking
#
if ($inout eq "in") {
    GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPIN, 0,
		    TBDB_STATS_FLAGS_START);
}
elsif ($inout eq "out") {
    GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPOUT, 0,
		    TBDB_STATS_FLAGS_START);
}
elsif ($inout eq "modify") {
    GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPMODIFY, 0,
		    TBDB_STATS_FLAGS_START);
}

582
583
584
#
# Remove old report file since its contents are going to be invalid.
#
585
if ($inout ne "restart" && -e $repfile) {
586
587
588
    unlink("$repfile");
}

589
590
591
592
#
# Sanity check states in case someone changes something.
#
if ($inout eq "out") {
593
594
595
596
    my $optarg = (($force || $idleswap) ? "-force" : "");
    
    print STDOUT "Running 'tbswap out $optarg $pid $eid'\n";
    if (system("$tbdir/tbswap out $optarg $pid $eid") != 0) {
597
	$errorstat = $? >> 8;
598
	fatal("tbswap out failed!");
599
    }
600
    SetExpState($pid, $eid, EXPTSTATE_SWAPPED);
601
}
602
elsif ($inout eq "in") {
603
    print STDOUT "Running 'tbswap in $pid $eid'\n";
Chad Barb's avatar
   
Chad Barb committed
604
    if (system("$tbdir/tbswap in $pid $eid") != 0) {
605
	$errorstat = $? >> 8;
606
	fatal("tbswap in failed!");
607
    }
608
    SetExpState($pid, $eid, EXPTSTATE_ACTIVE);
609

610
    system("$tbdir/tbreport -b $pid $eid 2>&1 > $repfile");
Chad Barb's avatar
Chad Barb committed
611
}
Chad Barb's avatar
   
Chad Barb committed
612
elsif ($inout eq "modify") {
Chad Barb's avatar
Chad Barb committed
613
    my $modifyError = "";
614
    my $oldstate    = $estate;
Chad Barb's avatar
Chad Barb committed
615

616
617
618
    GatherSwapStats($pid, $eid, $dbuid,
		    TBDB_STATS_SWAPMODIFY, 0, TBDB_STATS_FLAGS_PREMODIFY);

Chad Barb's avatar
Chad Barb committed
619
    print "Backing up old experiment state ... " . TBTimeStamp() . "\n";
620
    if (TBExptBackupVirtualState($pid, $eid)) {
621
	fatal("Could not backup experiment state; cannot safely continue!");
Chad Barb's avatar
Chad Barb committed
622
623
624
    }

    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
625
626
    # Rerun tbprerun if modifying, but only if new NS file provided.
    # Yep, we allow reswap without changing the NS file. For Shashi and SIM. 
Chad Barb's avatar
Chad Barb committed
627
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
628
629
630
631
632
    if (defined($modnsfile)) {
	print STDOUT "Running 'tbprerun $pid $eid $modnsfile'\n";
	if (system("$tbdir/tbprerun $pid $eid $modnsfile") != 0) {
	    $modifyError = "tbprerun failed!";
	}
Chad Barb's avatar
Chad Barb committed
633
634
    }

Chad Barb's avatar
   
Chad Barb committed
635
    #
636
    # Our next state depends on whether the experiment was active or swapped.
Chad Barb's avatar
   
Chad Barb committed
637
    #
638
639
640
    if (! $modifyError) {
	if ($estate eq EXPTSTATE_SWAPPED) {
	    SetExpState($pid, $eid, EXPTSTATE_SWAPPED);
Chad Barb's avatar
   
Chad Barb committed
641
	}
642
643
644
645
	else {
	    SetExpState($pid, $eid, EXPTSTATE_MODIFY_RESWAP);
	    
	    my $optarg = ($reboot ? "-reboot" : "");
646
	    $optarg .= ($eventsys_restart ? " -eventsys_restart" : "");
647
648
649
650
651
652

	    print STDOUT "Running 'tbswap update $optarg $pid $eid'\n";
	    if (system("$tbdir/tbswap update $optarg $pid $eid") != 0) {
		$errorstat = $? >> 8;
		$modifyError = "tbswap update failed!";
	    }
Chad Barb's avatar
   
Chad Barb committed
653

654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
	    #
	    # See what tbswap did. It might have swapped it out if there
	    # was an error. 
	    # 
	    if (! $modifyError) {
		SetExpState($pid, $eid, EXPTSTATE_ACTIVE);
		$estate = EXPTSTATE_ACTIVE;
	    }
	    elsif ($errorstat & 0x40) {
		#
		# Icky. Magic return code that says tbswap swapped it out.
		# We do not want tbswap to muck with states anymore, so
		# need to know what it did. At some point we should clean
		# up the exit reporting! Anyway, fatal() needs to know the
		# the right state to go back to (no longer ACTIVE).
		#
		$estate = EXPTSTATE_SWAPPED;
		$termswapstate = EXPTSTATE_SWAPPED;
                # Old accounting info.
		TBSetExpSwapTime($pid, $eid);
Chad Barb's avatar
   
Chad Barb committed
674
	    }
675
	}
Chad Barb's avatar
Chad Barb committed
676
677
678
    }

    if ($modifyError) {
679
	print STDOUT "Modify Error: $modifyError\n";
Chad Barb's avatar
Chad Barb committed
680
	print STDOUT "Recovering experiment state...\n";
681
	
682
683
	# Must deal with the prerender explicitly since it runs background.
	system("prerender -r $pid $eid");
684
	TBExptRemoveVirtualState($pid, $eid);
685
	
686
	if (TBExptRestoreVirtualState($pid, $eid) == 0) {
687
688
	    # Must deal with the prerender explicitly since it runs background.
	    system("prerender -t $pid $eid");
689
690
691
692
693
	    fatal("Update aborted; old state restored.");
	}
	else {
	    $modifyHosed = 1;
	    fatal("Experiment state could not be restored!");
Chad Barb's avatar
Chad Barb committed
694
	}
Chad Barb's avatar
   
Chad Barb committed
695
    }
696
    
697
    TBExptClearBackupState($pid, $eid);
698
    system("$tbdir/tbreport -b $pid $eid 2>&1 > $repfile");
699
}
Chad Barb's avatar
   
Chad Barb committed
700
else { # $inout eq "restart" assumed.
701
    print STDOUT "Running 'tbrestart $pid $eid'\n";
702
    if (system("$tbdir/tbrestart $pid $eid") != 0) {
703
	fatal("tbrestart failed!");
704
    }
705
    SetExpState($pid, $eid, EXPTSTATE_ACTIVE);
706
}
707

708
709
710
711
712
713
714
715
716
717
#
# 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.
#
718
system("cp -Rfp $workdir/ $userdir/tbdata/");
719

720
721
722
723
#
# Gather stats. 
#
if ($inout eq "in") {
724
    GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPIN, 0);
725
726
}
elsif ($inout eq "out") {
727
    GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPOUT, 0,
728
		    ($idleswap ? TBDB_STATS_FLAGS_IDLESWAP() : 0));
729
730
}
elsif ($inout eq "modify") {
731
    GatherSwapStats($pid, $eid, $dbuid, TBDB_STATS_SWAPMODIFY, 0);
732
}
733
734
# Old accounting info.
TBSetExpSwapTime($pid, $eid);
735

736
737
738
739
740
741
#
# Set the swapper uid on success only, and *after* gathering swap stats!
#
TBExptSetSwapUID($pid, $eid, $dbuid);

#
742
# In batch mode, just exit without sending email or unlocking. The
743
# batch daemon will take care of that.
744
745
746
747
748
#
if ($batch) {
    exit(0);
}

749
750
751
752
753
754
755
#
# Clear the log file so the web page stops spewing. 
#
if (defined($logname)) {
    TBExptCloseLogFile($pid, $eid);
}

756
757
758
#
# Must unlock before exit.
#
759
TBUnLockExp($pid, $eid);
760
761
762
763
764

#
# Since the swap completed, clear the cancel flag. This must be done
# after we change the experiment state (above). 
#
765
TBSetCancelFlag($pid, $eid, EXPTCANCEL_CLEAR);
766
767
768

print "Swap Success!\n";

769
770
771
772
#
# Send email notification to user.
#
my $message =
773
774
    "Experiment $eid in project $pid has been ";

775
if ($inout eq "out" && ($idleswap || $autoswap || $force) ) {
776
    $message .= "forcibly swapped out by\nEmulab";
777
778
779
780
781
    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) {
782
783
	$message .= " because it exceeded its Maximum Duration.\n".
	  "(See also the Max. Duration info in \n".
784
785
786
787
788
	  "$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";
    }
789
790
791
792
793
}
else {
    $message .= "$action.\n";
}

794
795
796
797
798
if ($inout eq "in") {
    # Add the swap settings...
    $message .="\nCurrent swap settings:\n$swapsettings";
}

799
800
$message .=
    "\n".
801
802
    "Appended below is the output. If you have any questions or comments,\n" .
    "please include the output in your message to $TBOPS\n";
803
804

SENDMAIL("$user_name <$user_email>",
805
	 "Experiment $pid/$eid \u$action",
806
	 $message,
807
	 ($idleswap ? $TBOPS : "$user_name <$user_email>"),
808
809
	 "Cc:  $expt_head_name <$expt_head_email>\n".
	 "Bcc: $TBLOGS",
810
811
	 (($inout eq "restart") ? ($logname) :
	  (($repfile, $logname), (defined($modnsfile) ? ($modnsfile) : ()))));
812
813
814
815
816
817

exit 0;

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

819
820
    print STDOUT "*** $0:\n".
	         "    $mesg\n";
821

822
823
824
825
826
827
828
829
830
831
832
833
834
    #
    # 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);
    }

835
836
837
838
839
840
841
    #
    # Clear backup state since not needed anymore; experiment is toast. 
    # 
    if ($inout eq "modify") {
	TBExptClearBackupState($pid, $eid);
    }

Chad Barb's avatar
   
Chad Barb committed
842
    #
843
    # If hosed, we entirely terminate the experiment.
Chad Barb's avatar
   
Chad Barb committed
844
    #
845
    if ($modifyHosed) {
Chad Barb's avatar
   
Chad Barb committed
846
	#
847
	# Note: $estate is indeed still set appropriately!
Chad Barb's avatar
   
Chad Barb committed
848
849
	#
	if ($estate eq EXPTSTATE_ACTIVE) {
850
	    print "Running 'tbswap out -force $pid $eid'\n";
Chad Barb's avatar
   
Chad Barb committed
851
852
853
854
	    if (system("$tbdir/tbswap out -force $pid $eid") != 0) {
		print "tbswap out failed!\n";
	    }
	}
Chad Barb's avatar
Chad Barb committed
855

856
	print "Running 'tbend -force $pid $eid'\n";
Chad Barb's avatar
   
Chad Barb committed
857
858
859
	if (system("$tbdir/tbend -force $pid $eid") != 0) {
	    print "tbend failed!\n";
	}
860
	# Must override since we are so badly hosed. 
861
	$termswapstate = EXPTSTATE_TERMINATED;
Chad Barb's avatar
   
Chad Barb committed
862
863
    }

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

867
868
869
    # Set proper state, which is typically the way we came in.
    SetExpState($pid, $eid, $termswapstate);

870
    #
871
    # In batch mode, exit without sending the email or unlocking. The
872
    # batch daemon will take care of that.
873
874
    #
    if ($batch) {
875
	exit($errorstat);
876
877
    }

878
    #
Chad Barb's avatar
Chad Barb committed
879
    # Clear the log file so the web page stops spewing.
880
881
882
883
884
    #
    if (defined($logname)) {
	TBExptCloseLogFile($pid, $eid);
    }

885
886
    # Unlock and reset state to its terminal value.
    TBUnLockExp($pid, $eid);
887
888
889
890
891

    #
    # Clear the cancel flag now that the operation is complete. Must be done
    # after we change the experiment state (above).
    #
892
    TBSetCancelFlag($pid, $eid, EXPTCANCEL_CLEAR);
893

894
895
896
897
    #
    # Send a message to the testbed list. Append the logfile.
    #
    SENDMAIL("$user_name <$user_email>",
898
	     "Swap ${inout} Failure: $pid/$eid",
899
	     $mesg,
900
	     ($idleswap ? $TBOPS : "$user_name <$user_email>"),
901
	     "Cc:  $expt_head_name <$expt_head_email>\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
902
	     "Cc:  $TBOPS",
903
	     (($logname), (defined($modnsfile) ? ($modnsfile) : ())));
904

Leigh B. Stoller's avatar
Leigh B. Stoller committed
905
    if ($modifyHosed) {
Chad Barb's avatar
   
Chad Barb committed
906
907
908
909
910
911
912
913
914
	#
	# 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
915
	TBExptDestroy($pid, $eid);
Chad Barb's avatar
   
Chad Barb committed
916
917
    }

918
    exit($errorstat);
919
}