showlease.in 11.4 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2013-2014 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      Show lock holders (admin only)\n";
41
    print STDERR "   -D      Describe the system-wide limits on leases\n";
42 43 44 45 46
    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);
}
47
my $optlist  = "dhaLDp:u:g";
48 49 50 51
my $debug = 0;
my $pid;
my $uid;
my $showall;
52
my $showgrace;
53
my $showlockers;
54
my $showsitevars;
55
my @lids = ();
56
my %sitevars = ();
57 58 59

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

#
# 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;
}
105 106 107
if (defined($options{g})) {
    $showgrace = 1;
}
108 109 110
if (defined($options{L})) {
    $showlockers = 1;
}
111 112 113
if (defined($options{D})) {
    $showsitevars = 1;
}
114 115 116 117 118 119 120 121 122
if (defined($options{p})) {
    $pid = $options{p};
}
if (defined($options{u})) {
    $uid = $options{u};
}
my @lnames = @ARGV;

if ($showall) {
123 124
    if ($pid || $uid || @lnames > 0) {
	print STDERR "Cannot use -p/-u or explicit list with -a\n";
125 126 127 128 129 130 131 132 133 134 135
	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();
}
136 137 138 139
if ($showlockers && !TBAdmin()) {
    print STDERR "Only admin can use -L\n";
    usage();
}
140 141 142 143 144 145

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

146 147 148 149 150
if ($showsitevars) {
    showsitevars();
    exit(0);
}

151
if ($showall) {
152 153 154 155 156 157
    # 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;
	}
    }
158
}
159
elsif ($pid) {
160 161 162 163 164 165
    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() &&
166
	!$project->AccessCheck($this_user, TB_PROJECT_READINFO())) {
167 168 169 170
	fatal("You do not have access to pid $pid");
    }
    @lids = Lease->AllProjectLeases($project);
}
171
elsif ($uid) {
172 173 174 175 176 177 178 179 180 181
    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);
}
182 183
elsif (@lnames > 0) {
    foreach my $name (@lnames) {
184
	my $lid;
185 186
	# lease names must have a project context
	if ($name =~ /^([-\w]+)\/([-\w]+)$/) {
187 188 189 190 191 192 193 194 195 196
	    $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");
197
	}
198
	push @lids, $lid;
199 200
    }
}
201 202 203 204
# special case: no args, show all leases owned by the caller
else {
    @lids = Lease->AllUserLeases($this_user);
}
205 206 207 208 209 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',
	"unapproved" => 'unappr',
	"grace" =>      'grace',
	"locked" =>     'locked',
	"expired" =>    'expire'
    );
217 218 219 220
    my $fmt;

    if ($showlockers) {
	$fmt = "%-24s %-8s %-10s %-6s %-15s %s\n";
221

222
	printf $fmt, "Pid/Name", "Owner", "Type", "State",
223 224 225 226 227
	    "Lock Time", "Locker Proc ID";
    } elsif ($showgrace) {
	$fmt = "%-24s %-8s %-10s %-11s %-15s %-15s %-15s %-15s %-15s\n";

	printf $fmt, "Pid/Name", "Owner", "Type", "Exp Policy", "Grace Start", "Grace End", "Last Checked", "Last Used", "Expiration";
228
    } else {
229
	$fmt = "%-24s %-8s %-10s %-6s %3s %3s %-15s %-15s %-15s %s\n";
230

231
	printf $fmt, "Pid/Name", "Owner", "Type", "State", "Use", "Ext",
232 233
	    "Inception", "End", "Last used", "Attrs";
    }
234 235

    foreach my $lease (@lids) {
236
	if ($showlockers) {
237 238
	    my $lname = $lease->pid() . "/" .
		$lease->gid() . "/" . $lease->lease_id();
239 240 241
	    my $lproc = $lease->lockpid();
	    my $ltime;
	    if ($lproc != 0) {
242
		$ltime = datestr($lease->locktime());
243 244 245 246 247 248 249 250 251
	    } else {
		$ltime = "Unlocked";
		$lproc = "";
	    }
	    printf $fmt, $lname, $lease->owner(), $lease->type(),
	        $states{$lease->state()}, $ltime, $lproc;
	    next;
	}

252 253 254 255 256 257
	if (!$lease->AccessCheck($this_user, LEASE_ACCESS_READINFO())) {
	    print STDERR "Admin cannot access lease $lease?!\n"
		if (TBAdmin());
	    next;
	}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	if ($showgrace) {
	    next
		if ($lease->state() ne LEASE_STATE_GRACE());

	    my $ltype = $lease->type();
	    if (!exists($sitevars{$ltype})) {
		$sitevars{$ltype} = Lease->SiteVars($ltype);
	    }
	    my $grace = $sitevars{$ltype}->{'graceperiod'};
	    next
		if (!defined($grace));
	    $grace += $lease->statestamp();

	    my $policy = "none";
	    if ($sitevars{$ltype}->{'maxlease'} != 0) {
		if ($sitevars{$ltype}->{'maxidle'} != 0) {
		    $policy = "expire+idle";
		} else {
		    $policy = "expire";
		}
	    } elsif ($sitevars{$ltype}->{'maxidle'} != 0) {
		$policy = "idle";
	    }
281 282
	    my $lname = $lease->pid() . "/" .
		$lease->gid() . "/" . $lease->lease_id();
283 284 285 286 287 288 289 290 291 292
	    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());
	    printf $fmt, $lname, $lease->owner(), $ltype, $policy,
	        $stime, $etime, $lchecked, $lused, $expire;
	    next;
	}

293 294 295 296 297 298 299 300 301 302 303 304
	# Attributes
	my $attrstr = "";
	my $attrs = $lease->GetAttributes();
	if ($attrs) {
	    my $del = "";
	    foreach my $k (keys %{$attrs}) {
		my $v = $attrs->{$k}->{'value'};
		$attrstr .= "$del$k=$v";
		$del = ",";
	    }
	}

305 306
	my $lname = $lease->pid() . "/" .
	    $lease->gid() . "/" . $lease->lease_id();
307 308 309
	my $stime = datestr($lease->inception());
	my $etime = datestr($lease->lease_end());
	my $ltime = datestr($lease->last_used());
310
	my $lusers = $lease->GetReservations();
311 312 313
	if (!$lusers) {
	    $lusers = ();
	}
314
	printf $fmt, $lname, $lease->owner(), $lease->type(),
315
	    $states{$lease->state()}, int(@$lusers), $lease->renewals(),
316
	    $stime, $etime, $ltime, $attrstr;
317 318 319 320 321
    }
}

exit(0);

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
#
# 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
338
# extendperiod	Length (days) of each user-requested extension
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 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
#		(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";
422
	    if ($grace != 0) {
423
		print " plus";
424 425 426
		if ($grace < 1) {
		    $v = sprintf("%.1f", $grace * 24);
		    print " $v hours";
427
		} else {
428 429
		    $v = sprintf("%.1f", $grace);
		    print " $v days";
430
		}
431
		print " grace period";
432 433 434
	    }
	}
	print ".\n";
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	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";
455 456 457
    }
}

458 459 460 461 462 463 464 465 466 467 468
sub datestr($)
{
    my ($date) = @_;

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

    return "never";
}

469 470 471 472 473 474 475
sub fatal($)
{
    my ($mesg) = $_[0];

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