create_instance.in 45.7 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
Leigh Stoller's avatar
Leigh Stoller committed
3
# Copyright (c) 2008-2016 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
# 
# {{{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;
32
use Getopt::Long;
33
use XML::Simple;
34
use File::Temp qw(tempfile :mktemp tmpnam :POSIX);
35
use Data::Dumper;
36
use Cwd qw(realpath);
37 38 39 40 41 42

#
# Create a quick VM.
# 
sub usage()
{
43
    print "Usage: quickvm [-u uuid] [--site site:1=aggregate ...] <xmlfile>\n";
44 45
    exit(1);
}
46
my @optlist = ('d', 'v', 'u=s', 'a=s', 'S', 'k=s', 'i');
47 48
my $debug   = 0;
my $verbose = 1;
49
my $ignorefailures = 0;
50
my $xmlfile;
51
my $webtask;
Leigh Stoller's avatar
Leigh Stoller committed
52
my $webtask_id;
53
my $localuser  = 0;
54
my $usestitcher= 0;
55
my $quickuuid;
56
my $this_user;
57
my $xmlparse;
58
my $instance;
59
my $privkeyfile;
60 61
my $slice;
my $sitemap;
62
my $usetracker = 0;
63 64
my @aggregate_urns = ();

65 66 67
# Protos
sub fatal($);
sub UserError($);
68
sub SnapShot($$$);
69
sub CreateDatasetCreds($$$$$);
70 71
sub CreateSlivers();
sub RunStitcher();
72 73 74 75 76 77 78 79

#
# Configure variables
#
my $TB		  = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
my $TBLOGS        = "@TBLOGSEMAIL@";
my $OURDOMAIN     = "@OURDOMAIN@";
80
my $MAINSITE      = @TBMAINSITE@;
81
my $PGENIDOMAIN   = "@PROTOGENI_DOMAIN@";
82
my $PROTOGENI_URL = "@PROTOGENI_URL@";
83 84 85
my $SACERT	  = "$TB/etc/genisa.pem";
my $CMCERT	  = "$TB/etc/genicm.pem";
my $SSHKEYGEN     = "/usr/bin/ssh-keygen";
86
my $SSHSETUP      = "$TB/sbin/aptssh-setup";
87
my $ADDPUBKEY     = "$TB/sbin/addpubkey";
88
my $UPDATEGENIUSER= "$TB/sbin/protogeni/updategeniuser";
89
my $STITCHER      = "$TB/gcf/src/stitcher.py";
90
my $OPENSSL       = "/usr/bin/openssl";
Leigh Stoller's avatar
Leigh Stoller committed
91
my $MANAGEINSTANCE= "$TB/bin/manage_instance";
92
my $MANAGEDATASET = "$TB/bin/manage_dataset";
93
my $DEFAULT_URN   = "urn:publicid:IDN+$OURDOMAIN+authority+cm";
94
my $GUEST_URN     = "urn:publicid:IDN+apt.emulab.net+authority+cm";
95
my $PROTOGENI_LOCALUSER= @PROTOGENI_LOCALUSER@;
96
my $default_aggregate_urn = $DEFAULT_URN;
97 98 99 100 101 102 103 104

# un-taint 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
#
105
$| = 1;
106 107 108

# Load the Testbed support stuff.
use lib "@prefix@/lib";
109
use EmulabConstants;
110 111
use libtestbed;
use libaudit;
Leigh Stoller's avatar
Leigh Stoller committed
112
use APT_Profile;
Leigh Stoller's avatar
Leigh Stoller committed
113
use APT_Instance;
114
use APT_Geni;
115
use APT_Dataset;
116
use APT_Aggregate;
117
use Experiment;
118
use User;
119
use Project;
120
use Group;
121 122
use OSinfo;
use emutil;
123
use libEmulab;
124 125 126 127 128 129 130 131 132
use GeniDB;
use GeniUser;
use GeniCertificate;
use GeniCredential;
use GeniSlice;
use GeniAuthority;
use GeniHRN;
use Genixmlrpc;
use GeniResponse;
133
use GeniXML;
134
use WebTask;
135
use Logfile;
136
use EmulabFeatures;
137 138 139 140 141

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
142
Getopt::Long::Configure("no_ignore_case");
143
my %options = ();
144
if (! GetOptions(\%options, @optlist, "site=s%" => \$sitemap)) {
145 146
    usage();
}
147
if (defined($options{"a"})) {
148
    $default_aggregate_urn = $options{"a"};
149
}
150 151 152
if (defined($options{"k"})) {
    $privkeyfile = $options{"k"};
}
153 154 155
if (defined($options{"d"})) {
    $debug = 1;
}
156 157 158
if (defined($options{"i"})) {
    $ignorefailures = 1;
}
159 160 161
if (defined($options{"v"})) {
    $verbose = 1;
}
162 163
if (defined($options{"S"})) {
    $usestitcher = 1;
164
}
165 166 167
if (defined($options{"u"})) {
    $quickuuid = $options{"u"};
}
168
if (@ARGV < 1) {
169 170
    usage();
}
171
$xmlfile = shift(@ARGV);
172

173 174 175 176 177 178
#
# Check the filename when invoked from the web interface; must be a
# file in /tmp.
#
if (getpwuid($UID) ne "nobody") {
    $this_user = User->ThisUser();
179

180 181 182 183 184 185 186 187 188 189 190
    if (! defined($this_user)) {
	fatal("You ($UID) do not exist!");
    }
    $localuser = 1;
}
if (!defined($this_user) || !$this_user->IsAdmin()) {
    if ($xmlfile =~ /^([-\w\.\/]+)$/) {
	$xmlfile = $1;
    }
    else {
	fatal("Bad data in pathname: $xmlfile");
191 192
    }

193 194 195 196 197 198 199
    # Use realpath to resolve any symlinks.
    my $translated = realpath($xmlfile);
    if ($translated =~ /^(\/tmp\/[-\w\.\/]+)$/) {
	$xmlfile = $1;
    }
    else {
	fatal("Bad data in translated pathname: $xmlfile");
200 201 202
    }
}

203 204
# Email record.
if (! $debug) {
Leigh Stoller's avatar
Leigh Stoller committed
205 206
    my $opts = LIBAUDIT_LOGTBLOGS()|LIBAUDIT_LOGONLY();
    AuditStart(0, undef, $opts);
207 208 209
    #
    # Once we determine the project, we can add the appropriate log CC
    #
210
}
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

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

#
# 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");
}
226 227 228 229
# Guest users default to the APT cluster on the Mothership.
if ($MAINSITE && !$localuser) {
    $default_aggregate_urn = $GUEST_URN;
}
230 231

#
232
# We use the normal XMLRPC route, so need a context.
233 234 235 236 237 238 239 240 241 242
#
my $context = Genixmlrpc->Context($sa_certificate);
if (!defined($context)) {
    fatal("Could not create context to talk to CM");
}
Genixmlrpc->SetContext($context);

#
# Must wrap the parser in eval since it exits on error.
#
243 244 245 246
$xmlparse = eval { XMLin($xmlfile,
			 VarAttr => 'name',
			 ContentKey => '-content',
			 SuppressEmpty => undef); };
247 248 249 250 251 252
fatal($@)
    if ($@);

#
# Make sure all the required arguments were provided.
#
253
foreach my $key ("username", "email", "profile", "portal") {
254 255 256 257 258 259 260 261 262
    fatal("Missing required attribute '$key'")
	if (! (exists($xmlparse->{'attribute'}->{"$key"}) &&
	       defined($xmlparse->{'attribute'}->{"$key"}) &&
	       $xmlparse->{'attribute'}->{"$key"} ne ""));
}

#
# Gather up args and sanity check.
#
263
my ($value, $user_urn, $user_uid, $user_hrn, $user_email, $project, $pid,
264
    $gid, $group, $sshkey, $profile, $profileid, $version, $rspecstr, $errmsg,
265 266 267 268
    $userslice_id, $portal);

# This is used internally to determine which portal was used.
$portal = $xmlparse->{'attribute'}->{"portal"}->{'value'};
269 270 271 272 273

#
# Username and email has to be acceptable to Emulab user system.
#
$value = $xmlparse->{'attribute'}->{"username"}->{'value'};
274
if (! TBcheck_dbslot($value, "users", "uid",
275
		     TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
276
    fatal("Illegal username: $value - " . TBFieldErrorString());
277 278 279 280 281 282 283 284 285 286 287 288
}
$user_uid = $value;
$user_urn = GeniHRN::Generate("$OURDOMAIN", "user", $user_uid);
$user_hrn = "${PGENIDOMAIN}.${user_uid}";

$value = $xmlparse->{'attribute'}->{"email"}->{'value'};
if (! TBcheck_dbslot($value, "users", "usr_email",
		     TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
    fatal("Illegal email address: $value");
}
$user_email = $value;

289 290 291 292 293 294 295 296 297 298
#
# The instance name is optional, we will make one up if not supplied.
#
if (exists($xmlparse->{'attribute'}->{"instance_name"}) &&
    $xmlparse->{'attribute'}->{"instance_name"}->{'value'} ne "") {
    $value = $xmlparse->{'attribute'}->{"instance_name"}->{'value'};
    if (! TBcheck_dbslot($value, "experiments", "eid",
			 TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	fatal("Illegal instance name: $value");
    }
299
    $userslice_id = $value;
300 301
}

302
#
303
# Profile.
304
#
305
# This is a safe lookup.
306 307 308
$value = $xmlparse->{'attribute'}->{"profile"}->{'value'};
$profile = APT_Profile->Lookup($value);
if (!defined($profile)) {
Leigh Stoller's avatar
Leigh Stoller committed
309
    fatal("No such profile: $value");
310
}
311 312
$profileid = $profile->profileid();
$version   = $profile->version();
313

314
#
315
# Optional rspec, as for a Parameterized Profile.
316
#
317 318
if (exists($xmlparse->{'attribute'}->{"rspec"})) {
    $rspecstr  = $xmlparse->{'attribute'}->{"rspec"}->{'value'};
319
}
320 321
else {
    $rspecstr  = $profile->CheckFirewall(!$localuser);
322 323
}

324 325 326 327 328 329
#
# Update rspec with site aggregate urns.
#
# SetSites will tell us if we must use stitcher.
#
my $needstitcher = 0;
330 331 332 333
my $tmp = APT_Profile::SetSites(\$rspecstr, $sitemap, $default_aggregate_urn,
				\@aggregate_urns, \$needstitcher, \$errmsg);
if ($tmp) {
    ($tmp < 0 ? fatal($errmsg) : UserError($errmsg));
334
}
335 336 337 338 339 340 341
#
# Yep, this can happen when users do not put any nodes in their rspec.
#
if (!@aggregate_urns) {
    UserError("There are no nodes in your experiment, syntax error?");
}

342 343
# but do not override command line force.
$usestitcher = 1 if ($needstitcher);
344

345 346
#
# Look for datasets; need to verify that the datasets being referenced
347 348
# actually exists, in so far as we can check. We check permissions
# below when we generate the credentials.
349 350
#
$errmsg = "Bad dataset";
351
if (APT_Profile::CheckDatasets($rspecstr, \$errmsg)) {
352 353 354
    UserError($errmsg);
}

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
#
# Use ssh-keygen to see if the key is valid and convertable. We first
# try to get the fingerprint, which will tells us if its already in
# openssh format. If not, try to convert it.
#
if (exists($xmlparse->{'attribute'}->{"sshkey"}) &&
    defined($xmlparse->{'attribute'}->{"sshkey"}) &&
    $xmlparse->{'attribute'}->{"sshkey"} ne "") {
    $sshkey = $xmlparse->{'attribute'}->{"sshkey"}->{'value'};
    my ($fh, $keyfile) = tempfile(UNLINK => 0);
    print $fh $sshkey;

    if (system("$SSHKEYGEN -l -f $keyfile >/dev/null 2>/dev/null")) {
	if (! open(KEYGEN, "$SSHKEYGEN -i -f $keyfile 2>/dev/null |")) {
	    fatal("Could not start ssh-keygen");
	}
	$sshkey = <KEYGEN>;
	if (!close(KEYGEN)) {
	    UserError("Could not parse ssh key!");
	}
    }
376 377
    close($fh);
    unlink($keyfile);
378
}
379 380
chomp($sshkey)
    if (defined($sshkey));
381 382 383 384 385 386

#
# See if the GeniUser exists. Create if not, but that means we
# have to create an ssl certificate (which the user will never see)
# so that we can operate on behalf of the user (via speaksfor).
#
387 388 389 390 391 392 393
# Note that we want to check for the user local account ahead of
# SA account, to bypass their guest account that might still be
# in the table.
#
my $geniuser;

if ($localuser) {
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    my $emulab_user = User->Lookup($user_uid);
    
    #
    # Hmm, users with real accounts who never used Geni, but now want
    # to use APT/Cloud, have no encrypted SSL certificate. Rather then
    # force them through the web ui (and have to explain it), create one
    # for them using a random passphrase. The user will not know the
    # passphrase, but for most users it will not matter.
    #
    # This is also going to catch expired certificates, we will regenerate
    # them using the existing passphrase.
    #
    if ($emulab_user->HasValidEncryptedCert() == 0 &&
	$emulab_user->GenEncryptedCert()) {
	fatal("Could not (re)generate encrypted certificate");
    }
Leigh Stoller's avatar
Leigh Stoller committed
410
    # Now this will work; without a certificate, above line would fail.
411 412 413
    if (defined($emulab_user)) {
	$geniuser = GeniUser::LocalUser->Create($emulab_user);
    }
414 415 416 417
}
else {
    $geniuser = GeniUser->Lookup($user_urn);

418 419 420
    #
    # In Utah, check for alternate SA
    #
421
    if (!defined($geniuser) && $MAINSITE) {
422 423 424 425 426
	foreach my $urn (@aggregate_urns) {
	    if ($urn ne $GUEST_URN) {
		UserError("Guests are not allowed to use cluster: $urn");
	    }
	}
427 428
	$user_urn = GeniHRN::Generate("aptlab.net", "user", $user_uid);
	$user_hrn = "aptlab.${user_uid}";
429
	$geniuser = GeniUser->LookupGuestOnly($user_urn);
430 431
    }
}
432
if (!defined($geniuser)) {
433 434 435
    if ($localuser) {
	fatal("Could not lookup local user $user_urn");
    }
436
    
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    #
    # Do not allow overlap with local users.
    #
    if (User->Lookup($user_uid)) {
	fatal("User $user_uid exists in the local user table");
    }

    print "Geni user does not exist; creating one ...\n"
	if ($debug);

    #
    # Want to remember the auth token we emailed for later.
    #
    my $auth_token = $xmlparse->{'attribute'}->{"auth_token"}->{'value'};
    if ($auth_token !~ /^[\w]+$/) {
	fatal("Bad auth token: $auth_token");
    }
454 455 456 457 458 459 460 461
    my $blob = {"urn"      => $user_urn,
		"hrn"      => $user_hrn,
		"email"    => $user_email,
		"showuuid" => 1};
    if ($MAINSITE) {
	$blob->{'useaptca'} = 1;
    }
    my $certificate = GeniCertificate->Create($blob);
462 463 464 465 466 467 468 469 470 471
    fatal("Could not create certificate")
	if (!defined($certificate));

    $geniuser = GeniUser->Create($certificate, $sa_authority);
    fatal("Could not create new geni user")
	if (!defined($geniuser));

    $geniuser->SetAuthToken($auth_token);

    #
Leigh Stoller's avatar
Leigh Stoller committed
472
    # Setup browser ssh.
473
    #
474 475 476
    system("$SSHSETUP " . $geniuser->uuid());
    fatal("Could not create ssh key pair")
	if ($?);
477 478 479 480
}
my $user_uuid = $geniuser->uuid();
# So we know this user has dome something lately.
$geniuser->BumpActivity();
481

482 483 484
if ($localuser) {
    my $emulab_user = $geniuser->emulab_user();
    if ($emulab_user->IsNonLocal()) {
485 486
	#
	# A user created from a Geni certificate via geni-login. We
487 488
	# asked for the current ssh keys from the MA when they logged
	# in, but we ask again to make sure have the latest keys.
489
	#
490
	system("$UPDATEGENIUSER -s " . $emulab_user->uid());
491
	if (0) {
492 493
	    fatal("Could not update ssh keys for nonlocal user");
	}
494 495 496 497 498 499
	#
	# Check project membership, must be a member of at least one
	# valid project at the GPO portal.
	#
	system("$UPDATEGENIUSER -p " . $emulab_user->uid());
	if ($?) {
500 501 502
	    UserError("Could not get your project membership from your ".
		      "member authority. It is probably offline, please try ".
		      "again later.");
503
	}
Leigh Stoller's avatar
Leigh Stoller committed
504 505
	# Nonlocal users get the holding project can now join/create
	# real projects, so we get the pid passed in.
506
    }
507
    elsif (defined($sshkey) && !$emulab_user->LookupSSHKey($sshkey)) {
508 509 510 511 512 513
	#
	# XXX This is deprecated; we no longer show the ssh box to
	#     real users. Remove when we are satisfied with new
	#     ssh key management page.
	#
	
514
	#
515 516 517 518
	# A local user. We mark keys that come through this path
	# with the isaptkey flag (-a to addpubkey) so that we know
	# which key in the DB it is. The reason for this is that the
	# user might be a classic emulab user, but is now using the
519 520 521 522 523
	# APT/Cloud UI. The key provided in the web interface will
	# replace this key.
	#
	# XXX This is in flux, we now allow APT users to manage all
	# their keys via the web interface.
524
	#
525 526 527
	my ($fh, $keyfile) = tempfile(UNLINK => 0);
	print $fh $sshkey;

528
	if (system("$ADDPUBKEY -a -u $user_uid -f $keyfile")) {
529 530 531 532 533
	    fatal("Could not add new ssh pubkey");
	}
	close($fh);
	unlink($keyfile);
    }
534 535 536 537 538 539 540 541 542 543 544 545 546 547
    #
    # Hmm, users with real accounts who never used Geni, but now want
    # to use APT/Cloud, have no encrypted SSL certificate. Rather then
    # force them through the web ui (and have to explain it), create one
    # for them using a random passphrase. The user will not know the
    # passphrase, but for most users it will not matter.
    #
    # This is also going to catch expired certificates, we will regenerate
    # them using the existing passphrase.
    #
    if ($emulab_user->HasValidEncryptedCert() == 0 &&
	$emulab_user->GenEncryptedCert()) {
	fatal("Could not (re)generate encrypted certificate");
    }
548 549 550 551 552 553 554 555 556 557

    # Local users are required to select a project.
    if (! exists($xmlparse->{'attribute'}->{"pid"})) {
	fatal("No project provided for new instance");
    }
    $project = Project->Lookup($xmlparse->{'attribute'}->{"pid"}->{"value"});
    if (!defined($project)) {
	fatal("Project provided does not exist");
    }
    if (!$project->AccessCheck($emulab_user, TB_PROJECT_CREATEEXPT)) {
558 559
	fatal("No permission to create experiments in project ".
	      $project->pid());
560 561
    }
    $pid = $project->pid();
562

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
    # Option subgroup.
    if (exists($xmlparse->{'attribute'}->{"gid"}) &&
	$xmlparse->{'attribute'}->{"gid"}->{"value"} ne "" &&
	$xmlparse->{'attribute'}->{"gid"}->{"value"} ne $pid) {
	my $val = $xmlparse->{'attribute'}->{"gid"}->{"value"};
	$group = $project->LookupGroup($val);
	if (!defined($group)) {
	    fatal("Group $val does not exist in project $pid");
	}
    }
    else {
	$group = $project->GetProjectGroup();
    }
    $gid = $group->gid();

578 579 580 581 582
    # 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));
583 584 585 586 587
}
elsif (!$localuser) {
    if (defined($sshkey)) {    #
	# Guest user; remember key. For now we accept only one key. We store
	# it simply so we can display it again for the user in the web
588
	# interface. We allow key reuse for existing users, see above.
589 590 591 592 593
	#
	$geniuser->DeleteKeys();
	$geniuser->AddKey($sshkey);
    }
    # Guest users get a holding project.
594
    $pid = $APT_HOLDINGPROJECT;
595
    $project = Project->Lookup($pid);
596
    $group = $project->GetProjectGroup();
Leigh Stoller's avatar
Leigh Stoller committed
597
    $gid = $group->gid();
598 599 600
    if (!defined($project)) {
	fatal("Project $pid does not exist");
    }
601
}
602 603 604
#
# Now we know where to send to logs.
#
605 606
if (!$debug) {
    AddAuditInfo("cc", $project->LogsEmailAddress());
Leigh Stoller's avatar
Leigh Stoller committed
607 608 609 610
    if ($MAINSITE && $project->isEmulab()) {
	# Mostly people use the Cloudlab UI.
	AddAuditInfo("cc", "cloudlab-logs\@cloudlab.us");
    }
611
}
612

613 614 615 616 617 618
# Check for expired certs and speaksfor.
my $retval = APT_Geni::VerifyCredentials($geniuser, \$errmsg);
if ($retval) {
    ($retval < 0 ? fatal($errmsg) : UserError($errmsg));
}

619 620 621
# Generate the extra credentials that tells the backend this experiment
# can access the datasets.
my @dataset_credentials = ();
622
if (defined($profile)) {
623
    my $retval = CreateDatasetCreds($rspecstr, $project, $geniuser, 
624 625 626 627
				    \$errmsg, \@dataset_credentials);
    if ($retval) {
	($retval < 0 ? fatal($errmsg) : UserError($errmsg));
    }
628 629
}

630
#
631 632 633
#
# Now generate a slice registration and credential
#
634
my $safe_uid    = $user_uid; $safe_uid =~ s/_/-/;
635 636
my $slice_id    = (defined($userslice_id) ? $userslice_id :
		   $safe_uid . "-QV" . TBGetUniqueIndex('next_quickvm', 1));
637 638 639
my $slice_auth  = ($pid eq $gid ? $pid : "${pid}:${gid}");
my $slice_urn   = GeniHRN::Generate("${OURDOMAIN}:${slice_auth}",
				    "slice", $slice_id);
640
my $slice_hrn   = "${PGENIDOMAIN}.${pid}.${slice_id}";
641
my $SERVER_NAME = (exists($ENV{"SERVER_NAME"}) ? $ENV{"SERVER_NAME"} : "");
642 643 644 645

#
# Make sure slice is unique. Probably retry here at some point. 
#
646 647 648 649
if (GeniSlice->Lookup($slice_hrn) || GeniSlice->Lookup($slice_urn) ||
    ($PROTOGENI_LOCALUSER &&
     (grep {$_ eq $DEFAULT_URN} @aggregate_urns) &&
     Experiment->Lookup($project->pid(), $userslice_id))) {
650
    if (defined($userslice_id)) {
651
	UserError("Experiment name already in use, please use another. If you ".
652 653 654 655 656 657
		  "just terminated an experiment with this name, it takes a ".
		  "minute or two for the name to become available again.");
    }
    else {
	fatal("Could not form a unique slice name");
    }
658
}
659

660 661 662 663 664 665 666 667 668 669 670 671 672
#
# Generate a certificate for this new slice.
#
my $slice_certificate =
    GeniCertificate->Create({'urn'  => $slice_urn,
			     'hrn'  => $slice_hrn,
			     'showuuid' => 1,
			     'email'=> $user_email});

if (!defined($slice_certificate)) {
    fatal("Could not generate certificate for $slice_urn");
}
# Slice is created as locked.
673 674
$slice = GeniSlice->Create($slice_certificate,
			   $geniuser, $sa_authority, undef, 1);
675 676 677 678
if (!defined($slice)) {
    $slice_certificate->Delete();
    fatal("Could not create new slice object for $slice_urn");
}
679 680
# These get quick expirations, unless it is a real user.
if ($slice->SetExpiration(time() + (($localuser ? 16 : 3) * 3600)) != 0) {
681 682 683 684
    fatal("Could not set the slice expiration for $slice_urn");
}
my $slice_uuid = $slice->uuid();

685 686 687 688 689 690 691 692 693 694 695
#
# Generate a new ssl key/cert to be used to derive an ssh key pair
# or whatever else is needed. This is sent along as an option when the
# sliver is created (or provisioned, when stitching).
#
# This is going to be a real geni certificate, albeit a slice
# certificate in the alternate CA domain, that can be used at the
# "portal" XMLRPC interface. The key is unencrypted and put on the
# nodes, hence the alternate CA, and the XMLRPC server will not allow
# this certificate to do anything, except at the portal RPC server.
#
696
my $alt_urn = GeniHRN::Generate("aptlab.net:${slice_auth}", "slice", $slice_id);
697 698 699 700 701 702 703 704 705
my $alt_hrn = "aptlab.${pid}.${slice_id}";
my $alt_url = "$PROTOGENI_URL/portal";

my $altblob = {"urn"      => $alt_urn,
	       "hrn"      => $alt_hrn,
	       "url"      => $alt_url,
	       "uuid"     => $slice_uuid,
	       "email"    => $user_email,
	       "nostore"  => 1,
706
               "keyfile"  => $privkeyfile,
707 708 709 710 711 712
	       "useaptca" => 1,
	       "showuuid" => 1};
my $alt_certificate = GeniCertificate->Create($altblob);
fatal("Could not create alt certificate")
    if (!defined($alt_certificate));

713 714 715 716 717 718 719
#
# Encrypt blocks.
#
$tmp = APT_Profile::EncryptBlocks(\$rspecstr, $alt_certificate, \$errmsg);
if ($tmp) {
    ($tmp < 0 ? fatal($errmsg) : UserError($errmsg));
}
720 721 722 723 724 725 726 727 728 729 730 731 732 733
#
# Tell the CM to do normal NFS mounts if this is the "Emulab" portal
# making the request. The CM is of course free to ignore this.
#
# XXX Need to handle this differently if we use the stitcher.
#
if ($portal ne "emulab") {
    if (APT_Profile::ClearPortalTag(\$rspecstr, $errmsg)) {
        fatal($errmsg);
    }
}
elsif (APT_Profile::AddPortalTag(\$rspecstr, $portal, $errmsg)) {
    fatal($errmsg);
}
734

735
#
736
# Generate credentials we need.
737
#
738
my ($slice_credential, $speaksfor_credential) =
739
    APT_Geni::GenCredentials($slice, $geniuser, undef, 0);
740 741 742
if (! (defined($speaksfor_credential) &&
       defined($slice_credential))) {
    fatal("Could not generate credentials");
743 744
}

745
#
746
# Got this far, lets create a quickvm record.
747
#
748
my $quickvm_uuid = (defined($quickuuid) ? $quickuuid : NewUUID());
749 750 751
if (!defined($quickvm_uuid)) {
    fatal("Could not generate a new uuid");
}
752
my $blob = {'uuid'         => $quickvm_uuid,
753
	    'name'         => $slice_id,
754 755 756 757 758 759 760
	    'profile_id'   => $profileid,
	    'profile_version' => $version,
	    'slice_uuid'   => $slice_uuid,
	    'creator'      => $geniuser->uid(),
	    'creator_idx'  => $geniuser->idx(),
	    'creator_uuid' => $geniuser->uuid(),
	    'status'       => "created",
761 762
	    'servername'   => $SERVER_NAME,
	    'rspec'        => $rspecstr,
763
	    'cert'         => $alt_certificate->cert(),
764
	    'privkey'      => $alt_certificate->PrivKeyDelimited(),
765 766 767 768
};
if (defined($project)) {
    $blob->{"pid"}     = $project->pid();
    $blob->{"pid_idx"} = $project->pid_idx();
769 770
    $blob->{"gid"}     = $group->gid();
    $blob->{"gid_idx"} = $group->gid_idx();
771
}
772
$errmsg = undef;
773
$instance = APT_Instance->Create($blob, \$errmsg);
Leigh Stoller's avatar
Leigh Stoller committed
774
if (!defined($instance)) {
775 776
    fatal(defined($errmsg) ? $errmsg :
	  "Could not create instance record for $quickvm_uuid");
777
}
778

779 780 781 782 783 784 785 786 787 788 789
#
# 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.
#
my $sshkeys;
if ($instance->GetSSHKeys(\$sshkeys) < 0 || !@{$sshkeys}) {
    $slice->Delete();
    $instance->Delete();
    fatal("Error constructing ssh key list");
}

790 791 792
# We use this list of references for ParRun below.
my @aggregate_list = ();
foreach my $aggregate_urn (@aggregate_urns) {
793 794 795 796 797 798 799 800
    my $aptaggregate = APT_Aggregate->Lookup($aggregate_urn);
    if (!defined($aptaggregate)) {
        UserError("$aggregate_urn is not a valid (known) aggregate");
    }
    # Check for disabled/adminonly aggregates.
    if ($aptaggregate->disabled()) {
        UserError("$aggregate_urn is currently offline, try again later");
    }
801 802
    if ($aptaggregate->adminonly() &&
        !(defined($this_user) && $this_user->IsAdmin())) {
803 804
        UserError("Only administrators may use $aggregate_urn");
    }
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
    my $authority = GeniAuthority->Lookup($aggregate_urn);
    if (!defined($authority)) {
	$authority = GeniAuthority->CreateFromRegistry("cm", $aggregate_urn);
	if (!defined($authority)) {    
	    fatal("Could not load CM authority object for $aggregate_urn");
	}
    }
    my $aggobj = $instance->AddAggregate($aggregate_urn);
    if (!defined($aggobj)) {
	fatal("Could not create aggregate object for $aggregate_urn");
    }
    $aggobj->_authority($authority);
    push(@aggregate_list, $aggobj);
}

Leigh Stoller's avatar
Leigh Stoller committed
820 821 822
# To keep stuff happy until multisite support finished.
$instance->Update({'aggregate_urn' => $aggregate_urns[0]});

823 824
#
# Create a webtask so that we can store additional information about
825
# the sliver while we wait.
826 827
#
$webtask = WebTask->Create($instance->uuid());
828 829 830
if (!defined($webtask)) {
    fatal("Could not create a webtask!");
}
Leigh Stoller's avatar
Leigh Stoller committed
831
$webtask_id = $webtask->task_id();
832 833 834 835 836 837 838 839 840 841 842 843 844 845
$webtask->AutoStore(1);

print STDERR "\n";
print STDERR "User:    $user_urn\n";
print STDERR "Email:   $user_email" . (!$localuser ? " (guest)" : "") . "\n";
if (defined($profile)) {
    print STDERR "Profile: " . $profile->name() . ":${version}\n";
}
print STDERR "Slice:   $slice_urn\n";
print STDERR "Server:  $SERVER_NAME\n";
print STDERR "Cluster: ";
print STDERR join(",", map($_->aggregate_urn(), @aggregate_list))  . "\n";
print STDERR "\n";
print STDERR "$rspecstr\n";
846 847 848 849 850

#
# Exit and let caller poll for status.
#
if (!$debug) {
851
    libaudit::AuditPrefork();
852 853 854 855 856 857 858 859
    my $child = fork();
    if ($child) {
	# Parent exits but avoid libaudit email.
	exit(0);
    }
    # All of the logging magic happens in here.
    libaudit::AuditFork();
}
860
# Bind the process id.
861 862
$webtask->SetProcessID($PID);

863 864 865
if ($usestitcher) {
    my $rval = RunStitcher();
    if ($rval) {
866 867
	$slice->UnLock();
	$instance->SetStatus("failed");
868 869 870
	$instance->RecordError($rval,
			       (defined($webtask->output()) ?
				$webtask->output() : ""));
871
	$webtask->Exited($rval);
872
	exit($rval);
873
    }
874
}
875 876
else {
    my $rval = CreateSlivers();
877
    #
878
    # We do not want email for most mapping errors, so look at the
879 880 881 882
    # return code to see if we want to kill the log (user will see the
    # error in the web ui). 
    #
    if ($rval) {
883 884 885
	$instance->RecordError($rval,
			       (defined($webtask->output()) ?
				$webtask->output() : ""));
886 887
	if ($rval == GENIRESPONSE_BADARGS ||
	    $rval == GENIRESPONSE_INSUFFICIENT_NODES ||
888 889 890
	    $rval == GENIRESPONSE_INSUFFICIENT_MEMORY ||
	    $rval == GENIRESPONSE_INSUFFICIENT_BANDWIDTH ||
	    $rval == GENIRESPONSE_NO_MAPPING) {
891 892 893 894 895 896
	    AuditAbort()
		if (!$debug);
	}
	$webtask->Exited($rval);
	exit($rval);
    }
897
}
Leigh Stoller's avatar
Leigh Stoller committed
898
$instance->SetStatus("provisioned");
899
$instance->ComputeNodeCounts();
900 901

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

908 909 910
#
# Okay, fire off the waits for each aggregate
#
911
my @return_codes = ();
912
if (ParRun({"maxwaittime" => 99999, "maxchildren" => scalar(@aggregate_list)},
913 914
	    \@return_codes,
	    \&APT_Instance::Aggregate::WaitForSliver, @aggregate_list)) {
915 916 917 918 919
    #
    # The parent caught a signal. Leave things intact so that we can
    # kill things cleanly later.
    #
    $slice->UnLock();
920 921
    print STDERR "Internal error in WaitForSlivers\n";
    $webtask->output("Internal error in WaitForSlivers");
922 923 924 925
    $instance->SetStatus("failed");
    $webtask->Exited(1);
    exit(-1);
}
926
print "$slice_urn\n";
927

928 929 930 931 932 933 934 935 936 937 938 939
#
# If we were canceled, then none of the stuff below matters, we
# are going to do a terminate.
#
if ($instance->IsCanceled()) {
    $instance->SetStatus("canceled");
    $slice->UnLock();

    system("$MANAGEINSTANCE -t $webtask_id terminate $quickvm_uuid");
    exit(0);
}

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

943 944 945 946 947 948 949 950 951
#
# Check the exit codes; any failure is a total failure (for now).
#
my $failed = 0;
foreach my $aggobj (@aggregate_list) {
    my $code = shift(@return_codes);

    # Updated in a forked child, must refresh. 
    $aggobj->Refresh();
952 953 954 955 956 957
    print $aggobj->aggregate_urn() . "\n";
    if ($code) {
	$failed++;
	print "WaitforSliver Failure!\n";
	if (defined($aggobj->webtask()->output())) {
	    $webtask->output($aggobj->webtask()->output());
958
	    $webtask->Exited($aggobj->webtask()->exitcode());
959 960 961 962 963
	    print $aggobj->webtask()->output() . "\n";
	}
	else {
	    $webtask->output("WaitforSliver Failure at " .
			     $aggobj->aggregate_urn());
964
	    $webtask->Exited(1);
965
	}
966 967 968
	# Promote the log up to the instance so that so its easy to find.
	$instance->SetPublicURL($aggobj->public_url())
	    if (defined($aggobj->public_url()));
969
    }
Leigh Stoller's avatar
Leigh Stoller committed
970 971 972 973 974 975 976 977 978 979 980 981
    else {
	my $statusblob = $aggobj->webtask()->sliverstatus();

	print Dumper($statusblob);

	foreach my $details (values(%{ $statusblob })) {
	    # Startup command is still running.
	    $startuprunning++
		if (exists($details->{'execute_state'}) &&
		    $details->{'execute_state'} ne "exited");
	}
    }
982 983
    if (defined($aggobj->public_url())) {
	print $aggobj->public_url() . "\n";
984
    }
985 986
    print "\n" . $aggobj->manifest() . "\n\n";
    print "------------------------------------------------------------\n\n";
987
}
988
$slice->UnLock();
989

990
if ($failed) {
991 992 993 994 995 996
    if ($ignorefailures) {
        $instance->SetStatus("ready");
    }
    else {
        $instance->SetStatus("failed");
    }
997
    # Webtask exit status set above.
998 999 1000
    $instance->RecordError($webtask->exitcode(),
			   (defined($webtask->output()) ?
			    $webtask->output() : ""));
1001 1002 1003
}
else {
    $instance->SetStatus("ready");
Leigh Stoller's avatar
Leigh Stoller committed
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

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

1020 1021 1022 1023 1024
#
# Create credentials to access datasets.
#
sub CreateDatasetCreds($$$$$)
{
1025
    my ($xml, $project, $geniuser, $pmsg, $pref) = @_;
1026 1027 1028 1029 1030 1031 1032 1033
    my @credentials = ();
    
    my $rspec = GeniXML::Parse($xml);
    if (! defined($rspec)) {
	print STDERR "CreateDatasetCreds: Could not parse rspec\n";
	return -1;
    }
    foreach my $ref (GeniXML::FindNodes("n:node", $rspec)->get_nodelist()) {
1034 1035 1036
	my $manager_urn  = GetManagerId($ref);
	
	foreach my $blockref (GeniXML::FindNodesNS("n:blockstore", $ref,
1037
				   $GeniXML::EMULAB_NS)->get_nodelist()) {
1038 1039
	    my $dataset_id = GeniXML::GetText("persistent", $blockref);
	    if (!defined($dataset_id)) {
1040
		# persistent is deprecated.
1041
		$dataset_id = GeniXML::GetText("dataset", $blockref);
1042
	    }
1043 1044 1045 1046 1047 1048

	    #
	    # We only care about datasets here, we let the backend
	    # do the error checking on ephemeral blockstores.
	    #
	    next
1049 1050 1051 1052 1053 1054
		if (!defined($dataset_id));

	    my $class = GeniXML::GetText("class", $blockref);
	    if (!defined($class)) {
		$class = "remote";
	    }
1055 1056
	    # Image backed referenced by URL. No checking since the
	    # image has to be global anyway. Needs more thought.
1057
	    next
1058 1059 1060 1061
		if ($class eq "local" && $dataset_id =~ /^(http|https):/);

	    my $dataset_urn = GeniHRN->new($dataset_id);
	    my $dataset = APT_Dataset->LookupByRemoteURN($dataset_urn);
1062
	    if (!defined($dataset)) {
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
		if ($dataset_urn->domain() eq $OURDOMAIN) {
		    #
		    # Local image backed dataset or lease.
		    #
		    my ($image,$lease);
		    my $pid = $dataset_urn->project();
		    my $id  = $dataset_urn->id();

		    if ($dataset_urn->type() eq "imdataset") {
			$image = Image->Lookup($pid, $id);
			if ($image && !$image->isdataset()) {
			    $$pmsg = "$dataset_urn is an image not a dataset ";
			    return 1;
			}
			#
			# Do a partial permission check here to catch
			# errors early.  The CM will do its own check
			# of course.
			#
			if (!$image->global() &&
			    $PROTOGENI_LOCALUSER && $geniuser->IsLocal() &&
			    !$image->AccessCheck($geniuser->emulab_user(),
						   TB_IMAGEID_ACCESS())) {
			    $$pmsg = "No permission to use $dataset_urn";
			    return 1;
1088 1089
			}
		    }
1090 1091 1092 1093 1094 1095 1096 1097 1098
		    else {
			$lease = Lease->Lookup($pid, $id);
		    }
		    # We cannot generate a credential for "legacy" datasets.
		    # So if it is not global, it cannot be transferred. Maybe
		    # this is okay, we will find out. We could generate a
		    # credential if we needed to.
		    next
			if ($image || $lease);
1099
		}
1100 1101
		$$pmsg = "Dataset '$dataset_urn' does not exist";
		return 1;
1102
	    }
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
	    #
	    # We do not need a credential for leases, only real users
	    # can use those, and so standard emulab permission checks
	    # are applied at the CM.
	    #
	    next
		if ($dataset->type() ne "imdataset");

	    #
	    # For image backed datasets, we need to send along a credential
	    # that allows the remote CM to securely download the dataset if
	    # it does not already have it. To do that we need to send it a
	    # credential from the CM where the dataset lives. We do that by
	    # requesting a credential, and delegating it to the target CM.
	    #
	    my $pid = $dataset->pid();
	    my $id  = $dataset->dataset_id();
	    my $cmd = "$MANAGEDATASET getcredential -a $manager_urn $pid/$id";
	    my $credential = `$cmd`;
	    if ($?) {
		$$pmsg = "Could not generate credential for $dataset_urn";
1124 1125
		return -1;
	    }
1126
	    push(@credentials, $credential);
1127 1128 1129 1130 1131 1132
	}
    }
    @$pref = @credentials;
    return 0;
}

1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
#
# 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->_authority();
    my $cmurl     = $authority->url();
    my $urn       = $authority->urn();
    $webtask->Refresh();

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

1152 1153
    Genixmlrpc->SetTimeout(900);

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
    #
    # This creates the sliver and starts it. We have to watch for the
    # server being too busy.
    #
    my $tries = 15;
    my $response;

    while (1) {
	$response =
	    Genixmlrpc::CallMethod($cmurl, undef,
				   "CreateSliver",
				   { "slice_urn"   => $slice_urn,
				     "rspec"       => $rspecstr,
1167
				     "keys"        => $sshkeys,
1168
				     "credentials" =>
1169 1170 1171
					 [$slice_credential->asString(),
					  $speaksfor_credential->asString(),
					  @dataset_credentials
1172
					 ],
1173
				     "certificate" => $alt_certificate->cert(),
1174 1175
				     "key"         =>
				        $alt_certificate->PrivKeyDelimited(),
1176
				     "usetracker"  => $usetracker,
1177
				   });
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

	if (!defined($response) || $response->code() != GENIRESPONSE_SUCCESS) {
	    if (defined($response) &&
		$response->code() == GENIRESPONSE_SERVER_UNAVAILABLE &&
		$tries >= 0) {
		print STDERR "Server for $urn reports too busy, ".
		    "waiting a while ...\n";
		sleep(int(rand(20)) + 10);
		$tries--;
		next;
	    }
	    if (defined($response)) {
		$webtask->output($response->output());
		$webtask->Exited($response->code());
	    }
	    else {
1194
		$webtask->Exited(-1);
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
	    }
	    $aggobj->SetStatus("failed");
	    
	    if (defined($response) && defined($response->logurl())) {
		$aggobj->SetPublicURL($response->logurl());
	    }
	    print STDERR "CreateSliver failed on $urn: ".
		(defined($response) ? $response->output() : "") . "\n";
	    return -1;
	}
	last;
    }
    # This will get overwritten later.
    if (defined($response) && defined($response->logurl())) {
	$aggobj->SetPublicURL($response->logurl());
    }
    my $manifest = $response->value()->[1];
    if (!defined($manifest)) {
1213
	$webtask->Exited(-1);
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	$aggobj->SetStatus("failed");
	print STDERR "CreateSliver $urn: No manifest returned\n";
	return -1;
    }
    $aggobj->SetStatus("provisioned");
    $aggobj->SetManifest($manifest);
    return 0;
}

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

Leigh Stoller's avatar
Leigh Stoller committed
1227 1228
    $instance->SetStatus("provisioning");
    
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
    my @return_codes = ();
    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.
	#
	$slice->UnLock();
	$instance->SetStatus("failed");
	return -1;
    }
    #
    # 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.
	# Need the manifests for the call to ComputeNodeCounts below.
	#
	$aggobj->Refresh();
	my $code = shift(@return_codes);
	if ($code) {
	    $slice->UnLock();
1253 1254 1255 1256
	    # Promote the log up to the instance so that so its easy to find.
	    $instance->SetPublicURL($aggobj->public_url())
		if (defined($aggobj->public_url()));
	    # Ditto the error output.
1257 1258
	    $webtask->output($aggobj->webtask()->output())
		if (defined($aggobj->webtask()->output()));
1259 1260 1261
	    # This will be the createsliver exit code if we got one, or -1.
	    $code = $aggobj->webtask()->exitcode()
		if (defined($aggobj->webtask()->exitcode()));
1262 1263 1264
	    # Do this last so that the web interface does not see failed
	    # before the reason is in the webtask.
	    $instance->SetStatus("failed");
1265
	    return $code;
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
	}
    }
    return 0;
}

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

    #
1290
    # The AM API uses a different ssh key structure.
1291
    #
1292 1293
    my $users = [];
    foreach my $user (@{$sshkeys}) {
1294 1295
	my @tmp = map { $_->{'key'} } @{$user->{'keys'}};
	
1296 1297
	push(@{$users},
	     {"urn"  => $user->{'urn'},
1298
	      "keys" => [ @tmp ] });
1299
    }
1300 1301 1302 1303 1304 1305

    #
    # Hey, I think stitcher/omni has as many options as snmpit. Wow!
    #
    my $command = "$STITCHER --fileDir $tmpdir --cred $speaksforfile ".
	"--slicecredfile $slicecredfile --usercredfile $slicecredfile ".
1306 1307 1308
	($speaksfor_credential->type() eq "speaksfor" ?
	 "--al2scredfile $al2scredfile " : "") .
	"--debug ".
Leigh Stoller's avatar
Leigh Stoller committed
1309 1310 1311
	# We do not want these two files defaulting to user home dir.
	"--GetVersionCacheName=$tmpdir/get_version_cache.json ".
	"--AggNickCacheName=$tmpdir/agg_nick_cache ".
1312
	"--scsURL https://nutshell.maxgigapop.net:8443/geni/xmlrpc  ".
1313
	"--speaksfor $speaksfor_urn -V3 allocate $slice_urn $rspecfile";
1314 1315 1316 1317 1318

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

    print "Using $tmpdir for stitcher\n"
1319
	if ($debug || $verbose);
1320
    print "Stitcher command: $command\n"
1321
	if ($debug || $verbose);
1322 1323 1324 1325 1326

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

    goto bad
Leigh Stoller's avatar
Leigh Stoller committed
1327 1328 1329 1330
	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"));
1331 1332 1333 1334 1335 1336 1337 1338

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

1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
    #
    # 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();
1350
	$logfile->SetPublic(1);
1351 1352 1353
	$instance->SetLogFile($logfile);
    }

1354 1355 1356 1357
    #
    # Okay, run the stitcher. Only to allocate, we will do the provisions
    # so that we can pass the ssh keys more easily.
    #
Leigh Stoller's avatar
Leigh Stoller committed
1358
    $instance->SetStatus("stitching");
1359
    system("cd $tmpdir; $command > $stdoutfile 2> $stderrfile");
1360
    if ($?) {
1361
	$errcode = $? >> 8;
1362
	$errcode = GENIRESPONSE_STITCHER_ERROR() if ($errcode == 1);
1363
	
1364
	if (-s $stderrfile) {
1365
	    my $stuff = `cat $stderrfile`;
1366 1367 1368 1369 1370 1371 1372 1373 1374 1375

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

			if ($_ =~ /protogeni AM code: (\d*):/) {
			    $errcode = $1;
			}
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
		    }
		    elsif (defined($morestuff)) {
			if ($_ =~ /^Allocate slivers in slice .* failed/) {
			    last;
			}
			$morestuff .= $_;
		    }
		}
		close(SLOG);
		$stuff .= "\n\n" . $morestuff
		    if (defined($morestuff));
	    }
1392
	    $webtask->output($stuff);
1393
	    print $morestuff . "\n\n" if (defined($morestuff));
1394 1395 1396 1397 1398
	    system("/bin/cat $stderrfile");
	}
	else {
	    $webtask->output("Stitcher failed!");
	}
1399 1400 1401 1402
	#
	# 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.
1403
	# 
1404 1405
	$failed = 1;
    }
1406 1407 1408 1409
    if (defined($logfile)) {
	$logfile->Close();
	$logfile->Store();
    }
1410 1411 1412 1413 1414 1415 1416 1417
    
    #
    # 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;
1418
    my $al2smanifest;
1419 1420 1421 1422 1423 1424 1425
    opendir(DIR, $tmpdir);
    my @files  = readdir(DIR);
    closedir(DIR);

    foreach my $file (@files) {
	if ($file =~ /amlist.txt$/) {
	    $agglistfile = "$tmpdir/$file";
1426 1427 1428
	}
	elsif ($file =~ /manifest-rspec-al2s/) {
	    $al2smanifest = "$tmpdir/$file";
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
	}
    }
    #
    # 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);
	$aggobj->_authority($authority);
	push(@aggregate_list, $aggobj);
    }
    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->_authority();
	my $urn       = $authority->urn();
	my $errmsg    = "Provision failed on $urn";
	$webtask->Refresh();

1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
	#
	# 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(-1);
		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(-1);
		return -1;
	    }
	    while (<MAN>) {
		$manifest_string .= $_;
	    }
	    close(MAN);
	    $aggobj->SetManifest($manifest_string);
	    $aggobj->SetStatus("provisioned");
1512
	    print STDERR $manifest_string . "\n";
1513 1514
	    return 0;
	}
1515
	print "Provisioning at $urn\n";
1516
	if ($aggobj->Provision(\$errmsg, $users,
1517
			       $alt_certificate->cert(),
1518
			       $alt_certificate->PrivKeyDelimited())) {
1519 1520 1521 1522 1523 1524
	    $aggobj->SetStatus("failed");
	    $webtask->output($errmsg);
	    $webtask->Exited(-1);
	    print STDERR "Provision failed on $urn: $errmsg\n";
	    return -1;
	}
Leigh Stoller's avatar
Leigh Stoller committed
1525
	$aggobj->SetStatus("provisioned");
1526 1527 1528 1529 1530 1531 1532 1533
	print "Requesting manifest from $urn\n";
	my $manifest = $aggobj->GetManifest();
	if (!defined($manifest)) {
	    $aggobj->SetStatus("failed");
	    $webtask->output("Could not get manifest from $urn");
	    $webtask->Exited(-1);
	    return -1;
	}
1534 1535 1536 1537
	# Web interface wants this as soon as possible.
	$aggobj->SetManifest($manifest);
	
	print "Forcing correct slice expiration\n";