sched_reload.in 3.5 KB
Newer Older
1 2
#!/usr/bin/perl -wT
use English;
3
use Getopt::Std;
4 5 6 7 8 9 10
   
#
# Schedule the reloading of a disk partition on a node. If the node is
# currently not reserved, start the loading now after reserving it to 
# testbed:reloading. Otherwise, put the right info into the database, and 
# nfree will do it when the node gets freed.
# 
11
# usage: sched_reload [-f] <imageid> <node> [node ...]
12 13 14
#
sub usage()
{
15
    print STDOUT "Usage: sched_reload [-f] <imageid> <node> [node ...]\n".
16 17 18 19
	"Use the -f to force reload. Fail if node cannot be reserved.\n";
    exit(-1);
}
my  $optlist = "f";
20 21 22 23

#
# Configure variables
#
24
my $TB     = "@prefix@";
25

26 27 28
#
# Load the Testbed support stuff. 
#
29 30
use lib "@prefix@/lib";
use libdb;
31 32 33

my $osload      = "$TB/bin/os_load";
my $nalloc      = "$TB/bin/nalloc";
34
my $name        = "";
35 36 37
my $error       = 0;
my $debug       = 0;
my $force	= 0;
38
my @nodes       = ();
39
my @row;
40 41 42 43 44 45 46 47

# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

$| = 1; #Turn off line buffering on output

#
48 49 50 51 52 53 54
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
55
if (@ARGV < 2) {
56
    usage();
57
}
58 59 60
if (defined($options{"f"})) {
    $force = $options{"f"};
}
61
my $imageid   = shift;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82

#
# Untaint args.
#
if ($imageid =~ /^([-\@\w.\+]+)$/) {
    $imageid = $1;
}
else {
    die("Bad data in $imageid.");
}

foreach my $node ( @ARGV ) {
    if ($node =~ /^([-\@\w]+)$/) {
	$node = $1;
    }
    else {
	die("Bad node name: $node.");
    }
    
    push(@nodes, $node);
}
83

84
#
85 86
# Root and admin types can do whatever they want.
# Mere users cannot schedule reloads.
87
#
88
if ($UID && !TBAdmin($UID)) {
89 90 91 92
    die("Only root or TB administrators can schedule disk reloads.\n");
}

#
93
# A loop.
94 95 96 97
# 
my @load_list=();
foreach my $node (@nodes) {
    my $pc = $node;
98
    my $allocated = 0;
99

100
    $sth = DBQueryFatal("select * from nodes where node_id='$pc'");
101 102 103 104 105
    if ($sth->num_rows() != 1) {
      print STDERR "Node $pc doesn't exist. Skipping $pc.\n";
      next;
    }
    
106
    print STDERR "Checking if $pc is reserved...";
107
    $sth = DBQueryFatal("select * from reserved where node_id='$pc'");
108
   
109 110
    if ( ($sth->num_rows()) < 1) {
        print STDERR "Available.\nReserving and adding to list.\n";
111 112 113 114
	#
	# XXX If you change testbed/reloading, be sure to update the
	#     test in doreboot() in tmcd/tmcd.c.
	# 
115
	my $cmd = "$nalloc testbed reloading $pc";
116 117 118
        if ( system($cmd) != 0 ) {
	    print STDERR "WARNING: Could not reserve $pc!\n";
	} else {
119 120 121 122
	    #
	    # Kill the last_reservation so that whoever gets the node next
	    # won't be fooled into thinking a reload is required.
	    #
123
	    DBQueryFatal("delete from last_reservation where node_id='$pc'");
124
	    push (@load_list,$pc);
125
	    $allocated = 1;
126 127 128 129
	}
    } else {
        print STDERR "Reserved.\n";      
    }
130 131 132 133 134 135 136 137 138 139

    #
    # If force and not able to reserve, do not pend a reload.
    # 
    if ($force && !$allocated) {
	$error++;
	next;
    }

    # Put it in the reloads table so TMCD knows to free it.
140
    print STDERR "Scheduling reload of $imageid for $pc:\n";
141 142
    DBQueryFatal("replace into reloads ".
		 "(node_id, image_id) values ('$pc', '$imageid')");
143 144 145 146
}

if (@load_list > 0) {
  print STDERR "Running os_load on ",join(", ",@load_list),":\n";
147
  $cmd = "$osload $imageid @load_list";
148 149 150 151 152 153 154
  print STDERR "Calling '$cmd'\n";
  if ( system($cmd) != 0 ) {
    print STDERR "WARNING: OS_LOAD FAILED ON @load_list!\n";
  }
}

print STDOUT "Reload Scheduling Done!\n";
155
exit $error;