tbprerun.in 3 KB
Newer Older
1 2 3 4
#!/usr/bin/perl -w

# tbprerun

Leigh Stoller's avatar
Leigh Stoller committed
5 6 7 8 9
# 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.
10

Leigh Stoller's avatar
Leigh Stoller committed
11
use DBI;
12

Leigh Stoller's avatar
Leigh Stoller committed
13 14
# Set up paths
$TBDB = "@TBDBNAME@";
15
$TBROOT = "@prefix@";
Leigh Stoller's avatar
Leigh Stoller committed
16 17 18
$ENV{'PATH'} = "/usr/bin:$TBROOT/libexec:$TBROOT/libexec/ns2ir" . 
    ":$TBROOT/sbin:$TBROOT/bin";

19 20 21
# Turn off line buffering.
$| = 1; 

22 23 24 25
use lib "@prefix@/lib";
use libtbsetup;

require exitonwarn; # exitonwarn isn't really a module, so just require it
26

27 28 29
if ($#ARGV < 2 || $#ARGV > 3 ||
    ($#ARGV == 3 && $ARGV[0] ne "-nologfile")) {
    print STDERR "Syntax: $0 [-nologfile] pid eid ns_file\n";
30 31
    exit(1);
}
32 33 34 35 36 37 38 39

if ($#ARGV == 3) {
    shift;
    $dolog = 0;
}
else {
    $dolog = 1;
}
40 41
($pid,$eid,$nsfile) = @ARGV;

Leigh Stoller's avatar
Leigh Stoller committed
42 43
$dbh=&tbs_initdbi($TBDB);

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

46 47 48 49
if ($dolog) {
    &tbs_initlog($logfile);
    &tbs_out("Log:    $logfile\n");
}
50 51 52 53 54 55 56 57 58 59 60

&tbs_out("Input:  $nsfile\n");
&tbs_out("\n");
&tbs_out("Beginning pre run for $pid-$eid ($nsfile). " 
	 . &ctime(time) . "\n");

if (! -r $nsfile) {
    &tbs_out("$nsfile does not exist or is not readable.\n");
    exit(1);
}

Leigh Stoller's avatar
Leigh Stoller committed
61 62 63 64 65
&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())) {
66
    &tbs_out("$0: *** No entry in experiments table.  Insane.\n");
67 68
    exit(1);
}
Leigh Stoller's avatar
Leigh Stoller committed
69 70
$sth->finish();
if ($state ne "new") {
71
    &tbs_out("$0: *** Experiment is not in the proper state: $state\n");
72 73 74
    exit(1);
}

Leigh Stoller's avatar
Leigh Stoller committed
75 76 77
$sth = $dbh->prepare("UPDATE experiments set state = \"prerunning\"" . 
		     "where pid = \"$pid\" and eid = \"$eid\"");
if (! $sth->execute()) {
78
    &tbs_out("$0: *** Failed to set intermediate state.\n");
79 80
    exit(1);
}
Leigh Stoller's avatar
Leigh Stoller committed
81
$sth->finish();
82

Leigh Stoller's avatar
Leigh Stoller committed
83 84 85 86 87 88
sub cleanup {
    &tbs_out("Cleaning up\n");
    &tbs_out("Clearing virtual state.\n");
    if (! $dbh->do("DELETE from virt_nodes where pid = \"$pid\"" . 
		   " and eid = \"$eid\"")) {
	
89
	&tbs_out("$0: *** Could not clear virt_nodes table.\n");
90
    }
Leigh Stoller's avatar
Leigh Stoller committed
91 92
    if (! $dbh->do("DELETE from virt_lans where pid = \"$pid\"" . 
		   " and eid = \"$eid\"")) {
93
	&tbs_out("$0: *** Could not clear virt_lans table.\n");
94
    }
Leigh Stoller's avatar
Leigh Stoller committed
95 96
    if (! $dbh->do("UPDATE experiments set state = \"new\"" . 
		   "where pid = \"$pid\" and eid = \"$eid\"")) {
97
	&tbs_out("$0: *** Could not reset experiment state.\n");
98
    }
Leigh Stoller's avatar
Leigh Stoller committed
99
};
100

Leigh Stoller's avatar
Leigh Stoller committed
101 102 103 104
# This setups virt_nodes, virt_names including all IP address calculation
# and tb-* handling.
&tbs_out("Running parser.\n");
if (&tbs_exec("parse.tcl $pid $eid $nsfile")) {
105
    &tbs_out("$0: *** Parsing failed.\n");
106 107 108
    exit(1);
}

Leigh Stoller's avatar
Leigh Stoller committed
109 110 111 112
&tbs_out("Marking as prerunned.\n");
$sth = $dbh->prepare("UPDATE experiments set state = \"swapped\"" . 
		     "where pid = \"$pid\" and eid = \"$eid\"");
if (! $sth->execute()) {
113
    &tbs_out("$0: *** Failed to set state.\n");
Leigh Stoller's avatar
Leigh Stoller committed
114
    cleanup;
115 116
    exit(1);
}
Leigh Stoller's avatar
Leigh Stoller committed
117
$sth->finish();
118 119 120 121

&tbs_out("Pre run finished - " . &ctime(time) . "\n");

0;