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.

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

# tbend

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
10
use DBI;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
11

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
18 19
# Turn off line buffering.
$| = 1; 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
20

21 22 23 24
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
25

Leigh B. Stoller's avatar
Leigh B. Stoller committed
26 27 28 29 30 31 32 33 34 35 36 37
$dolog = 1;
$force = 0;
while ($#ARGV > 1) {
    $arg = shift;
    if ($arg eq "-nologfile") {
	$dolog = 0;
    } elsif ($arg eq "-force") {
	$force = 1;
    } else {
	print STDERR "Syntax: $0 [-nologfile] [-force] pid eid\n";
	exit(1);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
38
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
39 40 41
if ($#ARGV < 1) {
    print STDERR "Syntax: $0 [-nologfile] [-force] pid eid\n";
    exit(1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
42 43 44
}
($pid,$eid) = @ARGV;

Leigh B. Stoller's avatar
Leigh B. Stoller committed
45
$dbh = &tbs_initdbi($TBDB);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
46

Leigh B. Stoller's avatar
Leigh B. Stoller committed
47
$logfile = "$pid-$eid.log";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
48 49 50

if ($dolog) {
    &tbs_initlog($logfile);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
51
    &tbs_out("Log:    $logfile\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
52 53
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
54 55
&tbs_out("Beginning cleanup for $pid-$eid. "
	 . &ctime(time) . "\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
56

Leigh B. Stoller's avatar
Leigh B. Stoller committed
57
$errors = 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
58

Leigh B. Stoller's avatar
Leigh B. Stoller committed
59 60 61 62 63
&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())) {
64
    &tbs_out("$0: *** No entry in experiments table.  Insane.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
65 66
    exit(1);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
67 68
if ($force == 0) {
    if ($state eq "active") {
69 70
	&tbs_out("$0: *** Experiment already running." .
		 " Try swapping out first.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
71 72
	exit(1);
    } elsif ($state ne "swapped") {
73
	&tbs_out("$0: *** Experiment in strange state: $state.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
74 75 76
	exit(1);
    }
} elsif ($state ne "swapped") {
77
    &tbs_out("$0: *** WARNING: Ignoring experimental state of $state.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
78 79
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
80 81
if (! $dbh->do("UPDATE experiments set state = \"terminating\" " . 
	       "where pid=\"$pid\" and eid=\"$eid\"")) {
82
    &tbs_out("$0: *** Could not set intermediate experiment state.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
83 84
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
85 86 87 88
&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");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
90 91 92 93
    $errors = 1;
}
if (! $dbh->do("DELETE from virt_lans where pid = \"$pid\"" . 
	       " and eid = \"$eid\"")) {
94
    &tbs_out("$0: *** Could not clear virt_lans table.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
95 96 97 98
    $errors = 1;
}
if (! $dbh->do("DELETE from portmap where pid = \"$pid\"" . 
	       " and eid = \"$eid\"")) {
99
    &tbs_out("$0: *** Could not clear portmap table.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
100 101 102 103 104 105 106 107
    $errors = 1;
}
$sth->finish();

if ($errors == 0) {
    &tbs_out("Marking as ended.\n");
    if (! $dbh->do("UPDATE experiments set state = \"ended\"" . 
		   " where pid=\"$pid\" and eid=\"$eid\"")) {
108
	&tbs_out("$0: *** Could not set experiment state.\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
109 110
	$errors = 1;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
111 112 113 114
}

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

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