sched_reload.in 7.63 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7 8
# All rights reserved.
#

9
use English;
10
use Getopt::Std;
11 12 13 14 15 16 17
   
#
# 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.
# 
18 19
sub usage()
{
20
    print STDOUT "Usage: sched_reload [-f | -n] [-m <imageid>] ".
21
	         "[[-p <pid>] -i <imagename>] <node> [node ...]\n".
22
		 "       sched_reload <options> -e pid,eid\n".
23
		 "       sched_reload <options> -t type [type ...]\n".
24 25
	"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
26
	"Use -f to force reload. Fail if node cannot be reserved.\n".
27
	"Use -n to pend reload for the reload daemon.\n".
28 29
        "Use -e to schedule a reload for all nodes in an experiment.\n".
        "Use -t to schedule a reload for all nodes of a particular type.\n";
30 31
    exit(-1);
}
32
my  $optlist = "fnp:i:e:m:t";
33 34 35 36

#
# Configure variables
#
37
my $TB     = "@prefix@";
38

39 40 41
#
# Load the Testbed support stuff. 
#
42 43
use lib "@prefix@/lib";
use libdb;
44

45
#
46
# These come from the library.
47
# 
48 49 50
my $RELOADPID	= NODERELOADING_PID;
my $RELOADEID	= NODERELOADING_EID;
my $PENDINGEID	= NODERELOADPENDING_EID;
51

Leigh B. Stoller's avatar
Leigh B. Stoller committed
52
my $osload      = "$TB/bin/os_load -s";
53
my $nalloc      = "$TB/bin/nalloc";
54
my $name        = "";
55 56 57
my $error       = 0;
my $debug       = 0;
my $force	= 0;
58
my $pend	= 0;
59
my @nodes       = ();
60
my $type	= TB_DEFAULT_RELOADTYPE;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
61
my $usedefault  = 1;
62
my $imagename;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
63
my $imageid;
64
my $imagepid    = TB_OPSPID;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
65
my %imagenodes  = ();
66
my @row;
67
my $eidmode     = 0;
68 69
my $typemode    = 0;
my @types	= ();
70 71
my $pid;
my $eid;
72 73 74 75 76 77 78 79

# 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

#
80 81 82 83 84 85 86 87
# 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"})) {
88
    $force = 1;
89 90
}
if (defined($options{"n"})) {
91
    $pend = 1;
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
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();
    }
}
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
elsif (defined($options{"t"})) {
    if (!@ARGV) {
	usage();
    }
    
    $typemode = 1;

    #
    # Untaint types,
    #
    foreach my $type ( @ARGV ) {
	if ($type =~ /^([-\w]+)$/) {
	    $type = $1;
	}
	else {
	    die("Bad type name: $type.");
	}
    
	push(@types, $type);
    }
}
168 169 170 171 172
else {
    if (@ARGV < 1) {
	usage();
    }
}
173

174 175 176 177
if ($eidmode) {
    if (! (@nodes = ExpNodes($pid, $eid))) {
	die("*** $0:\n".
	    "    There are no nodes allocated to experiment $pid/$eid!");
178
    }
179
}
180 181 182 183 184 185 186 187 188 189 190
elsif ($typemode) {
    foreach my $type ( @types ) {
	my $query_result =
	    DBQueryFatal("select node_id from nodes ".
			 "where type='$type' and role='testnode'");

	while (my ($node) = $query_result->fetchrow_array()) {
	    push(@nodes, $node);
	}
    }
}
191 192 193 194 195 196 197 198 199 200 201
else {
    #
    # Untaint nodes.
    #
    foreach my $node ( @ARGV ) {
	if ($node =~ /^([-\@\w]+)$/) {
	    $node = $1;
	}
	else {
	    die("Bad node name: $node.");
	}
202
    
203 204
	push(@nodes, $node);
    }
205
}
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
#
# 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);
}

224
#
225 226
# Root and admin types can do whatever they want.
# Mere users cannot schedule reloads.
227
#
228
if ($UID && !TBAdmin($UID)) {
229 230
    die("*** $0:\n".
	"     Only root or TB administrators can schedule disk reloads.\n");
231 232 233
}

#
234
# Confirm a valid imagename if one was specified. Otherwise we are going
Leigh B. Stoller's avatar
Leigh B. Stoller committed
235
# to pull it out of the DB for each node.
236
#
237 238
if (defined($imagename)) {
    if (! ($imageid = TBImageID($imagepid, $imagename))) {
239
	die("*** $0:\n".
240
	    "    No such image $imagename in project $imagepid!\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
241
    }
242 243 244
}

#
245
# Loop through each node.
246 247 248 249
# 
my @load_list=();
foreach my $node (@nodes) {
    my $pc = $node;
250
    my $allocated = 0;
251

252
    $sth = DBQueryFatal("select node_id from nodes where node_id='$pc'");
253 254 255 256
    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
257 258 259 260 261 262

    #
    # Get default imageid for this node if none specified on comand line.
    #
    if ($usedefault) {
	if (! ($imageid = DefaultImageID($node))) {
263 264
	    die("*** $0:\n".
		"    No default imageid is defined for $node!\n");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
265 266
	}
    }
267
    
268
    print STDERR "Checking if $pc is reserved...";
269
    $sth = DBQueryFatal("select node_id from reserved where node_id='$pc'");
270
   
271 272
    if ( ($sth->num_rows()) < 1) {
        print STDERR "Available.\nReserving and adding to list.\n";
273 274 275 276 277 278 279 280 281
	my $eid;
	if ($pend) {
	    $eid = $PENDINGEID;
	}
	else {
	    $eid = $RELOADEID;
	}
	my $cmd = "$nalloc $RELOADPID $eid $pc";

282 283 284
        if ( system($cmd) != 0 ) {
	    print STDERR "WARNING: Could not reserve $pc!\n";
	} else {
285 286 287 288
	    #
	    # Kill the last_reservation so that whoever gets the node next
	    # won't be fooled into thinking a reload is required.
	    #
289
	    DBQueryFatal("delete from last_reservation where node_id='$pc'");
290
	    $allocated = 1;
291 292 293 294
	}
    } else {
        print STDERR "Reserved.\n";      
    }
295 296 297 298 299 300 301 302 303 304

    #
    # 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.
305 306 307 308 309
    if (! TBSetSchedReload($pc, $imageid, $type)) {
	die("*** $0:\n".
	    "    Could not set scheduled reload for $pc!"); 
    }
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    #
    # 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);
	}
    }
327 328
}

329
if ($pend) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
330
    print STDOUT "Reload Scheduling Done! There were $error failures!\n";
331 332 333
    exit $error;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
334 335 336 337 338 339 340
#
# 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} };

341
    my $cmd = "$osload -m $id @nodelist";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
342 343 344 345 346 347

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

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