NodeType.pm.in 13.2 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2005-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 29 30 31 32 33 34
#
package NodeType;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

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

use libdb;
use libtestbed;
35
use EmulabConstants;
36 37 38 39 40 41 42 43 44 45 46
use English;
use Data::Dumper;
use overload ('""' => 'Stringify');

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

# Cache of instances to avoid regenerating them.
my %nodetypes	= ();
my %nodeclasses = ();
47 48 49 50
BEGIN { use emutil;
	emutil::AddCache(\%nodetypes);
	emutil::AddCache(\%nodeclasses); }

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
my $debug	= 0;

# Little helper and debug function.
sub mysystem($)
{
    my ($command) = @_;

    print STDERR "Running '$command'\n"
	if ($debug);
    return system($command);
}

#
# Lookup a (physical) node and create a class instance to return.
#
sub Lookup($$)
{
    my ($class, $type) = @_;

    # Look in cache first
    return $nodetypes{$type}
        if (exists($nodetypes{$type}));

    my $query_result =
	DBQueryWarn("select * from node_types where type='$type'");

    return undef
	if (!$query_result || !$query_result->numrows);

80 81 82 83 84 85 86
    return LookupRow($class, $type, $query_result->fetchrow_hashref());
}

sub LookupRow($$$)
{
    my ($class, $type, $row) = @_;

87 88 89
    my $self         = {};
    # Do not use the embedded type field, cause of auxtypes.
    $self->{"TYPE"}  = $type;
90
    $self->{"DBROW"} = $row;
91
    $self->{"CLASS"} = ($type eq $row->{"class"} ? $type : undef);
92
    $self->{"ATTRS"} = undef;
Leigh Stoller's avatar
Leigh Stoller committed
93
    $self->{"ISAUX"} = 0;
94 95 96 97 98 99 100

    bless($self, $class);

    # Add to cache.
    $nodetypes{$type} = $self;
    return $self;
}
101

102 103 104 105 106
# accessors
sub field($$)  { return ((! ref($_[0])) ? -1 : $_[0]->{'DBROW'}->{$_[1]}); }
# Do not use the embedded type field, cause of auxtypes.
sub type($)           { return $_[0]->{'TYPE'}; }
sub class($)          { return field($_[0], 'class'); }
Leigh Stoller's avatar
Leigh Stoller committed
107
sub IsAuxType($)      { return $_[0]->{'ISAUX'}; }
108
sub architecture($)   { return field($_[0], 'architecture'); }
109 110 111 112 113 114 115 116
sub isvirtnode($)     { return field($_[0], 'isvirtnode'); }
sub isjailed($)       { return field($_[0], 'isjailed'); }
sub isdynamic($)      { return field($_[0], 'isdynamic'); }
sub isremotenode($)   { return field($_[0], 'isremotenode'); }
sub issubnode($)      { return field($_[0], 'issubnode'); }
sub isplabdslice($)   { return field($_[0], 'isplabdslice'); }
sub isplabphysnode($) { return field($_[0], 'isplabphysnode'); }
sub issimnode($)      { return field($_[0], 'issimnode'); }
117
sub isgeninode($)     { return field($_[0], 'isgeninode'); }
118
sub isfednode($)      { return field($_[0], 'isfednode'); }
119
sub isswitch($)       { return field($_[0], 'isswitch'); }
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

#
# Force a reload of the data.
#
sub LookupSync($$)
{
    my ($class, $type) = @_;

    # delete from cache
    delete($nodetypes{$type})
        if (exists($nodetypes{$type}));
    delete($nodeclasses{$type})
        if (exists($nodeclasses{$type}));

    return Lookup($class, $type);
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#
# Find me a type, or a class, or by auxtype. Just find me something!
#
sub LookupAny($$)
{
    my ($class, $type) = @_;

    my $obj = NodeType->Lookup($type);
    return $obj
	if (defined($obj));

    my $query_result =
	DBQueryWarn("select type from node_types where class='$type' limit 1");
    return undef
	if (!$query_result);
    if ($query_result->numrows) {
	my ($ntype) = $query_result->fetchrow_array();
154 155 156 157 158
	my $obj = NodeType->Lookup($ntype);
	return undef
	    if (!defined($obj));
	$obj->{'CLASS'} = $type;
	return $obj;
159 160
    }

Leigh Stoller's avatar
Leigh Stoller committed
161 162
    # Try for an auxtype.
    return NodeType->LookupAuxType($type);
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
#
# Return a list of all types.
#
sub AllTypes($)
{
    my ($class)  = @_;
    my @alltypes = ();
    
    my $query_result =
	DBQueryWarn("select type from node_types");
    
    return undef
	if (!$query_result || !$query_result->numrows);

    while (my ($type) = $query_result->fetchrow_array()) {
	my $typeinfo = Lookup($class, $type);

	# Something went wrong?
	return undef
	    if (!defined($typeinfo));
	
	push(@alltypes, $typeinfo);
    }
    return @alltypes;
}

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
#
# Lookup all types of a given architecture.
#
sub LookupArchitectureTypes($$)
{
    my ($class,$arch)  = @_;
    my @alltypes = ();
    
    my $query_result =
	DBQueryWarn("select type from node_types where architecture='$arch'");
    
    return undef
	if (!$query_result || !$query_result->numrows);

    while (my ($type) = $query_result->fetchrow_array()) {
	my $typeinfo = Lookup($class, $type);

	# Something went wrong?
	return undef
	    if (!defined($typeinfo));
	
	push(@alltypes, $typeinfo);
    }
    return @alltypes;
}

Leigh Stoller's avatar
Leigh Stoller committed
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 265 266
#
# Lookup an auxtype.
#
sub LookupAuxType($$)
{
    my ($class, $auxtype)  = @_;
    
    my $query_result =
	DBQueryWarn("select nt.type from node_types_auxtypes as at ".
		    "left join node_types as nt on nt.type=at.type ".
		    "where at.auxtype='$auxtype'");
    
    return undef
	if (!$query_result || !$query_result->numrows);

    my ($type) = $query_result->fetchrow_array();
    my $typeinfo = Lookup($class, $type);

    # Something went wrong?
    return undef
	if (!defined($typeinfo));

    #
    # Generate a new type entry, but named by the auxtype instead.
    # Underlying data is shared; might need to change that.
    #
    my $newtype         = {};
    $newtype->{"TYPE"}  = $auxtype;
    $newtype->{"DBROW"} = $typeinfo->{"DBROW"};
    $newtype->{"ATTRS"} = $typeinfo->{"ATTRS"};
    $newtype->{"ISAUX"} = 1;
    $newtype->{"BASE"}  = $typeinfo;
    bless($newtype, $class);

    # Add to cache.
    $nodetypes{$auxtype} = $newtype;
    return $newtype;
}
sub BaseType($)
{
    my ($self) = @_;
    
    return undef
	if (!$self->IsAuxType());
    return $self->{'BASE'};
}

#
# Return list of all auxtypes.
#
267 268 269 270 271 272 273 274 275 276
sub AuxTypes($)
{
    my ($class)  = @_;
    my @auxtypes = ();
    
    my $query_result =
	DBQueryFatal("select at.auxtype,nt.type ".
		     "  from node_types_auxtypes as at ".
		     "left join node_types as nt on nt.type=at.type ");
    
Leigh Stoller's avatar
Leigh Stoller committed
277
    return ()
278 279 280
	if (!$query_result || !$query_result->numrows);

    while (my ($auxtype, $type) = $query_result->fetchrow_array()) {
Leigh Stoller's avatar
Leigh Stoller committed
281
	my $typeinfo = LookupAuxType($class, $auxtype);
282 283 284 285 286

	# Something went wrong?
	return undef
	    if (!defined($typeinfo));
	
Leigh Stoller's avatar
Leigh Stoller committed
287
	push(@auxtypes, $typeinfo);
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
    }
    return @auxtypes;
}

#
# Load attributes if not already loaded.
#
sub LoadAttributes($)
{
    my ($self) = @_;

    return -1
	if (!ref($self));

    return 0
	if (defined($self->{"ATTRS"}));

    #
    # Get the attribute array.
    #
    my $type = $self->type();
    
    my $query_result =
	DBQueryWarn("select attrkey,attrvalue,attrtype ".
		    "  from node_type_attributes ".
		    "where type='$type'");

    $self->{"ATTRS"} = {};
    while (my ($key,$val,$type) = $query_result->fetchrow_array()) {
	$self->{"ATTRS"}->{$key} = { "key"   => $key,
				     "value" => $val,
				     "type"  => $type };
    }
    return 0;
}

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $type  = $self->type();
    my $class = $self->class();

    return "[NodeType: $type/$class]";
}

337
#
338 339
# Did we find this type cause it was a class lookup. Bogus, need a
# different object for this.
340
#
341
sub IsClass($)
342
{
343
    my ($self) = @_;
344

345
    return (defined($self->{'CLASS'}) ? 1 : 0);
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
#
# Look for an attribute.
#
sub GetAttribute($$;$$)
{
    my ($self, $attrkey, $pattrvalue, $pattrtype) = @_;
    
    goto bad
	if (!ref($self));

    $self->LoadAttributes() == 0
	or goto bad;

    if (!exists($self->{"ATTRS"}->{$attrkey})) {
	return undef
	    if (!defined($pattrvalue));
	$$pattrvalue = undef;
	return 0;
    }

    my $ref = $self->{"ATTRS"}->{$attrkey};

    # Return value instead if a $pattrvalue not provided. 
    return $ref->{'value'}
	if (!defined($pattrvalue));
    
    $$pattrvalue = $ref->{'value'};
    $$pattrtype  = $ref->{'type'}
        if (defined($pattrtype));

    return 0;
    
  bad:
    return undef
	if (!defined($pattrvalue));
    $$pattrvalue = undef;
    return -1;
}
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

#
# Look for an attribute.
#
sub GetAttributes($)
{
    my ($self) = @_;
    
    return undef
	if (!ref($self));

    $self->LoadAttributes() == 0
	or return undef;

    return $self->{"ATTRS"};
}

403 404
# Shortcuts for typical attributes.
sub default_osid($;$)  {return GetAttribute($_[0], "default_osid", $_[1]); }
405
sub default_imageid($;$){return GetAttribute($_[0], "default_imageid",$_[1]); }
406 407 408 409 410 411 412
sub delay_osid($;$)    {return GetAttribute($_[0], "delay_osid", $_[1]); }
sub jail_osid($;$)     {return GetAttribute($_[0], "jail_osid", $_[1]); }
sub imageable($;$)     {return GetAttribute($_[0], "imageable", $_[1]); }
sub memory($;$)        {return GetAttribute($_[0], "memory", $_[1]); }
sub disksize($;$)      {return GetAttribute($_[0], "disksize", $_[1]); }
sub disktype($;$)      {return GetAttribute($_[0], "disktype", $_[1]); }
sub bootdisk_unit($;$) {return GetAttribute($_[0], "bootdisk_unit", $_[1]); }
413
sub boot_method($;$)   {return GetAttribute($_[0], "boot_method", $_[1]); }
414 415 416 417 418 419 420 421
sub pxe_boot_path($;$) {return GetAttribute($_[0], "pxe_boot_path", $_[1]); }
sub processor($;$)     {return GetAttribute($_[0], "processor", $_[1]); }
sub frequency($;$)     {return GetAttribute($_[0], "frequency", $_[1]); }
sub bios_waittime($;$) {return GetAttribute($_[0], "bios_waittime", $_[1]); }
sub control_iface($;$) {return GetAttribute($_[0], "control_interface",$_[1]);}
sub adminmfs_osid($;$) {return GetAttribute($_[0], "adminmfs_osid",$_[1]);}
sub rebootable($;$)    {return GetAttribute($_[0], "rebootable",$_[1]);}
sub power_delay($;$)   {return GetAttribute($_[0], "power_delay",$_[1]);}
422
sub shared($;$)        {return GetAttribute($_[0], "shared",$_[1]);}
423
sub cyclewhenoff($;$)  {return GetAttribute($_[0], "cyclewhenoff",$_[1]);}
424

425 426 427
sub initial_experiment($;$) {
    return GetAttribute($_[0], "initial_experiment",$_[1]);
}
428 429 430 431 432 433 434 435 436 437 438 439
sub virtnode_capacity($;$) {
    return GetAttribute($_[0], "virtnode_capacity", $_[1]);
}
sub simnode_capacity($;$) {
    return GetAttribute($_[0], "simnode_capacity", $_[1]);
}
sub delay_capacity($;$) {
    return GetAttribute($_[0], "delay_capacity", $_[1]);
}
sub trivlink_maxspeed($;$) {
    return GetAttribute($_[0], "trivlink_maxspeed", $_[1]);
}
440 441 442
sub isdedicatedremote($;$) {
    return GetAttribute($_[0], "dedicated_widearea", $_[1]);
}
443 444 445
sub isfakenode($;$) {
    return GetAttribute($_[0], "fakenode", $_[1]);
}
446 447 448
sub isblackbox($;$) {
    return GetAttribute($_[0], "blackbox", $_[1]);
}
449 450
sub control_interface($;$) { return control_iface($_[0], $_[1]); }

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
#
# Set the value of an attribute
#
sub SetAttribute($$$;$)
{
    my ($self, $attrkey, $attrvalue, $attrtype) = @_;
    
    goto bad
	if (!ref($self));

    $self->LoadAttributes() == 0
	or return -1;

    $attrtype = "string"
	if (!defined($attrtype));
    my $safe_attrvalue = DBQuoteSpecial($attrvalue);
    my $type = $self->type();

    DBQueryWarn("replace into node_type_attributes set ".
		"  type='$type', attrkey='$attrkey', ".
		"  attrtype='$attrtype', attrvalue=$safe_attrvalue")
	or return -1;

    $self->{"ATTRS"}->{$attrkey} = $attrvalue;
    return 0;
}

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
#
# Free/Inuse count for a type.
#
sub Counts($)
{
    my ($self) = @_;
    my $type   = $self->type();
    my $total  = 0;
    my $free   = 0;
    
    my $query_result =
	DBQueryWarn("select count(*) from nodes where type='$type'");
    return undef
	if (!defined($query_result));
    if ($query_result->numrows) {
	($total) = $query_result->fetchrow_array();
    }
    $query_result =
	DBQueryWarn("select count(a.node_id) from nodes as a ".
		     "left join reserved as b on a.node_id=b.node_id ".
		     "where b.node_id is null and a.type='$type' and ".
                     "    a.reserved_pid is null and ".
                     "    (a.eventstate='" . TBDB_NODESTATE_ISUP . "' or ".
                     "     a.eventstate='" . TBDB_NODESTATE_POWEROFF . "' or ".
                     "     a.eventstate='" . TBDB_NODESTATE_ALWAYSUP . "' or ".
                     "     a.eventstate='" . TBDB_NODESTATE_PXEWAIT . "')");
    return undef
	if (!defined($query_result));
    if ($query_result->numrows) {
	($free) = $query_result->fetchrow_array();
    }
    return {"total" => $total, "free" => $free};
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
}

#
# Utility function to look up a global vtype and return the list.
#
sub GlobalVtypeTypes($$)
{
    my ($class, $vtype) = @_;
    my @result = ();

    my $query_result =
	DBQueryWarn("select types from global_vtypes where vtype='$vtype'");
    return ()
	if (!defined($query_result) || !$query_result->numrows);

    my ($typestring) = $query_result->fetchrow_array();
    foreach my $t (split(/\s+/, $typestring)) {
	my $type = NodeType->Lookup($t);
	push(@result, $type)
	    if (defined($type));
    }
    return @result;
}
533

534 535 536
# _Always_ make sure that this 1 is at the end of the file...

1;