tbswapout.in 3.89 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/perl -w

# tbswapout

# This is the third program in the
# tbprerun/tbswapin/tbswapout/.../tbend sequences.  It's purpose is
# to reverse what tbswapin did.  I.e. tear down all switch and node
# state and remove the entries from the delays and vlans table.  
# When this script succesfully completes the testbed will be in
# a state for tbswapin or tbend to be run.

use DBI;

# Set up paths
15
$TESTMODE = @TESTMODE@;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
16 17 18 19 20 21 22 23
$TBDB = "@TBDBNAME@";
$TBROOT = "@prefix@";
$ENV{'PATH'} = "/usr/bin:$TBROOT/libexec:$TBROOT/libexec/ns2ir" . 
    ":$TBROOT/sbin:$TBROOT/bin";

# Turn off line buffering.
$| = 1; 

24 25 26 27
use lib "@prefix@/lib";
use libtbsetup;

require exitonwarn; # exitonwarn isn't really a module, so just require it
Leigh B. Stoller's avatar
Leigh B. Stoller committed
28 29 30

$dolog = 1;
$force = 0;
31 32

sub show_help {
33
    print STDERR "Syntax: $0 [-nologfile] [-force] pid eid\n";
34 35 36
    exit(1);
};

Leigh B. Stoller's avatar
Leigh B. Stoller committed
37 38 39 40 41 42 43
while ($#ARGV > 1) {
    $arg = shift;
    if ($arg eq "-nologfile") {
	$dolog = 0;
    } elsif ($arg eq "-force") {
	$force = 1;
    } else {
44
	&show_help;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45 46
    }
}
47 48
if ($#ARGV != 1) {
    &show_help;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
}
($pid,$eid) = @ARGV;

$dbh = &tbs_initdbi($TBDB);

$logfile = "$pid-$eid.log";

if ($dolog) {
    &tbs_initlog($logfile);
    &tbs_out("Log:    $logfile\n");
}

&tbs_out("Beginning swap out for $pid-$eid. " 	 . &ctime(time) . "\n");


&tbs_out("Checking sanity.\n");
$sth = $dbh->prepare("SELECT state from experiments where pid = \"$pid\"" .
		     " and eid = \"$eid\"");
$sth->execute();
if (! (($state) = $sth->fetchrow_array())) {
69
    &tbs_out("$0: *** No entry in experiments table.  Insane.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
70 71 72
    exit(1);
}
if ($force == 0) {
73
    if ($TESTMODE) {
74
	if ($state ne "testing") {
75
	    &tbs_out("$0: *** Experiment should be in testing.\n");
76 77 78 79
	    exit(1);
	}
    } else {
	if ($state eq "swapped") {
80
	    &tbs_out("$0: *** Experiment already swapped. ".
81 82 83
		     "Try swapping in first or use -force.\n");
	    exit(1);
	} elsif ($state ne "active") {
84 85
	    &tbs_out("$0: *** Experiment is not running $state." .
		     " Try -force option\n");
86 87
	    exit(1);
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
88 89
    }
} elsif ($state ne "active") {
90
    &tbs_out("$0: *** WARNING: Ignoring experimental state of $state.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
91 92 93 94
}

if (! $dbh->do("UPDATE experiments set state=\"swapping\" " .
	       "where pid=\"$pid\" and eid=\"$eid\"")) {
95
    &tbs_out("$0: *** Could not set intermediate experiment state.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
96 97 98 99
}

$errors = 0;

100
if (! $TESTMODE) {
101 102
    &tbs_out("Clearing VLANs\n");
    if (&tbs_exec("snmpit -reset $pid $eid")) {
103
	&tbs_out("$0: *** Failed to reset VLANs\n");
104 105 106 107 108
	$errors = 1;
    }
    
    &tbs_out("Backing up VLAN configuration\n");
    if (&tbs_exec("savevlans")) {
109
	&tbs_out("$0: *** WARNING: Failed to back up VLAN configuration\n");
110
    }
111
}
112
    
113 114
&tbs_out("Freeing resources\n");
if (&tbs_exec("nfree $pid $eid")) {
115
    &tbs_out("$0: *** Failed to free resources.\n");
116 117 118 119
    $errors=1;
}

if (! $TESTMODE) {
120 121
    &tbs_out("Resetting mountpoints\n");
    if (&tbs_exec("exports_setup")) {
122
	&tbs_out("$0: *** Failed to reset mountpoints.\n");
123 124 125 126 127
	$errors=1;
    }
    
    &tbs_out("Resetting named maps.\n");
    if (&tbs_exec("named_setup")) {
128
	&tbs_out("$0: *** Failed to reset named map.\n");
129 130 131 132 133 134 135
	#
	# This is a non-fatal error.
	# 
    }

    &tbs_out("Resetting email lists.\n");
    if (&tbs_exec("genelists")) {
136
	&tbs_out("$0: *** Failed to reset email lists.\n");
137 138 139 140
	#
	# This is a non-fatal error.
	# 
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
141 142 143 144 145
}

&tbs_out("Resetting DB.\n");

if (! $dbh->do("DELETE from delays where pid = \"$pid\" and eid=\"$eid\"")) {
146
    &tbs_out("$0: *** Could not clear delays table.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
147 148 149
    $errors = 1;
}
if (! $dbh->do("DELETE from vlans where pid = \"$pid\" and eid = \"$eid\"")) {
150
    &tbs_out("$0: *** Could not clear vlans table.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
151 152 153 154 155 156 157
    $errors = 1;
}

if ($errors == 0) {
    &tbs_out("Marking as dormant.\n");
    if (! $dbh->do("UPDATE experiments set state=\"swapped\"" .
		   " where pid=\"$pid\" and eid=\"$eid\"")) {
158
	&tbs_out("$0: *** Could not set experiment state.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
159 160 161 162 163 164 165 166
	$errors = 1;
    }
}

&tbs_out("Swap out finished - " . &ctime(time) . "\n");

exit $errors;