prereserve.in 13.3 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2003-2016 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
my $pid;
my $count;
67
my $project;
68 69 70

# Protos
sub fatal($);
71
sub StartReservation($);
72 73 74 75 76

#
# Configure variables
#
my $TB		 = "@prefix@";
77
my $TBOPS        = "@TBOPSEMAIL@";
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

#
# 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;
}
124 125 126 127 128 129
if (defined($options{"m"})) {
    $sendmail = 1;
}
if (defined($options{"a"})) {
    $activate = 1;
}
130 131 132 133 134 135 136 137 138
if (defined($options{t})) {
    $typelist = $options{t};
}
if (defined($options{i})) {
    $info = 1;
}
if (defined($options{l})) {
    $list = 1;
}
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
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)) {
154 155 156 157 158 159 160 161 162
    usage()
	if (@ARGV != 1 || ($revoke && !$clear));
    
    $pid = $ARGV[0];
}
elsif ($list) {
    usage()
	if (@ARGV);
}
163 164 165 166 167 168
elsif ($activate) {
    usage()
	if (@ARGV != 1);

    exit(StartReservation($ARGV[0]));
}
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
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 = 
186 187
	DBQueryFatal("select *,(count>0) as needed from node_reservations ".
		     "order by needed desc, priority desc, created asc");
188 189

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

213 214 215 216 217 218 219 220 221 222 223 224 225 226
	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";
	}
227 228 229 230
    }
    exit(0);
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
if ($pid =~ /^(.*):(.*)$/) {
    require GeniHRN;

    my $urn = GeniHRN::Generate($pid, "authority", "sa");

    $project = Project->LookupNonLocal($urn);
    if (!defined($project)) {
	fatal("No such nonlocal project $pid\n");
    }
    $pid = $project->pid();
}
else {
    $project = Project->Lookup($pid);

    if (!defined($project)) {
	fatal("No such project $pid\n");
    }
248 249 250 251 252 253 254 255 256
}
my $pid_idx = $project->pid_idx();

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

260 261
    ($current) = $nodes_result->numrows
	if ($nodes_result && $nodes_result->numrows);
262
    
263
    my $query_result =
264 265 266 267 268 269 270 271 272 273 274
	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'};
275 276 277
	my $starttime = $row->{'start'};
	my $endtime   = $row->{'end'};
	my $active    = $row->{'active'};
278

279 280
	printf("%-4s  %-3s %-10s %-18s %-3s %s\n",
	       "Need", "Got", "Creator", "When", "Pri", "Types");
281
	print "-------------------------------------------------------------\n";
282 283 284 285 286 287 288 289 290 291 292 293 294
	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";
	}
295
    }
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    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);
	}
    }
313 314 315
    exit(0);
}

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 344 345 346 347 348 349 350
#
# 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();

351
# Sanity check the type list.
352
if (defined($typelist)) {
353
    my @types = split(",", $typelist);
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    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");
}

#
373
# Enter the table info, but mark as not active until later.
374
#
Leigh B Stoller's avatar
Leigh B Stoller committed
375
my $typearg  = (defined($typelist)  ? ",types='$typelist'" : "");
376 377
my $startarg = "";
my $endarg   = "";
378

379 380 381 382 383 384 385 386 387 388 389 390 391
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";
392

393 394 395 396 397 398
#
# 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));
399
}
400
exit(0);
401 402

#
403 404
# Activate a reservation request; find as many nodes as possible,
# and then mark it as active. 
405
#
406 407 408
sub StartReservation($)
{
    my ($pid) = @_;
409

410 411 412 413 414 415 416
    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");
417
    }
418 419 420 421 422 423 424 425 426 427 428 429
    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;
    }
    
430
    #
431 432
    # First see if we can find enough (or any) nodes to satisfy the
    # prereserve, from nodes already prereserved.
433 434
    #
    $query_result =
435 436 437 438 439 440 441 442 443 444 445 446 447
	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;
	}
    }

    #
448
    # Then check free/allocated nodes of each type.
449 450
    #
    foreach my $type (@types) {
451 452
	last
	    if ($current >= $count);
453 454 455 456 457 458

	my $tcount  = 0;
	my $tclause = "";
	if ($type ne "*") {
	    $tclause = "and node_types.type='$type'";
	}
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

	#
	# check for nodes already reserved to the project, but without
	# reserved_pid set; they count against a prereserve request and
	# also need to be updated.
	#
	$query_result =
	    DBQueryFatal("select reserved.node_id from reserved ".
			 "left join nodes on nodes.node_id=reserved.node_id ".
			 "left join node_types on node_types.type=nodes.type ".
			 "where nodes.role='testnode' and ".
			 "      node_types.class='pc' and ".
			 "      nodes.reserved_pid is null and ".
			 "      reserved.pid='$pid' $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 (allocated)" .
		($type eq "*" ? "" : " $type") . " nodes.\n";

	    last
		if ($current >= $count);
	}

490 491 492 493 494 495 496 497 498
	$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");
499 500

	$tcount = 0;
501 502 503 504 505 506 507 508 509 510 511 512
	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";
	}
513
    }
514
    if ($current >= $count) {
515
	print "Got as many nodes as you wanted. Yippie!\n";
516
    }
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
    #
    # 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
535 536 537 538 539 540 541 542 543 544
}

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

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