tbend.in 2.7 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1
#!/usr/bin/perl -w
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4 5 6 7 8

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#

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

# tbend

Leigh B. Stoller's avatar
Leigh B. 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 B. Stoller's avatar
Leigh B. Stoller committed
22

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

28
# Untaint the path
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29 30
$ENV{'PATH'} = "/usr/bin:$TBROOT/libexec:$TBROOT/libexec/ns2ir" . 
    ":$TBROOT/sbin:$TBROOT/bin";
Leigh B. Stoller's avatar
Leigh B. 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 B. Stoller's avatar
Leigh B. 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 B. Stoller's avatar
Leigh B. Stoller committed
49 50
while ($#ARGV > 1) {
    $arg = shift;
51
    if ($arg eq "-force") {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
52 53
	$force = 1;
    } else {
54
	usage();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
55
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
56
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
57
if ($#ARGV < 1) {
58
    usage();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
59
}
60
my ($pid,$eid) = @ARGV;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
61

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

64 65
if (! ($state = ExpState($pid, $eid))) {
    print STDERR "*** No such experiment $pid/$eid\n";
Leigh B. Stoller's avatar
Leigh B. 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 B. Stoller's avatar
Leigh B. Stoller committed
71
	exit(1);
72 73 74
    }
    elsif ($state ne EXPTSTATE_SWAPPED) {
	print STDERR "*** Experiment is in the wrong state: $state.\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
75 76 77
	exit(1);
    }
}
78 79
elsif ($state ne EXPTSTATE_SWAPPED) {
    print STDERR "*** WARNING: Ignoring improper state: $state.\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
80
}
81 82 83
if (! SetExpState($pid, $eid, EXPTSTATE_TERMINATING)) {
    print STDERR "*** Failed to set intermediate experiment state.\n";
    exit(1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
84
}
85 86 87 88 89 90

print "Clearing out virtual state.\n";
DBQueryWarn("DELETE from virt_nodes where pid='$pid' and eid='$eid'") or
    $errors++;
DBQueryWarn("DELETE from virt_lans  where pid='$pid' and eid='$eid'") or
    $errors++;
91 92
DBQueryWarn("DELETE from virt_trafgens  where pid='$pid' and eid='$eid'") or
    $errors++;
93 94
DBQueryWarn("DELETE from virt_agents  where pid='$pid' and eid='$eid'") or
    $errors++;
95 96
DBQueryWarn("DELETE from virt_routes  where pid='$pid' and eid='$eid'") or
    $errors++;
97 98
DBQueryWarn("DELETE from virt_vtypes  where pid='$pid' and eid='$eid'") or
    $errors++;
99 100
DBQueryWarn("DELETE from nseconfigs  where pid='$pid' and eid='$eid'") or
    $errors++;
101 102
DBQueryWarn("DELETE from eventlist  where pid='$pid' and eid='$eid'") or
    $errors++;
103 104
DBQueryWarn("DELETE from ipsubnets  where pid='$pid' and eid='$eid'") or
    $errors++;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
105 106

if ($errors == 0) {
107 108
    SetExpState($pid, $eid, EXPTSTATE_TERMINATED) or
	$errors++;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
109 110
}

111
print "Cleanup finished! " . TBTimeStamp() . "\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
112

Leigh B. Stoller's avatar
Leigh B. Stoller committed
113 114
# We exit with whether we had errors or not.
exit $errors;