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.

tbswapin.in 5.06 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 15
#!/usr/bin/perl -w

# tbswapin

# This is the second program in the
# tbprerun/tbswapin/tbswapout/.../tbend sequences.  It's purpose is
# to setup the testbed for experimental use.  The first part centers
# around assign and converting the virtual topology to a physical
# actuality and setting up the vlans and delays table.  The second
# part calls a number of scripts to set up switch, node, and 
# nameserver state.

use DBI;

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

# Turn off line buffering.
$| = 1; 

25 26 27 28
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
29

30 31 32
$dolog = 1;

sub show_help {
33
    print STDERR "Syntax: $0 [-nologfile] pid eid\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
34
    exit(1);
35
};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
36

37 38 39 40 41 42 43
while ($#ARGV > 1) {
    $arg = shift;
    if ($arg eq "-nologfile") {
	$dolog = 0;
    } else {
	&show_help;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
44
}
45 46 47

if ($#ARGV != 1) {
    &show_help;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
48
}
49

Leigh B. Stoller's avatar
Leigh B. Stoller committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
($pid,$eid) = @ARGV;

$dbh = &tbs_initdbi($TBDB);

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

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

sub cleanup {
    &tbs_out("Cleaning up\n");

    if ($cleanvlans) {
	&tbs_out("Removing VLANs\n");
	if (&tbs_exec("snmpit -reset $pid $eid")) {
68
	    &tbs_out("$0: *** Failed to clean up VLANs\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
69 70 71 72 73
	    $errors = 1;
	}

	&tbs_out("Backing up VLAN configuration\n");
	if (&tbs_exec("savevlans")) {
74 75
	    &tbs_out("$0: *** WARNING: Failed to back up" .
		     " VLAN configuration\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
76 77 78 79 80
	}
    }
    
    &tbs_out("Freeing up nodes.\n");
    if (&tbs_exec("nfree $pid $eid")) {
81
	&tbs_out("$0: *** Could not free resources.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
82 83 84 85 86
    }
    
    &tbs_out("Resetting DB.\n");
    if (! $dbh->do("DELETE from delays where ".
		   "pid = \"$pid\" and eid=\"$eid\"")) {
87
	&tbs_out("$0: *** Could not clear delays table.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
88 89 90 91
	$errors = 1;
    }
    if (! $dbh->do("DELETE from vlans where ".
		   "pid = \"$pid\" and eid = \"$eid\"")) {
92
	&tbs_out("$0: *** Could not clear vlans table.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
93 94 95 96
	$errors = 1;
    }
    if (! $dbh->do("UPDATE experiments set state = \"swapped\"" . 
		   "where pid = \"$pid\" and eid = \"$eid\"")) {
97
	&tbs_out("$0: *** Could not reset experiment state.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
98 99 100 101 102 103 104 105 106 107 108
	$errors = 1;
    }
};

&tbs_out("Beginning swap in 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())) {
109
    &tbs_out("$0: *** No entry in experiments table.  Insane.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
110 111 112
    exit(1);
}
if ($state eq "active") {
113
    &tbs_out("$0: *** Experiment is already running. Must be swapped out first.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
114 115
    exit(1);
} elsif ($state ne "swapped") {
116
    &tbs_out("$0: *** Experiment is not in the swapped state: $state.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
117 118 119 120 121 122
    exit(1);
}

$sth = $dbh->prepare("UPDATE experiments set state = \"activating\"" . 
		     "where pid = \"$pid\" and eid = \"$eid\"");
if (! $sth->execute()) {
123
    &tbs_out("$0: *** Failed to set intermediate state.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
124 125 126 127 128 129
}
$sth->finish();

# This does all the virtual to physical mapping and updating the DB state.
&tbs_out("Mapping to physical reality\n");
if ($exitcode = &tbs_exec("assign_wrapper $pid $eid")) {
130
    &tbs_out("$0: *** Failed to map to reality.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
131 132 133 134 135
    cleanup;
    # Pass exit code through
    exit($exitcode >> 8);
}

136
# Exit here if we are testing.
137
if ($TESTMODE) {
138 139 140
    &tbs_out("Testing run - Stopping here.\n");
    if (! $dbh->do("UPDATE experiments set state=\"testing\"" .
		   " where pid=\"$pid\" and eid=\"$eid\"")) {
141
	&tbs_out("$0: *** Could not set state to tested.\n");
142 143 144 145 146 147
	cleanup;
	exit(1);
    }
    exit(0);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
148 149 150 151
# Everything from now on sets up switch and node state.

&tbs_out("Setting up VLANs\n");
if (&tbs_exec("snmpit -t $pid $eid")) {
152
    &tbs_out("$0: *** Failed to set up VLANs\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
153 154 155 156 157 158 159 160 161 162 163
    cleanup;
    exit(1);
}

#
# An error now means that the VLANS need to be cleaned up.
#
$cleanvlans = 1;

&tbs_out("Backing up VLAN configuration\n");
if (&tbs_exec("savevlans")) {
164
    &tbs_out("$0: *** WARNING: Failed to back up VLAN configuration\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
165 166 167 168
}

&tbs_out("Setting up mountpoints\n");
if (&tbs_exec("exports_setup")) {
169
    &tbs_out("$0: *** Failed to setup mountpoints.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
170 171 172 173 174 175
    cleanup;
    exit(1);
}

&tbs_out("Resetting OS and rebooting.\n");
if (&tbs_exec("os_setup $pid $eid")) {
176
    &tbs_out("$0: *** Failed to reset OS and reboot nodes.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
177 178 179 180 181 182
    cleanup;
    exit(1);
}

&tbs_out("Setting up named maps.\n");
if (&tbs_exec("named_setup")) {
183
    &tbs_out("$0: *** WARNING: Failed to add node names to named map.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
184 185 186 187 188
    #
    # This is a non-fatal error.
    # 
}

189 190
&tbs_out("Setting up email lists.\n");
if (&tbs_exec("genelists")) {
191
    &tbs_out("$0: *** WARNING: Failed to update email lists.\n");
192 193 194 195 196
    #
    # This is a non-fatal error.
    # 
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
197 198 199
&tbs_out("Marking as running.\n");
if (! $dbh->do("UPDATE experiments set state=\"active\"" .
	       " where pid=\"$pid\" and eid=\"$eid\"")) {
200
    &tbs_out("$0: *** Could not set experiment state.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
201 202 203 204 205 206 207 208
    cleanup;
    exit(1);
}

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

0;