manage_extensions.in 11.8 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 36 37 38 39 40
#!/usr/bin/perl -w
#
# Copyright (c) 2000-2017 University of Utah and the Flux Group.
# 
# {{{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 English;
use strict;
use Getopt::Std;
use Data::Dumper;
use Date::Parse;
use File::Basename;

#
# Manage extension policies for users and projects, optionally applying
# changes to existing instances.
#
sub usage()
{
    print("Usage:\n".
	  "  manage_extensions disable [-a] [-m reason]".
	  " -p pid | -g pid,gid | -u uid\n".
	  "  manage_extensions enable [-a] -p pid | -g pid,gid | -u uid\n".
41 42 43
	  "  manage_extensions limit [-a] [-m reason] -A ".
	  " -p pid | -g pid,gid | -u uid <days>\n".
	  "  manage_extensions unlimit [-a] -p pid | -g pid,gid | -u uid\n".
44 45
	  "  manage_extensions remove [-a] -p pid | -g pid,gid | -u uid\n".
	  "  manage_extensions show [-p pid | -u uid]\n".
46
	  "  manage_extensions apply [-p pid | -u uid]\n".
47 48 49 50 51
	  "Options:\n".
	  "  -p     - Apply to entire project.\n".
	  "  -g     - Apply to project group. \n".
	  "  -u     - Apply to user.\n".
	  "  -m     - Short explanation if desired.\n".
52 53 54
	  "  -a     - Apply changes to matching experiments.\n".
	  "  -A     - For 'limit', extensions require admin approval\n".
	  "           instead of being outright denied.\n"
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
	);
    exit(-1);
}
my $optlist     = "d";
my $debug       = 0;

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

#
# Untaint the path
#
$ENV{'PATH'} = "$TB/bin:$TB/sbin:/bin:/usr/bin:/usr/bin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

#
# Load the Testbed support stuff.
#
use lib "@prefix@/lib";
use emdb;
use emutil;
use User;
use Project;
use Group;
use libEmulab;
use libtestbed;
use APT_Profile;
use APT_Instance;

# Protos
sub fatal($);
96
sub DoAction($);
97
sub DoShow();
98
sub DoApply();
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
sub Apply($);

# Locals

#
# 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{"d"})) {
    $debug++;
}
if (@ARGV < 1) {
    usage();
}
my $action = shift(@ARGV);

# For credentials.
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}
if (!$this_user->IsAdmin()) {
    fatal("Only admins can do this");
}

128 129 130
if ($action eq "disable" || $action eq "enable" || $action eq "remove" ||
    $action eq "limit" || $action eq "unlimit") {
    DoAction($action);
131 132 133 134
}
elsif ($action eq "show") {
    DoShow();
}
135 136 137
elsif ($action eq "apply") {
    DoApply();
}
138 139 140 141 142 143 144 145
else {
    usage();
}
exit(0);

#
# Enable/Disable extensions.
#
146
sub DoAction($)
147 148 149
{
    my ($action) = @_;
    
150
    my $optlist = "ap:g:u:m:A";
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
    my $apply   = 0;
    my $target;
    my $reason;
    
    my %options = ();
    if (! getopts($optlist, \%options)) {
	usage();
    }
    if (defined($options{"p"})) {
	$target = Project->Lookup($options{"p"});
	fatal("No such project")
	    if (!defined($target));
    }
    elsif (defined($options{"g"})) {
	$target = Group->Lookup($options{"g"});
	fatal("No such group")
	    if (!defined($target));
    }
    elsif (defined($options{"u"})) {
	$target = User->Lookup($options{"u"});
	fatal("No such user")
	    if (!defined($target));
    }
    else {
	usage();
    }
    if (defined($options{"a"})) {
	$apply = 1;
    }
    if (defined($options{"m"})) {
	$reason = $options{"m"};
	if ($reason eq "") {
	    fatal("How about a real reason?");
	}
    }
    my $creator     = $this_user->uid();
    my $creator_idx = $this_user->uid_idx();
    
    #
    # At the moment, the only flag is disable/enable.
    #
    if ($action eq "remove") {
	if (ref($target) eq "Project") {
	    my $pid_idx = $target->pid_idx();
	    DBQueryFatal("delete from apt_extension_group_policies ".
			 "where pid_idx='$pid_idx' and pid_idx=gid_idx");
	}
	elsif (ref($target) eq "Group") {
	    my $pid_idx = $target->pid_idx();
	    my $gid_idx = $target->gid_idx();
	    DBQueryFatal("delete from apt_extension_group_policies ".
			 "where pid_idx='$pid_idx' and gid_idx='$gid_idx'");
	}
	elsif (ref($target) eq "User") {
	    my $uid_idx = $target->uid_idx();
	    DBQueryFatal("delete from apt_extension_user_policies ".
			 "where uid_idx='$uid_idx'");
	}
    }
    else {
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	my $clause = "";
	if ($action eq "enable" || $action eq "disable") {
	    my $disabled = ($action eq "disable" ? 1 : 0);
	    $clause = "disabled='$disabled'";
	}
	elsif ($action eq "limit" || $action eq "unlimit") {
	    # When setting a limit, clear the disable flag.
	    $clause  = "disabled='0',`limit`=";
	    if ($action eq "unlimit") {
		$clause .= "'0'";
	    }
	    else {
		usage()
		    if (!@ARGV);
		my $days = shift(@ARGV);
		usage()
		    if ($days !~ /^\d+$/);
		$clause .= "'" . $days * 24 . "'";

		if (defined($options{"A"})) {
		    $clause .= ",admin_after_limit='1'";
		}
		else {
		    $clause .= ",admin_after_limit='0'";
		}
	    }
	}
238 239 240 241 242 243 244 245 246
	my $reason_clause = "";
	if (defined($reason)) {
	    $reason_clause = "reason=" . DBQuoteSpecial($reason) . ", ";
	}
	
	if (ref($target) eq "Project") {
	    my $pid     = $target->pid();
	    my $pid_idx = $target->pid_idx();
	    DBQueryFatal("replace into apt_extension_group_policies set ".
247
			 " $clause, created=now(), ".
248 249 250 251 252 253 254 255 256 257 258
			 " creator='$creator', creator_idx='$creator_idx', ".
			 " $reason_clause ".
			 " pid='$pid', gid='$pid', ".
			 " pid_idx='$pid_idx', gid_idx='$pid_idx' ");
	}
	elsif (ref($target) eq "Group") {
	    my $pid     = $target->pid();
	    my $pid_idx = $target->pid_idx();
	    my $gid     = $target->gid();
	    my $gid_idx = $target->gid_idx();
	    DBQueryFatal("replace into apt_extension_group_policies set ".
259
			 " $clause, created=now(), ".
260 261 262 263 264 265 266 267 268
			 " creator='$creator', creator_idx='$creator_idx', ".
			 " $reason_clause ".
			 " pid='$pid', gid='$gid', ".
			 " pid_idx='$pid_idx', gid_idx='$gid_idx' ");
	}
	elsif (ref($target) eq "User") {
	    my $uid     = $target->uid();
	    my $uid_idx = $target->uid_idx();
	    DBQueryFatal("replace into apt_extension_user_policies set ".
269
			 " $clause, created=now(), ".
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 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
			 " creator='$creator', creator_idx='$creator_idx', ".
			 " $reason_clause ".
			 " uid_idx='$uid_idx', uid='$uid'");
	}
    }
    if ($apply) {
	Apply($target);
    }
    exit(0);
}

#
# Show existing for target.
#
sub DoShow()
{
    my $optlist = "p:u:";
    my $target;
    
    my %options = ();
    if (! getopts($optlist, \%options)) {
	usage();
    }
    if (defined($options{"p"})) {
	$target = Project->Lookup($options{"p"});
	fatal("No such project")
	    if (!defined($target));
	
    }
    elsif (defined($options{"u"})) {
	$target = User->Lookup($options{"u"});
	fatal("No such user")
	    if (!defined($target));
    }
    if (!defined($target)) {
	my $query_result =
	    DBQueryFatal("select * from apt_extension_group_policies");
	while (my $row = $query_result->fetchrow_hashref()) {
	    my $pid      = $row->{"pid"};
	    my $gid      = $row->{"gid"};
	    my $disabled = $row->{"disabled"};
	    my $creator  = $row->{"creator"};
	    my $created  = $row->{"created"};
	    my $reason   = $row->{"reason"};
	    print "$pid/$gid: ";
	    print ($disabled ? "disabled, " : "enabled, ");
	    print "$creator, $created";
	    print ", $reason" if (defined($reason));
	    print "\n";
	}
	$query_result =
	    DBQueryFatal("select * from apt_extension_user_policies");
	while (my $row = $query_result->fetchrow_hashref()) {
	    my $uid      = $row->{"uid"};
	    my $disabled = $row->{"disabled"};
	    my $created  = $row->{"created"};
	    my $creator  = $row->{"creator"};
	    my $reason   = $row->{"reason"};
	    print "$uid: ";
	    print ($disabled ? "disabled, " : "enabled, ");
	    print "$creator, $created";
	    print ", $reason" if (defined($reason));
	    print "\n";
	}
    }
    elsif (ref($target) eq "Project") {
	my $pid_idx = $target->pid_idx();
	my $query_result =
	    DBQueryFatal("select * from apt_extension_group_policies ".
			 "where pid_idx='$pid_idx'");
	while (my $row = $query_result->fetchrow_hashref()) {
	    my $pid      = $row->{"pid"};
	    my $gid      = $row->{"gid"};
	    my $disabled = $row->{"disabled"};
	    my $creator  = $row->{"creator"};
	    my $created  = $row->{"created"};
	    my $reason   = $row->{"reason"};
	    print "$pid/$gid: ";
	    print ($disabled ? "disabled, " : "enabled, ");
	    print "$creator, $created";
	    print ", $reason" if (defined($reason));
	    print "\n";
	}
    }
    elsif (ref($target) eq "User") {
	my $uid_idx = $target->uid_idx();
	my $query_result =
	    DBQueryFatal("select * from apt_extension_user_policies ".
			 "where uid_idx='$uid_idx'");
	if ($query_result->numrows) {
	    my $row = $query_result->fetchrow_hashref();
	    my $disabled = $row->{"disabled"};
	    my $created  = $row->{"created"};
	    my $creator  = $row->{"creator"};
	    my $reason   = $row->{"reason"};
	    print ($disabled ? "disabled, " : "enabled, ");
	    print "$creator, $created";
	    print ", $reason" if (defined($reason));
	    print "\n";
	}
    }
    exit(0);
}

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
#
# Apply policies to target.
#
sub DoApply()
{
    my $optlist = "p:u:";
    my $target;
    
    my %options = ();
    if (! getopts($optlist, \%options)) {
	usage();
    }
    if (defined($options{"p"})) {
	$target = Project->Lookup($options{"p"});
	fatal("No such project")
	    if (!defined($target));
	
    }
    elsif (defined($options{"u"})) {
	$target = User->Lookup($options{"u"});
	fatal("No such user")
	    if (!defined($target));
    }
    else {
	usage();
    }
    Apply($target);
    return 0;
}

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
sub Apply($)
{
    my ($target)  = @_;
    my @instances = ();
    my $query_result;

    if (ref($target) eq "Project" || ref($target) eq "Group") {
	my $pid_idx = $target->pid_idx();
	$query_result =
	    DBQueryFatal("select uuid from apt_instances ".
			 "where pid_idx='$pid_idx'");
    }
    elsif (ref($target) eq "User") {
	my $uid_idx = $target->uid_idx();
	$query_result =
	    DBQueryFatal("select uuid from apt_instances ".
			 "where creator_idx='$uid_idx'");
    }
    while (my ($uuid) = $query_result->fetchrow_array()) {
	my $instance = APT_Instance->Lookup($uuid);
	next
	    if (!defined($instance));
426 427 428 429
	my $current_disabled = $instance->extension_disabled();
	my $current_limit    = $instance->extension_limit();
	# Perl is stupid.
	$current_limit = -1 if (!defined($current_limit));
430 431 432 433 434 435 436 437

	if ($instance->ApplyExtensionPolicies()) {
	    print STDERR "Could not apply extension policies to $instance\n";
	    next;
	}
	$instance->Refresh();
	my $disabled = ($instance->extension_disabled() ?
			"disabled" : "enabled");
438 439 440 441 442 443 444 445 446 447
	my $limit    = $instance->extension_limit();
	$limit = -1 if (!defined($limit));
	my $limited  = ($limit > 0 ?
			"limited to " . APT_Instance::HoursToEnglish($limit) :
			"unlimited");
	my $pid      = $instance->pid();
	my $name     = $instance->name();
	my $which1   = ($current_disabled != $instance->extension_disabled() ?
			"now" : "still");	
	my $which2   = ($current_limit != $limit ? "now" : "still");	
448
	
449 450
	print "Extensions for $pid/$name are $which1 $disabled\n";
	print "Extensions for $pid/$name are $which2 $limited\n";
451 452 453 454 455 456 457 458 459 460 461 462
    }
}

sub fatal($)
{
    my ($mesg) = @_;

    print STDERR "*** $0:\n".
	         "    $mesg\n";
    # Exit with negative status so web interface treats it as system error.
    exit(-1);
}