nfree 1.5 KB
Newer Older
Mac Newbold's avatar
Mac Newbold committed
1 2 3 4 5
#!/usr/local/bin/perl -w
use Mysql;

my $dbh = Mysql->connect("localhost","tbdb","script","none");

6 7 8 9 10
if ($#ARGV < 1) {
  die("Usage: nfree <pid> <eid> [<node> <node> <...>]\n".
      "Releases all nodes in the specified experiment. If nodes are listed,\n".
      "nfree releases only the listed nodes.\n");
}
Mac Newbold's avatar
Mac Newbold committed
11

12
my $error = 0;
13
my $pid = shift;
Mac Newbold's avatar
Mac Newbold committed
14 15 16 17 18 19 20 21
my $eid = shift;
my @node_names=@ARGV;
my @machines = @node_names;
foreach my $m (@machines) { $m = "node_id='".$m."'"; }
my $list= join(" or ",@machines);

my $cmd = "";
my $sth = "";
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
$cmd = "select * from experiments where eid='$eid' and pid='$pid'";
$sth = $dbh->query($cmd);
if ($sth->numrows < 1) {	
  die("There is no experiment '$eid' in project '$pid'.\n");
}
 
# If list is empty, put in all the nodes
if ($#node_names == -1) {
  print "Releasing all nodes from experiment '$eid' in project '$pid'...\n";
  $sth = $dbh->
    query("select node_id from reserved where pid='$pid' and eid='$eid'");
  while (@row = $sth->fetchrow_array()) {
    push(@node_names, $row[0]);
  }
}

Mac Newbold's avatar
Mac Newbold committed
39 40 41 42
foreach my $n (@node_names) { 
    $sth = $dbh->query("select * from reserved where node_id='$n' ".
		       "and eid='$eid'");
    if ($sth->numrows == 0) {
43
	print "Node '$n' is not reserved by your experiment.\n";
44
	$error++;
Mac Newbold's avatar
Mac Newbold committed
45 46 47 48 49
	next;
    } 
    print "Releasing node '$n'...";
    $cmd = "delete from reserved where node_id='$n' and eid='$eid'";
    $sth = $dbh->query($cmd) && print "Succeeded.\n"
50 51
	|| (print "Failed Command:\n$cmd\nError string is:".$dbh->errstr."\n"
	    && $error++);
Mac Newbold's avatar
Mac Newbold committed
52 53
}

54
exit($error);