tbend.in 2.09 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1
#!/usr/bin/perl -w
Leigh Stoller's avatar
Leigh Stoller committed
2 3 4

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

9
use English;
Leigh Stoller's avatar
Leigh Stoller committed
10 11 12

# tbend

Leigh Stoller's avatar
Leigh Stoller committed
13 14 15
# This is the fourth and final program in the
# tbprerun/tbswapin/tbswapout/.../tbend sequences.  It's purpose is to
# reverse what tbprerun did.  I.e. clear the virt_nodes and virt_lans
16 17 18 19 20 21
# tables.
sub usage()
{
    print STDERR "Usage: $0 [-force] pid eid\n";
    exit(-1);
}
Leigh Stoller's avatar
Leigh Stoller committed
22

23 24 25 26
#
# Configure variables
#
my $TBROOT   = "@prefix@";
Leigh Stoller's avatar
Leigh Stoller committed
27

28
# Untaint the path
Leigh Stoller's avatar
Leigh Stoller committed
29
$ENV{'PATH'} = "/usr/bin:$TBROOT/libexec:$TBROOT/libexec/ns2ir" . 
30
    ":$TBROOT/libexec/vis:$TBROOT/sbin:$TBROOT/bin";
Leigh Stoller's avatar
Leigh Stoller committed
31

32 33 34
#
# Testbed Support libraries
#
35
use lib "@prefix@/lib";
36 37
use libdb;
use libtestbed;
38
require exitonwarn; # exitonwarn isn't really a module, so just require it
Leigh Stoller's avatar
Leigh Stoller committed
39

40 41 42 43 44 45 46 47 48
#
# Turn off line buffering on output
#
$| = 1;

my $force  = 0;
my $errors = 0;
my $state;

Leigh Stoller's avatar
Leigh Stoller committed
49 50
while ($#ARGV > 1) {
    $arg = shift;
51
    if ($arg eq "-force") {
Leigh Stoller's avatar
Leigh Stoller committed
52 53
	$force = 1;
    } else {
54
	usage();
Leigh Stoller's avatar
Leigh Stoller committed
55
    }
Leigh Stoller's avatar
Leigh Stoller committed
56
}
Leigh Stoller's avatar
Leigh Stoller committed
57
if ($#ARGV < 1) {
58
    usage();
Leigh Stoller's avatar
Leigh Stoller committed
59
}
60
my ($pid,$eid) = @ARGV;
Leigh Stoller's avatar
Leigh Stoller committed
61

62
print "Beginning cleanup for $pid/$eid. " . TBTimeStamp() . "\n";
Leigh Stoller's avatar
Leigh Stoller committed
63

64 65
if (! ($state = ExpState($pid, $eid))) {
    print STDERR "*** No such experiment $pid/$eid\n";
Leigh Stoller's avatar
Leigh Stoller committed
66 67
    exit(1);
}
68 69 70
if (! $force) {
    if ($state eq EXPTSTATE_ACTIVE) {
	print STDERR "*** Experiment is running. Must be swapped out.\n";
Leigh Stoller's avatar
Leigh Stoller committed
71
	exit(1);
72 73 74
    }
    elsif ($state ne EXPTSTATE_SWAPPED) {
	print STDERR "*** Experiment is in the wrong state: $state.\n";
Leigh Stoller's avatar
Leigh Stoller committed
75 76 77
	exit(1);
    }
}
78 79
elsif ($state ne EXPTSTATE_SWAPPED) {
    print STDERR "*** WARNING: Ignoring improper state: $state.\n";
Leigh Stoller's avatar
Leigh Stoller committed
80
}
81 82 83
if (! SetExpState($pid, $eid, EXPTSTATE_TERMINATING)) {
    print STDERR "*** Failed to set intermediate experiment state.\n";
    exit(1);
Leigh Stoller's avatar
Leigh Stoller committed
84
}
85 86

print "Clearing out virtual state.\n";
87
$errors += TBExptRemoveVirtualState( $pid, $eid );
Leigh Stoller's avatar
Leigh Stoller committed
88

89
# This will kill a running prerender.
90
print "Removing visualization data...\n";
91
system("prerender -r $pid $eid");
92

Leigh Stoller's avatar
Leigh Stoller committed
93
if ($errors == 0) {
94 95
    SetExpState($pid, $eid, EXPTSTATE_TERMINATED) or
	$errors++;
Leigh Stoller's avatar
Leigh Stoller committed
96 97
}

98
print "Cleanup finished! " . TBTimeStamp() . "\n";
Leigh Stoller's avatar
Leigh Stoller committed
99

Leigh Stoller's avatar
Leigh Stoller committed
100 101
# We exit with whether we had errors or not.
exit $errors;
102