APT_Instance.pm.in 21.2 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2007-2015 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# 
# {{{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_Instance;

use strict;
use Carp;
28 29 30
use English;
use Data::Dumper;
use POSIX qw(tmpnam);
Leigh Stoller's avatar
Leigh Stoller committed
31 32 33 34 35 36 37 38 39 40
use Exporter;
use vars qw(@ISA @EXPORT $AUTOLOAD);

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

# Must come after package declaration!
use EmulabConstants;
use emdb;
use libtestbed;
41
use Brand;
42
use APT_Profile;
43 44 45 46
use APT_Geni;
use Genixmlrpc;
use GeniResponse;
use GeniCertificate;
Leigh Stoller's avatar
Leigh Stoller committed
47
use GeniCredential;
48
use GeniHRN;
Leigh Stoller's avatar
Leigh Stoller committed
49 50 51 52 53
use overload ('""' => 'Stringify');

# Configure variables
my $TB		  = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
54
my $GENEXTENDCRED = "$TB/sbin/protogeni/genextendcred";
Leigh Stoller's avatar
Leigh Stoller committed
55 56 57 58 59

# Cache of instances to avoid regenerating them.
my %instances  = ();
my $debug      = 0;

60
# Debugging
61
my $usemydevtree  = 0;
62

Leigh Stoller's avatar
Leigh Stoller committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#
# Lookup by uuid.
#
sub Lookup($$;$)
{
    my ($class, $uuid) = @_;

    if ($uuid !~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	return undef
    }

    # Look in cache first
    return $instances{"$uuid"}
        if (exists($instances{"$uuid"}));
    
    my $query_result =
	DBQueryWarn("select * from apt_instances where uuid='$uuid'");

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

    my $self            = {};
    $self->{'INSTANCE'} = $query_result->fetchrow_hashref();
86
    $self->{'BRAND'}    = Brand->Create($self->{'INSTANCE'}->{'servername'});
Leigh Stoller's avatar
Leigh Stoller committed
87 88 89 90 91 92 93 94 95 96 97

    bless($self, $class);
    
    # Add to cache. 
    $instances{"$uuid"} = $self;
    
    return $self;
}

AUTOLOAD {
    my $self  = $_[0];
98
    my $type  = ref($self) or confess "$self is not an object";
Leigh Stoller's avatar
Leigh Stoller committed
99 100 101 102 103 104 105 106 107 108
    my $name  = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    # A DB row proxy method call.
    if (exists($self->{'INSTANCE'}->{$name})) {
	return $self->{'INSTANCE'}->{$name};
    }
    carp("No such slot '$name' field in class $type");
    return undef;
}
109 110 111
sub Brand($)	{ return $_[0]->{'BRAND'}; }
sub isAPT($)	{ return $_[0]->Brand()->isAPT() ? 1 : 0; }
sub isCloud($)	{ return $_[0]->Brand()->isCloud() ? 1 : 0; }
Leigh Stoller's avatar
Leigh Stoller committed
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

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

    $self->{'INSTANCE'} = undef;
}

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

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

    my $uuid = $self->uuid();
    
    my $query_result =
	DBQueryWarn("select * from apt_instances where uuid='$uuid'");

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

    $self->{'INSTANCE'} = $query_result->fetchrow_hashref();

    return 0;
}

#
# Create an Instance
#
sub Create($$)
{
    my ($class, $argref) = @_;
    my $uuid;

    if (exists($argref->{'uuid'})) {
	$uuid = $argref->{'uuid'};
	delete($argref->{'uuid'});
    }
    else {
	$uuid = NewUUID();
    }

    #
    # The uuid has to be unique, so lock the table for the check/insert.
    #
    DBQueryWarn("lock tables apt_instances write")
	or return undef;

    my $query_result =
	DBQueryWarn("select uuid from apt_instances where uuid='$uuid'");

    if ($query_result->numrows) {
	DBQueryWarn("unlock tables");
	tberror("Instance uuid $uuid already exists!");
	return undef;
    }

    my $query = "insert into apt_instances set created=now(),uuid='$uuid', ".
	join(",", map("$_=" .
		      DBQuoteSpecial($argref->{$_}), keys(%{$argref})));

    if (! DBQueryWarn($query)) {
	DBQueryWarn("unlock tables");
	tberror("Error inserting new apt_instance record for $uuid!");
	return undef;
    }
    DBQueryWarn("unlock tables");
    return Lookup($class, $uuid);
}

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

    return "[APT_Instance: $uuid]";
}

199 200
sub LookupBySlice($$)
{
201 202
    my ($class, $token) = @_;
    my $slice_uuid;
203

204 205 206 207 208 209 210 211 212
    if ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	$slice_uuid = $token;
    }
    elsif (GeniHRN::IsValid($token)) {
	#
	# We should put the slice_urn into the apt_instances table.
	#
	require GeniSlice;
	my $slice = GeniSlice->Lookup($token);
213
	return undef
214 215 216 217 218
	    if (!defined($slice));
	$slice_uuid = $slice->uuid();
    }
    else {
	return undef;
219
    }
220

221 222 223
    my $query_result =
	DBQueryWarn("select uuid from apt_instances ".
		    "where slice_uuid='$slice_uuid'");
224
	
225 226 227 228 229 230 231
    return undef
	if (!$query_result || !$query_result->numrows);

    my ($uuid) = $query_result->fetchrow_array();
    return APT_Instance->Lookup($uuid);
}

Leigh Stoller's avatar
Leigh Stoller committed
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 267 268 269 270 271
#
# Perform some updates ...
#
sub Update($$)
{
    my ($self, $argref) = @_;

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

    my $uuid = $self->uuid();

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

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

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

    return Refresh($self);
}

sub Delete($)
{
    my ($self) = @_;

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

    my $uuid = $self->uuid();

    DBQueryWarn("delete from apt_instances where uuid='$uuid'") or
	return -1;

    return 0;
}

272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
#
# Lock and unlock operate on the underlying slice.
#
sub Lock($)
{
    my ($self) = @_;
    my $slice  = $self->GetGeniSlice();
    if (!defined($slice)) {
	return -1;
    }
    return $slice->Lock();
}
sub Unlock($)
{
    my ($self) = @_;
    my $slice  = $self->GetGeniSlice();
    if (!defined($slice)) {
	return -1;
    }
    return $slice->UnLock();
}

Leigh Stoller's avatar
Leigh Stoller committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
sub SetStatus($$)
{
    my ($self,$status) = @_;

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

    my $uuid = $self->uuid();

    DBQueryWarn("update apt_instances set status='$status' ".
		"where uuid='$uuid'") or
	return -1;

    $self->{'INSTANCE'}->{'status'} = $status;
    return 0;
    
Leigh Stoller's avatar
Leigh Stoller committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
}
sub SetPublicURL($$)
{
    my ($self,$url) = @_;
    my $safe_url = DBQuoteSpecial($url);

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

    my $uuid = $self->uuid();

    DBQueryWarn("update apt_instances set public_url=$safe_url ".
		"where uuid='$uuid'") or
	return -1;

    $self->{'INSTANCE'}->{'public_url'} = $url;
    return 0;
    
Leigh Stoller's avatar
Leigh Stoller committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
}
sub SetManifest($$)
{
    my ($self,$manifest) = @_;

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

    my $uuid = $self->uuid();
    my $safe_manifest = DBQuoteSpecial($manifest);    

    DBQueryWarn("update apt_instances set manifest=$safe_manifest ".
		"where uuid='$uuid'") or
	return -1;

    $self->{'INSTANCE'}->{'manifest'} = $manifest;
    return 0;
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
}

#
# Set/Clear the lockdown bits.
#
sub SetLockdown($$$)
{
    my ($self,$which,$clear) = @_;

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

    my $uuid = $self->uuid();
    if ($which eq "admin") {
	$which = "admin_lockdown";
    }
    elsif ($which eq "user") {
	$which = "user_lockdown";
    }
    else {
	return -1;
    }
    my $value = ($clear ? 0 : 1);

    DBQueryWarn("update apt_instances set ${which}=${value} ".
		"where uuid='$uuid'") or
	return -1;

    $self->{'INSTANCE'}->{$which} = $value;
    return 0;
}

# Return a URL for the status page.
sub webURL($)
{
    my ($self) = @_;
    my $link = $self->Brand()->wwwBase();
    $link    = $link . "/status.php?uuid=" . $self->uuid();

    return $link;
Leigh Stoller's avatar
Leigh Stoller committed
389 390
}

391 392 393 394 395 396 397 398 399 400 401
#
# Find the profile for this instance.
#
sub Profile($)
{
    my ($self) = @_;
    
    return APT_Profile->Lookup($self->profile_id(),
			       $self->profile_version());
}

402 403 404 405 406 407 408 409 410 411 412
#
# Record a history entry.
# We do this when the instance is terminated. 
#
sub RecordHistory($)
{
    my ($self) = @_;
    my $uuid = $self->uuid();

    DBQueryWarn("replace into apt_instance_history ".
		"select uuid,profile_id,profile_version,slice_uuid, ".
413
		"       creator,creator_idx,creator_uuid,pid,pid_idx, ".
414 415
		"       aggregate_urn,public_url,created,now(), ".
		"       extension_count,extension_days, ".
416
		"       physnode_count,virtnode_count, ".
417
		"       servername,rspec,params,manifest ".
418 419 420 421 422 423
		" from apt_instances where uuid='$uuid'")
	or return -1;

    return 0;
}

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
#
# Locate Geni objects for creator and slice.
#
sub GetGeniSlice($)
{
    my ($self) = @_;
    require GeniSlice;
    
    return GeniSlice->Lookup($self->slice_uuid());
}

sub GetGeniUser($)
{
    my ($self) = @_;
    require GeniUser;
    
    return GeniUser->Lookup($self->creator_uuid(), 1);
}

sub GetGeniAuthority($)
{
    my ($self) = @_;
    require GeniAuthority;

    return APT_Geni::GetAuthority($self->aggregate_urn());
}

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
#
# Warn creator that the experiment is going to expire. This is hooked
# in from the sa_daemon, so we can send a message that is less geni like
# and more APT/Cloud.
#
sub WarnExpiring($$)
{
    my ($self, $when) = @_;
    require GeniDB;
    require GeniSlice;

    my $slice     = GeniSlice->Lookup($self->slice_uuid());;
    if (!defined($slice)) {
	print STDERR "WarnExpiring: No slice for $self\n";
	return -1;
    }
    my $slicename = $slice->slicename();
    my $geniuser  = GeniUser->Lookup($slice->creator_uuid(), 1);
    if (!defined($geniuser)) {
	print STDERR "WarnExpiring: No creator for $self\n";
	return -1;
    }
    if (!defined($geniuser->email())) {
	print STDERR "WarnExpiring: no email address for $geniuser\n";
	return 0;
    }
477
    my $link = $self->webURL();
478 479 480 481 482 483 484 485 486 487 488 489 490 491
    
    $self->Brand()->SendEmail($geniuser->email(),
		     "Your experiment is expiring soon!",
		     "Your experiment $slicename is going to expire in ${when}.\n".
		     "If you want to extend this experiment, please go to:\n\n".
		     "    $link\n\n".
		     "and click on the Extend button.\n".
		     "\n".
		     "Thanks!",
		     $self->Brand()->OpsEmailAddress());
    
    return 0;
}

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

sub ComputeNodeCounts($)
{
    my ($self) = @_;
    my $physnode_count = 0;
    my $virtnode_count = 0;

    my $manifest = GeniXML::Parse($self->manifest());
    if (! defined($manifest)) {
	print STDERR "Could not parse manifest\n";
	return -1;
    }
    foreach my $ref (GeniXML::FindNodes("n:node", $manifest)->get_nodelist()) {
	my $virtualization_type = GeniXML::GetVirtualizationSubtype($ref);

	if (defined($virtualization_type) &&
	    $virtualization_type eq "emulab-xen") {

	    $virtnode_count++;
	    next;
	}
	$physnode_count++;
    }
    $self->Update({"physnode_count" => $physnode_count,
		   "virtnode_count" => $virtnode_count})
	== 0 or return -1;
    return 0;
}

521 522 523
#
# Ask aggregate for the console URL for a node.
#
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
sub ConsoleInfo($$)
{
    my ($self, $sliver_urn) = @_;
    my $authority = $self->GetGeniAuthority();
    my $geniuser  = $self->GetGeniUser();
    my $slice     = $self->GetGeniSlice();
    my $context   = APT_Geni::GeniContext();
    return undef
	if (! (defined($geniuser) && defined($authority) &&
	       defined($slice) && defined($context)));

    my ($slice_credential, $speaksfor_credential) =
	APT_Geni::GenCredentials($slice, $geniuser);
    return undef
	if (! (defined($speaksfor_credential) &&
	       defined($slice_credential)));

    my $args = {
	"slice_urn"   => $slice->urn(),
	"sliver_urn"  => $sliver_urn,
	"credentials" => [$slice_credential->asString(),
			  $speaksfor_credential->asString()],
    };
    my $cmurl = $authority->url();
    $cmurl =~ s/protogeni/protogeni\/stoller/ if ($usemydevtree);

    return Genixmlrpc::CallMethod($cmurl, $context, "ConsoleInfo", $args);
}
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
sub ConsoleURL($$)
{
    my ($self, $sliver_urn) = @_;
    my $authority = $self->GetGeniAuthority();
    my $geniuser  = $self->GetGeniUser();
    my $slice     = $self->GetGeniSlice();
    my $context   = APT_Geni::GeniContext();
    return undef
	if (! (defined($geniuser) && defined($authority) &&
	       defined($slice) && defined($context)));

    my ($slice_credential, $speaksfor_credential) =
	APT_Geni::GenCredentials($slice, $geniuser);
    return undef
	if (! (defined($speaksfor_credential) &&
	       defined($slice_credential)));

    my $args = {
	"slice_urn"   => $slice->urn(),
	"sliver_urn"  => $sliver_urn,
	"credentials" => [$slice_credential->asString(),
			  $speaksfor_credential->asString()],
    };
575 576
    my $cmurl = $authority->url();
    $cmurl =~ s/protogeni/protogeni\/stoller/ if ($usemydevtree);
577

578
    return Genixmlrpc::CallMethod($cmurl, $context, "ConsoleURL", $args);
579 580 581 582 583 584 585 586
}

#
# Ask aggregate to terminate.
#
sub Terminate($)
{
    my ($self) = @_;
Leigh Stoller's avatar
Leigh Stoller committed
587
    my $credentials;
588 589 590 591 592 593 594 595
    my $authority = $self->GetGeniAuthority();
    my $geniuser  = $self->GetGeniUser();
    my $slice     = $self->GetGeniSlice();
    my $context   = APT_Geni::GeniContext();
    return undef
	if (! (defined($geniuser) && defined($authority) &&
	       defined($slice) && defined($context)));

Leigh Stoller's avatar
Leigh Stoller committed
596 597 598 599 600 601 602 603 604
    #
    # If the slice is expired, it is most likely gone at the cluster,
    # but we want to make sure, so change the expiration so that the
    # credential we generate is not also expired. 
    #
    if ($slice->IsExpired()) {
	$slice->SetExpiration(time() + 3600);
    }

605 606 607 608 609 610
    my ($slice_credential, $speaksfor_credential) =
	APT_Geni::GenCredentials($slice, $geniuser);
    return undef
	if (! (defined($speaksfor_credential) &&
	       defined($slice_credential)));

Leigh Stoller's avatar
Leigh Stoller committed
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
    #
    # Special case; if the speaksfor_credential has expired cause it
    # was for a nonlocal user, we have no choice but to throw away
    # these credentials and generate a new one issued to the local SA
    # instead of the user. 
    #
    if ($speaksfor_credential->IsExpired()) {
	print STDERR "speaksfor credential has expired, generating a new one\n";

	$slice_credential =
	    GeniCredential->CreateSigned($slice, $context,
					 $GeniCredential::LOCALSA_FLAG);
	if (!defined($slice_credential)) {
	    print STDERR "Could not generate SA credential\n";
	    return undef;
	}
	$credentials = [$slice_credential->asString()];
    }
    else {
	$credentials = [$slice_credential->asString(),
			$speaksfor_credential->asString()];
    }
633 634
    my $args = {
	"slice_urn"   => $slice->urn(),
Leigh Stoller's avatar
Leigh Stoller committed
635
	"credentials" => $credentials,
636
    };
637 638
    my $cmurl = $authority->url();
    $cmurl =~ s/protogeni/protogeni\/stoller/ if ($usemydevtree);
639 640 641 642 643 644 645 646 647 648

    #
    # We have to watch for resource busy errors, and retry. For a while
    # at least. Eventually give up cause it might be a permanently locked
    # slice cause of earlier error. 
    #
    my $response;
    my $tries = 10;
    while ($tries) {
	$response =
649
	    Genixmlrpc::CallMethod($cmurl, $context, "DeleteSlice", $args);
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713

	# SEARCHFAILED is success.
	return $response
	    if ($response->code() == GENIRESPONSE_SUCCESS ||
		$response->code() == GENIRESPONSE_SEARCHFAILED);

	return $response
	    if ($response->code() != GENIRESPONSE_BUSY);

	#
	# Wait for a while and try again. 
	#
	$tries--;
	if ($tries) {
	    print STDERR "Slice is busy, will retry again in a bit ...\n";
	    sleep(30);
	}
    }
    return $response;
}

#
# Ask to extend. 
#
sub Extend($$)
{
    my ($self, $new_expires) = @_;
    my $authority = $self->GetGeniAuthority();
    my $geniuser  = $self->GetGeniUser();
    my $slice     = $self->GetGeniSlice();
    my $context   = APT_Geni::GeniContext();
    return undef
	if (! (defined($geniuser) && defined($authority) &&
	       defined($slice) && defined($context)));

    #
    # We need a special credentential in case the aggregate is enforcing
    # limits (as do Utah aggregates).
    #
    my $slice_urn = $slice->urn();
    my $extcred   = "";
    my $credname  = tmpnam();
    my $userarg   = "-u " . $geniuser->urn();
    my ($fh,$certfile);
    #
    # But if a nonlocal user from Geni, then the user we have in the database
    # is not in the same domain as the speaksfor, so we use the geni certificate
    # that the trusted signer gave us and is stored in the DB.
    #
    if ($geniuser->IsLocal() && $geniuser->emulab_user()->IsNonLocal()) {
	my (undef, $certificate_string) =
	    $geniuser->emulab_user()->GetStoredCredential();
	if (! defined($certificate_string)) {
	    print STDERR "Could not get stored certificate for $geniuser\n";
	    return undef;
	}
	my $certificate = GeniCertificate->LoadFromString($certificate_string);
	if (!defined($certificate)) {
	    print STDERR "Could not load stored certificate for $geniuser\n";
	    return undef;
	}
	$certfile = $certificate->WriteToFile();
	$userarg = "-c $certfile";
    }
Leigh Stoller's avatar
Leigh Stoller committed
714
    system("$GENEXTENDCRED -a -o $credname -s $slice_urn -t 180 $userarg");
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
    if ($?) {
	print STDERR "Could not create extended credential\n";
	return undef;
    }
    if (!open(EXT, $credname)) {
	print STDERR "Could not open ext credfile $credname\n";
	return undef;
    }
    while (<EXT>) {
	$extcred .= $_;
    }
    close(EXT);
    unlink($credname);
    chomp($extcred);

    my ($slice_credential, $speaksfor_credential) =
	APT_Geni::GenCredentials($slice, $geniuser);
    return undef
	if (! (defined($speaksfor_credential) &&
	       defined($slice_credential)));

    my $args = {
	"slice_urn"   => $slice->urn(),
	"expiration"  => $new_expires,
	"credentials" => [$slice_credential->asString(),
			  $speaksfor_credential->asString(),
			  $extcred],
    };
743 744
    my $cmurl = $authority->url();
    $cmurl =~ s/protogeni/protogeni\/stoller/ if ($usemydevtree);
745

746
    return Genixmlrpc::CallMethod($cmurl, $context, "RenewSlice", $args);
747 748 749 750 751
}

#
# Create an Image,
#
752
sub CreateImage($$$;$)
753
{
754
    my ($self, $sliver_urn, $imagename, $bsname) = @_;
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
    my $authority = $self->GetGeniAuthority();
    my $geniuser  = $self->GetGeniUser();
    my $slice     = $self->GetGeniSlice();
    my $context   = APT_Geni::GeniContext();
    return undef
	if (! (defined($geniuser) && defined($authority) &&
	       defined($slice) && defined($context)));

    my ($slice_credential, $speaksfor_credential) =
	APT_Geni::GenCredentials($slice, $geniuser);
    return undef
	if (! (defined($speaksfor_credential) &&
	       defined($slice_credential)));

    my $args = {
	"slice_urn"   => $slice->urn(),
	"imagename"   => $imagename,
	"sliver_urn"  => $sliver_urn,
773
	"global"      => 1,
774 775 776
	"credentials" => [$slice_credential->asString(),
			  $speaksfor_credential->asString()],
    };
777 778
    $args->{'bsname'} = $bsname
	if (defined($bsname));
779

780 781 782 783
    my $cmurl = $authority->url();
    $cmurl =~ s/protogeni/protogeni\/stoller/ if ($usemydevtree);
    
    return Genixmlrpc::CallMethod($cmurl, $context, "CreateImage", $args);
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
}

#
# Ask for status.
#
sub SliceStatus($)
{
    my ($self) = @_;
    my $authority = $self->GetGeniAuthority();
    my $geniuser  = $self->GetGeniUser();
    my $slice     = $self->GetGeniSlice();
    my $context   = APT_Geni::GeniContext();
    return undef
	if (! (defined($geniuser) && defined($authority) &&
	       defined($slice) && defined($context)));

    my ($slice_credential, $speaksfor_credential) =
	APT_Geni::GenCredentials($slice, $geniuser);
    return undef
	if (! (defined($speaksfor_credential) &&
	       defined($slice_credential)));

    my $args = {
	"slice_urn"   => $slice->urn(),
	"credentials" => [$slice_credential->asString(),
			  $speaksfor_credential->asString()],
    };
811 812
    my $cmurl = $authority->url();
    $cmurl =~ s/protogeni/protogeni\/stoller/ if ($usemydevtree);
813

814
    return Genixmlrpc::CallMethod($cmurl, $context, "SliverStatus", $args);
815 816
}

817 818 819
#
# Reboot some nodes
#
820
sub SliverAction($$@)
821
{
822 823
    my ($self, $which, @slivers) = @_;
    my $method    = ($which eq "reboot" ? "RestartSliver" : "ReloadSliver");
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
    my $authority = $self->GetGeniAuthority();
    my $geniuser  = $self->GetGeniUser();
    my $slice     = $self->GetGeniSlice();
    my $context   = APT_Geni::GeniContext();
    return undef
	if (! (defined($geniuser) && defined($authority) &&
	       defined($slice) && defined($context)));

    my ($slice_credential, $speaksfor_credential) =
	APT_Geni::GenCredentials($slice, $geniuser);
    return undef
	if (! (defined($speaksfor_credential) &&
	       defined($slice_credential)));

    my $args = {
	"sliver_urns" => \@slivers,
	"credentials" => [$slice_credential->asString(),
			  $speaksfor_credential->asString()],
    };
843 844
    my $cmurl = $authority->url();
    $cmurl =~ s/protogeni/protogeni\/stoller/ if ($usemydevtree);
845

846
    return Genixmlrpc::CallMethod($cmurl, $context, $method, $args);
847
}
848

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
#
# Lockdown
#
sub Lockdown($$)
{
    my ($self, $clear) = @_;
    my $authority = $self->GetGeniAuthority();
    my $geniuser  = $self->GetGeniUser();
    my $slice     = $self->GetGeniSlice();
    my $context   = APT_Geni::GeniContext();
    return undef
	if (! (defined($geniuser) && defined($authority) &&
	       defined($slice) && defined($context)));

    my ($slice_credential, $speaksfor_credential) =
	APT_Geni::GenCredentials($slice, $geniuser);
    return undef
	if (! (defined($speaksfor_credential) &&
	       defined($slice_credential)));

    my $args = {
	"slice_urn"   => $slice->urn(),
	"credentials" => [$slice_credential->asString(),
			  $speaksfor_credential->asString()],
    };
    $args->{"clear"} = 1
	if ($clear);
    my $cmurl = $authority->url();
    $cmurl =~ s/protogeni/protogeni\/stoller/ if ($usemydevtree);

    return Genixmlrpc::CallMethod($cmurl, $context, "Lockdown", $args);
}

Leigh Stoller's avatar
Leigh Stoller committed
882 883
# _Always_ make sure that this 1 is at the end of the file...
1;