EmulabFeatures.pm.in 11.2 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2009-2011 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 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
#
package EmulabFeatures;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT $debug $verbose);

@ISA    = "Exporter";
@EXPORT = qw();

# Configure variables
my $TB		= "@prefix@";
my $BOSSNODE    = "@BOSSNODE@";
my $TBDOCBASE   = "@TBDOCBASE@";
my $MAINSITE    = @TBMAINSITE@;

$debug   = 0;
$verbose = 1;

use emdb;

#
# Lookup a feature by its name.
#
sub Lookup($$)
{
    my ($class, $token) = @_;

    if (! ($token =~ /^[-\w]+$/)) {
	return undef;
    }
    my $query_result =
	DBQueryWarn("select * from emulab_features where feature='$token'");
    return undef
	if (! ($query_result && $query_result->numrows));

    my $self            = {};
    $self->{"DBROW"}    = $query_result->fetchrow_hashref();
    
    bless($self, $class);
    return $self;
}
sub feature($)		{ return $_[0]->{'DBROW'}->{'feature'}; }
sub description($)      { return $_[0]->{'DBROW'}->{'description'}; }
# Global disable flag.
sub disabled($)         { return $_[0]->{'DBROW'}->{'disabled'}; }
# Global enable flag.
sub enabled($)          { return $_[0]->{'DBROW'}->{'enabled'}; }
sub added($)            { return $_[0]->{'DBROW'}->{'added'}; }

#
# Make a new feature.
#
sub Create($$$)
{
    my ($class, $featurename, $description) = @_;

    my $feature = EmulabFeatures->Lookup($featurename);
    return $feature
	if (defined($feature));

    my $safe_description = DBQuoteSpecial($description);

    DBQueryWarn("replace into emulab_features set ".
		" feature='$featurename', added=now(), ".
		" description=$safe_description")
	or return undef;

    return EmulabFeatures->Lookup($featurename);
}

#
# Delete a feature.
#
sub Delete($)
{
    my ($self) = @_;
    my $featurename = $self->feature();

    DBQueryWarn("delete from user_features where feature='$featurename'")
	or return -1;
    DBQueryWarn("delete from group_features where feature='$featurename'")
	or return -1;
    DBQueryWarn("delete from experiment_features where feature='$featurename'")
	or return -1;
    DBQueryWarn("delete from emulab_features where feature='$featurename'")
	or return -1;

    return 0;
}

#
# Set/Clear the global enable and disable flags
#
sub SetGlobalEnable($$)
{
    my ($self, $value) = @_;
    my $featurename = $self->feature();

    $value = ($value ? 1 : 0);
    
124
    DBQueryWarn("update emulab_features set enabled='$value' ".
125 126 127 128 129 130 131 132 133 134 135 136
		"where feature='$featurename'")
	or return -1;

    return 0;
}
sub SetGlobalDisable($$)
{
    my ($self, $value) = @_;
    my $featurename = $self->feature();

    $value = ($value ? 1 : 0);
    
Leigh Stoller's avatar
Leigh Stoller committed
137
    DBQueryWarn("update emulab_features set disabled='$value' ".
138 139 140 141 142 143 144 145 146 147 148 149 150 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 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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
		"where feature='$featurename'")
	or return -1;

    return 0;
}

#
# Add a feature to a group or user.
#
sub Enable($$)
{
    my ($self, $target) = @_;
    my $featurename = $self->feature();

    if (ref($target) eq "User") {
	my $uid_idx = $target->uid_idx();
	my $uid     = $target->uid();
	
	my $query_result =
	    DBQueryWarn("select * from user_features ".
			"where feature='$featurename' and uid_idx='$uid_idx'");
	return -1
	    if (!$query_result);
	return 0
	    if ($query_result->numrows);
	DBQueryWarn("replace into user_features set ".
		    " feature='$featurename', added=now(), ".
		    " uid='$uid', uid_idx='$uid_idx'")
	    or return -1;
	
	return 0;
    }
    elsif (ref($target) eq "Group" || ref($target) eq "Project") {
	if (ref($target) eq "Project") {
	    $target = $target->GetProjectGroup();
	}
	my $pid_idx = $target->pid_idx();
	my $gid_idx = $target->gid_idx();
	my $pid     = $target->pid();
	my $gid     = $target->gid();
	
	my $query_result =
	    DBQueryWarn("select * from group_features ".
			"where feature='$featurename' and gid_idx='$gid_idx'");
	return -1
	    if (!$query_result);
	return 0
	    if ($query_result->numrows);
	DBQueryWarn("replace into group_features set ".
		    " feature='$featurename', added=now(), ".
		    " gid='$gid', gid_idx='$gid_idx', ".
		    " pid='$pid', pid_idx='$pid_idx'")
	    or return -1;
	
	return 0;
    }
    elsif (ref($target) eq "Experiment") {
	my $exptidx = $target->idx();
	my $pid     = $target->pid();
	my $eid     = $target->eid();
	
	my $query_result =
	    DBQueryWarn("select * from experiment_features ".
			"where feature='$featurename' and exptidx='$exptidx'");
	return -1
	    if (!$query_result);
	return 0
	    if ($query_result->numrows);
	DBQueryWarn("replace into experiment_features set ".
		    " feature='$featurename', added=now(), ".
		    " eid='$eid', pid='$pid', exptidx='$exptidx'")
	    or return -1;
	
	return 0;
    }
    return -1;
}

#
# Remove a feature from a group or user.
#
sub Disable($$)
{
    my ($self, $target) = @_;
    my $featurename = $self->feature();

    if (ref($target) eq "User") {
	my $uid_idx = $target->uid_idx();
	my $uid     = $target->uid();
	
	DBQueryWarn("delete from user_features ".
		    "where feature='$featurename' and uid_idx='$uid_idx'")
	    or return -1;
	return 0;
    }
    elsif (ref($target) eq "Group" || ref($target) eq "Project") {
	if (ref($target) eq "Project") {
	    $target = $target->GetProjectGroup();
	}
	my $pid_idx = $target->pid_idx();
	my $gid_idx = $target->gid_idx();
	my $pid     = $target->pid();
	my $gid     = $target->gid();
	
	DBQueryWarn("delete from group_features ".
		    "where feature='$featurename' and gid_idx='$gid_idx'")
	    or return -1;
	
	return 0;
    }
    elsif (ref($target) eq "Experiment") {
	my $exptidx = $target->idx();
	
	DBQueryWarn("delete from experiment_features ".
		    "where feature='$featurename' and exptidx='$exptidx'")
	    or return -1;
	
	return 0;
    }
    return -1;
}

#
# Delete all features for a target, as when that target is deleted.
#
sub DeleteAll($$)
{
Leigh Stoller's avatar
Leigh Stoller committed
265
    my ($class, $target) = @_;
266 267 268 269 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

    if (ref($target) eq "User") {
	my $uid_idx = $target->uid_idx();
	
	DBQueryWarn("delete from user_features where uid_idx='$uid_idx'")
	    or return -1;
	return 0;
    }
    elsif (ref($target) eq "Group") {
	my $gid_idx = $target->gid_idx();
	
	DBQueryWarn("delete from group_features where gid_idx='$gid_idx'")
	    or return -1;

	return 0;
    }
    elsif (ref($target) eq "Project") {
	my $pid_idx = $target->pid_idx();
	
	DBQueryWarn("delete from group_features where pid_idx='$pid_idx'")
	    or return -1;

	return 0;
    }
    elsif (ref($target) eq "Experiment") {
	my $exptidx = $target->idx();
	
	DBQueryWarn("delete from experiment_features where exptidx='$exptidx'")
	    or return -1;
	
	return 0;
    }
    return -1;
}

sub FeatureEnabled($$$$)
{
    my ($class, $featurename, $user, $group, $experiment) = @_;

305 306
    print STDERR "Checking for feature $featurename.\n"
	if ($verbose);
307 308 309 310 311 312 313 314

    #
    # See if feature is globally disabled;
    #
    my $feature = EmulabFeatures->Lookup($featurename);
    # A non existent feature is always disabled.
    # Do not warn; not all sites will have the same set.
    if (!defined($feature)) {
315 316
	print STDERR "*** WARNING: ".
	    "Checking for non-existent Emulab Feature: $featurename\n"
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 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 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
	    if ($MAINSITE || $debug);
	return 0;
    }
    # Globally disabled.
    if ($feature->disabled()) {
	print STDERR "  Feature is globally disabled\n"
	    if ($debug);
	return 0;
    }
    # Globally enabled.
    if ($feature->enabled()) {
	print STDERR "  Feature is globally enabled\n"
	    if ($debug);
	return 1;
    }
    my $enabled = 0;

    if (defined($user)) {
	my $uid_idx = $user->uid_idx();
	
	my $query_result =
	    DBQueryWarn("select * from user_features ".
			"where feature='$featurename' and uid_idx='$uid_idx'");

	return 0
	    if (!$query_result);

	print STDERR "  Feature is " .
	    ($query_result->numrows ? "enabled" : "disabled") . " for $user\n"
	    if ($debug);

	$enabled += $query_result->numrows;
    }

    if (defined($group)) {
	my $pid_idx = $group->pid_idx();
	my $gid_idx = $group->gid_idx();
	
	my $query_result =
	    DBQueryWarn("select * from group_features ".
			"where feature='$featurename' and ".
			"      pid_idx='$pid_idx' and gid_idx='$gid_idx'");

	return 0
	    if (!$query_result);

	print STDERR "  Feature is " .
	    ($query_result->numrows ? "enabled" : "disabled") . " for $group\n"
	    if ($debug);

	$enabled += $query_result->numrows;
    }

    if (defined($experiment)) {
	my $exptidx = $experiment->idx();
	
	my $query_result =
	    DBQueryWarn("select * from experiment_features ".
			"where feature='$featurename' and ".
			"      exptidx='$exptidx'");

	return 0
	    if (!$query_result);

	print STDERR "  Feature is " .
	    ($query_result->numrows ? "enabled" : "disabled") .
	    " for $experiment\n"
	    if ($debug);

	$enabled += $query_result->numrows;
    }
    print STDERR "  Feature is " . ($enabled ? "enabled\n" : "disabled\n")
	if ($verbose);
    
    return $enabled;
}

#
# List of all current features.
#
sub List($)
{
    my ($class)   = @_;
    my @features  = ();

    my $query_result =
	DBQueryWarn("select feature from emulab_features order by feature");
    return undef
	if (!defined($query_result));

    while (my ($featurename) = $query_result->fetchrow_array()) {
	my $feature = EmulabFeatures->Lookup($featurename);
	push(@features, $feature)
	    if (defined($feature));
    }
    return @features;
}

#
# List users and groups a feature is enabled for.
#
sub ListEnabled($$$$)
{
    my ($self, $pusers, $pgroups, $pexp) = @_;
    my $featurename = $self->feature();
    my @users  = ();
    my @groups = ();
    my @experiments = ();
    require Group;
    require User;
    require Experiment;

    my $query_result =
	DBQueryWarn("select uid_idx from user_features ".
		    "where feature='$featurename' ".
		    "order by uid");
    return -1
	if (!defined($query_result));

    while (my ($uid_idx) = $query_result->fetchrow_array()) {
	my $user = User->Lookup($uid_idx);
	push(@users, $user)
	    if (defined($user));
    }

    $query_result =
	DBQueryWarn("select gid_idx from group_features ".
		    "where feature='$featurename' ".
		    "order by pid,gid");
    return -1
	if (!defined($query_result));

    while (my ($gid_idx) = $query_result->fetchrow_array()) {
	my $group = Group->Lookup($gid_idx);
	push(@groups, $group)
	    if (defined($group));
    }

    $query_result =
	DBQueryWarn("select exptidx from experiment_features ".
		    "where feature='$featurename' ".
		    "order by pid,eid");
    return -1
	if (!defined($query_result));

    while (my ($idx) = $query_result->fetchrow_array()) {
	my $experiment = Experiment->Lookup($idx);
	push(@experiments, $experiment)
	    if (defined($experiment));
    }

    @$pusers  = @users;
    @$pgroups = @groups;
    @$pexp    = @experiments;
    return 0;
}

1;