sched_reload.in 3.75 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 29 30 31 32 33
#
# Load the Testbed support stuff. 
#
push(@INC, "$TB/lib");
require libdb;

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 98
# 
my @load_list=();
foreach my $node (@nodes) {
    my $pc = $node;

99
    $sth = DBQuery("select * from nodes where node_id='$pc'");
100
    if ($sth == 0) {
101
	die("Database lookup failed (exists). Aborted...\n");
102 103 104 105 106 107
    }
    if ($sth->num_rows() != 1) {
      print STDERR "Node $pc doesn't exist. Skipping $pc.\n";
      next;
    }
    
108
    print STDERR "Checking if $pc is reserved...";
109
    $sth = DBQuery("select * from reserved where node_id='$pc'");
110 111 112
    if ($sth == 0) {
	die("Database lookup failed (reserved). Aborted...\n");
    }
113 114 115

    my $allocated = 0;
   
116 117
    if ( ($sth->num_rows()) < 1) {
        print STDERR "Available.\nReserving and adding to list.\n";
118 119 120 121
	#
	# XXX If you change testbed/reloading, be sure to update the
	#     test in doreboot() in tmcd/tmcd.c.
	# 
122
	my $cmd = "$nalloc testbed reloading $pc";
123 124 125
        if ( system($cmd) != 0 ) {
	    print STDERR "WARNING: Could not reserve $pc!\n";
	} else {
126 127 128 129
	    #
	    # Kill the last_reservation so that whoever gets the node next
	    # won't be fooled into thinking a reload is required.
	    #
130
	    $sth = DBQuery("delete from last_reservation where node_id='$pc'");
131
	    push (@load_list,$pc);
132
	    $allocated = 1;
133 134 135 136
	}
    } else {
        print STDERR "Reserved.\n";      
    }
137 138 139 140 141 142 143 144 145 146

    #
    # 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.
147
    print STDERR "Scheduling reload of $imageid for $pc:\n";
148 149
    $sth = DBQuery("replace into reloads ".
		   "(node_id, image_id) values ('$pc', '$imageid')");
150 151 152
    if ($sth == 0) {
      die("Database update failed (reloads). Aborted...\n");
    }
153 154 155 156
}

if (@load_list > 0) {
  print STDERR "Running os_load on ",join(", ",@load_list),":\n";
157
  $cmd = "$osload $imageid @load_list";
158 159 160 161 162 163 164
  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";
165
exit $error;