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

4
5
6
7
8
9
10
#
# Configure variables
#
my $TB     = "@prefix@/bin";
my $DBNAME = "@TBDBNAME@";

my $dbh = Mysql->connect("localhost",$DBNAME,"script","none");
Mac Newbold's avatar
Mac Newbold committed
11

12
13
14
15
16
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
17

18
my $error = 0;
19
my $consetup="$TB/console_setup";
20
21
22
my $osload="$TB/os_load";
my $reloadpid="testbed";
my $reloadeid="reloading";
23
my $pid = shift;
Mac Newbold's avatar
Mac Newbold committed
24
25
26
27
28
29
30
31
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 = "";
32

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
$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]);
  }
}

49
my %reloads = ();
Mac Newbold's avatar
Mac Newbold committed
50
foreach my $n (@node_names) { 
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  $sth = $dbh->query("select * from reserved where node_id='$n' ".
		     "and eid='$eid'");
  if ($sth->numrows == 0) {
    print "Node '$n' is not reserved by your experiment.\n";
    $error++;
    next;
  } 

  # If the node has a reloads entry, change the reservation and start it
  $cmd = "select node_id,partition,image_id,path from reloads where ".
    "node_id='$n'";
  $sth = $dbh->query($cmd)
    || (print "Failed Command:\n$cmd\nError string is:".$dbh->errstr."\n"
	&& $error++);
  if ( ($sth->num_rows()) > 0) {
    my @reload;
    print STDERR "Adding reloads for $n to the list.\n";
    my $i = 0;
    my $max = $sth->num_rows();
    while ( $i < $max  ) {
      $i++;
      @reload = $sth->fetchrow_array();
      my ($node, $part, $image, $path) = @reload;
      
      if (! defined($reloads{"$image $part $path"})) {
	my @list = ($node);
	$reloads{"$image $part $path"} = \@list;
      } else {
	my @list = @{$reloads{"$image $part $path"}};
	push(@list,$node);
	$reloads{"$image $part $path"} = \@list;
      }

    }

    # Change reservation (don't delete or we'll get races)
    $cmd = "update reserved set pid='$reloadpid',eid='$reloadeid' where ".
      "node_id='$n'";
    $sth = $dbh->query($cmd)
      || (print "Failed Command:\n$cmd\nError string is:".$dbh->errstr."\n"
	  && $error++ && next);
  } else {
    # No reloads to be done, so really free the node
Mac Newbold's avatar
Mac Newbold committed
94
95
96
    print "Releasing node '$n'...";
    $cmd = "delete from reserved where node_id='$n' and eid='$eid'";
    $sth = $dbh->query($cmd) && print "Succeeded.\n"
97
98
99
      || (print "Failed Command:\n$cmd\nError string is:".$dbh->errstr."\n"
	  && $error++);
  }
mac's avatar
mac committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

  if (! ($n =~ /^tbsh/)) { #If its not a shark
    # Find the control net interface for this node type
    $sth = $dbh->query("select control_net from node_types as t left join ".
		       "nodes  as n on n.type=t.type where node_id='$n'");
    my @row= $sth->fetchrow_array();
    my $control= $row[0];
    # Clean out all IPs except the control net
    $cmd = 
      "update interfaces set IP='' where node_id='$n' and card!='$control'";
    $sth = $dbh->query($cmd)
      || (print "Failed Command:\n$cmd\nError string is:".$dbh->errstr."\n"
	  && $error++);
  }

115
116
  system("$consetup $n") == 0 or
      print STDERR "WARNING: $consetup $n failed!";
117
118
119
120
121
122
123
124
125
126
127
128
}

foreach $reload ( keys(%reloads)) {
  @list = @{$reloads{$reload}};
  # Call os_load to start the reload
  print STDERR "Starting reload for @list:\n";
  $cmd = "$osload $reload @list";
  print STDERR "Calling '$cmd'\n";
  if ( system($cmd) != 0 ) {
    print STDERR "WARNING: OS_LOAD FAILED ON @list!\n";
  }
  print STDERR "Reload for @list complete.\n";
Mac Newbold's avatar
Mac Newbold committed
129
130
}

131
exit($error);