showlease.in 12.1 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2013-2017 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
# 
# {{{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/>.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use Date::Parse;
use POSIX qw(strftime);

#
# Show all leases the caller has access to.
#
sub usage()
{
    print STDERR "Usage: showlease [-hda] [-p pid] [-u uid] lid ...\n";
    print STDERR "   -h      This message\n";
    print STDERR "   -d      Print additional debug info\n";
    print STDERR "   -a      Show all leases (admin only)\n";
39
    print STDERR "   -g      Show only leases in the 'grace' state\n";
40
    print STDERR "   -l      Long listing\n";
41
    print STDERR "   -L      Show lock holders (admin only)\n";
42
    print STDERR "   -D      Describe the system-wide limits on leases\n";
43 44 45 46 47
    print STDERR "   -p pid  Show all leases for project <pid>\n";
    print STDERR "   -u uid  Show all leases owned by user <uid>\n";
    print STDERR "   lid ... Show named leases (name is <pid>/<id>)\n";
    exit(-1);
}
48
my $optlist  = "dhaLDp:u:gl";
49 50 51 52
my $debug = 0;
my $pid;
my $uid;
my $showall;
53
my $showgrace;
54
my $showlockers;
55
my $showsitevars;
56
my $verbose = 0;
57
my @lids = ();
58
my %sitevars = ();
59 60 61

# Protos
sub fatal($);
62
sub datestr($);
63
sub showsitevars();
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

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

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use Lease;
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{d})) {
    $debug++;
}
if (defined($options{a})) {
    $showall = 1;
}
107 108 109
if (defined($options{g})) {
    $showgrace = 1;
}
110 111 112
if (defined($options{l})) {
    $verbose = 1;
}
113 114 115
if (defined($options{L})) {
    $showlockers = 1;
}
116 117 118
if (defined($options{D})) {
    $showsitevars = 1;
}
119 120 121 122 123 124 125 126 127
if (defined($options{p})) {
    $pid = $options{p};
}
if (defined($options{u})) {
    $uid = $options{u};
}
my @lnames = @ARGV;

if ($showall) {
128 129
    if ($pid || $uid || @lnames > 0) {
	print STDERR "Cannot use -p/-u or explicit list with -a\n";
130 131 132 133 134 135 136 137 138 139 140
	usage();
    }
}
if ($pid && $uid) {
    print STDERR "Specify only one of -p and -u\n";
    usage();
}
if (($pid || $uid) && @lnames > 0) {
    print STDERR "Specify only one of -p/-u and explicit list of lids\n";
    usage();
}
141 142 143 144
if ($showlockers && !TBAdmin()) {
    print STDERR "Only admin can use -L\n";
    usage();
}
145 146 147 148 149 150

my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}

151 152 153 154 155
if ($showsitevars) {
    showsitevars();
    exit(0);
}

156
if ($showall) {
157 158 159 160 161 162
    # Admin can see all leases, others not so much
    foreach my $lid (Lease->AllLeases()) {
	if ($lid->AccessCheck($this_user, LEASE_ACCESS_READINFO())) {
	    push @lids, $lid;
	}
    }
163
}
164
elsif ($pid) {
165 166 167 168 169 170
    my $project = Project->Lookup($pid);
    if (!defined($project)) {
	fatal("No such project $pid\n");
    }
    # must be admin or have access to the project
    if (!TBAdmin() &&
171
	!$project->AccessCheck($this_user, TB_PROJECT_READINFO())) {
172 173 174 175
	fatal("You do not have access to pid $pid");
    }
    @lids = Lease->AllProjectLeases($project);
}
176
elsif ($uid) {
177 178 179 180 181 182 183 184 185 186
    my $user = User->Lookup($uid);
    if (!defined($user)) {
	fatal("No such user $uid\n");
    }
    # must be admin or be the specified user
    if (!TBAdmin() && !$user->SameUser($this_user)) {
	fatal("You do not have access to uid $uid");
    }
    @lids = Lease->AllUserLeases($user);
}
187 188
elsif (@lnames > 0) {
    foreach my $name (@lnames) {
189
	my $lid;
190 191
	# lease names must have a project context
	if ($name =~ /^([-\w]+)\/([-\w]+)$/) {
192 193 194 195 196 197 198 199 200 201
	    $lid = Lease->Lookup($1, $2);
	}
	elsif ($name =~ /^([-\w]+)\/([-\w]+)\/([-\w]+)$/) {
	    $lid = Lease->Lookup($1, $2, $3);
	}
	else {
	    fatal("Lease name $name not in the form <pid>/<lname>");
	}
	if (!defined($lid)) {
	    fatal("Could not access lease $name");
202
	}
203
	push @lids, $lid;
204 205
    }
}
206 207 208 209
# special case: no args, show all leases owned by the caller
else {
    @lids = Lease->AllUserLeases($this_user);
}
210 211 212 213 214 215 216

print STDERR "Found ", scalar(@lids), " leases\n"
    if ($debug);
@lids = sort { $a->pid() cmp $b->pid() || $a->lease_id() cmp $b->lease_id() } @lids;
if (@lids > 0) {
    my %states = (
	"valid" =>      'valid',
217
	"failed" =>     'failed',
218 219 220 221 222
	"unapproved" => 'unappr',
	"grace" =>      'grace',
	"locked" =>     'locked',
	"expired" =>    'expire'
    );
223 224 225
    my $fmt;

    if ($showlockers) {
226
	$fmt = "%-44s %-8s %4s %-6s %-15s %s\n";
227

228
	printf $fmt, "Pid/(Gid/)Name", "Owner", "Type", "State",
229 230
	    "Lock Time", "Locker Proc ID";
    } elsif ($showgrace) {
231
	$fmt = "%-44s %-8s %5s %8s %-11s %-15s %-15s %-15s %-15s %-15s\n";
232

233
	printf $fmt, "Pid/(Gid/)Name", "Owner", "Type", "Size", "Exp Policy", "Grace Start", "Grace End", "Last Checked", "Last Used", "Expiration";
234
    } elsif ($verbose) {
235
	$fmt = "%-44s %-8s %5s %8s %-6s %3s %3s %-15s %-15s %-15s %s\n";
236

237
	printf $fmt, "Pid/(Gid/)Name", "Owner", "Type", "Size", "State", "Use", "Ext",
238
	    "Inception", "End", "Last used", "Attrs";
239
    } else {
240
	$fmt = "%-44s %-8s %5s %8s %-6s %3s\n";
241

242
	printf $fmt, "Pid/(Gid/)Name", "Owner", "Type", "Size", "State", "Use";
243
    }
244 245

    foreach my $lease (@lids) {
246 247 248 249 250 251 252 253
	my $lname;
	if ($lease->pid() eq $lease->gid()) {
	    $lname = $lease->pid() . "/" . $lease->lease_id();
	} else {
	    $lname = $lease->pid() . "/" . $lease->gid() . "/" .
		$lease->lease_id();
	}

254 255 256 257 258 259
	my $ltype = $lease->type();
	if ($ltype eq "ltdataset") {
	    $ltype = "long";
	} elsif ($ltype eq "stdataset") {
	    $ltype = "short";
	}
260 261 262 263
	if ($showlockers) {
	    my $lproc = $lease->lockpid();
	    my $ltime;
	    if ($lproc != 0) {
264
		$ltime = datestr($lease->locktime());
265 266 267 268
	    } else {
		$ltime = "Unlocked";
		$lproc = "";
	    }
269
	    printf $fmt, $lname, $lease->owner(), $ltype,
270 271 272 273
	        $states{$lease->state()}, $ltime, $lproc;
	    next;
	}

274 275 276 277 278 279
	if (!$lease->AccessCheck($this_user, LEASE_ACCESS_READINFO())) {
	    print STDERR "Admin cannot access lease $lease?!\n"
		if (TBAdmin());
	    next;
	}

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
	#
	# Get the attributes.
	# Note that we treat size special and remove it from the attrlist.
	#
	my $lsize = "";
	my $attrstr = "";
	my $attrs = $lease->GetAttributes();
	if ($attrs) {
	    my $del = "";
	    foreach my $k (sort keys %{$attrs}) {
		my $v = $attrs->{$k}->{'value'};
		if ($k eq "size") {
		    $lsize = $v;
		    next;
		}
		$attrstr .= "$del$k=$v";
		$del = ",";
	    }
	}

300 301 302 303
	if ($showgrace) {
	    next
		if ($lease->state() ne LEASE_STATE_GRACE());

304 305 306
	    my $lt = $lease->type();
	    if (!exists($sitevars{$lt})) {
		$sitevars{$lt} = Lease->SiteVars($lt);
307
	    }
308
	    my $grace = $sitevars{$lt}->{'graceperiod'};
309 310 311 312 313
	    next
		if (!defined($grace));
	    $grace += $lease->statestamp();

	    my $policy = "none";
314 315
	    if ($sitevars{$lt}->{'maxlease'} != 0) {
		if ($sitevars{$lt}->{'maxidle'} != 0) {
316 317 318 319
		    $policy = "expire+idle";
		} else {
		    $policy = "expire";
		}
320
	    } elsif ($sitevars{$lt}->{'maxidle'} != 0) {
321 322 323 324 325 326 327
		$policy = "idle";
	    }
	    my $stime = datestr($lease->statestamp());
	    my $etime = datestr($grace);
	    my $lchecked = datestr($lease->last_checked());
	    my $lused = datestr($lease->last_used());
	    my $expire = datestr($lease->lease_end());
328
	    printf $fmt, $lname, $lease->owner(), $ltype, $lsize, $policy,
329 330 331 332 333 334 335
	        $stime, $etime, $lchecked, $lused, $expire;
	    next;
	}

	my $stime = datestr($lease->inception());
	my $etime = datestr($lease->lease_end());
	my $ltime = datestr($lease->last_used());
336
	my $lusers = $lease->GetReservations();
337 338 339
	if (!$lusers) {
	    $lusers = ();
	}
340
	if ($verbose) {
341
	    printf $fmt, $lname, $lease->owner(), $ltype, $lsize,
342 343 344
		$states{$lease->state()}, int(@$lusers), $lease->renewals(),
		$stime, $etime, $ltime, $attrstr;
	} else {
345
	    printf $fmt, $lname, $lease->owner(), $ltype, $lsize,
346 347
		$states{$lease->state()}, int(@$lusers);
	}
348 349 350 351 352
    }
}

exit(0);

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
#
# Per-type Lease sitevars:
#
# maxsize	Max size (MiB) of a dataset
#		(0 == unlimited)
# maxlease	Max time (days) from creation before lease is marked expired
#		(0 == unlimited)
# maxidle	Max time (days) from last use before lease is marked expired
#		(0 == unlimited)
# graceperiod	Time (days) before an expired dataset will be destroyed
#		(0 == no grace period, unlimited makes no sense here)
# autodestroy	If non-zero, destroy expired datasets after grace period
#		otherwise lock them
# usequotas	If non-zero, enforce per-project dataset quotas
# maxextend	Number of times a user can extend the lease
#		(0 == unlimited)
Mike Hibler's avatar
Mike Hibler committed
369
# extendperiod	Length (days) of each user-requested extension
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
#		(0 == do not allow extensions)
#
sub showsitevars()
{
    my %vars = ();

    foreach my $ltype ("stdataset", "ltdataset") {
	$vars{$ltype} = Lease->SiteVars($ltype);
	my $maxsize = $vars{$ltype}->{'maxsize'};
	my $maxlease = $vars{$ltype}->{'maxlease'} / (24 * 60 * 60);
	my $maxidle = $vars{$ltype}->{'maxidle'} / (24 * 60 * 60);
	my $grace = $vars{$ltype}->{'graceperiod'} / (24 * 60 * 60);
	my $autod = $vars{$ltype}->{'autodestroy'};
	my $quotas = $vars{$ltype}->{'usequotas'};
	my $maxext = $vars{$ltype}->{'maxextend'};
	my $extend = $vars{$ltype}->{'extendperiod'} / (24 * 60 * 60);
	my $v;

	print "$ltype:\n";

	print "  Maximum size:";
	if ($quotas != 0) {
	    print " determined by project quota";
	} elsif ($maxsize == 0) {
	    print " unlimited";
	} else {
	    if ($maxsize < 1024) {
		$v = sprintf("%.2f", $maxsize);
		print " $v MiB";
	    } elsif ($maxsize < (1024 * 1024)) {
		$v = sprintf("%.2f", $maxsize / 1024);
		print " $v GiB";
	    } else {
		$v = sprintf("%.2f", $maxsize / (1024 * 1024));
		print " $v TiB";
	    }
	    $v = $maxsize * 1024 * 1024;
	    print " ($v bytes)";
	}
	print ".\n";

	print "  Expiration:";
	if ($maxidle == 0 && $maxlease == 0) {
	    print " never";
	} else {
	    print " after";
	    if ($maxidle != 0) {
		if ($maxidle < 1) {
		    $v = sprintf("%.1f", $maxidle * 24);
		    print " $v hours";
		} else {
		    $v = sprintf("%.1f", $maxidle);
		    print " $v days";
		}
		print " idle";
	    }
	    if ($maxlease != 0) {
		if ($maxidle != 0) {
		    print " or after";
		}
		print " a lease-specific time period (maximum of";
		if ($maxlease < 1) {
		    $v = sprintf("%.1f", $maxlease * 24);
		    print " $v hours";
		} else {
		    $v = sprintf("%.1f", $maxlease);
		    print " $v days";
		}
		print " from creation)";
	    }
	}
	print ".\n";

	print "  Disposition:";
	if ($maxidle == 0 && $maxlease == 0) {
	    print " N/A";
	} else {
	    if ($autod) {
		print " destroyed";
	    } else {
		print " locked-down";
	    }
	    print " after expiration";
453
	    if ($grace != 0) {
454
		print " plus";
455 456 457
		if ($grace < 1) {
		    $v = sprintf("%.1f", $grace * 24);
		    print " $v hours";
458
		} else {
459 460
		    $v = sprintf("%.1f", $grace);
		    print " $v days";
461
		}
462
		print " grace period";
463 464 465
	    }
	}
	print ".\n";
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	print "  Extensions:";
	if ($extend != 0) {
	    print " allows";
	    if ($maxext == 0) {
		print " an unlimited number of";
	    } else {
		print " up to $maxext";
	    }
	    if ($extend < 1) {
		$v = sprintf("%.1f", $extend * 24);
		print " $v hour";
	    } else {
		$v = sprintf("%.1f", $extend);
		print " $v day";
	    }
	    print " extensions during grace period";
	} else {
	    print " none";
	}
	print ".\n";
486 487 488
    }
}

489 490 491 492 493 494 495 496 497 498 499
sub datestr($)
{
    my ($date) = @_;

    if (defined($date)) {
	return strftime("%D %R", localtime($date));
    }

    return "never";
}

500 501 502 503 504 505 506
sub fatal($)
{
    my ($mesg) = $_[0];

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