libEmulab.pm.in 8.52 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2000-2016, 2018 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
#
package libEmulab;
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);
@ISA    = qw(Exporter);
29
@EXPORT = qw(SiteVarExists CreateSiteVar GetSiteVar SetSiteVar
30 31
	     GetSiteVarDesc SetSiteVarDesc NoLogins IsShutDown.
	     IsMultiPCArchitecture PCArchitectures);
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

use emdb;

#
# Check if a site-specific variable exists.
#
# usage: SiteVarExists($name)
#        returns 1 if variable exists;
#        returns 0 otherwise.
#
sub SiteVarExists($)
{
    my($name) = @_;

    $name = DBQuoteSpecial( $name );

    my $query_result =
	DBQueryWarn("select name from sitevariables where name=$name");

    return 0
	if (!$query_result);
    
    return $query_result->numrows;
}

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
#
# Create a new site variable.
#
# usage: CreateSiteVar($name, $desc, $defvalue, $value, $nsinclude)
#        All but $name and $desc are optional.
#        Returns 1 on success, 0 if sitevar exists or on failure.
#
sub CreateSiteVar($$;$$$)
{
    my ($name,$desc,$defval,$val,$nsinc) = @_;

    return 0
	if (SiteVarExists($name));
    return 0
	if (!defined($desc));

    $name = DBQuoteSpecial($name);
    $desc = DBQuoteSpecial($desc);
    if (defined($defval)) {
	$defval = DBQuoteSpecial($defval);
    } else {
	$defval = "NULL";
    }
    if (defined($val)) {
	$val = DBQuoteSpecial($val);
    } else {
	$val = "NULL";
    }
    if (defined($nsinc)) {
	$nsinc = ($nsinc != 0);
    } else {
	$nsinc = 0;
    }

    my $query_result =
	DBQueryWarn("insert into sitevariables ".
		    "(name,value,defaultvalue,description,ns_include) ".
		    "values ($name,$val,$defval,$desc,$nsinc)");

    return $query_result ? 1 : 0;
}

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 128 129 130 131 132 133 134 135 136 137 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
#
# Get site-specific variable.
# Get the value of the variable, or the default value if
# the value is undefined (NULL).
#

# usage: GetSiteVar($name, char \*rptr )
#        Without rptr: returns value if variable is defined; dies otherwise.
#        With rptr:    returns value in $rptr if variable is defined; returns
#                      zero otherwise, or any failure.
#
sub GetSiteVar($;$)
{
    my ($name, $rptr) = @_;
    my $value;

    $name = DBQuoteSpecial( $name );
    
    my $query_string =
	"select value,defaultvalue from sitevariables where name=$name";

    my $query_result;
    if (defined($rptr)) {
	#
	# I added the result parameter as an option to avoid changing every
	# call to TBGetSiteVar(). Sorry. When called in this manner, it is
	# up to the caller to decide what to do when it fails. 
	#
	$query_result = DBQueryWarn($query_string);

	return 0
	    if (! $query_result)
    }
    else {
	$query_result = DBQueryFatal($query_string);
    }

    if ($query_result->numrows > 0) {
	my ($curvalue, $defaultvalue) = $query_result->fetchrow_array();

	if (defined($curvalue)) {
	    $value = $curvalue;
	}
	elsif (defined($defaultvalue)) {
	    $value = $defaultvalue;
	}
    }
    if (defined($rptr)) {
	if (defined($value)) {
	    $$rptr = $value;
	    return 1;
	}
	return 0;
    }
    elsif (defined($value)) {
	return $value;
    }
    die("*** $0:\n".
	"    Attempted to fetch unknown site variable $name\n");
}

#
# Set a sitevar. Assumed to be a real sitevar.
#
# usage: SetSiteVar($name, $value)
#
sub SetSiteVar($$)
{
    my ($name, $value) = @_;

    $name  = DBQuoteSpecial($name);
    $value = DBQuoteSpecial($value);

    my $query_result =
	DBQueryWarn("update sitevariables set value=$value where name=$name");

    return 0
	if (!$query_result);
    return 1;
}

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
# usage: GetSiteVarDesc($name)
#        Returns description if variable is defined; dies otherwise.
#
sub GetSiteVarDesc($)
{
    my ($name) = @_;
    my $value;

    $name = DBQuoteSpecial( $name );
    
    my $query_string =
	"select description from sitevariables where name=$name";

    my $query_result = DBQueryFatal($query_string);
    if ($query_result->numrows > 0) {
	my ($desc) = $query_result->fetchrow_array();
	return $desc;
    }
    die("*** $0:\n".
	"    Attempted to fetch description of unknown site variable $name\n");
}

#
# Set a sitevar description. Assumed to be a real sitevar.
#
# usage: SetSiteVarDesc($name, $desc)
#
sub SetSiteVarDesc($$)
{
    my ($name, $desc) = @_;

    $name  = DBQuoteSpecial($name);
    $desc = DBQuoteSpecial($desc);

    my $query_result =
	DBQueryWarn("update sitevariables set description=$desc where name=$name");

    return 0
	if (!$query_result);
    return 1;
}

222 223 224 225 226 227 228
#
# Check for nologins; web interface disabled means other interfaces
# should be disabled. Not using libdb:GetSiteVar cause do not want to
# drag all that stuff in. Predicate; retun 1 if no logins is set.
#
sub NoLogins()
{
229 230 231 232
    my $shutdown = IsShutDown();
    return 1
	if ($shutdown);
    
233 234 235 236 237 238 239 240 241 242 243 244 245
    my $query_result =
	DBQueryWarn("select value from sitevariables ".
		    "where name='web/nologins'");

    return 1
	if (!$query_result);
    return 0
	if (!$query_result->numrows);
    my ($value) = $query_result->fetchrow_array();

    return ($value ? 1 : 0);
}

246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
# Ditto shutdown.
sub IsShutDown()
{
    my $query_result =
	DBQueryWarn("select value from sitevariables ".
		    "where name='general/testbed_shutdown'");

    return 1
	if (!$query_result);
    return 0
	if (!$query_result->numrows);
    my ($value) = $query_result->fetchrow_array();

    return ($value ? 1 : 0);
}

262 263 264 265 266 267 268
#
# Lock and Unlock.
#
sub EmulabLock($)
{
    my ($name) = @_;

269 270 271
    EmulabCreateLock($name) == 0
	or return -1;

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    my $query_result =
	DBQueryWarn("update emulab_locks set value=1 " .
		    "where name='$name' and value=0");

    if (! $query_result || $query_result->affectedrows == 0) {
	return -1;
    }
    return 0;
}

sub EmulabUnlock($)
{
    my ($name) = @_;

    DBQueryWarn("update emulab_locks set value=0 where name='$name'")
	or return -1;

    return 0;
}

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
sub EmulabCreateLock($)
{
    my ($name) = @_;
    
    my $query_result =
	DBQueryWarn("select * from emulab_locks where name='$name'");
    return -1
	if (! $query_result);
    if (!$query_result->numrows) {
	DBQueryWarn("lock tables emulab_locks write")
	    or return -1;

	$query_result =
	    DBQueryWarn("select * from emulab_locks where name='$name'");
	if (! $query_result) {
	    DBQueryWarn("unlock tables");
	    return -1;
	}
	if (!$query_result->numrows &&
	    !DBQueryWarn("insert into emulab_locks set ".
			 "  name='$name',value=0")) {
	    DBQueryWarn("unlock tables");
	    return -1;
	}
	DBQueryWarn("unlock tables")
	    or return -1;
    }
    return 0;
}

322 323 324 325 326 327 328
#
# Count up/down locking.
#
sub EmulabCountLock($$)
{
    my ($name, $count) = @_;

329 330 331
    EmulabCreateLock($name) == 0
	or return -1;

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    my $query_result =
	DBQueryWarn("update emulab_locks set value=value+1 " .
		    "where name='$name' and value<${count}");

    if (! $query_result || $query_result->affectedrows == 0) {
	return -1;
    }
    return 0;
}

sub EmulabCountUnlock($)
{
    my ($name) = @_;

    DBQueryWarn("update emulab_locks set value=value-1 ".
Leigh Stoller's avatar
Leigh Stoller committed
347
		"where name='$name' and value>0")
348 349 350 351 352
	or return -1;

    return 0;
}

353 354 355 356 357 358 359 360 361 362
#
# Are we multi architecture (pc class only). Only consider an architecture
# if we have actual nodes for it.
#
sub IsMultiPCArchitecture()
{
    my $query_result =
	DBQueryWarn("select distinct architecture from node_types as nt ".
		    "left join nodes as n on n.type=nt.type ".
		    "where architecture is not null and class='pc' and ".
363 364
		    "   n.node_id is not null and n.role='testnode' and ".
		    "   architecture!=''");
365 366 367 368 369 370 371 372 373
    return $query_result->numrows > 1 ? 1 : 0;
}
sub PCArchitectures()
{
    my @results = ();
    
    my $query_result =
	DBQueryWarn("select distinct architecture from node_types as nt ".
		    "left join nodes as n on n.type=nt.type ".
374 375 376
		    "where architecture is not null and ".
		    "   architecture!='' and ".
		    "   class='pc' and ".
377 378 379 380 381 382 383
		    "   n.node_id is not null and n.role='testnode'");
    while (my ($architecture) = $query_result->fetchrow_array()) {
	push(@results, $architecture);
    }
    return @results;
}

384
1;