APT_Profile.pm.in 29.5 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2007-2015 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
# 
# {{{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/>.
# 
# }}}
#
package APT_Profile;

26 27 28 29 30 31 32 33 34 35 36
#
# Note about permissions bits.
#
# listed - The profile will be listed on the home page for anyone to see/use.
# public - Anyone can instantiate the profile, regardless of its listed bit
#          Say, if you send a URL to someone. 
# shared - Shared with logged in users. If not listed, then the default is
#          that only project members can see/use the profile, unless the public
#          is set (but they need a url). Shared says any logged in user can
#          see and use the profile.  

37 38 39 40 41 42 43 44 45 46
use strict;
use Carp;
use Exporter;
use vars qw(@ISA @EXPORT $AUTOLOAD);

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

# Must come after package declaration!
use EmulabConstants;
47
use emutil;
48
use emdb;
49
use APT_Dataset;
50
use GeniXML;
51
use GeniHRN;
52 53 54 55 56 57 58 59 60
use libtestbed;
use English;
use Data::Dumper;
use overload ('""' => 'Stringify');

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

61 62
my $debug = 0;

63 64 65 66 67 68 69 70
# Concat id/vers.
sub versid($)
{
    my ($self) = @_;

    return $self->profileid() . ":" . $self->version();
}

Leigh B Stoller's avatar
Leigh B Stoller committed
71 72 73 74 75 76 77 78
# Concat name/vers.
sub versname($)
{
    my ($self) = @_;

    return $self->name() . ":" . $self->version();
}

79 80 81 82 83 84 85 86 87 88
sub BlessRow($$)
{
    my ($class, $row) = @_;
    
    my $self           = {};
    $self->{'DBROW'}   = $row;

    bless($self, $class);
    return $self;
}
89 90

#
91
# Lookup. 
92
#
93
sub Lookup($$;$$)
94
{
95
    my ($class, $arg1, $arg2, $arg3) = @_;
96 97

    #
98 99
    # A single arg is either an index or "pid,profile[:version]" or
    # "pid/profile[:version]" string.
100 101 102
    #
    if (!defined($arg2)) {
	if ($arg1 =~ /^(\d*)$/) {
103
	    my $result =
104 105
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    " from apt_profiles as i ".
106 107 108 109 110 111 112 113
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.profileid='$arg1'");
	    return undef
		if (! $result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
114 115
	}
	elsif ($arg1 =~ /^([-\w]*),([-\w\.\+]*)$/ ||
116 117
		$arg1 =~ /^([-\w]*)\/([-\w\.\+]*)$/) {
	    my $result =
118 119
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    " from apt_profiles as i ".
120 121 122 123 124 125 126 127 128 129 130 131
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.pid='$1' and i.name='$2'");
	    return undef
		if (! $result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref());
	}
	elsif ($arg1 =~ /^([-\w]*),([-\w\.\+]*):(\d*)$/ ||
		$arg1 =~ /^([-\w]*)\/([-\w\.\+]*):(\d*)$/) {
	    my $result =
132 133
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    " from apt_profiles as i ".
134 135 136 137 138 139 140 141
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid ".
			    "where i.pid='$1' and i.name='$2' and ".
			    "      v.version='$3' and v.deleted is null");
	    return undef
		if (!$result || !$result->numrows);

	    return BlessRow($class, $result->fetchrow_hashref())
142 143
	}
	elsif ($arg1 =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
144 145 146 147 148
	    #
	    # First look to see if the uuid is for the profile itself,
	    # which means current version. Otherwise look for a
	    # version with the uuid.
	    #
149
	    my $result =
150 151 152 153 154 155 156 157 158 159
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.uuid='$arg1'");
	    return undef
		if (! $result);
	    return BlessRow($class, $result->fetchrow_hashref())
		if ($result->numrows);
160

161 162 163 164 165 166 167
	    $result =
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    "  from apt_profile_versions as v ".
			    "left join apt_profiles as i on ".
			    "     v.profileid=i.profileid ".
			    "where v.uuid='$arg1' and ".
			    "      v.deleted is null");
168 169
	    return undef
		if (! $result || !$result->numrows);
170
	    return BlessRow($class, $result->fetchrow_hashref());
171 172 173
	}
	return undef;
    }
174 175 176
    elsif (!defined($arg3)) {
	if ($arg1 =~ /^\d+$/ && $arg2 =~ /^\d+$/) {
	    my $result =
177 178
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    "  from apt_profiles as i ".
179 180 181 182 183 184
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid ".
			    "where i.profileid='$arg1' and v.version='$arg2' ".
			    " and  v.deleted is null");
	    return undef
		if (! $result || !$result->numrows);
185

186 187 188 189
	    return BlessRow($class, $result->fetchrow_hashref());
	}
	elsif ($arg1 =~ /^[-\w]*$/ && $arg2 =~ /^([-\w\.\+]*):(\d+)$/) {
	    my $result =
190 191
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    " from apt_profiles as i ".
192 193 194 195 196 197
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid ".
			    "where i.pid='$arg1' and i.name='$1' and ".
			    "      v.version='$2'");
	    return undef
		if (! $result || !$result->numrows);
198

199 200 201 202
	    return BlessRow($class, $result->fetchrow_hashref());
	}
	elsif ($arg1 =~ /^[-\w]*$/ && $arg2 =~ /^[-\w\.\+]*$/) {
	    my $result =
203 204
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    "  from apt_profiles as i ".
205 206 207 208 209 210
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.pid='$arg1' and i.name='$arg2'");
	    return undef
		if (! $result || !$result->numrows);
211

212 213 214
	    return BlessRow($class, $result->fetchrow_hashref());
	}
	return undef;
215
    }
216 217 218 219
    else {
	if ($arg1 =~ /^[-\w]*$/ &&
	    $arg2 =~ /^[-\w\.\+]*$/ && $arg3 =~ /^\d+$/) {
	    my $result =
220 221
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    "  from apt_profiles as i ".
222 223 224 225 226 227
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid ".
			    "where i.pid='$arg1' and i.name='$arg2' and ".
			    "      v.version='$arg3' and v.deleted is null");
	    return undef
		if (!$result || !$result->numrows);
228

229 230 231 232
	    return BlessRow($class, $result->fetchrow_hashref());
	}
    }
    return undef;
233 234 235 236 237 238 239 240 241
}

AUTOLOAD {
    my $self  = $_[0];
    my $type  = ref($self) or croak "$self is not an object";
    my $name  = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    # A DB row proxy method call.
242 243
    if (exists($self->{'DBROW'}->{$name})) {
	return $self->{'DBROW'}->{$name};
244 245 246 247 248 249 250 251 252
    }
    carp("No such slot '$name' field in class $type");
    return undef;
}

# Break circular reference someplace to avoid exit errors.
sub DESTROY {
    my $self = shift;

253
    $self->{'DBROW'} = undef;
254 255 256 257 258 259 260 261 262 263 264 265
}

#
# Refresh a class instance by reloading from the DB.
#
sub Refresh($)
{
    my ($self) = @_;

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

266 267
    my $profileid = $self->profileid();
    my $version   = $self->version();
268 269
    
    my $query_result =
270 271
	DBQueryWarn("select * from apt_profile_versions ".
		    "where profileid='$profileid' and version='$version'");
272 273 274 275

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

276
    $self->{'DBROW'} = $query_result->fetchrow_hashref();
277 278 279 280 281 282 283

    return 0;
}

#
# Create a profile
#
284
sub Create($$$$$$)
285
{
286
    my ($class, $parent, $project, $creator, $argref, $usrerr_ref) = @_;
287 288 289 290 291 292 293 294 295 296

    my $name    = DBQuoteSpecial($argref->{'name'});
    my $pid     = $project->pid();
    my $pid_idx = $project->pid_idx();
    my $uid     = $creator->uid();
    my $uid_idx = $creator->uid_idx();

    #
    # The pid/imageid has to be unique, so lock the table for the check/insert.
    #
297 298
    DBQueryWarn("lock tables apt_profiles write, apt_profile_versions write, ".
		"            emulab_indicies write")
299 300 301 302 303 304 305 306 307 308 309 310
	or return undef;

    my $query_result =
	DBQueryWarn("select name from apt_profiles ".
		    "where pid_idx='$pid_idx' and name=$name");

    if ($query_result->numrows) {
	DBQueryWarn("unlock tables");
	$$usrerr_ref = "Profile already exists in project!";
	return undef;
    }
    
311
    my $profileid = TBGetUniqueIndex("next_profile", undef, 1);
312 313
    my $puuid     = NewUUID();
    my $vuuid     = NewUUID();
314 315 316
    my $rspec     = DBQuoteSpecial($argref->{'rspec'});
    my $cquery    = "";
    my $vquery    = "";
317

318 319 320 321 322 323 324 325
    #
    # This part is common between the two tables.
    #
    $cquery .= "name=$name,profileid='$profileid'";
    $cquery .= ",pid='$pid',pid_idx='$pid_idx'";

    # And the versions table.
    $vquery  = $cquery;
326
    $vquery .= ",uuid='$vuuid',created=now()";
327 328 329 330 331 332 333 334
    $vquery .= ",creator='$uid',creator_idx='$uid_idx'";
    $vquery .= ",rspec=$rspec";

    # Set derived from pointer.
    if (defined($parent)) {
	$vquery .= ",parent_profileid=" . $parent->profileid();
	$vquery .= ",parent_version=" . $parent->version();
    }
335 336
    if (exists($argref->{'script'}) && $argref->{'script'} ne "") {
	$vquery .= ",script=" . DBQuoteSpecial($argref->{'script'});
337 338 339
	if (exists($argref->{'paramdefs'}) && $argref->{'paramdefs'} ne "") {
	    $vquery .= ",paramdefs=" . DBQuoteSpecial($argref->{'paramdefs'});
	}
340
    }
341

342
    # Back to the main table.
343
    $cquery .= ",uuid='$puuid'";
344
    $cquery .= ",public=1"
345
	if (exists($argref->{'public'}) && $argref->{'public'});
346
    $cquery .= ",listed=1"
347
	if (exists($argref->{'listed'}) && $argref->{'listed'});
348
    $cquery .= ",shared=1"
349
	if (exists($argref->{'shared'}) && $argref->{'shared'});
350 351
    $cquery .= ",topdog=1"
	if (exists($argref->{'topdog'}) && $argref->{'topdog'});
352

353 354 355 356 357 358 359 360 361
    # Create the main entry:
    if (! DBQueryWarn("insert into apt_profiles set $cquery")) {
	DBQueryWarn("unlock tables");
	tberror("Error inserting new apt_profiles record!");
	return undef;
    }
    # And the versions entry.
    if (! DBQueryWarn("insert into apt_profile_versions set $vquery")) {
	DBQueryWarn("delete from apt_profiles where profileid='$profileid'");
362
	DBQueryWarn("unlock tables");
363
	tberror("Error inserting new apt_profile_versions record!");
364 365 366 367 368 369
	return undef;
    }
    DBQueryWarn("unlock tables");
    return Lookup($class, $pid, $argref->{'name'});
}

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
#
# Create a new version of a profile.
#
sub NewVersion($$)
{
    my ($self, $creator) = @_;
    my $profileid   = $self->profileid();
    my $version     = $self->version();
    my $uid         = $creator->uid();
    my $uid_idx     = $creator->uid_idx();

    DBQueryWarn("lock tables apt_profiles write, ".
		"            apt_profile_versions write, ".
		"            apt_profile_versions as v write")
	or return undef;

    #
    # This might not be the head version, so have to find the
    # current max.
    #
    my $query_result =
	DBQueryWarn("select max(version) from apt_profile_versions ".
		    "where profileid='$profileid'");
    goto bad
	if (!$query_result || !$query_result->numrows);

    my ($newvers) = $query_result->fetchrow_array() + 1;

    #
    # Insert new version. The "current" version becomes this one.
    #
    goto bad
	if (! DBQueryWarn("insert into apt_profile_versions ".
			  "  (name,profileid,version,pid,pid_idx, ".
			  "   creator,creator_idx,created,uuid, ".
405 406
			  "   parent_profileid,parent_version,rspec, ".
			  "   script,paramdefs) ".
407 408
			  "select name,profileid,'$newvers',pid,pid_idx, ".
			  "  '$uid','$uid_idx',now(),uuid(),parent_profileid, ".
409
			  "  '$version',rspec,script,paramdefs ".
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
			  "from apt_profile_versions as v ".
			  "where v.profileid='$profileid' and ".
			  "      v.version='$version'"));
    if (! DBQueryWarn("update apt_profiles set version=$newvers ".
		      "where profileid='$profileid'")) {
	DBQueryWarn("delete from apt_profile_versions ".
		    "where profileid='$profileid' and version='$version'");
	goto bad;
    }
    DBQueryWarn("unlock tables");
    return APT_Profile->Lookup($profileid, $newvers);
  bad:
    DBQueryWarn("unlock tables");
    return undef;
}

Leigh B Stoller's avatar
Leigh B Stoller committed
426 427 428 429 430 431 432 433 434
#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $pid       = $self->pid();
    my $name      = $self->name();
435
    my $version   = $self->version();
Leigh B Stoller's avatar
Leigh B Stoller committed
436

437
    return "[Profile: $pid,$name:$version]";
Leigh B Stoller's avatar
Leigh B Stoller committed
438 439
}

440 441 442
#
# Perform some updates ...
#
443
sub UpdateVersion($$)
444 445 446 447 448 449 450
{
    my ($self, $argref) = @_;

    # Must be a real reference. 
    return -1
	if (! ref($self));

451 452
    my $profileid = $self->profileid();
    my $version   = $self->version();
453

454
    my $query = "update apt_profile_versions set ".
455 456
	join(",", map("$_=" . DBQuoteSpecial($argref->{$_}), keys(%{$argref})));

457
    $query .= " where profileid='$profileid' and version='$version'";
458 459 460 461 462 463 464

    return -1
	if (! DBQueryWarn($query));

    return Refresh($self);
}

465 466 467 468
#
# Perform some updates ...
#
sub UpdateMetaData($$)
469
{
470
    my ($self, $argref) = @_;
471 472 473 474 475

    # Must be a real reference. 
    return -1
	if (! ref($self));

476
    my $profileid = $self->profileid();
477

478 479 480 481
    #
    # This is the only metadata we can update.
    #
    my %mods = ();
482
    foreach my $key ("listed", "shared", "public", "topdog") {
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
	if (exists($argref->{$key})) {
	    $mods{$key} = $argref->{$key};
	}
    }
    my $query = "update apt_profiles set ".
	join(",", map("$_=" . DBQuoteSpecial($mods{$_}), keys(%mods)));

    $query .= " where profileid='$profileid'";

    return -1
	if (! DBQueryWarn($query));

    return Refresh($self);
}

sub Delete($$)
{
    my ($self, $purge) = @_;
    my $profileid = $self->profileid();
502

503 504 505 506 507 508 509 510 511 512 513
    $purge = 0
	if (!defined($purge));
    
    DBQueryWarn("lock tables apt_profiles write, apt_profile_versions write")
	or return -1;

    DBQueryWarn("delete from apt_profiles where profileid='$profileid'")
	or goto bad;
    
    if ($purge) {
	goto bad
514 515
	    if (! DBQueryWarn("delete from apt_profile_versions ".
			      "where profileid='$profileid'"));
516 517 518 519 520 521 522 523 524
    }
    else {
	# Set deleted on all of the versions.
	DBQueryWarn("update apt_profile_versions set ".
		    "    deleted=now(),locked=null,locker_pid=0 ".
		    "where profileid='$profileid'")
	    or goto bad;
    }
    DBQueryWarn("unlock tables");
525
    return 0;
526 527 528 529

  bad:
    DBQueryWarn("unlock tables");
    return -1;
530 531
}

532
#
533 534
# Delete a profile version, only allow it if it is the highest
# numbered version.
535
#
536
sub DeleteVersion($)
537 538 539
{
    my ($self) = @_;

540 541
    DBQueryWarn("lock tables apt_profile_versions write, apt_profiles write")
	or return -1;
542

543 544
    my $profileid = $self->profileid();
    my $version   = $self->version();
545

546 547 548 549 550 551 552 553
    #
    # Only the "head" version can be deleted
    #
    my $query_result =
	DBQueryWarn("select max(version) from apt_profile_versions ".
		    "where profileid='$profileid'");
    goto bad
	if (!$query_result || !$query_result->numrows);
554

555 556 557 558 559 560 561 562 563 564 565 566 567 568
    my ($head) = $query_result->fetchrow_array();
    if ($head != $version) {
	print STDERR "Profile::DeleteVersion: not the head version of $self\n";
	goto bad;
    }
    goto bad
	if (!DBQueryWarn("delete from apt_profile_versions ".
			 "where profileid='$profileid' and ".
			 "      version='$version'"));
    goto bad
	if (!DBQueryWarn("update apt_profiles set version=version-1 ".
			 "where profileid='$profileid' and ".
			 "      version='$version'"));
    DBQueryWarn("unlock tables");
569
    return 0;
570 571 572
  bad:
    DBQueryWarn("unlock tables");
    return -1;
573 574
}

575 576 577 578
#
# Condomize a profile rspec by inserting the necessary firewall section
# to each of the nodes.
#
579
sub CheckFirewall($$)
580
{
581
    my ($self, $condomize) = @_;
582 583 584 585 586 587 588 589 590 591 592

    # Must be a real reference. 
    return -1
	if (! ref($self));

    my $rspec = GeniXML::Parse($self->rspec());
    if (! defined($rspec)) {
	print STDERR "Could not parse rspec\n";
	return undef;
    }
    foreach my $ref (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
	my @routable_control_ip =
	    GeniXML::FindNodesNS("n:routable_control_ip",
				 $ref,
				 $GeniXML::EMULAB_NS)->get_nodelist();
	my $virtualization_type = GeniXML::GetVirtualizationSubtype($ref);
	#
	# If a XEN container but not a routable IP, then use the basic
	# rules instead of closed, so that ssh is allowed in on the
	# alternate port. That is the only real difference between basic
	# and closed.
	#
	my $style = "closed";
	if (defined($virtualization_type) &&
	    $virtualization_type eq "emulab-xen" && !@routable_control_ip) {
	    $style = "basic";
	}
	
610 611 612 613 614 615 616
	if ($condomize) {
	    #
	    # No settings is easy; wrap it tight.
	    #
	    if (!GeniXML::HasFirewallSettings($ref)) {
		my $firewall = GeniXML::AddElement("firewall", $ref,
						   $GeniXML::EMULAB_NS);
617
		GeniXML::SetText("style", $firewall, $style);
618 619 620 621 622 623 624 625 626
		next;
	    }
	    #
	    # Make sure the existing section has a reasonable setting.
	    #
	    my $settings = GeniXML::FindNodesNS("n:firewall", $ref,
						$GeniXML::EMULAB_NS)->pop();
	    my $style = GeniXML::GetText("style", $settings);
	    if (!defined($style) || $style ne "basic" || $style ne "closed") {
627
		GeniXML::SetText("style", $settings, $style);
628
	    }
629 630
	}
	#
631 632
	# Quick pass over the exceptions to see if we need to substitute
	# the callers IP address.
633
	#
634 635 636 637 638 639
	foreach my $exception (GeniXML::FindNodesNS("n:firewall/n:exception",
				$ref, $GeniXML::EMULAB_NS)->get_nodelist()) {
	    my $ip = GeniXML::GetText("ip", $exception);
	    if (defined($ip) && $ip eq "myip" && exists($ENV{'REMOTE_ADDR'})) {
		GeniXML::SetText("ip", $exception, $ENV{'REMOTE_ADDR'});
	    }
640 641 642 643
	}
    }
    return GeniXML::Serialize($rspec);
}
Leigh B Stoller's avatar
Leigh B Stoller committed
644 645 646 647 648 649 650

#
# Lock and Unlock
#
sub Lock($)
{
    my ($self) = @_;
651
    my $profileid = $self->profileid();
Leigh B Stoller's avatar
Leigh B Stoller committed
652 653 654 655 656

    return -1
	if (!DBQueryWarn("lock tables apt_profiles write"));

    my $query_result =
657 658
	DBQueryWarn("update apt_profiles set locked=now(),locker_pid='$PID' " .
		    "where profileid='$profileid' and locked is null");
Leigh B Stoller's avatar
Leigh B Stoller committed
659 660 661 662 663 664 665

    if (! $query_result ||
	$query_result->numrows == 0) {
	DBQueryWarn("unlock tables");
	return -1;
    }
    DBQueryWarn("unlock tables");
666
    $self->{'DBROW'}->{'locked'} = time();
Leigh B Stoller's avatar
Leigh B Stoller committed
667 668 669 670 671 672
    return 0;
}

sub Unlock($)
{
    my ($self) = @_;
673
    my $profileid = $self->profileid();
Leigh B Stoller's avatar
Leigh B Stoller committed
674 675

    return -1
676 677 678 679 680 681 682 683 684
	if (! DBQueryWarn("update apt_profiles set ".
			  "   locked=null,locker_pid=0 ".
			  "where profileid='$profileid'"));
    
    $self->{'DBROW'}->{'locked'} = 0;
    return 0;
}

#
685 686 687
# Update the disk image inside a profile. We update the URL for the
# specified node, and if $all is set, we change all nodes with the
# same original disk image as the specified node.
688
#
689
sub UpdateDiskImage($$@)
690
{
691
    my ($self, $node_id, $image_url, $all) = @_;
692 693 694 695 696
    my $rspec = GeniXML::Parse($self->rspec());
    if (! defined($rspec)) {
	print STDERR "UpdateDiskImage: Could not parse rspec\n";
	return -1;
    }
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
    #
    # Find all the nodes we want to update, might be just the one or
    # all with the same image.
    #
    my @nodes = ();
    my $node;

    # First find the specified node.
    foreach my $ref (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
	if (GeniXML::GetVirtualId($ref) eq $node_id) {
	    $node = $ref;
	    last;
	}
    }
    if (!defined($node)) {
	print STDERR "$node_id not in rspec\n";
	return -1;
    }
    if ($all) {
	#
	# Pull out the disk url/urn of the specified node.
	#
719
	my $Odiskref = GeniXML::GetDiskImage($node);
720 721
	my $image_urn;
	my $image_url;
722 723 724
	if (defined($Odiskref)) {
	    $image_url = GeniXML::GetText("url", $Odiskref);
	    $image_urn = GeniXML::GetText("name", $Odiskref);
725 726 727 728 729 730 731 732 733 734 735 736 737
	    if (defined($image_url) || defined($image_urn)) {
		# Watch for url in the name, flipflop. 
		if (defined($image_urn) && $image_urn =~ /^http/) {
		    $image_url = $image_urn;
		    $image_urn = undef;
		}
	    }
	}
	#
	# Now find all nodes using the same disk urn/url and change.
	#
	foreach my $ref (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
	    my $diskref   = GeniXML::GetDiskImage($ref);
738 739 740 741 742 743 744 745 746
	    #
	    # If the both this node and the original node did not
	    # specify a disk image, then we update it. 
	    #
	    if (!defined($diskref)) {
		push(@nodes, $ref)
		    if (!defined($Odiskref));
		next;
	    }
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
	    my $this_url = GeniXML::GetText("url", $diskref);
	    my $this_urn = GeniXML::GetText("name", $diskref);
	    next
		if (!(defined($image_url) || defined($image_urn)));

	    # Watch for url in the name, flipflop. 
	    if (defined($this_urn) && $this_urn =~ /^http/) {
		$this_url = $this_urn;
		$this_urn = undef;
	    }
	    if (defined($image_url)) {
		push(@nodes, $ref)
		    if (defined($this_url) && $this_url eq $image_url);
	    }
	    else {
		push(@nodes, $ref)
 		    if (defined($this_urn) && $this_urn eq $image_urn);
	    }
	}
    }
    else {
	@nodes = ($node);
    }
    if (!@nodes) {
	print STDERR "Could not find any nodes to update disk image\n";
	return -1;
    }
    foreach my $node (@nodes) {
	GeniXML::SetDiskImage($node, $image_url);
    }
777 778 779 780 781 782 783
    if ($self->UpdateVersion({"rspec" => GeniXML::Serialize($rspec)})) {
	print STDERR "UpdateDiskImage: Could not update rspec\n";
	return -1;
    }
    return 0;
}

784
# Total nonsense, to be thrown away.
785
sub CheckNodeConstraints($$$)
786
{
787
    my ($self, $default_aggregate_urn, $pmsg) = @_;
788
    my $cloudwww = "www.utah.cloudlab.us";
789
    my $cloudurn = "urn:publicid:IDN+utah.cloudlab.us+authority+cm";
790 791 792 793 794 795 796 797 798
    require URI;

    my $rspec = GeniXML::Parse($self->rspec());
    if (! defined($rspec)) {
	print STDERR "Could not parse rspec\n";
	return -1;
    }
    foreach my $ref (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
	my $client_id = GetVirtualId($ref);
799
	my $virtualization_type = GeniXML::GetVirtualizationSubtype($ref);
800 801 802 803 804
	my $manager_urn = GetManagerId($ref);
	if (! defined($manager_urn)) {
	    $manager_urn = $default_aggregate_urn;
	}
	my $iscloudlab = ($manager_urn eq $cloudurn ? 1 : 0);
805 806 807 808 809 810 811

	if (defined($virtualization_type) && $iscloudlab &&
	    $virtualization_type eq "emulab-xen") {
	    $$pmsg = "Node '$client_id' is a XEN VM, which is ".
		"not supported on the Cloudlab cluster";
	    return -1;
	}
812 813 814 815
	my $diskref   = GeniXML::GetDiskImage($ref);
	next
	    if (!defined($diskref));
	my $image_url = GeniXML::GetText("url", $diskref);
816
	my $image_urn = GeniXML::GetText("name", $diskref);
817
	next
818 819
	    if (!(defined($image_url) || defined($image_urn)));

820
	# Watch for url in the name, flipflop. 
Leigh B Stoller's avatar
Leigh B Stoller committed
821
	if (defined($image_urn) && $image_urn =~ /^http/) {
822 823 824 825
	    $image_url = $image_urn;
	    $image_urn = undef;
	}

826
	if (defined($image_urn)) {
Leigh B Stoller's avatar
Leigh B Stoller committed
827 828 829
	    if ($image_urn =~ /UBUNTU14\-10\-64\-OS/) {
		return 0;
	    }
830 831
	    elsif ($iscloudlab &&
		   !($image_urn =~ /ARM/ || $image_urn =~ /HPC/)) {
832 833
		$$pmsg = "The disk image specified for node '$client_id' ".
		    "will not run on the Cloudlab cluster";
834 835 836
		return -1;
	    }
	    elsif (!$iscloudlab && $image_urn =~ /ARM/) {
837 838
		$$pmsg = "The disk image specified for node '$client_id' ".
		    "will only run on the Cloudlab cluster";
839 840 841 842 843
		return -1;
	    }
	}
	next if
	    (!defined($image_url));
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

	# Get the hostname for the image URL.
	my $uri = URI->new($image_url);
	if (!defined($uri)) {
	    print STDERR "Could not parse $image_url\n";
	    return -1;
	}
	my $image_host = $uri->host();

	if ($iscloudlab) {
	    if ($image_host ne $cloudwww) {
		$$pmsg = "The disk image specified for node '$client_id' ".
		    "will not run on the Cloudlab cluster";
		return -1;
	    }
	}
	else {
	    if ($image_host eq $cloudwww) {
		$$pmsg = "The disk image specified for node '$client_id' ".
		    "will not run on cluster you selected";
		return -1;
	    }
	}
    }
    return 0;
}

871 872 873 874 875
#
# Check blockstores. 
#
sub CheckDatasets($$$)
{
876 877 878
    my ($xml, $ppid, $pmsg) = @_;
    my $pid = $ppid;
    
879 880 881 882 883 884
    my $rspec = GeniXML::Parse($xml);
    if (! defined($rspec)) {
	print STDERR "CheckDatasets: Could not parse rspec\n";
	return -1;
    }
    foreach my $ref (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
885 886
	my $manager_urn = GetManagerId($ref);	
	
887 888 889
	foreach my $blockref (GeniXML::FindNodesNS("n:blockstore",
						   $ref,
				   $GeniXML::EMULAB_NS)->get_nodelist()) {
890 891
	    my $dataset_id  = GeniXML::GetText("dataset", $blockref);
	    my $class       = GeniXML::GetText("class", $blockref);
892 893 894 895 896 897

	    #
	    # We only care about datasets here, we let the backend
	    # do the error checking on ephemeral blockstores.
	    #
	    next
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
		if (!defined($dataset_id));

	    if (!defined($class)) {
		$class = "remote";
	    }
	    elsif ($class ne "local" && $class ne "remote") {
		$$pmsg = "class must be local or remote";
		return 1;
	    }

	    #
	    # If the dataset is local and its a URL, then make sure its
	    # a valid URL.
	    #
	    if ($class eq "local" && $dataset_id =~ /^(http|https):/) {
		if (!TBcheck_dbslot($dataset_id, "virt_nodes", "osname",
				TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)){
		    $$pmsg = "Invalid url for dataset";
		    return 1;
		}
		next;
	    }
	    if (!GeniHRN::IsValid($dataset_id)) {
		$$pmsg = "Persistent dataset is not a valid URN";
922 923
		return 1;
	    }
924 925 926 927
	    my $dataset_urn = $dataset_id;
	    my ($dataset_authority, $type, $id) = GeniHRN::Parse($dataset_urn);
	    my ($dataset_domain) = split(":", $dataset_authority);
	    
928 929 930 931 932 933 934 935
	    #
	    # Separate project from name; this is how the rspec specifies
	    # the dataset they want, since it might be in another project
	    #
	    if ($id =~ /^([-\w]+)\/\/(.+)$/) {
		$pid = $1;
		$id  = $2;
	    }
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952

	    #
	    # The domain of the dataset has to match the domain of aggregate.
	    # We also use this when creating a profile, so rspec might not
	    # be bound.
	    #
	    if (defined($manager_urn)) {
		my ($manager_authority) = GeniHRN::Parse($manager_urn);
		my ($manager_domain) = split(":", $manager_authority);
		
		if ($manager_domain ne $dataset_domain) {
		    $$pmsg = "Dataset $id is not located on $manager_authority";
		    return 1;
		}
	    }
	    next
		if ($class eq "local");
953
	    
954 955 956
	    #
	    # Not all backends have blockstore support.
	    #
957 958
	    if (!APT_Dataset::ValidBlockstoreBackend($dataset_authority)) {
		$$pmsg = "Dataset $id is not on a valid aggregate";
959 960
		return 1;
	    }
961

962 963
	    my $dataset = APT_Dataset->Lookup("$pid/$id");
	    if (!defined($dataset)) {
964
		$dataset = APT_Dataset->LookupByRemoteURN($dataset_urn);
965 966 967 968
		if (!defined($dataset)) {
		    $$pmsg = "Persistent dataset '$pid/$id' does not exist";
		    return 1;
		}
969
	    }
970
	    #
971
	    # XXX Need basic frontend permission checks?
972
	    #
973 974 975 976 977
	}
    }
    return 0;
}

978 979 980
#
# Set the component_manager_urn for the sites.
#
981
sub SetSites($$$$)
982
{
983
    my ($prspecstr, $sitemap, $pneedstitcher, $perrmsg) = @_;
984
    my %interface_map = ();
985 986 987 988 989 990 991 992 993 994 995

    my $rspec = GeniXML::Parse($$prspecstr);
    if (! defined($rspec)) {
	$$perrmsg = "Could not parse rspec\n";
	return -1;
    }
    foreach my $ref (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
	my $client_id   = GetVirtualId($ref);
	my $site_id     = GeniXML::GetJacksSiteId($ref);

	if (!defined($site_id)) {
996
	    $$perrmsg = "No site ID for node $client_id";
997 998 999 1000
	    return -1;
	}
	my $site_mid = "site:" . $site_id;
	if (!exists($sitemap->{$site_mid})) {
1001
	    $$perrmsg = "No site mapping for node $client_id ($site_id)";
1002 1003 1004 1005
	    return -1;
	}
	GeniXML::SetManagerId($ref, $sitemap->{$site_mid});
	GeniXML::SetJacksSiteManagerId($ref, $sitemap->{$site_mid});
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015

	#
	# Get all of the interfaces, we need those for the links, so
	# we can add the managers to them, according to site id.
	#
	foreach my $iref (GeniXML::FindNodes("n:interface",
					     $ref)->get_nodelist()) {
	    my $client_id = GeniXML::GetInterfaceId($iref);
	    $interface_map{$client_id} = $site_mid;
	}
1016
    }
1017 1018 1019 1020
    foreach my $ref (GeniXML::FindNodes("n:link", $rspec)->get_nodelist()) {
	my %linksites = ();
	my $client_id = GetVirtualId($ref);

1021 1022 1023 1024 1025
	foreach my $iref (GeniXML::FindNodes("n:interface_ref",
					     $ref)->get_nodelist()) {
	    my $client_id = GeniXML::GetInterfaceId($iref);
	    next
		if (!exists($interface_map{$client_id}));
1026

1027
	    my $site_mid = $interface_map{$client_id};
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
	    GeniXML::AddManagerToLink($ref, $sitemap->{$site_mid});
	    $linksites{$sitemap->{$site_mid}} = 1;
	}
	# if more then one site for a link, must use the stitcher.
	$$pneedstitcher = 1
	    if (keys(%linksites) > 1);
    }
    $$prspecstr = GeniXML::Serialize($rspec);
    return 0;
}

#
# Set the component_manager_urn for the rspec
#
sub BindRspec($$$)
{
    my ($prspecstr, $aggregate_urn, $perrmsg) = @_;

    my $rspec = GeniXML::Parse($$prspecstr);
    if (! defined($rspec)) {
	$$perrmsg = "Could not parse rspec\n";
	return -1;
    }
    foreach my $ref (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
	GeniXML::SetManagerId($ref, $aggregate_urn);
    }
1054 1055 1056 1057
    $$prspecstr = GeniXML::Serialize($rspec);
    return 0;
}

1058 1059 1060
sub IsHead($)
{
    my ($self) = @_;
Leigh B Stoller's avatar
Leigh B Stoller committed
1061

1062
    my $profileid = $self->profileid();
Leigh B Stoller's avatar
Leigh B Stoller committed
1063

1064 1065 1066
    my $query_result =
	DBQueryWarn("select max(version) from apt_profile_versions ".
		    "where profileid='$profileid'");
Leigh B Stoller's avatar
Leigh B Stoller committed
1067
    return -1
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
	if (!$query_result || !$query_result->numrows);

    my ($head) = $query_result->fetchrow_array();
    return ($head == $self->version() ? 1 : 0);
}

#
# Publish a profile. Not sure what this really means yet.
#
sub Publish($)
{
    my ($self) = @_;
    my $profileid = $self->profileid();
    my $version   = $self->version();

    return -1
	if (! DBQueryWarn("update apt_profile_versions set published=now() ".
			  "where profileid='$profileid' and ".
			  "      version='$version'"));
Leigh B Stoller's avatar
Leigh B Stoller committed
1087
    
1088
    $self->{'DBROW'}->{'published'} = time();
Leigh B Stoller's avatar
Leigh B Stoller committed
1089
    return 0;
1090

1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
}

#
# Manage URL
#
sub AdminURL($)
{
    my ($self) = @_;
    my $uuid   = $self->uuid();
    
    require Project;
    
    my $project = Project->Lookup($self->pid_idx());
    return undef
	if (!defined($project));
    
    my $wwwbase = $project->wwwBase();
    $wwwbase .= "/apt"
	if ($project->Brand()->isEmulab());

    return $wwwbase . "/manage_profile.php?uuid=$uuid";
}
Leigh B Stoller's avatar
Leigh B Stoller committed
1113

1114 1115
# _Always_ make sure that this 1 is at the end of the file...
1;