create_slivers.in 35.8 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2008-2019 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 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
# 
# {{{GENIPUBLIC-LICENSE
# 
# GENI Public License
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
# 
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# 
# }}}
#
use strict;
use English;
use Getopt::Std;
use XML::Simple;
use File::Temp qw(tempfile :mktemp tmpnam :POSIX);
use Date::Parse;
use Data::Dumper;
use Cwd qw(realpath);

#
# Create a quick VM.
# 
sub usage()
{
    print "Usage: create_slivers [-S] [-i] uuid\n";
    exit(1);
}
my $optlist         = "dSLif";
my $debug           = 0;
my $verbose         = 1;
my $foreground      = 0;
my $usestitcher     = 0;
my $ignorefailures  = 0;
my $usetracker      = 0;
my $takelock        = 0; # Use when called from create_instance.
my $errmsg;
my $sshkeys;
my @aggregate_list  = ();
my $sendemail       = 0;

#
# Configure variables
#
my $TB		     = "@prefix@";
my $TBOPS            = "@TBOPSEMAIL@";
my $MAINSITE         = @TBMAINSITE@;
my $OURDOMAIN	     = "@OURDOMAIN@";
my $ANNOUNCE         = "$TB/sbin/announce";
my $MYURN	     = "urn:publicid:IDN+${OURDOMAIN}+authority+cm";
my $SACERT	     = "$TB/etc/genisa.pem";
my $MANAGEINSTANCE   = "$TB/bin/manage_instance";
my $STITCHER         = "$TB/gcf/src/stitcher.py";

#
# Untaint the path
#
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Turn off line buffering on output
#
$| = 1;

#
# Load the Testbed support stuff.
#
# Load the Testbed support stuff.
use lib "@prefix@/lib";
use EmulabConstants;
use libtestbed;
use libaudit;
use APT_Profile;
use APT_Instance;
use APT_Geni;
use APT_Dataset;
use APT_Aggregate;
use Experiment;
use User;
use emutil;
use libEmulab;
use GeniDB;
use GeniCertificate;
use GeniCredential;
use GeniSlice;
use GeniAuthority;
use GeniHRN;
use Genixmlrpc;
use GeniResponse;
use GeniXML;
use WebTask;
use Logfile;
use EmulabFeatures;

# Connect to the SA DB.
DBConnect(GENISA_DBNAME());

# Protos
sub fatal($);
119
sub MyExit($$);
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
sub CreateSlivers();
sub RunStitcher();
sub CallMethodOnAggregate($$$);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug++;
}
if (defined($options{"i"})) {
    $ignorefailures++;
}
if (defined($options{"f"})) {
    $foreground++;
}
if (defined($options{"S"})) {
    $usestitcher++;
}
if (defined($options{"L"})) {
    $takelock++;
}
if (@ARGV < 1) {
    usage();
}
my $instance = APT_Instance->Lookup($ARGV[0]);
if (!defined($instance)) {
    fatal("No such instance");
}
154 155 156
my $webtask     = $instance->webtask();
my $webtask_id  = $webtask->task_id();
my $genislice   = $instance->GetGeniSlice();
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
if (!defined($genislice)) {
    fatal("Could not get genislice for $instance\n");
}
if ($takelock) {
    $genislice->TakeLock();
}
else {
    if ($genislice->Lock()) {
	print STDERR "Could not get the lock\n";
	exit(1);
    }
}

#
# Anything to do? See create_instance; Note that we skip anything
# that is already being worked on.
#
foreach my $agg ($instance->AggregateList()) {
175 176
    my $aptagg = $agg->GetAptAggregate();
    
177 178 179 180
    next
	if (! ($agg->status() eq "created" || $agg->status() eq "deferred"));

    #
181 182
    # See if the aggregate is online, lets not go to a ton of trouble
    # if the aggregate is dead.
183
    #
184
    if ($aptagg->CheckStatus(\$errmsg)) {
185
	print STDERR "$errmsg\n";
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	#
	# If the cluster is not deferrable, then its an error and the
	# instance has failed.
	#
	if (!$aptagg->deferrable()) {
	    $instance->SetStatus("failed");

	    if (defined($webtask)) {
		$webtask->output($errmsg);
		$webtask->Exited(1);
	    }
	    $instance->RecordError(1, $errmsg);
	    exit(1);
	}
	# Mark as deferred (it might still be "created" if not scheduled).
	$agg->SetStatus("deferred")
	    if ($agg->status() eq "created");
203 204 205
	next;
    }
    push(@aggregate_list, $agg);
206 207 208 209 210 211 212 213
    #
    # We send email when all deferred aggregates are started.  Note
    # that if the status is still "created", we are coming from
    # create_instance() (not a scheduled experiment), so do not send
    # email, no need to since the user is looking at the web interface.
    #
    $sendemail = 1
	if ($instance->status() ne "created");
214 215 216
}
if (!@aggregate_list) {
    print "No uncreated aggregates to process.\n";
217 218 219 220 221 222 223
    #
    # If not a scheduled experiment, we need to set the instance status
    # to something that makes sense for the web interface since we were
    # not able to start anything at all. Later, if we get some started,
    #
    if ($instance->status() eq "created") {
	$instance->SetStatus("pending");
Leigh Stoller's avatar
Leigh Stoller committed
224
	$instance->Defer();
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 267 268 269 270 271 272 273 274 275
    $genislice->UnLock();
    exit(0);
}

my $geniuser = $instance->GetGeniUser();
if (!defined($geniuser)) {
    fatal("Could not get geniuser for $instance\n");
}
my $emulab_user = $geniuser->emulab_user();
my $slice_urn   = $genislice->urn();
my $uuid        = $instance->uuid();
my $project     = $instance->GetProject();
my $rspecstr    = $instance->rspec();
$webtask->AutoStore(1);

#
# Load the SA cert to act as caller context.
#
my $sa_certificate = GeniCertificate->LoadFromFile($SACERT);
if (!defined($sa_certificate)) {
    fatal("Could not load certificate from $SACERT\n");
}
my $sa_authority = GeniAuthority->Lookup($sa_certificate->urn());
if (!defined($sa_authority)) {
    fatal("Could not load SA authority object");
}

#
# We use the normal XMLRPC route, so need a context.
#
my $context = Genixmlrpc->Context($sa_certificate);
if (!defined($context)) {
    fatal("Could not create context to talk to CM");
}
Genixmlrpc->SetContext($context);

#
# Generate credentials we need.
#
my ($slice_credential, $speaksfor_credential) =
    APT_Geni::GenCredentials($genislice, $geniuser, undef, 0);
if (! (defined($speaksfor_credential) &&
       defined($slice_credential))) {
    fatal("Could not generate credentials");
}

#
# Generate the extra credentials that tells the backend this experiment
# can access the datasets.
#
276 277
my $dataset_credentials = {};
my $retval = $instance->CreateDatasetCreds(\$errmsg, \$dataset_credentials);
278
if ($retval) {
279 280 281 282
    # Map user error to BADARGS,
    $retval = GENIRESPONSE_BADARGS()
	if ($retval == 1);
    MyExit($retval, "Could not generate dataset credentials: $errmsg");
283 284
}
# Ditto images that are not global (also checks user permission).
285 286
my $image_credentials = {};
$retval = $instance->CreateImageCreds(\$errmsg, \$image_credentials);
287
if ($retval) {
288 289 290 291
    # Map user error to BADARGS,
    $retval = GENIRESPONSE_BADARGS()
	if ($retval == 1);
    MyExit($retval, "Could not generate image credentials: $errmsg");
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
}

#
# Tell the CM the portal information, which is used by Emulab based
# CMs to send email links.
#
# Special: This also tells the CM to do normal NFS mounts if this is
# the "Emulab" portal making the request.
#
# XXX The local instance will not have these tags, but no big deal.
# XXX Need to handle this differently if we use the stitcher?
#
if (APT_Profile::AddPortalTag(\$rspecstr,
			      $instance->Brand()->brand(),
			      $instance->webURL(),
			      \$errmsg)) {
    fatal($errmsg);
}

#
# Check to see if we need to use the stitcher, but command line argument
# overrides.
#
if (!$usestitcher) {
    $usestitcher = APT_Profile::NeedStitcher($rspecstr, \$errmsg);
    if ($usestitcher < 0) {
	fatal("Could not determine if profile needs the stitcher: $errmsg");
    }
}

#
# Get the set of keys (accounts) that need to be sent along. We build
# them in CM format, but convert to AM format later if needed.
#
if ($instance->GetSSHKeys(\$sshkeys) < 0 || !@{$sshkeys}) {
    fatal("Error constructing ssh key list");
}

# Use of the Image Tracker is a Portal directive at the moment.
$usetracker = 1
    if (GetSiteVar("protogeni/use_imagetracker") &&	
	EmulabFeatures->FeatureEnabled("APT_UseImageTracker",
				       $emulab_user, $project));

#
337
# Exit and let caller poll for status. We use the libaudit daemonize option.
338 339
#
if (!($debug || $foreground)) {
340 341 342
    if (AuditStart(1, undef,
		   LIBAUDIT_LOGTBLOGS()|LIBAUDIT_LOGONLY()|LIBAUDIT_NOCHILD())){
	# Parent exits
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
	exit(0);
    }
    AddAuditInfo("cc", $project->LogsEmailAddress());
    AddAuditInfo("brand", $project->Brand());
}
    
# Bind the process id.
$webtask->SetProcessID($PID);

if ($usestitcher) {
    my $rval = RunStitcher();
    if ($rval) {
	$genislice->UnLock();
	$instance->SetStatus("failed");
	$instance->RecordError($rval,
			       (defined($webtask->output()) ?
				$webtask->output() : ""));
	$webtask->Exited($rval);
	exit($rval);
    }
}
else {
    my $rval = CreateSlivers();
    #
    # We do not want email for most mapping errors, so look at the
    # return code to see if we want to kill the log (user will see the
    # error in the web ui). 
    #
    if ($rval) {
	$genislice->UnLock();
	my $exitcode = -1;
	
	$instance->RecordError($rval,
			       (defined($webtask->output()) ?
				$webtask->output() : ""));
	if ($rval == GENIRESPONSE_BADARGS ||
	    $rval == GENIRESPONSE_INSUFFICIENT_NODES ||
	    $rval == GENIRESPONSE_INSUFFICIENT_MEMORY ||
	    $rval == GENIRESPONSE_INSUFFICIENT_BANDWIDTH ||
	    $rval == GENIRESPONSE_NOSPACE ||
	    $rval == GENIRESPONSE_NO_CONNECT ||
	    $rval == GENIRESPONSE_MAPPING_IMPOSSIBLE ||
	    $rval == GENIRESPONSE_NO_MAPPING) {
386 387
	    AuditAbort()
		if (!($debug || $foreground));
388 389
	    $exitcode = 1;
	}
390 391
	# Stop trying to instantiate.
	$instance->ResolveDefer();
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
	$webtask->Exited($rval);
	exit($exitcode);
    }
}

#
# Because of deferred aggregates, might need to prune the list
# since we do not want to wait for the deferred ones.
#
my @tmp = ();
foreach my $aggobj (@aggregate_list) {
    push(@tmp, $aggobj)
	if ($aggobj->status() ne "deferred");

}
if (!@tmp) {
    print STDERR "No progress made, ".
	"all pending aggregates deferred till later\n";
    $genislice->UnLock();
    exit(0);
}
@aggregate_list = @tmp;

#
# If all the aggregates are resolved, then we can resolve the instance.
#
if (!$instance->DeferredAggregateList()) {
    $instance->ResolveDefer();
}
421 422 423 424
else {
    # This updates the record so we know when last retry was.
    $instance->Defer();
}
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 475 476 477 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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 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 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609

#
# Cause of early return, we have to Check to see if all aggregates provisioned.
# This will update status and node counts.
#
$instance->CheckProvisioned();

#
# Now wait for the sliver to be ready, which means polling.
#
# Shorten default timeout.
#
Genixmlrpc->SetTimeout(60);

#
# Okay, fire off the waits for each aggregate
#
my @return_codes = ();
if (ParRun({"maxwaittime" => 99999,
	    "maxchildren" => scalar(@aggregate_list)},
	   \@return_codes,
	   \&APT_Instance::Aggregate::WaitForSliver, @aggregate_list)) {
    #
    # The parent caught a signal. Leave things intact so that we can
    # kill things cleanly later.
    #
    $genislice->UnLock();
    print STDERR "Internal error in WaitForSlivers\n";
    $webtask->output("Internal error in WaitForSlivers");
    $instance->SetStatus("failed");
    $webtask->Exited(1);
    exit(-1);
}

#
# If we were canceled, then none of the stuff below matters, we
# are going to do a terminate.
#
if ($instance->IsCanceled()) {
    $genislice->UnLock();

    #
    # If someone gets the lock, this will fail. But the apt daemon will
    # see the canceled flag too and fire off a termination. 
    #
    system("$MANAGEINSTANCE -t $webtask_id terminate $uuid");
    exit(0);
}

# Count up nodes running a startup service.
my $startuprunning = 0;

#
# Check the exit codes; any failure is a total failure (for now).
#
my $hosed  = 0;
my $failed = 0;
foreach my $aggobj (@aggregate_list) {
    my $code = shift(@return_codes);
    my $cluster = $aggobj->GetAptAggregate()->name();

    # Updated in a forked child, must refresh. 
    $aggobj->Refresh();
    print $aggobj->aggregate_urn() . "\n";
    if ($code) {
	$failed++;
	my $exitcode = $aggobj->webtask()->exitcode();
	my $output   = $aggobj->webtask()->output();
	#
	# Some of the errors should go the user, others to tbops.
	#
	if (! (($exitcode >= GENIRESPONSE_SETUPFAILURE() &&
		$exitcode <= GENIRESPONSE_SETUPFAILURE_MAXERROR()) ||
	       $exitcode == GENIRESPONSE_TIMEDOUT())) {
	    $hosed++;
	    $output = "Internal error creating experiment on the ".
		"$cluster cluster";
	}
	$webtask->output($output);
	$webtask->Exited($exitcode);

	# Promote the log up to the instance so that so its easy to find.
	$instance->SetPublicURL($aggobj->public_url())
	    if (defined($aggobj->public_url()));
    }
    else {
	my $sliverstatus = $aggobj->SliverStatus();

	print Dumper($sliverstatus);

	foreach my $status (values(%{ $sliverstatus })) {
	    # Startup command is still running.
	    $startuprunning++
		if (exists($status->{"sliver_data"}->{'execute_state'}) &&
		    $status->{"sliver_data"}->{'execute_state'} ne "exited");
	}
    }
    if (defined($aggobj->public_url())) {
	print $aggobj->public_url() . "\n";
    }
    print "\n" . $aggobj->manifest() . "\n\n";
    print "------------------------------------------------------------\n\n";
}
$genislice->UnLock();

if ($failed) {
    if ($ignorefailures) {
        $instance->SetStatus("ready");
    }
    else {
        $instance->SetStatus("failed");
    }
    # Webtask exit status set above.
    $instance->RecordError($webtask->exitcode(),
			   (defined($webtask->output()) ?
			    $webtask->output() : ""));
}
else {
    $instance->SetStatus("ready");

    if ($sendemail) {
	my $brand = $instance->Brand();
	my $pid   = $project->pid();
	my $eid   = $instance->name();
	my $user  = $instance->GetGeniUser();
	my $email = $user->email();
	my ($subject, $message);

	my $headers =
	    "CC: " . $brand->OpsEmailAddress() . "\n" .
	    "BCC: " . $brand->LogsEmailAddress();
	
	if ($instance->start_at()) {
	    my $start_at = TBDateStringUTC($instance->start_at());
	    
	    $subject = "Scheduled experiment $pid/$eid is now running";
	    $message = "Experiment $pid/$eid, which was scheduled to\n".
		"start at $start_at, is now running.";
	    if ($instance->DeferredAggregateList()) {
		$message .= "\n" .
		    "But some resources are still unreachable, we will send\n".
		    "email when those resources become available.";
	    }
	}
	else {
	    $subject = "Deferred experiment $pid/$eid is now fully running";
	    $message = "Experiment $pid/$eid, which had offline resources\n".
		"when started, is now fully running.";
	}
	$message .= "\n\n" . $instance->webURL() . "\n\n";
	
	$instance->Brand()->SendEmail($email, $subject, $message,
				      $brand->OpsEmailAddress(), $headers);
    }

    #
    # If there are still execute services running, lets keep polling
    # using the monitor.
    #
    if ($startuprunning) {
	print "$MANAGEINSTANCE -t $webtask_id monitor $uuid -w\n";
	
	system("$MANAGEINSTANCE -t $webtask_id monitor $uuid -w")
    }
    else {
	$webtask->Exited(0);
    }
}
exit($hosed);

#
# Create a sliver at a single aggregate. This is called from parrun
# so it needs to return success or failure, we lookup the results
# in the DB.
#
sub CreateSliver($)
{
    my ($ref) = @_;
    my $aggobj = $ref;
    $aggobj->Refresh();
    my $webtask   = $aggobj->webtask();
    my $authority = $aggobj->GetGeniAuthority();
    my $status    = $aggobj->status();
    my $cmurl     = $authority->url();
    my $urn       = $authority->urn();
610
    my @dsetcreds = ();
611
    my @imcreds   = ();
612 613 614 615 616 617 618 619 620 621 622
    my $manifest;
    $webtask->Refresh();

    # Mark that it is doing something.
    $aggobj->SetStatus("provisioning");

    # Debugging
    $cmurl = APT_Instance::devurl($cmurl);

    Genixmlrpc->SetTimeout(900);

623 624 625 626 627 628 629
    #
    # Dataset and image credentials are per aggregate cause of speaksfor
    # rules checking at the CM.
    #
    if (exists($dataset_credentials->{$authority->urn()})) {
	@dsetcreds = @{$dataset_credentials->{$authority->urn()}};
    }
630 631 632
    if (exists($image_credentials->{$authority->urn()})) {
	@imcreds = @{$image_credentials->{$authority->urn()}};
    }
633

634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
    #
    # This creates the sliver and starts it. We have to watch for the
    # server being too busy.
    #
    my $tries = 1;
    my $response;
    my $earlyreturn = 0;
    my $async = 0;
    my $errmsg;

    while (1) {
	$response =
	    Genixmlrpc::CallMethod($cmurl, undef,
				   "CreateSliver",
				   { "slice_urn"   => $slice_urn,
				     "rspec"       => $rspecstr,
				     "keys"        => $sshkeys,
				     "credentials" =>
					 [$slice_credential->asString(),
					  $speaksfor_credential->asString(),
654 655
					  @dsetcreds,
					  @imcreds,
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 714 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 743 744 745 746 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 777 778 779 780 781 782 783 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 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 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 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 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 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
					 ],
				     "certificate" => $instance->cert(),
				     "key"         => $instance->privkey(),
				     "usetracker"  => $usetracker,
				     "asyncmode"   => 1,
				   });

	if ($response->code() != GENIRESPONSE_SUCCESS) {
	    if ($response->code() == GENIRESPONSE_SERVER_UNAVAILABLE ||
		($response->code() == GENIRESPONSE_NETWORK_ERROR &&
		 $response->value() == GENIRESPONSE_NETWORK_ERROR_NOCONNECT)) {
		if ($tries) {
		    print STDERR "Server for $urn reports too busy or not ".
			"reachable; waiting a while ...\n";
		    sleep(int(rand(20)) + 10);
		    $tries--;
		    next;
		}
	    }
	    elsif ($response->code() == GENIRESPONSE_NETWORK_ERROR &&
		   $response->value() == GENIRESPONSE_NETWORK_ERROR_TIMEDOUT) {
		#
		# If the slice actually exists, then lets not fail
		# yet, but instead see if we can sync back up during
		# WaitForSlivers().
		#
		Genixmlrpc->SetTimeout(15);
		my $tmp = $aggobj->SliceResolve();
		print STDERR "SliceStatus: " . $tmp->code() . "\n";
		    
		if ($tmp->code() == GENIRESPONSE_RPCERROR ||
		    $tmp->code() == GENIRESPONSE_SEARCHFAILED) {
		    #
		    # Okay, we can bail. 
		    #
		    print STDERR "Read timeout, bailing\n";
		}
		else {
		    #
		    # Accept that we do not have a manifest, but the CM
		    # is reachable and the slice exists.
		    #
		    $earlyreturn = 1;
		    last;
		}
	    }
	    elsif ($response->code() == GENIRESPONSE_INPROGRESS()) {
		$async = 1;
		#print STDERR Dumper($response);
		last;
	    }
	    print STDERR "CreateSliver failed on $urn: " .
		$response->error() . "\n";
	    
	    $webtask->output($response->error());
	    $webtask->Exited($response->code());
	    $aggobj->SetStatus("failed");
	    
	    if (defined($response->logurl())) {
		$aggobj->SetPublicURL($response->logurl());
	    }
	    return -1;
	}
	last;
    }
    # This will get overwritten later.
    if (defined($response->logurl())) {
	$aggobj->SetPublicURL($response->logurl());
    }

    #
    # Watch for async return. We will keep looping waiting for an error
    # or completion.
    #
    if ($async || $earlyreturn) {
	if ($earlyreturn) {
	    print STDERR "Server returned early for read timeout\n";
	}
	else {
	    print STDERR "Server returned early for async mode\n";
	}
	Genixmlrpc->SetTimeout(30);

	my $code;
	my $errmsg;
	#
	# Loop waiting for a manifest or the slice to disappear or for an
	# async error indicator. Hard to say how long we should wait ...
	#
	my $seconds  = 7200;
	my $interval = 15;
	while ($seconds > 0) {
	    sleep($interval);
	    $seconds -= $interval;
	    
	    my $tmp = $aggobj->SliceResolve();
	    $code = $tmp->code();

	    print STDERR "Resolve returned $code\n";
	    #print STDERR Dumper($tmp);

	    # Just keep going, we will get there eventually.
	    # Lets say RPC errors will clear up at some point, even though
	    # they are more drastic. 
	    if ($code == GENIRESPONSE_SERVER_UNAVAILABLE ||
		$code == GENIRESPONSE_NETWORK_ERROR ||
		$code == GENIRESPONSE_RPCERROR) {
		next;
	    }
	    # Slice is gone. Unusual.	    
	    if ($code == GENIRESPONSE_SEARCHFAILED()) {
		$code = GENIRESPONSE_ERROR;
		$errmsg = "Experiment no longer exists";
		last;
	    }
	    # Other errors are unusual.
	    if ($code != GENIRESPONSE_SUCCESS) {
		$errmsg = $tmp->error();
		last;
	    }
	    my $blob = $tmp->value();
	    if (ref($blob) ne "HASH") {
		$code = GENIRESPONSE_ERROR;
		$errmsg = "Unexpected return value from resolve";
		last;
	    }
	    if (exists($blob->{'manifest'})) {
		# We have forward motion, so keep going.
		print STDERR "We got a manifest from resolve\n";
		$manifest = $blob->{'manifest'};
		last;
	    }
	    if (exists($blob->{'async_code'})) {
		# We are getting back async error status.
		$code   = $blob->{'async_code'};
		$errmsg = $blob->{'async_output'};
		last;
	    }
	}
	if ($seconds < 0) {
	    # XXX Need better handling for timeout.
	    $code = GENIRESPONSE_TIMEDOUT;
	    $errmsg = "Experiment setup timed out";
	}
	if ($code != GENIRESPONSE_SUCCESS) {
	    print STDERR "Resolve loop failure: $code, $errmsg\n";
	    $webtask->Exited($code);
	    $webtask->output($errmsg);
	    $aggobj->SetStatus("failed");
	    return -1;
	}
    }
    else {
	my $code = 0;
	my $errmsg;
	
	if (ref($response->value()) ne "ARRAY") {
	    $code = GENIRESPONSE_ERROR;
	    $errmsg = "Unexpected return value";
	}
	else {
	    $manifest = $response->value()->[1];
	    if (!defined($manifest)) {
		$code = GENIRESPONSE_ERROR;
		$errmsg = "No manifest returned";
	    }
	}
	if ($code) {
	    print STDERR "CreateSliver $urn: $errmsg\n";
	    $webtask->Exited($code);
	    $webtask->output($errmsg);
	    $aggobj->SetStatus("failed");
	    return -1;
	}
    }
    $aggobj->SetStatus("provisioned");
    $aggobj->SetManifest($manifest);
    return 0;
}

sub CreateSlivers()
{
    my ($perrmsg) = @_;
    my @return_codes = ();

    #
    # If this is a scheduled experiment, we want to update the start
    # time for the web interface.
    #
    if ($instance->status() eq "deferred") {
	$instance->Start();
    }
    $instance->SetStatus("provisioning");

    #
    # Use parrun here even for a single aggregate; then we can kill
    # the child if something goes wrong, and the parent will do the
    # correct cleanup.
    #
    if (ParRun({"maxwaittime" => 99999,
		"maxchildren" => scalar(@aggregate_list)},
	       \@return_codes, \&CreateSliver, @aggregate_list)) {
	#
	# The parent caught a signal. Leave things intact so that we can
	# kill things cleanly later.
	#
	$instance->SetStatus("failed");
	return -1;
    }
    #
    # Check the exit codes; any failure is a total failure (for now).
    # 
    # Remember any aggregates that need to be deferred till later.
    #
    my @deferred = ();
    
    foreach my $aggobj (@aggregate_list) {
	my $cluster    = $aggobj->GetAptAggregate()->name();
	my $deferrable = $aggobj->GetAptAggregate()->deferrable();
	
	#
	# Have to refresh the sliver objects since they were updated in a fork.
	# Need the manifests for the call to ComputeNodeCounts below.
	#
	$aggobj->Refresh();
	my $code = shift(@return_codes);
	if ($code) {
	    # Promote the log up to the instance so that so its easy to find.
	    $instance->SetPublicURL($aggobj->public_url())
		if (defined($aggobj->public_url()));

	    my $output   = $aggobj->webtask()->output();
	    my $exitcode = $aggobj->webtask()->exitcode();

	    if ($exitcode == GENIRESPONSE_SERVER_UNAVAILABLE && $deferrable) {
		print STDERR "$cluster is offline; deferring till later\n";
		push(@deferred, $aggobj);
		next;
	    }

	    #
	    # We do not want to show some errors to users, we are going to
	    # end up sending email to tbops.
	    #
	    if ($exitcode == GENIRESPONSE_RPCERROR() ||
		$exitcode == GENIRESPONSE_SERVERERROR()) {
		$output = "Internal error creating experiment on the ".
		    "$cluster cluster";
	    }
	    $webtask->output($output);
	    
	    # This will be the createsliver exit code if we got one, or -1.
	    $code = $exitcode;
	    
	    # Do this last so that the web interface does not see failed
	    # before the reason is in the webtask.
	    $instance->SetStatus("failed");
	    return $code;
	}
    }
    #
    # No errors, but we have deferred aggregates. Need to schedule these
    # for later retry.
    #
    if (@deferred) {
	foreach my $aggobj (@deferred) {
	    $aggobj->Defer();
	}
	$instance->Defer();
    }
    return 0;
}

#
# Run the stitcher to allocate resources. We use this whenever we have
# links that cross aggregates
#
sub RunStitcher()
{
    my $tmpdir        = mktemp("/tmp/stitcher.XXXXXX");
    my $slicecredfile = "$tmpdir/slicecred.xml";
    my $speaksforfile = "$tmpdir/speaksforcred.xml";
    my $al2scredfile  = "$tmpdir/al2scred.xml";
    my $rspecfile     = "$tmpdir/rspec.xml";
    my $stdoutfile    = "$tmpdir/stitcher.stdout";
    my $stderrfile    = "$tmpdir/stitcher.stderr";
    # Who we are speaking for.
    my $speaksfor_urn = $slice_credential->owner_urn();
    my $failed        = 0;
    my $errcode       = GENIRESPONSE_STITCHER_ERROR();

    #
    # Mark the aggregates as doing something, we never retry these
    # on any stitcher failure.
    #
    foreach my $agg (@aggregate_list) {
	$agg->SetStatus("provisioning");
    }

    #
    # The AM API uses a different ssh key structure.
    #
    my $users = [];
    foreach my $user (@{$sshkeys}) {
	my @tmp = map { $_->{'key'} } @{$user->{'keys'}};
	
	push(@{$users},
	     {"urn"   => $user->{'urn'},
	      "privs" => $user->{'privs'},
	      "keys"  => [ @tmp ] });
    }

    #
    # Hey, I think stitcher/omni has as many options as snmpit. Wow!
    #
    my $command = "$STITCHER --fileDir $tmpdir --cred $speaksforfile ".
	"--slicecredfile $slicecredfile --usercredfile $slicecredfile ".
	($speaksfor_credential->type() eq "speaksfor" ?
	 "--al2scredfile $al2scredfile " : "") .
	"--debug ".
	# We do not want these two files defaulting to user home dir.
	"--GetVersionCacheName=$tmpdir/get_version_cache.json ".
	"--AggNickCacheName=$tmpdir/agg_nick_cache ".
	"--scsURL http://scs.scs.scs.emulab.net:8081/geni/xmlrpc  ".
	"--speaksfor $speaksfor_urn -V3 allocate $slice_urn $rspecfile";

    return -1
	if (! mkdir("$tmpdir", 0755));

    print "Using $tmpdir for stitcher\n"
	if ($debug || $verbose);
    print "Stitcher command: $command\n"
	if ($debug || $verbose);

    goto bad
	if ($instance->WriteCredentials($tmpdir));

    goto bad
	if (system("/bin/cp -fp /usr/testbed/etc/stitcher/omni_config $tmpdir"));
    goto bad
	if (system("/bin/cp -fp /usr/testbed/etc/stitcher/agg_nick_cache ".
		   "$tmpdir/agg_nick_cache"));

    if (!open(XML, ">$rspecfile")) {
	print STDERR "Could not open $rspecfile: $!\n";
	goto bad;
    }
    print XML $rspecstr;
    close(XML);

    #
    # Use a log file for the stitcher output, so we can spew it.
    # The file in the tmp dir has to exist.
    #
    system("/bin/cp /dev/null $stdoutfile");
    my $logfile = Logfile->Create($project->GetProjectGroup(), $stdoutfile);
    if (!defined($logfile)) {
	print STDERR "Could not create logfile\n";
    }
    else {
	$logfile->Open();
	$logfile->SetPublic(1);
	$instance->SetLogFile($logfile);
    }

    #
    # Okay, run the stitcher. Only to allocate, we will do the provisions
    # so that we can pass the ssh keys more easily.
    #
    $instance->SetStatus("stitching");
    system("cd $tmpdir; $command > $stdoutfile 2> $stderrfile");
    if ($?) {
	$errcode = $? >> 8;
	$errcode = GENIRESPONSE_STITCHER_ERROR() if ($errcode == 1);
	
	if (-s $stderrfile) {
	    my $stuff = `cat $stderrfile`;

	    #
	    # Try to find some useful output.
	    #
	    my $morestuff;
	    
	    if (open(SLOG, $stdoutfile)) {
		while (<SLOG>) {
		    if ($_ =~ /^Allocation of slivers in .* failed:/) {
			$morestuff = $_;

			if ($_ =~ /protogeni AM code: (\d*):/) {
			    $errcode = $1;
			}
		    }
		    elsif (defined($morestuff)) {
			if ($_ =~ /^Allocate slivers in slice .* failed/) {
			    last;
			}
			$morestuff .= $_;
		    }
		}
		close(SLOG);
		$stuff .= "\n\n" . $morestuff
		    if (defined($morestuff));
	    }
	    $webtask->output($stuff);
	    print $morestuff . "\n\n" if (defined($morestuff));
	    system("/bin/cat $stderrfile");
	}
	else {
	    $webtask->output("Stitcher failed!");
	}
	#
	# Even if we fail, want to pick up whatever aggregates the stitcher
	# decided to use, so that we can ensure all slivers get cleaned up
	# at termination.
	# 
	$failed = 1;
    }
    if (defined($logfile)) {
	$logfile->Close();
	$logfile->Store();
    }
    
    #
    # The stitcher puts the list of aggregates into a file, read that so
    # we can add any new ones to the instance. Just read the directory and
    # find it. 
    #
    my @agglist = ();
    my $agglistfile;
    my $al2smanifest;
    opendir(DIR, $tmpdir);
    my @files  = readdir(DIR);
    closedir(DIR);

    foreach my $file (@files) {
	if ($file =~ /amlist.txt$/) {
	    $agglistfile = "$tmpdir/$file";
	}
	elsif ($file =~ /manifest-rspec-al2s/) {
	    $al2smanifest = "$tmpdir/$file";
	}
    }
    #
    # The stitcher will not create this file if it passes straight through
    # to omni, as it will do if no cross aggregate links.
    #
    if (defined($agglistfile)) {
	if (!open(TXT, $agglistfile)) {
	    print STDERR "Could not open $agglistfile: $!\n";
	    goto bad;
	}
	while (<TXT>) {
	    next
		if ($_ =~ /^#/);
	    if ($_ =~ /^([^,]*),(.*)$/) {
		push(@agglist, $2);
	    }
	}
	if (!@agglist) {
	    print STDERR "Stitcher did not contact any aggregates!\n";
	    goto bad;
	}
	close(TXT);
    }
    foreach my $urn (@agglist) {
	next
	    if (exists($instance->AggregateHash()->{$urn}));
	
	my $authority = GeniAuthority->Lookup($urn);
	if (!defined($authority)) {
	    $authority = GeniAuthority->CreateFromRegistry("CM", $urn);
	    if (!defined($authority)) {
		print STDERR "Could not lookup authority: $urn\n";
		goto bad;
	    }
	}
	my $aggobj = $instance->AddAggregate($urn);
	push(@aggregate_list, $aggobj);
	# Mark that it is doing something.
	$aggobj->SetStatus("provisioning");
    }
    goto bad
	if ($failed);

    #
    # Pass this function to ParRun; Provision slivers and request manifests.
    #
    my $coderef = sub {
	my ($ref) = @_;
	my $aggobj = $ref;
	$aggobj->Refresh();
	my $webtask   = $aggobj->webtask();
	my $authority = $aggobj->GetGeniAuthority();
	my $urn       = $authority->urn();
	my $errmsg    = "Provision failed on $urn";
	$webtask->Refresh();

	#
	# AL2S was done with a createsliver, so we know its ready.
	# The thing we lack is a manifest, and I cannot seem to get
	# get ListResources to work there, so just read the file. 
	#
	if ($aggobj->isAL2S()) {
	    if (!defined($al2smanifest) || ! -e $al2smanifest) {
		print STDERR "No manifest for AL2S\n";
		$aggobj->SetStatus("failed");
		$webtask->output("No manifest for AL2S");
		$webtask->Exited(GENIRESPONSE_ERROR);
		return -1;
	    }
	    my $manifest_string = "";
	    if (! open(MAN, $al2smanifest)) {
		print STDERR "Could not open $al2smanifest\n";
		$aggobj->SetStatus("failed");
		$webtask->output("Could not open manifest file");
		$webtask->Exited(GENIRESPONSE_ERROR);
		return -1;
	    }
	    while (<MAN>) {
		$manifest_string .= $_;
	    }
	    close(MAN);
	    $aggobj->SetManifest($manifest_string);
	    $aggobj->SetStatus("provisioned");
	    print STDERR $manifest_string . "\n";
	    return 0;
	}
	print "Provisioning at $urn\n";
	my $response =
	    CallMethodOnAggregate($aggobj,
				  sub {
				      return $aggobj->Provision($users,
					  $instance->cert(),
					  $instance->privkey());
				  }, 10);
	if ($response->code() != GENIRESPONSE_SUCCESS) {
	    $aggobj->SetStatus("failed");
	    $webtask->output("Provision failed on $urn: ".$response->error());
	    $webtask->Exited($response->code());
	    print STDERR "Provision failed on $urn: ".$response->error()."\n";
	    return -1;
	}
	$aggobj->SetStatus("provisioned");
	print "Requesting manifest from $urn\n";
	$response = CallMethodOnAggregate($aggobj, "SliceResolve", 10);
	if ($response->code() != GENIRESPONSE_SUCCESS) {
	    $aggobj->SetStatus("failed");
	    $webtask->output("Could not Resolve at $urn: ".$response->error());
	    $webtask->Exited($response->code());
	    print STDERR "Could not Resolve at $urn: ".$response->error()."\n";
	    return -1;
	}
	# Web interface wants this as soon as possible.
	$aggobj->SetManifest($response->value()->{'manifest'});
	# This will get overwritten later during the wait.
	$aggobj->SetPublicURL($response->value()->{'public_url'})
	    if (exists($response->value()->{'public_url'}));

	print "Forcing correct slice expiration\n";
	$response =
	    CallMethodOnAggregate($aggobj,
		  sub {
		      return $aggobj->Extend($genislice->ExpirationGMT(),
					     $emulab_user);
		  }, 10);
	if ($response->code() != GENIRESPONSE_SUCCESS) {
	    $aggobj->SetStatus("failed");
	    $webtask->output("Renew failed on $urn: ". $response->error());
	    $webtask->Exited($response->code());
	    print STDERR "Renew failed on $urn: ". $response->error() . "\n";
	    return -1;
	}
	print "Calling SliverStart at $urn\n";
	$response = 
	    CallMethodOnAggregate($aggobj,
				  sub {
				      return $aggobj->SliverAction("start");
				  }, 10);
	if ($response->code() != GENIRESPONSE_SUCCESS) {
	    $aggobj->SetStatus("failed");
	    $webtask->output("Start failed on $urn: ". $response->error());
	    $webtask->Exited($response->code());
	    print STDERR "Start failed on $urn: ". $response->error() . "\n";
	    return -1;
	}
	return 0;
    };
    
    my @return_codes = ();
    if (ParRun({"maxwaittime" => 99999,
		"maxchildren" => scalar(@aggregate_list)},
	       \@return_codes, $coderef, @aggregate_list)) {
	#
	# The parent caught a signal. Leave things intact so that we can
	# kill things cleanly later.
	#
	goto bad;
    }
    #
    # Check the exit codes; any failure is a total failure (for now).
    #
    foreach my $aggobj (@aggregate_list) {
	#
	# Have to refresh the sliver objects since they were updated in a fork.
	#
	$aggobj->Refresh();
	my $code = shift(@return_codes);
	if ($code) {
	    $webtask->output($aggobj->webtask()->output())
		if (defined($aggobj->webtask()->output()));
	    goto bad;
	}
    }
#    system("/bin/rm -rf $tmpdir")
#	if (!$debug && defined($tmpdir) && -e $tmpdir);
    return 0;

  bad:
    #
    # Dump the stitcher output. Ick.
    #
    if (defined($tmpdir) && -e "$tmpdir/stitcher.log") {
	print "------------- Stitcher Log ---------------\n";
	system("/bin/cat $tmpdir/stitcher.log");
	print "-----------------------------------------\n";
    }
#    system("/bin/rm -rf $tmpdir")
#	if (!$debug && defined($tmpdir) && -e $tmpdir);
    return $errcode;
}

1287
sub MyExit($$)
1288
{
1289
    my ($code, $mesg) = @_;
1290 1291 1292

    $genislice->UnLock()
	if (defined($genislice));
1293 1294
    $instance->SetStatus("failed")
	if (defined($instance));
1295 1296 1297
    
    if (defined($webtask)) {
	$webtask->output($mesg);
1298
	$webtask->Exited($code);
1299 1300 1301
    }
    print STDERR "*** $0:\n".
	         "    $mesg\n";
1302 1303 1304 1305 1306 1307 1308 1309
    exit($code);
}

sub fatal($)
{
    my ($mesg) = @_;

    MyExit(-1, $mesg);
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
}

#
# Call an aggregate method with retry,
#
sub CallMethodOnAggregate($$$)
{
    my ($aggregate, $method, $retries) = @_;
    my $response;

    while ($retries) {
	#
	# This can be a coderef for more complicated invocations.
	#
	if (ref($method) eq "CODE") {
	    $response = &$method($aggregate);
	}
	else {
	    $response = $aggregate->$method();
	}
	if (!defined($response)) {
	    # We want to know about this, something is very wrong.
	    $response =
		GeniResponse->new(GENIRESPONSE_ERROR, -1,
			  "Internal error calling method on $aggregate");
	    last;
	}
	print Dumper($response);
	last
	    if ($response->code() == GENIRESPONSE_SUCCESS);
	    
	# We can keep trying for these, but not an RPC error.
	last
	    if (! ($response->code() == GENIRESPONSE_BUSY ||
		   $response->code() == GENIRESPONSE_SERVER_UNAVAILABLE ||
		   ($response->code() == GENIRESPONSE_NETWORK_ERROR &&
		    $response->value() ==
		    GENIRESPONSE_NETWORK_ERROR_NOCONNECT)));
	#
	# Wait for a while and try again. 
	#
	$retries--;
	if ($retries) {
	    sleep(10);
	}
    }
    return $response;
}