killbatchexp.in 2.85 KB
Newer Older
1 2 3 4 5
#!/usr/bin/perl -wT
use English;
use Getopt::Std;

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6
# Cancel batch experiment.
7 8 9 10 11 12 13 14 15 16 17 18
#
sub usage()
{
    print STDOUT "Usage: killbatchexp $pid $eid\n";
    exit(-1);
}
my  $optlist = "";

#
# Configure variables
#
my $TB       = "@prefix@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
19
my $batchdir = "$TB/batch";
20

21 22 23 24 25 26 27
#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;

28 29 30 31 32 33
#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/usr/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#
# Turn off line buffering on output
#
$| = 1;

#
# Parse command arguments. Once we return from getopts, all that should
# be left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 2) {
    usage();
}
my $pid = $ARGV[0];
my $eid = $ARGV[1];

#
# Untaint the arguments.
#
if ($pid =~ /^([-\@\w.]+)$/) {
    $pid = $1;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
59 60 61
else {
    die("Tainted argument $pid\n");
}
62 63 64
if ($eid =~ /^([-\@\w.]+)$/) {
    $eid = $1;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
65 66 67
else {
    die("Tainted argument $eid\n");
}
68 69 70 71

#
# This is where we currently hold the batch goo.
#
72
my $dirname = "$batchdir/$pid-$eid";
73 74 75 76 77 78 79

#
# Need to lock the table for this. We could avoid the locking if we just
# set the canceled bit and let the batch_daemon clean things up, but that
# introduces needless (and annoying) delay when killing a batch experiment
# that is not even running. See corresponding lock in the batch_daemon.
#
80
DBQueryFatal("lock tables batch_experiments write");
81 82 83 84 85

#
# Set the canceled state right away. This will prevent the batch_daemon
# from trying to run it. It might already be running, but thats okay.
#
86 87
DBQueryFatal("UPDATE batch_experiments set canceled=1 ".
	     "WHERE eid='$eid' and pid='$pid'");
88 89 90 91 92 93 94

#
# Now its safe to look at the state. If its in the "new" state, then
# all we need to do is kill the record and the directory, since the
# batch daemon will not bother with it once the canceled bit is set.
#
$query_result = 
95 96
    DBQueryFatal("SELECT status from batch_experiments ".
		 "WHERE eid='$eid' and pid='$pid'");
97

98
DBQueryFatal("unlock tables");
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

@row = $query_result->fetchrow_array();
$state = $row[0];

if ($state ne "new") {
    #
    # Daemon does the rest ...
    #
    print STDOUT
	"Batch Experiment $eid in project $pid is running on the testbed\n".
	"You will receive email notification when the experiment is torn\n".
	"down and you can reuse the experiment name\n";

    #
    # exit status is special. Tells the caller that cancelation is pending.
    # The web script will say something useful.
    #
    exit(1);
}

#
# Delete the DB record. LOCKING!
#
122
DBQueryFatal("DELETE from batch_experiments WHERE eid='$eid' and pid='$pid'");
123 124 125 126 127 128 129 130 131 132 133 134 135 136

#
# And kill the directory.
#
system("rm -rf $dirname");

#
# Lets not bother with an email message. Just print out something nice
# and tell the caller (the php script) to say something nice too).
# 
print STDOUT
    "Batch Experiment $eid in project $pid has been canceled!\n";

exit(0);