sched_reload.in 6.37 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.
# 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
11
# usage: sched_reload [-f | -p] [-r] [-i <imageid>] <node> [node ...]
12 13 14
#
sub usage()
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
15 16
    print STDOUT "Usage: sched_reload [-f | -p] [-r] [-i <imageid>] ".
	         "<node> [node ...]\n".
17
		 "       sched_reload <options> -e pid,eid\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
18 19 20
	"Use -i to specify an imageid. Use node default otherwise.\n".
	"Use -f to force reload. Fail if node cannot be reserved.\n".
	"Use -p to pend reload for the reload daemon.\n".
21
	"Use -e to schedule a reload for all nodes in an experiment.\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
22
	"Use -r to use Frisbee rather than netdisk (experimental).\n";
23 24
    exit(-1);
}
25
my  $optlist = "fpri:e:";
26 27 28 29

#
# Configure variables
#
30
my $TB     = "@prefix@";
31

32 33 34
#
# Load the Testbed support stuff. 
#
35 36
use lib "@prefix@/lib";
use libdb;
37

38
#
39
# These come from the library.
40
# 
41 42 43
my $RELOADPID	= NODERELOADING_PID;
my $RELOADEID	= NODERELOADING_EID;
my $PENDINGEID	= NODERELOADPENDING_EID;
44

45 46
my $osload      = "$TB/bin/os_load";
my $nalloc      = "$TB/bin/nalloc";
47
my $name        = "";
48 49 50
my $error       = 0;
my $debug       = 0;
my $force	= 0;
51
my $pend	= 0;
52
my $frisbee	= 0;
53
my @nodes       = ();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
54 55 56
my $usedefault  = 1;
my $imageid;
my %imagenodes  = ();
57
my @row;
58 59 60
my $eidmode     = 0;
my $pid;
my $eid;
61 62 63 64 65 66 67 68

# 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

#
69 70 71 72 73 74 75 76 77 78
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"f"})) {
    $force = $options{"f"};
}
79 80 81
if (defined($options{"p"})) {
    $pend = $options{"p"};
}
82 83 84 85
if (defined($options{"r"})) {
    $frisbee = 1;
	$osload .= " -r ";
}
86 87 88
if ($pend and $force) {
    usage();
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
89 90 91 92 93 94 95 96 97 98 99
if (defined($options{"i"})) {
    $imageid    = $options{"i"};
    $usedefault = 0;

    if ($imageid =~ /^([-\@\w.\+]+)$/) {
	$imageid = $1;
    }
    else {
	die("Bad data in $imageid.");
    }
}
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
if (defined($options{"e"})) {
    if (@ARGV) {
	usage();
    }
    
    $eidmode = $options{"e"};
    if ($eidmode =~ /([-\w]*),([-\w]*)/) {
	$pid = $1;
	$eid = $2;
    }
    else {
	print STDOUT "Invalid argument to -e option: $eidmode\n";
	usage();
    }
}
else {
    if (@ARGV < 1) {
	usage();
    }
}
120

121
# This type will be put into the database, to allow programs 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
122
# such as nfree to figure out what kind of reload is supposed
123 124
# to occur
if ($frisbee) {
125
    $type = TB_RELOADTYPE_FRISBEE;
126
} else {
127
    $type = TB_RELOADTYPE_NETDISK;
128 129
}

130 131 132 133
if ($eidmode) {
    if (! (@nodes = ExpNodes($pid, $eid))) {
	die("*** $0:\n".
	    "    There are no nodes allocated to experiment $pid/$eid!");
134
    }
135 136 137 138 139 140 141 142 143 144 145 146
}
else {
    #
    # Untaint nodes.
    #
    foreach my $node ( @ARGV ) {
	if ($node =~ /^([-\@\w]+)$/) {
	    $node = $1;
	}
	else {
	    die("Bad node name: $node.");
	}
147
    
148 149
	push(@nodes, $node);
    }
150
}
151

152
#
153 154
# Root and admin types can do whatever they want.
# Mere users cannot schedule reloads.
155
#
156
if ($UID && !TBAdmin($UID)) {
157 158
    die("*** $0:\n".
	"     Only root or TB administrators can schedule disk reloads.\n");
159 160 161
}

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
162 163
# Confirm a valid imageid if one was specified. Otherwise we are going
# to pull it out of the DB for each node.
164
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
165 166 167
if (defined($imageid)) {
    my $query_result =
	DBQueryFatal("select * from images where imageid='$imageid'");
168
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
169
    if ($query_result->numrows < 1) {
170 171
	die("*** $0:\n".
	    "    No such imageid $imageid is registered in the DB!");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
172
    }
173 174 175
}

#
176
# A loop.
177 178 179 180
# 
my @load_list=();
foreach my $node (@nodes) {
    my $pc = $node;
181
    my $allocated = 0;
182

183
    $sth = DBQueryFatal("select * from nodes where node_id='$pc'");
184 185 186 187
    if ($sth->num_rows() != 1) {
      print STDERR "Node $pc doesn't exist. Skipping $pc.\n";
      next;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
188 189 190 191 192 193

    #
    # Get default imageid for this node if none specified on comand line.
    #
    if ($usedefault) {
	if (! ($imageid = DefaultImageID($node))) {
194 195
	    die("*** $0:\n".
		"    No default imageid is defined for $node!\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
196 197
	}
    }
198
    
199
    print STDERR "Checking if $pc is reserved...";
200
    $sth = DBQueryFatal("select * from reserved where node_id='$pc'");
201
   
202 203
    if ( ($sth->num_rows()) < 1) {
        print STDERR "Available.\nReserving and adding to list.\n";
204 205 206 207 208 209 210 211 212
	my $eid;
	if ($pend) {
	    $eid = $PENDINGEID;
	}
	else {
	    $eid = $RELOADEID;
	}
	my $cmd = "$nalloc $RELOADPID $eid $pc";

213 214 215
        if ( system($cmd) != 0 ) {
	    print STDERR "WARNING: Could not reserve $pc!\n";
	} else {
216 217 218 219
	    #
	    # Kill the last_reservation so that whoever gets the node next
	    # won't be fooled into thinking a reload is required.
	    #
220
	    DBQueryFatal("delete from last_reservation where node_id='$pc'");
221
	    $allocated = 1;
222 223 224 225
	}
    } else {
        print STDERR "Reserved.\n";      
    }
226 227 228 229 230 231 232 233 234 235

    #
    # 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.
236 237 238 239 240
    if (! TBSetSchedReload($pc, $imageid, $type)) {
	die("*** $0:\n".
	    "    Could not set scheduled reload for $pc!"); 
    }
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    #
    # The point of this hash table is so that we can gather up all the
    # nodes per imageid and issue single requests to os_load, so that it
    # can optimize things (say, for Frisbee). It is possible to get multiple
    # imageids when using the node defaults instead of a command line imageid.
    #
    # Note that building up a hashed array of lists is a mighty odd operation
    # in PERL, hence this funny looking code!
    #
    if ($allocated) {
	if (! defined($imagenodes{$imageid})) {
	    $imagenodes{$imageid} = [ $node ];
	}
	else {
	    push(@{ $imagenodes{$imageid} }, $node);
	}
    }
258 259
}

260
if ($pend) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
261
    print STDOUT "Reload Scheduling Done! There were $error failures!\n";
262 263 264
    exit $error;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278
#
# Now issue the reloads, one per imageid. The hash table we created above
# stores a list of nodes for each imageid.
#
foreach my $id ( keys(%imagenodes) ) {
    my @nodelist = @{ $imagenodes{$imageid} };

    my $cmd = "$osload -i $id @nodelist";

    print "Issuing $cmd\n";
    if (system($cmd)) {
	print STDERR "*** WARNING: Failed $cmd\n";
	$error++;
    }
279 280
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
281
print STDOUT "Reload Scheduling Done! There were $error failures!\n";
282
exit $error;