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

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

9
use English;
10 11 12

# tbprerun

Leigh Stoller's avatar
Leigh Stoller committed
13 14 15 16 17
# This is the first program in the
# tbprerun/tbswapin/tbswapout/.../tbend sequences.  It's main purpose
# is to interpret the NS file and create the appropriate entries in
# virt_nodes and virt_lans.  After this script ends successfully the
# NS file is no longer necessary.
18 19 20
#
sub usage()
{
21
    print STDERR "Usage: $0 [-force] pid eid nsfile\n";
22 23 24 25
    exit(-1);
}
my $force = 0;
my $state;
26

27 28 29 30
#
# Configure variables
#
my $TBROOT   = "@prefix@";
31

32
# Untaint the path
33
$ENV{'PATH'} = "/usr/bin:/bin:$TBROOT/libexec:$TBROOT/libexec/ns2ir" . 
34
    ":$TBROOT/libexec/vis:$TBROOT/sbin:$TBROOT/bin";
Leigh Stoller's avatar
Leigh Stoller committed
35

36 37 38
#
# Testbed Support libraries
#
39
use lib "@prefix@/lib";
40 41
use libdb;
use libtestbed;
42

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

48
#
Leigh Stoller's avatar
Leigh Stoller committed
49
# Parse args
50
#
Leigh Stoller's avatar
Leigh Stoller committed
51 52 53
if (!@ARGV) {
    usage();
}
54 55 56 57
if ($ARGV[0] eq "-force") {
    $force = 1;
    shift;
}
58

59 60
if (@ARGV != 3) {
    usage();
61
}
62
my ($pid,$eid,$nsfile) = @ARGV;
63 64

if (! -r $nsfile) {
65 66
    die("*** $0:\n".
	"    NS File '$nsfile' does not exist!\n");
67 68
}

69
print "Beginning pre run for $pid/$eid. " . TBTimeStamp() . "\n";
70
TBDebugTimeStamp("tbprerun started");
71 72

if (! ($state = ExpState($pid, $eid))) {
73 74
    die("*** $0:\n".
	"    No such experiment $pid/$eid\n");
75
}
76

77 78 79 80 81
if (! ($gid = ExpGroup($pid,$eid))) {
    die("*** $0:\n".
	"    Unable to get group for $pid/$eid\n");
}

82 83 84 85 86 87 88 89
#
# These are the valid states for running this script.
#
if (!$force &&
    !($state eq EXPTSTATE_PRERUN || $state eq EXPTSTATE_MODIFY_PARSE ||
      $state eq EXPTSTATE_MODIFY_REPARSE)) {
    die("*** $0:\n".
	"    Experiment is not in the proper state: $state\n");
90 91
}

92
#
93 94
# Cleanup if something goes wrong.
#
Leigh Stoller's avatar
Leigh Stoller committed
95
sub cleanup {
96 97 98 99 100 101
    print STDERR "Cleaning up after errors.\n";
    # Must kill the prerender process before we remove virt state.
    system("prerender -r $pid $eid");

    # When doing a modify, this is handled elsewhere. 
    if ($state eq EXPTSTATE_PRERUN) {
102 103
	print "Removing experiment state ... " . TBTimeStamp() . "\n";
	TBExptRemoveVirtualState($pid, $eid );
104 105 106
    }
}

107 108 109 110 111 112
# Must kill any prerender process first!
system("prerender -r $pid $eid");

# Kill old virtual state.
TBExptRemoveVirtualState($pid, $eid);

Leigh Stoller's avatar
Leigh Stoller committed
113 114
# This setups virt_nodes, virt_names including all IP address calculation
# and tb-* handling.
115
print "Running parser ... " . TBTimeStamp() . "\n";
116
TBDebugTimeStamp("parser started");
117
if (system("parse-ns $pid $gid $eid $nsfile")) {
118
    cleanup();
119 120
    die("*** $0:\n".
	"    Parsing failed!\n");
121
}
122
TBDebugTimeStamp("parser finished");
123
print "Parser done! " . TBTimeStamp() . "\n";
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#
# Put the nsfile into the DB, now that we know it parses okay.
#
$nsfile_string = `cat $nsfile`;

if ($nsfile_string) {
    $nsfile_string = DBQuoteSpecial($nsfile_string);

    if (length($nsfile_string) >= DBLIMIT_NSFILESIZE()) {
	cleanup();
	die("*** $0:\n".
	    "    NS File is way too big!\n");
    }
    if (!DBQueryWarn("delete from nsfiles where eid='$eid' and pid='$pid'") ||
	!DBQueryWarn("insert into nsfiles (pid, eid, nsfile) ".
		     "VALUES('$pid', '$eid', $nsfile_string)")) {
	cleanup();
	die("*** $0:\n".
	    "    Error storing the NS file into the database!\n");
    }
}

147
TBDebugTimeStamp("prerender started in background");
148 149
print "Precomputing visualization ...\n";
system("prerender -t $pid $eid");
150

151
#
152
# See if using the new ipassign.
153
#
154 155 156 157 158
my $query_result =
    DBQueryWarn("select use_ipassign, ipassign_args " .
		"from experiments where pid='$pid' and eid='$eid'");

if (!$query_result || ($query_result->num_rows() != 1)) {
159 160
	cleanup();
	die("*** $0:\n".
161
	    "    Error getting ipassign arguments!\n");
162
}
163
my ($use_ipassign, $ipassign_args) = $query_result->fetchrow();
164 165 166 167
if ($use_ipassign) {
    if (!defined $ipassign_args) {
	$ipassign_args = "";
    }
168 169 170
    TBDebugTimeStamp("ipassign_wrapper started");
    print "Doing IP assignment ...\n";
    
171 172 173
    if (system("ipassign_wrapper $ipassign_args $pid $eid")) {
	cleanup();
	die("*** $0:\n".
174
	    "    ipassign_wrapper failed!\n");
175
    }
176
}
177

178
#
179 180 181 182 183 184
# Fire up the route calculator. 
# 
TBDebugTimeStamp("static route calculator started");
print "Setting up static routes (if requested) ... \n";

if (system("staticroutes $pid $eid")) {
185 186
    cleanup();
    die("*** $0:\n".
187
	"    Static route calculation failed!\n");
188 189
}

190 191 192 193
#
# Generate a topo map that is used by the remote nodes to create the
# routes (ddijk) and the /etc/hosts file.
#
194 195 196
TBDebugTimeStamp("gentopofile started");
print "Generating topomap ...\n";

197 198 199 200 201 202
if (system("gentopofile $pid $eid")) {
    cleanup();
    die("*** $0:\n".
	"    gentopofile failed!\n");
}

203
TBDebugTimeStamp("tbprerun finished");
204 205
print "Pre run finished. " . TBTimeStamp() . "\n";
exit(0);
206