prereserve.in 12.1 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2003-2012 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26
#
use strict;
use English;
use Getopt::Std;
27
use Date::Parse;
28 29 30 31 32 33

#
# Set up and clear node pre-reservations.
#
sub usage()
{
34 35
    print STDERR "Usage: prereserve [-t typelist] [-n priority] ".
	"[-s start] [-e end [-r]] pid count\n";
36 37
    print STDERR "       prereserve -c [-r] pid\n";
    print STDERR "       prereserve -i pid\n";
38
    print STDERR "       prereserve -a pid\n";
39 40 41 42 43 44 45 46
    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";
47 48 49 50
    print STDERR "   -s   Optional start time to begin pre reservation\n";
    print STDERR "   -e   Optional end time for pre reservation.\n";
    print STDERR "        Implies -c -r options at termination time.\n";
    print STDERR "   -a   Activate a pending reservation (internal option)\n";
51 52
    exit(-1);
}
53
my $optlist  = "hdct:n:ilre:s:ma";
54 55 56 57 58 59
my $priority = 0;
my $debug    = 0;
my $info     = 0;
my $list     = 0;
my $clear    = 0;
my $revoke   = 0;
60 61 62 63
my $sendmail = 0;
my $activate = 0;
my $starttime;
my $endtime;
64
my $typelist;
65 66 67 68 69
my $pid;
my $count;

# Protos
sub fatal($);
70
sub StartReservation($);
71 72 73 74 75 76 77 78 79 80 81 82 83

#
# 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@";
84
my $TBOPS        = "@TBOPSEMAIL@";
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

#
# 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;
}
131 132 133 134 135 136
if (defined($options{"m"})) {
    $sendmail = 1;
}
if (defined($options{"a"})) {
    $activate = 1;
}
137 138 139 140 141 142 143 144 145
if (defined($options{t})) {
    $typelist = $options{t};
}
if (defined($options{i})) {
    $info = 1;
}
if (defined($options{l})) {
    $list = 1;
}
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
if (defined($options{"e"})) {
    $endtime = $options{"e"};
    if (!defined(str2time($endtime))) {
	fatal("Could not parse -e option.");
    }
}
if (defined($options{"s"})) {
    $starttime = $options{"s"};
    
    if (!defined(str2time($starttime))) {
	fatal("Could not parse -s option.");
    }
}

if ($info || $clear || ($revoke && !$endtime)) {
161 162 163 164 165 166 167 168 169
    usage()
	if (@ARGV != 1 || ($revoke && !$clear));
    
    $pid = $ARGV[0];
}
elsif ($list) {
    usage()
	if (@ARGV);
}
170 171 172 173 174 175
elsif ($activate) {
    usage()
	if (@ARGV != 1);

    exit(StartReservation($ARGV[0]));
}
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
else {
    usage()
	if (@ARGV != 2);
    
    $pid   = $ARGV[0];
    $count = $ARGV[1];

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

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

    if ($query_result->numrows) {
197 198
	printf("%-18s %-4s  %-3s  %-10s %-18s %-3s %s\n",
	       "Project", "Need", "Got", "Creator", "Created", "Pri", "Types");
199 200 201 202 203 204 205 206 207 208
	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'};
209 210 211
	my $starttime = $row->{'start'};
	my $endtime   = $row->{'end'};
	my $active    = $row->{'active'};
212 213 214 215 216 217 218 219
	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);

220 221 222 223 224 225 226 227 228 229 230 231 232 233
	printf("%-20s %-4d %-3d %-10s %-18s %-3d %s\n",
	       $pid, $count, $current, $creator, $created,
	       $priority, $types);

	if (defined($starttime)) {
	    print "  *** Starts: $starttime";
	    if (! $active) {
		print " (pending)";
	    }
	    if (defined($endtime)) {
		print " Ends: $endtime";
	    }
	    print "\n";
	}
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    }
    exit(0);
}

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

#
# Show and exit.
#
if ($info) {
    my $current = 0;
    
250 251
    my $nodes_result =
	DBQueryFatal("select node_id from nodes where reserved_pid='$pid'");
252

253 254
    ($current) = $nodes_result->numrows
	if ($nodes_result && $nodes_result->numrows);
255
    
256
    my $query_result =
257 258 259 260 261 262 263 264 265 266 267
	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'};
268 269 270
	my $starttime = $row->{'start'};
	my $endtime   = $row->{'end'};
	my $active    = $row->{'active'};
271

272 273
	printf("%-4s  %-3s %-10s %-18s %-3s %s\n",
	       "Need", "Got", "Creator", "When", "Pri", "Types");
274
	print "-------------------------------------------------------------\n";
275 276 277 278 279 280 281 282 283 284 285 286 287
	printf("%-4s  %-3s %-10s %-18s %-3d %s\n",
	       $count, $current, $creator, $created, $priority, $types);

	if (defined($starttime)) {
	    print "*** Starts: $starttime";
	    if (! $active) {
		print " (pending)";
	    }
	    if (defined($endtime)) {
		print " Ends: $endtime";
	    }
	    print "\n";
	}
288
    }
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    if ($current) {
	print "-------------------------------------------------------------\n";
	printf("%-15s %-10s %-32s\n",
	       "NodeID", "Type", "Pid/Eid");
	print "-------------------------------------------------------------\n";

	while (my ($node_id) = $nodes_result->fetchrow_array()) {
	    my $node   = Node->Lookup($node_id);
	    my $type   = $node->type();
	    my $pideid = "--";

	    if ($node->IsReserved()) {
		$pideid = $node->pid() . "/" . $node->eid();
	    }
	    printf("%-15s %-10s %-32s\n", $node_id, $type, $pideid);
	}
    }
306 307 308
    exit(0);
}

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
#
# Verify user, must be admin or root.
#
my $this_user;
if ($UID) {
    $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!");
    }
}

#
# 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);
}

#
# Do not allow this as root; we want proper history.
#
if ($UID == 0) {
    fatal("Please do not run this as root!");
}
my $uid = $this_user->uid();
my $uid_idx = $this_user->uid_idx();

344
# Sanity check the type list.
345
if (defined($typelist)) {
346
    my @types = split(",", $typelist);
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    foreach my $typename (@types) {
	my $type = NodeType->Lookup($typename);
	if (!defined($type)) {
	    fatal("No such node type $typename");
	}
    }
}

# 
# 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");
}

#
366
# Enter the table info, but mark as not active until later.
367
#
368 369 370
my $typearg  = (defined($typelist)  ? ",type='$typelist'" : "");
my $startarg = "";
my $endarg   = "";
371

372 373 374 375 376 377 378 379 380 381 382 383 384
if ($starttime) {
    my $tmp = str2time($starttime);
    $startarg = ",start=FROM_UNIXTIME($tmp)";
}
if ($endtime) {
    my $tmp = str2time($endtime);
    $endarg = ",end=FROM_UNIXTIME($tmp)";
}
DBQueryFatal("insert into node_reservations set ".
	     "  pid='$pid', pid_idx='$pid_idx', count='$count', ".
	     "  creator='$uid', creator_idx='$uid_idx', ".
	     "  created=now(),active=0 $typearg $startarg $endarg");
print "Node reservation request for $count nodes has been created.\n";
385

386 387 388 389 390 391
#
# Activate, although note that the cron job (prereserve_check) might
# have beat us to it already. We check the active bit below.
#
if (!defined($starttime) || str2time($starttime) <= time()) {
    exit(StartReservation($pid));
392
}
393
exit(0);
394 395

#
396 397
# Activate a reservation request; find as many nodes as possible,
# and then mark it as active. 
398
#
399 400 401
sub StartReservation($)
{
    my ($pid) = @_;
402

403 404 405 406 407 408 409
    DBQueryFatal("lock tables nodes write, node_types read, ".
		 " node_reservations write, reserved read");
    
    my $query_result =
	DBQueryFatal("select * from node_reservations where pid='$pid'");
    if (!$query_result->numrows) {
	fatal("No reservation defined for project");
410
    }
411 412 413 414 415 416 417 418 419 420 421 422
    my $row = $query_result->fetchrow_hashref();
    my $active = $row->{'active'};
    my $count  = $row->{'count'};
    my $types  = $row->{'types'};
    my @types  = (defined($types) ? split(",", $types) : ("*"));

    # Someone beat us to it. 
    if ($active) {
	DBQueryFatal("unlock tables");
	return 0;
    }
    
423
    #
424
    # First see if we can find enough (or any) nodes to satisfy the prereserve.
425 426
    #
    $query_result =
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	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) {
	    goto done;
	}
    }

    #
    # First check free nodes.
    #
    foreach my $type (@types) {
443 444
	last
	    if ($current >= $count);
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473

	my $tcount  = 0;
	my $tclause = "";
	if ($type ne "*") {
	    $tclause = "and node_types.type='$type'";
	}
    
	$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";
	}
474
    }
475 476
    if ($current >= $count) {
	print "Got as many nodes as you wanted from the free pool. Yippie!\n";
477
    }
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
    #
    # Update the reservation entry, and mark as active.
    #
  done:
    $count -= $current;
    $count = 0 if ($count < 0);

    DBQueryFatal("update node_reservations set ".
		 "       count='$count',active=1 ".
		 "where pid='$pid'");
    DBQueryFatal("unlock tables");

    if ($count == 0 && $sendmail) {
	SENDMAIL($TBOPS, "Pre Reservation for $pid has completed",
		 "The pre reservation request for project $pid, ".
		 "has been fullfilled\n", $TBOPS);
    }
    return 0
496 497 498 499 500 501 502 503 504 505
}

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

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