prereserve.in 7.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2003-2011 University of Utah and the Flux Group.
# All rights reserved.
#
use strict;
use English;
use Getopt::Std;

#
# Set up and clear node pre-reservations.
#
sub usage()
{
    print STDERR "Usage: prereserve [-t typelist] [-n priority] pid count\n";
    print STDERR "       prereserve -c [-r] pid\n";
    print STDERR "       prereserve -i pid\n";
    print STDERR "       prereserve -l\n";
    print STDERR "   -h   This message\n";
    print STDERR "   -t   Comma separated list of node types\n";
    print STDERR "   -n   Priority. Defaults to zero (least priority)\n";
    print STDERR "   -c   Clear pending prereserve for project\n";
    print STDERR "   -r   Revoke current prereserve for project (use with -c)\n";
    print STDERR "   -i   Show pending prereserve for project\n";
    print STDERR "   -l   List all pending prereserves\n";
    exit(-1);
}
my $optlist  = "hdct:n:ilr";
my $priority = 0;
my $debug    = 0;
my $info     = 0;
my $list     = 0;
my $clear    = 0;
my $revoke   = 0;
Leigh Stoller's avatar
Leigh Stoller committed
36
my $typelist = "";
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
my $pid;
my $count;

# Protos
sub fatal($);

#
# Please do not run as root. Hard to track what has happened.
#
if ($UID == 0) {
    die("*** $0:\n".
	"    Please do not run this as root!\n");
}

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

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use emdb;
use NodeType;
use Node;
use libtestbed;
use Experiment;
use Project;
use User;

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{h})) {
    usage();
}
if (defined($options{n})) {
    $priority = $options{n};
}
if (defined($options{c})) {
    $clear = 1;
}
if (defined($options{r})) {
    $revoke = 1;
}
if (defined($options{d})) {
    $debug = 1;
}
if (defined($options{t})) {
    $typelist = $options{t};
}
if (defined($options{i})) {
    $info = 1;
}
if (defined($options{l})) {
    $list = 1;
}
if ($info || $clear || $revoke) {
    usage()
	if (@ARGV != 1 || ($revoke && !$clear));
    
    $pid = $ARGV[0];
}
elsif ($list) {
    usage()
	if (@ARGV);
}
else {
    usage()
	if (@ARGV != 2);
    
    $pid   = $ARGV[0];
    $count = $ARGV[1];

    if ($priority && ! ($priority =~ /^\d*$/)) {
	usage();
    }
}

#
# Verify user, must be admin.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}
if (!$this_user->IsAdmin()) {
    fatal("You are not a testbed administrator!");
}
my $uid = $this_user->uid();
my $uid_idx = $this_user->uid_idx();

#
# List all pending prereserves.
#
if ($list) {
    my $query_result = 
	DBQueryFatal("select * from node_reservations ".
		     "order by priority desc, pid asc");

    if ($query_result->numrows) {
	printf("%-15s %-10s %-10s %-18s %-3s %s\n",
	       "Project", "Cnt (Cur)", "Creator", "When", "Pri", "Types");
	print "-------------------------------------------------------------\n";
    }
    
    while (my $row = $query_result->fetchrow_hashref()) {
	my $pid     = $row->{'pid'};
	my $count   = $row->{'count'};
	my $created = $row->{'created'};
	my $creator = $row->{'creator'};
	my $types   = $row->{'types'} || "";
	my $priority= $row->{'priority'};
	my $current = 0;
    
	my $current_result =
	    DBQueryFatal("select count(*) from nodes where reserved_pid='$pid'");

	($current) = $current_result->fetchrow_array()
	    if ($current_result && $current_result->numrows);

	printf("%-15s %-10s %-10s %-18s %-3d %s\n",
	       $pid, "$count ($current)", $creator, $created, $priority, $types);
    }
    exit(0);
}

my $project = Project->Lookup($pid);
if (!defined($project)) {
    fatal("No such project $pid\n");
}
my $pid_idx = $project->pid_idx();

#
# Clear and exit.
#
if ($clear) {
    DBQueryFatal("delete from node_reservations where pid_idx='$pid_idx'");
    if ($revoke) {
	DBQueryFatal("update nodes set reserved_pid=null ".
		     "where reserved_pid='$pid'");
    }
    exit(0);
}

#
# Show and exit.
#
if ($info) {
    my $current = 0;
    
    my $query_result =
	DBQueryFatal("select count(*) from nodes where reserved_pid='$pid'");

    ($current) = $query_result->fetchrow_array()
	if ($query_result && $query_result->numrows);
    
    $query_result =
	DBQueryFatal("select * from node_reservations ".
		     "where pid_idx='$pid_idx'");

    if ($query_result->numrows) {
	my $row = $query_result->fetchrow_hashref();
	my $pid     = $row->{'pid'};
	my $count   = $row->{'count'};
	my $created = $row->{'created'};
	my $creator = $row->{'creator'};
	my $types   = $row->{'types'} || "*";
	my $priority= $row->{'priority'};

	printf("%-15s %-10s %-10s %-18s %-3s %s\n",
	       "Project", "Cnt (Cur)", "Creator", "When", "Pri", "Types");
	print "-------------------------------------------------------------\n";
	printf("%-15s %-10s %-10s %-18s %-3d %s\n",
	       $pid, "$count ($current)", $creator, $created, $priority, $types);
    }
    exit(0);
}

# Sanity check the type list.
my @types = ();
Leigh Stoller's avatar
Leigh Stoller committed
234
if ($typelist ne "") {
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    @types = split(",", $typelist);
    foreach my $typename (@types) {
	my $type = NodeType->Lookup($typename);
	if (!defined($type)) {
	    fatal("No such node type $typename");
	}
    }
}
else {
    @types = ("*");
}

# 
# Lets say that a current request is an error. delete and recreate.
#
my $query_result =
    DBQueryFatal("select * from node_reservations where pid_idx='$pid_idx'");

if ($query_result->numrows) {
    fatal("Already have a reservation request for $pid, please clear it first");
}

#
# First see if we can find enough (or any) nodes to satisfy the prereserve.
#
$query_result =
    DBQueryFatal("select node_id from nodes where reserved_pid='$pid'");

my $current = $query_result->numrows;

if ($current) {
    print "There are currently $current nodes with a pre-reservation ".
	"for project $pid.\n";
    if ($current >= $count) {
Leigh Stoller's avatar
Leigh Stoller committed
269
	goto done;
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    }
}

#
# First check free nodes.
#
foreach my $type (@types) {
    last
	if ($current >= $count);

    my $tcount  = 0;
    my $tclause = "";
    if ($type ne "*") {
	$tclause = "and node_types.type='$type'";
    }
    #
    # It is not possible to combine multiple table update and a limit.
    # So, have to lock the nodes and reserved table for a moment, and
    # do it the hard way. 
    #
    DBQueryFatal("lock tables nodes write, node_types read, reserved read");
    
    $query_result =
	DBQueryFatal("select nodes.node_id from nodes ".
		     "left join reserved on reserved.node_id=nodes.node_id ".
		     "left join node_types on node_types.type=nodes.type ".
		     "where reserved.node_id is null and ".
		     "      nodes.role='testnode' and ".
		     "      node_types.class='pc' and ".
		     "      nodes.reserved_pid is null $tclause");
    
    while (my ($node_id) = $query_result->fetchrow_array()) {
	DBQueryFatal("update nodes set reserved_pid='$pid' ".
		     "where node_id='$node_id'");
	$current++;
	$tcount++;
	last
	    if ($current >= $count);
    }
    if ($tcount) {
	print "Set reserved_pid for $tcount (free)" .
	    ($type eq "*" ? "" : " $type") . " nodes.\n";
    }
}
DBQueryFatal("unlock tables");
if ($current >= $count) {
Leigh Stoller's avatar
Leigh Stoller committed
316
    print "Got as many nodes as you wanted the free pool. Yippie!\n";
317 318 319 320 321 322
}

#
# Still need more nodes. Insert a node_reservation entry for nfree, for the
# remaining nodes we need.
#
Leigh Stoller's avatar
Leigh Stoller committed
323
done:
324
$count -= $current;
Leigh Stoller's avatar
Leigh Stoller committed
325
$count = 0 if ($count < 0);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

DBQueryFatal("insert into node_reservations set ".
	     "  pid='$pid', pid_idx='$pid_idx', count='$count', ".
	     "  types='$typelist', creator='$uid', creator_idx='$uid_idx', ".
	     "  created=now()");
print "Node reservation request for $count nodes has been created.\n";
exit(0);

sub fatal($)
{
    my ($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}