sched_reload.in 7.38 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 12
sub usage()
{
13 14
    print STDOUT "Usage: sched_reload [-f | -p] [-r | -n] [-m <imageid>] ".
	         "[[-p <pid>] -i <imagename>] <node> [node ...]\n".
15
		 "       sched_reload <options> -e pid,eid\n".
16 17
	"Use -i to specify an image name. Use node default otherwise.\n".
	"Use -m to specify an image ID (internal name, TB admins only!).\n".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
18 19
	"Use -f to force reload. Fail if node cannot be reserved.\n".
	"Use -p to pend reload for the reload daemon.\n".
20
	"Use -e to schedule a reload for all nodes in an experiment.\n".
21 22
	"Use -r to use Frisbee for reloading disks.\n" .
	"Use -n to use netdisk for reloading disks.\n";
23 24
    exit(-1);
}
25
my  $optlist = "fprni:e:m:";
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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
45
my $osload      = "$TB/bin/os_load -s";
46
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 $type	= TB_DEFAULT_RELOADTYPE;
53
my @nodes       = ();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
54
my $usedefault  = 1;
55
my $imagename;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
56
my $imageid;
57
my $imagepid    = TB_OPSPID;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
58
my %imagenodes  = ();
59
my @row;
60 61 62
my $eidmode     = 0;
my $pid;
my $eid;
63 64 65 66 67 68 69 70

# 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

#
71 72 73 74 75 76 77 78 79 80
# 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"};
}
81 82 83
if (defined($options{"p"})) {
    $pend = $options{"p"};
}
84
if (defined($options{"r"})) {
85 86 87 88 89 90 91
    if (defined($options{"n"})) {
	die "Only one of -r or -n should be given\n";
    }
    $type = TB_RELOADTYPE_FRISBEE;
}
if (defined($options{"n"})) {
	$type = TB_RELOADTYPE_NETDISK;
92
}
93 94 95
if ($pend and $force) {
    usage();
}
96 97 98
if (defined($options{"i"}) && defined($options{"m"})) {
    usage();
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
99
if (defined($options{"i"})) {
100
    $imagename  = $options{"i"};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
101 102
    $usedefault = 0;

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    if ($imagename =~ /^([-\w\.\+]+)$/) {
	$imagename = $1;
    }
    else {
	die("Bad data in $imagename.");
    }
    
    if (defined($options{"p"})) {
	$imagepid = $options{"p"};
	
	if ($imagepid =~ /^([-\w\.\+]+)$/) {
	    $imagepid = $1;
	}
	else {
	    die("*** Bad data in $imagepid.\n");
	}
    }
}
if (defined($options{"m"})) {
    $imageid = $options{"m"};
    $usedefault = 0;

    if ($imageid =~ /^([-\w\.\+]+)$/) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
126 127 128
	$imageid = $1;
    }
    else {
129
	die("*** Bad data in $imageid\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
130 131
    }
}
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
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();
    }
}
152

153 154 155 156 157 158 159 160
#
# Depending on the reload type, we pass different flags to os_load
#
my $osload_flags = "";
if ($type eq TB_RELOADTYPE_NETDISK) {
    $osload_flags .= ' -n ';
} elsif ($type eq TB_RELOADTYPE_FRISBEE) {
    $osload_flags .= ' -r ';
161 162
}

163 164 165 166
if ($eidmode) {
    if (! (@nodes = ExpNodes($pid, $eid))) {
	die("*** $0:\n".
	    "    There are no nodes allocated to experiment $pid/$eid!");
167
    }
168 169 170 171 172 173 174 175 176 177 178 179
}
else {
    #
    # Untaint nodes.
    #
    foreach my $node ( @ARGV ) {
	if ($node =~ /^([-\@\w]+)$/) {
	    $node = $1;
	}
	else {
	    die("Bad node name: $node.");
	}
180
    
181 182
	push(@nodes, $node);
    }
183
}
184

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
#
# VIRTNODE HACK: Virtual nodes are special. Do not reload!
#
my @temp = ();
foreach my $node ( @nodes ) {
    if (TBIsNodeVirtual($node)) {
	print "*** Skipping virtual node $node ...\n";
	next;
    }
    push(@temp, $node);
}
@nodes = @temp;
if (! @nodes) {
    print "No nodes to load. Exiting ...\n";
    exit(0);
}

202
#
203 204
# Root and admin types can do whatever they want.
# Mere users cannot schedule reloads.
205
#
206
if ($UID && !TBAdmin($UID)) {
207 208
    die("*** $0:\n".
	"     Only root or TB administrators can schedule disk reloads.\n");
209 210 211
}

#
212
# Confirm a valid imagename if one was specified. Otherwise we are going
Leigh B. Stoller's avatar
Leigh B. Stoller committed
213
# to pull it out of the DB for each node.
214
#
215 216
if (defined($imagename)) {
    if (! ($imageid = TBImageID($imagepid, $imagename))) {
217
	die("*** $0:\n".
218
	    "    No such image $imagename in project $imagepid!\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
219
    }
220 221 222
}

#
223
# Loop through each node.
224 225 226 227
# 
my @load_list=();
foreach my $node (@nodes) {
    my $pc = $node;
228
    my $allocated = 0;
229

230
    $sth = DBQueryFatal("select * from nodes where node_id='$pc'");
231 232 233 234
    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
235 236 237 238 239 240

    #
    # Get default imageid for this node if none specified on comand line.
    #
    if ($usedefault) {
	if (! ($imageid = DefaultImageID($node))) {
241 242
	    die("*** $0:\n".
		"    No default imageid is defined for $node!\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
243 244
	}
    }
245
    
246
    print STDERR "Checking if $pc is reserved...";
247
    $sth = DBQueryFatal("select * from reserved where node_id='$pc'");
248
   
249 250
    if ( ($sth->num_rows()) < 1) {
        print STDERR "Available.\nReserving and adding to list.\n";
251 252 253 254 255 256 257 258 259
	my $eid;
	if ($pend) {
	    $eid = $PENDINGEID;
	}
	else {
	    $eid = $RELOADEID;
	}
	my $cmd = "$nalloc $RELOADPID $eid $pc";

260 261 262
        if ( system($cmd) != 0 ) {
	    print STDERR "WARNING: Could not reserve $pc!\n";
	} else {
263 264 265 266
	    #
	    # Kill the last_reservation so that whoever gets the node next
	    # won't be fooled into thinking a reload is required.
	    #
267
	    DBQueryFatal("delete from last_reservation where node_id='$pc'");
268
	    $allocated = 1;
269 270 271 272
	}
    } else {
        print STDERR "Reserved.\n";      
    }
273 274 275 276 277 278 279 280 281 282

    #
    # 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.
283 284 285 286 287
    if (! TBSetSchedReload($pc, $imageid, $type)) {
	die("*** $0:\n".
	    "    Could not set scheduled reload for $pc!"); 
    }
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    #
    # 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);
	}
    }
305 306
}

307
if ($pend) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
308
    print STDOUT "Reload Scheduling Done! There were $error failures!\n";
309 310 311
    exit $error;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
312 313 314 315 316 317 318
#
# 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} };

319
    my $cmd = "$osload $osload_flags -i $id @nodelist";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
320 321 322 323 324 325

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

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