sched_reload.in 6.59 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 | -p] [-r|-n] [-i <imageid>] <node> [node ...]
12 13 14
#
sub usage()
{
15
    print STDOUT "Usage: sched_reload [-f | -p] [-r | -n] [-i <imageid>] ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
16
	         "<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".
22 23
	"Use -r to use Frisbee for reloading disks.\n" .
	"Use -n to use netdisk for reloading disks.\n";
24 25
    exit(-1);
}
26
my  $optlist = "fprni:e:";
27 28 29 30

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

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

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

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

# 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

#
70 71 72 73 74 75 76 77 78 79
# 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"};
}
80 81 82
if (defined($options{"p"})) {
    $pend = $options{"p"};
}
83
if (defined($options{"r"})) {
84 85 86 87 88 89 90
    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;
91
}
92 93 94
if ($pend and $force) {
    usage();
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
95 96 97 98 99 100 101 102 103 104 105
if (defined($options{"i"})) {
    $imageid    = $options{"i"};
    $usedefault = 0;

    if ($imageid =~ /^([-\@\w.\+]+)$/) {
	$imageid = $1;
    }
    else {
	die("Bad data in $imageid.");
    }
}
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
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();
    }
}
126

127 128 129 130 131 132 133 134
#
# 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 ';
135 136
}

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

159
#
160 161
# Root and admin types can do whatever they want.
# Mere users cannot schedule reloads.
162
#
163
if ($UID && !TBAdmin($UID)) {
164 165
    die("*** $0:\n".
	"     Only root or TB administrators can schedule disk reloads.\n");
166 167 168
}

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

#
183
# A loop.
184 185 186 187
# 
my @load_list=();
foreach my $node (@nodes) {
    my $pc = $node;
188
    my $allocated = 0;
189

190
    $sth = DBQueryFatal("select * from nodes where node_id='$pc'");
191 192 193 194
    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
195 196 197 198 199 200

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

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

    #
    # 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.
243 244 245 246 247
    if (! TBSetSchedReload($pc, $imageid, $type)) {
	die("*** $0:\n".
	    "    Could not set scheduled reload for $pc!"); 
    }
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    #
    # 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);
	}
    }
265 266
}

267
if ($pend) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
268
    print STDOUT "Reload Scheduling Done! There were $error failures!\n";
269 270 271
    exit $error;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
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} };

279
    my $cmd = "$osload $osload_flags -i $id @nodelist";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
280 281 282 283 284 285

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

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