APT_Instance.pm.in 86.3 KB
Newer Older
Leigh B Stoller's avatar
Leigh B Stoller committed
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2007-2018 University of Utah and the Flux Group.
Leigh B Stoller's avatar
Leigh B Stoller committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
package APT_Instance;

use strict;
use Carp;
28 29
use English;
use Data::Dumper;
30
use Date::Parse;
31
use File::Temp qw(tempfile tmpnam);
32
use JSON;
Leigh B Stoller's avatar
Leigh B Stoller committed
33
use Exporter;
34
use vars qw(@ISA @EXPORT $AUTOLOAD
35 36 37
	    $RECORDHISTORY_TERMINATED $RECORDHISTORY_EXPIRED
	    $APT_HOLDINGPROJECT $CLOUD_HOLDINGPROJECT);

Leigh B Stoller's avatar
Leigh B Stoller committed
38
@ISA    = "Exporter";
39 40 41 42 43

# RecordHistory flags.
$RECORDHISTORY_TERMINATED	= 0x00;
$RECORDHISTORY_EXPIRED		= 0x01;

44 45 46 47 48 49
# Names of the holding projects.
$APT_HOLDINGPROJECT   = "aptguests";
$CLOUD_HOLDINGPROJECT = "CloudLab";

@EXPORT = qw($RECORDHISTORY_TERMINATED $RECORDHISTORY_EXPIRED
             $APT_HOLDINGPROJECT $CLOUD_HOLDINGPROJECT);
Leigh B Stoller's avatar
Leigh B Stoller committed
50 51 52 53 54

# Must come after package declaration!
use EmulabConstants;
use emdb;
use libtestbed;
55
use Brand;
56
use APT_Profile;
57
use APT_Aggregate;
58 59 60 61
use APT_Geni;
use Genixmlrpc;
use GeniResponse;
use GeniCertificate;
Leigh B Stoller's avatar
Leigh B Stoller committed
62
use GeniCredential;
63
use GeniUser;
64
use GeniHRN;
65 66
use GeniXML;
use WebTask;
67
use Logfile;
Leigh B Stoller's avatar
Leigh B Stoller committed
68 69 70 71 72
use overload ('""' => 'Stringify');

# Configure variables
my $TB		  = "@prefix@";
my $TBOPS         = "@TBOPSEMAIL@";
73
my $TBAUDIT       = "@TBAUDITEMAIL@";
74
my $GENEXTENDCRED = "$TB/sbin/protogeni/genextendcred";
75
my $GENIUSER      = "geniuser";
76
my $MAINSITE      = @TBMAINSITE@;
Leigh B Stoller's avatar
Leigh B Stoller committed
77 78 79

# Cache of instances to avoid regenerating them.
my %instances  = ();
80
BEGIN { use emutil; emutil::AddCache(\%instances); }
Leigh B Stoller's avatar
Leigh B Stoller committed
81 82
my $debug      = 0;

83
# Debugging
Leigh B Stoller's avatar
Leigh B Stoller committed
84
my $usemydevtree  = 0;
85 86 87 88 89 90
sub devurl($)
{
    my ($cmurl) = @_;

    if ($usemydevtree) {
	$cmurl =~ s/protogeni/protogeni\/stoller/;
91
#	$cmurl =~ s/12369/12396/;
92 93 94
    }
    return $cmurl;
}
95

Leigh B Stoller's avatar
Leigh B Stoller committed
96 97 98
#
# Lookup by uuid.
#
99
sub Lookup($$)
Leigh B Stoller's avatar
Leigh B Stoller committed
100
{
101 102
    my ($class, $token) = @_;
    my $query_result;
Leigh B Stoller's avatar
Leigh B Stoller committed
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    if ($token =~ /^([-\w]*),([-\w]*)$/ ||
	$token =~ /^([-\w]*)\/([-\w]*)$/) {

	$query_result =
	    DBQueryWarn("select uuid from apt_instances ".
			"where pid='$1' and name='$2'");
	return undef
	    if (! (defined($query_result) && $query_result->numrows));

	my ($uuid) = $query_result->fetchrow_array();
	
	# Look in cache first
	return $instances{$uuid}
	    if (exists($instances{$uuid}));
	
	$query_result =
	    DBQueryWarn("select * from apt_instances where uuid='$uuid'");
    }
    elsif ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
123 124 125
	# Look in cache first
	return $instances{$token}
            if (exists($instances{$token}));
Leigh B Stoller's avatar
Leigh B Stoller committed
126

127 128 129 130 131 132
	$query_result =
	    DBQueryWarn("select * from apt_instances where uuid='$token'");
    }
    else {
	return undef;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
133 134 135
    return undef
	if (!$query_result || !$query_result->numrows);

136 137 138 139
    my $self              = {};
    $self->{'INSTANCE'}   = $query_result->fetchrow_hashref();
    $self->{'BRAND'}      = Brand->Create($self->{'INSTANCE'}->{'servername'});
    $self->{'HASH'}       = {};
140
    $self->{'AGGREGATES'} = {};
141
    $self->{'WEBTASK'}    = undef;
Leigh B Stoller's avatar
Leigh B Stoller committed
142
    bless($self, $class);
143 144 145 146

    #
    # Lookup existing aggregates.
    #
147 148
    my $aggregates = APT_Instance::Aggregate->LookupForInstance($self);
    if (!keys(%{$aggregates}) && defined($self->aggregate_urn())) {
149 150 151 152
	#
	# Make up a fake one; eventually the old ones will die or
	# I will create entries for them. Not worrying about it now.
	#
153 154
	$aggregates = {$self->aggregate_urn() =>
			   APT_Instance::Aggregate->GenTemp($self)};
155
    }
156
    $self->{'AGGREGATES'} = $aggregates;
Leigh B Stoller's avatar
Leigh B Stoller committed
157 158
    
    # Add to cache. 
159
    $instances{$self->uuid()} = $self;
Leigh B Stoller's avatar
Leigh B Stoller committed
160 161 162 163 164 165
    
    return $self;
}

AUTOLOAD {
    my $self  = $_[0];
166
    my $type  = ref($self) or confess "$self is not an object";
Leigh B Stoller's avatar
Leigh B Stoller committed
167 168 169 170 171 172 173
    my $name  = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    # A DB row proxy method call.
    if (exists($self->{'INSTANCE'}->{$name})) {
	return $self->{'INSTANCE'}->{$name};
    }
174 175 176 177 178 179 180 181 182
    # Or it is for a local storage slot.
    if ($name =~ /^_.*$/) {
	if (scalar(@_) == 2) {
	    return $self->{'HASH'}->{$name} = $_[1];
	}
	elsif (exists($self->{'HASH'}->{$name})) {
	    return $self->{'HASH'}->{$name};
	}
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
183 184 185
    carp("No such slot '$name' field in class $type");
    return undef;
}
186 187 188
sub Brand($)	{ return $_[0]->{'BRAND'}; }
sub isAPT($)	{ return $_[0]->Brand()->isAPT() ? 1 : 0; }
sub isCloud($)	{ return $_[0]->Brand()->isCloud() ? 1 : 0; }
189
sub isPNet($)	{ return $_[0]->Brand()->isPNet() ? 1 : 0; }
190
sub isPowder($)	{ return $_[0]->Brand()->isPowder() ? 1 : 0; }
191 192
sub AggregateList($) { return values(%{ $_[0]->{'AGGREGATES'} }); }
sub AggregateHash($) { return $_[0]->{'AGGREGATES'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
193

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
#
# Grab the webtask. Backwards compat mode, see if there is one associated
# with the object, use that. Otherwise create a new one.
#
sub webtask($)
{
    my ($self) = @_;
    my $webtask;

    return $self->{'WEBTASK'}
        if (defined($self->{'WEBTASK'}));

    if (defined($self->webtask_id())) {
	$webtask = WebTask->Lookup($self->webtask_id());
    }
    if (!defined($webtask)) {
	$webtask = WebTask->LookupByObject($self->uuid());
	if (!defined($webtask)) {
	    $webtask = WebTask->Create();
	    return undef
		if (!defined($webtask));
	}
	$self->Update({"webtask_id" => $webtask->task_id()}) == 0
	    or return undef;
    }
    $self->{'WEBTASK'} = $webtask;
    return $webtask;
}

Leigh B Stoller's avatar
Leigh B Stoller committed
223 224 225 226
# Break circular reference someplace to avoid exit errors.
sub DESTROY {
    my $self = shift;

227 228 229 230
    $self->{'INSTANCE'}   = undef;
    $self->{'BRAND'}      = undef;
    $self->{'AGGREGATES'} = undef;
    $self->{'HASH'}       = undef;
231
    $self->{'WEBTASK'}    = undef;
Leigh B Stoller's avatar
Leigh B Stoller committed
232 233
}

234 235 236 237 238 239 240
#
# Flush from our little cache, as for the expire daemon.
#
sub Flush($)
{
    my ($self) = @_;

241 242 243 244 245 246 247 248 249 250 251
    delete($instances{$self->uuid()});
}
#
# We use this in long running daemons, since we have a circular dependency
# that causes the perl GC to leak memory. Normally not a problem, but the
# aptevent_daemon runs for a long time! 
#
sub Purge($)
{
    my ($self) = @_;

252 253
    #
    # Break circular dependency so that the object is released
254
    # (the aggregate objects point back to the instance).
255
    #
256 257
    $self->{'AGGREGATES'} = undef;
    $self->Flush();
258 259
}

Leigh B Stoller's avatar
Leigh B Stoller committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
#
# Refresh a class instance by reloading from the DB.
#
sub Refresh($)
{
    my ($self) = @_;

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

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

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

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

    return 0;
}

#
# Create an Instance
#
286
sub Create($$$)
Leigh B Stoller's avatar
Leigh B Stoller committed
287
{
288
    my ($class, $argref, $perrmsg) = @_;
Leigh B Stoller's avatar
Leigh B Stoller committed
289 290 291 292 293 294 295 296 297
    my $uuid;

    if (exists($argref->{'uuid'})) {
	$uuid = $argref->{'uuid'};
	delete($argref->{'uuid'});
    }
    else {
	$uuid = NewUUID();
    }
298 299
    my $pid_idx = $argref->{'pid_idx'};
    my $name    = $argref->{'name'};
Leigh B Stoller's avatar
Leigh B Stoller committed
300 301 302 303 304 305 306 307 308 309 310 311

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

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

    if ($query_result->numrows) {
	DBQueryWarn("unlock tables");
312 313 314 315 316 317 318 319 320 321
	$$perrmsg = "Instance uuid $uuid already exists!";
	return undef;
    }
    $query_result =
	DBQueryWarn("select name from apt_instances ".
		    "where name='$name' and pid_idx='$pid_idx'");

    if ($query_result->numrows) {
	DBQueryWarn("unlock tables");
	$$perrmsg = "Instance name already in use!";
Leigh B Stoller's avatar
Leigh B Stoller committed
322 323 324 325 326 327 328 329 330
	return undef;
    }

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

    if (! DBQueryWarn($query)) {
	DBQueryWarn("unlock tables");
331
	$$perrmsg = "Error inserting new apt_instance record for $uuid!";
Leigh B Stoller's avatar
Leigh B Stoller committed
332 333 334 335 336 337 338 339 340 341 342 343 344 345
	return undef;
    }
    DBQueryWarn("unlock tables");
    return Lookup($class, $uuid);
}

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $uuid  = $self->uuid();
346 347
    my $pid   = $self->pid();
    my $name  = $self->name();
Leigh B Stoller's avatar
Leigh B Stoller committed
348

349
    return "[APT_Instance: $pid,$name]";
Leigh B Stoller's avatar
Leigh B Stoller committed
350 351
}

352 353
sub LookupBySlice($$)
{
354 355
    my ($class, $token) = @_;
    my $slice_uuid;
356

357 358 359 360 361 362 363 364 365
    if ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	$slice_uuid = $token;
    }
    elsif (GeniHRN::IsValid($token)) {
	#
	# We should put the slice_urn into the apt_instances table.
	#
	require GeniSlice;
	my $slice = GeniSlice->Lookup($token);
366
	return undef
367 368
	    if (!defined($slice));
	$slice_uuid = $slice->uuid();
369
	$slice->Flush();
370 371 372
    }
    else {
	return undef;
373
    }
374

375 376 377
    my $query_result =
	DBQueryWarn("select uuid from apt_instances ".
		    "where slice_uuid='$slice_uuid'");
378
	
379 380 381 382
    return undef
	if (!$query_result || !$query_result->numrows);

    my ($uuid) = $query_result->fetchrow_array();
383 384
    my $foo = APT_Instance->Lookup($uuid);
    return $foo;
385 386
}

Leigh B Stoller's avatar
Leigh B Stoller committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400
#
# Perform some updates ...
#
sub Update($$)
{
    my ($self, $argref) = @_;

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

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

    my $query = "update apt_instances set ".
401 402 403 404
	join(",", map("$_=" .
		      (defined($argref->{$_}) ?
		       DBQuoteSpecial($argref->{$_}) : "NULL"),
		      keys(%{$argref})));
Leigh B Stoller's avatar
Leigh B Stoller committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418

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

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

    return Refresh($self);
}

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

419
    foreach my $agg ($self->AggregateList()) {
420 421 422
	$agg->Delete() == 0
	    or return -1;
    }
423 424
    # We do not "own" the webtask until create_sliver exits successfully.
    $self->webtask()->Delete() if (defined($self->{'WEBTASK'}));
Leigh B Stoller's avatar
Leigh B Stoller committed
425 426 427 428 429 430
    DBQueryWarn("delete from apt_instances where uuid='$uuid'") or
	return -1;

    return 0;
}

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
#
# Lock and unlock operate on the underlying slice.
#
sub Lock($)
{
    my ($self) = @_;
    my $slice  = $self->GetGeniSlice();
    if (!defined($slice)) {
	return -1;
    }
    return $slice->Lock();
}
sub Unlock($)
{
    my ($self) = @_;
    my $slice  = $self->GetGeniSlice();
    if (!defined($slice)) {
	return -1;
    }
    return $slice->UnLock();
}
452 453 454 455 456 457 458 459 460
sub Locked($)
{
    my ($self) = @_;
    my $slice  = $self->GetGeniSlice();
    if (!defined($slice)) {
	return 0;
    }
    return $slice->locked();
}
461

Leigh B Stoller's avatar
Leigh B Stoller committed
462 463 464 465 466 467 468 469 470 471
sub SetStatus($$)
{
    my ($self,$status) = @_;

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

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

472 473 474 475
    DBQueryWarn("update apt_instances set ".
		"  status='$status',status_timestamp=now() ".
		"where uuid='$uuid'")
	or return -1;
Leigh B Stoller's avatar
Leigh B Stoller committed
476 477 478 479

    $self->{'INSTANCE'}->{'status'} = $status;
    return 0;
    
Leigh B Stoller's avatar
Leigh B Stoller committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
}
sub SetPublicURL($$)
{
    my ($self,$url) = @_;
    my $safe_url = DBQuoteSpecial($url);

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

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

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

    $self->{'INSTANCE'}->{'public_url'} = $url;
    return 0;
    
Leigh B Stoller's avatar
Leigh B Stoller committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
}
sub SetManifest($$)
{
    my ($self,$manifest) = @_;

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

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

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

    $self->{'INSTANCE'}->{'manifest'} = $manifest;
    return 0;
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
sub MarkCanceled($)
{
    my ($self) = @_;

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

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

    DBQueryWarn("update apt_instances set ".
		"  canceled=1,canceled_timestamp=now() ".
		"where uuid='$uuid'")
	or return -1;

    $self->{'INSTANCE'}->{'canceled'} = 1;
    return 0;
}
# We need to bypass the in memory state for this.
sub IsCanceled($)
{
    my ($self) = @_;
    my $uuid = $self->uuid();

    my $query_result =
	DBQueryWarn("select canceled from apt_instances ".
		    "where uuid='$uuid'");
	
    return -1
	if (!$query_result);
    return 0
	if (!$query_result->numrows);
    my ($canceled) = $query_result->fetchrow_array();
    return $canceled;
}

555 556 557 558 559 560 561 562 563 564 565 566 567
#
# Set to use the logfile. 
#
sub SetLogFile($$)
{
    my ($self, $logfile) = @_;

    return -1
	if ($self->Update({'logfileid' => $logfile->logid()}));

    return 0;
}

568 569 570 571 572
#
# Set/Clear the lockdown bits.
#
sub SetLockdown($$$)
{
573
    my ($self,$admin,$user) = @_;
574 575 576 577 578 579

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

    my $uuid = $self->uuid();
580 581
    $admin = ($admin ? 1 : 0);
    $user  = ($user ? 1 : 0);
582

583 584
    DBQueryWarn("update apt_instances set ".
		" admin_lockdown='$admin',user_lockdown='$user' ".
585 586 587
		"where uuid='$uuid'") or
	return -1;

588 589
    $self->{'INSTANCE'}->{"admin_lockdown"} = $admin;
    $self->{'INSTANCE'}->{"user_lockdown"}  = $user;
590 591 592
    return 0;
}

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
#
# Set/Clear the panic flag.
#
sub SetPanic($$)
{
    my ($self,$clear) = @_;

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

    my $uuid  = $self->uuid();
    my $value = ($clear ? 0 : 1);

    DBQueryWarn("update apt_instances set paniced=${value} ".
		(!$clear ? ",paniced_timestamp=now() " : "") .
		"where uuid='$uuid'") or
	return -1;

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

616 617 618 619 620 621 622 623
# Return a URL for the status page.
sub webURL($)
{
    my ($self) = @_;
    my $link = $self->Brand()->wwwBase();
    $link    = $link . "/status.php?uuid=" . $self->uuid();

    return $link;
Leigh B Stoller's avatar
Leigh B Stoller committed
624
}
625 626 627 628 629 630 631 632
sub adminURL($)
{
    my ($self) = @_;
    my $link = $self->Brand()->wwwBase();
    $link    = $link . "/adminextend.php?uuid=" . $self->uuid();

    return $link;
}
Leigh B Stoller's avatar
Leigh B Stoller committed
633

634 635 636 637 638 639 640 641 642 643 644
#
# Find the profile for this instance.
#
sub Profile($)
{
    my ($self) = @_;
    
    return APT_Profile->Lookup($self->profile_id(),
			       $self->profile_version());
}

645 646 647 648
#
# Record a history entry.
# We do this when the instance is terminated. 
#
649
sub RecordHistory($$)
650
{
651
    my ($self, $flags) = @_;
652
    my $uuid = $self->uuid();
653
    my $expired = ($flags ? 1 : 0);
654

655 656 657 658
    DBQueryWarn("replace into apt_instance_aggregate_history ".
		"select * from apt_instance_aggregates where uuid='$uuid'")
	or return -1;

659
    DBQueryWarn("replace into apt_instance_history ".
660
		"select uuid,name,profile_id,profile_version,slice_uuid, ".
661
		"       creator,creator_idx,creator_uuid,pid,pid_idx, ".
662
		"       gid,gid_idx, ".
663 664
		"       aggregate_urn,public_url,logfileid, ".
		"       created,now(),$expired, ".
665
		"       extension_count,extension_days,extension_hours, ".
666
		"       physnode_count,virtnode_count, ".
667 668
		"       servername,repourl,reponame,reporef,repohash, ".
		"       rspec,script,params,manifest ".
669 670 671 672 673 674
		" from apt_instances where uuid='$uuid'")
	or return -1;

    return 0;
}

675 676 677 678 679 680 681 682 683
sub RecordError($$$)
{
    my ($self, $exitcode, $message) = @_;
    my $uuid = $self->uuid();
    my $safe_message = DBQuoteSpecial($message);

    DBQueryWarn("replace into apt_instance_failures ".
		"select uuid,name,profile_id,profile_version,slice_uuid, ".
		"       creator,creator_idx,creator_uuid,pid,pid_idx, ".
684
		"       gid,gid_idx, ".
685 686
		"       created,'$exitcode',$safe_message,public_url, ".
		"       logfileid ".
687 688 689 690 691 692
		" from apt_instances where uuid='$uuid'")
	or return -1;

    return 0;
}

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

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

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

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

720 721 722 723 724 725 726 727
sub GetProject($)
{
    my ($self) = @_;
    require Project;
    
    return Project->Lookup($self->pid_idx());
}

728 729 730 731 732 733 734 735
sub GetGroup($)
{
    my ($self) = @_;
    require Group;
    
    return Group->Lookup($self->gid_idx());
}

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

    my $slice     = GeniSlice->Lookup($self->slice_uuid());;
    if (!defined($slice)) {
	print STDERR "WarnExpiring: No slice for $self\n";
	return -1;
    }
    my $slicename = $slice->slicename();
    my $geniuser  = GeniUser->Lookup($slice->creator_uuid(), 1);
    if (!defined($geniuser)) {
	print STDERR "WarnExpiring: No creator for $self\n";
	return -1;
    }
    if (!defined($geniuser->email())) {
	print STDERR "WarnExpiring: no email address for $geniuser\n";
	return 0;
    }
762
    my $link = $self->webURL();
763 764 765 766 767 768 769 770 771
    
    $self->Brand()->SendEmail($geniuser->email(),
		     "Your experiment is expiring soon!",
		     "Your experiment $slicename is going to expire in ${when}.\n".
		     "If you want to extend this experiment, please go to:\n\n".
		     "    $link\n\n".
		     "and click on the Extend button.\n".
		     "\n".
		     "Thanks!",
772
		     $self->Brand()->LogsEmailAddress());
773 774 775 776
    
    return 0;
}

777 778 779
#
# Go through all the manifests and count up nodes.
#
780 781 782 783 784
sub ComputeNodeCounts($)
{
    my ($self) = @_;
    my $physnode_count = 0;
    my $virtnode_count = 0;
785
    my @slivers = $self->AggregateList();
786 787
    if (!@slivers) {
	print STDERR "No slivers for $self\n";
788 789
	return -1;
    }
790 791 792 793 794 795
    foreach my $sliver (@slivers) {
	my $manifest = GeniXML::Parse($sliver->manifest());
	if (! defined($manifest)) {
	    print STDERR "Could not parse manifest for $sliver\n";
	    return -1;
	}
796 797 798
	my $pcount = 0;
	my $vcount = 0;
	
799
	foreach my $ref (GeniXML::FindNodes("n:node",
800 801 802 803
					    $manifest)->get_nodelist(),
			 GeniXML::FindNodesNS("n:vhost",
				      $manifest,
				      $GeniXML::EMULAB_NS)->get_nodelist()) {
804
	    my $virtualization_type = GeniXML::GetVirtualizationSubtype($ref);
805 806 807 808 809 810
	    my $manager_urn = GetManagerId($ref);

	    # Combined rspec.
	    next
		if (!defined($manager_urn) ||
		    $manager_urn ne $sliver->aggregate_urn());
811 812

	    if (defined($virtualization_type) &&
813 814
		($virtualization_type eq "emulab-xen" ||
		 $virtualization_type eq "emulab-blockstore")) {
815
		$vcount++;
816 817
		next;
	    }
818
	    $pcount++;
819
	}
820 821 822 823 824 825
	$sliver->Update({"physnode_count" => $pcount,
			 "virtnode_count" => $vcount})
	    == 0 or return -1;
	    
	$physnode_count += $pcount;
	$virtnode_count += $vcount;
826 827 828 829 830 831 832
    }
    $self->Update({"physnode_count" => $physnode_count,
		   "virtnode_count" => $virtnode_count})
	== 0 or return -1;
    return 0;
}

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
#
# Go through all the manifests and return a list of nodes.
#
sub GetNodeDetails($)
{
    my ($self) = @_;
    my $rval   = {};

    my @slivers = $self->AggregateList();
    if (!@slivers) {
	print STDERR "No slivers for $self\n";
	return undef;
    }
    foreach my $sliver (@slivers) {
	my $manifest = GeniXML::Parse($sliver->manifest());
	if (! defined($manifest)) {
	    print STDERR "Could not parse manifest for $sliver\n";
	    return undef;
	}
	my $nodes = {};
	
	foreach my $ref (GeniXML::FindNodes("n:node",
					    $manifest)->get_nodelist(),
			 GeniXML::FindNodesNS("n:vhost",
				      $manifest,
				      $GeniXML::EMULAB_NS)->get_nodelist()) {
	    my $virtualization_type = GeniXML::GetVirtualizationSubtype($ref);
	    my $manager_urn = GetManagerId($ref);
	    my $isvnode = 0;

	    # Combined rspec.
	    next
		if (!defined($manager_urn) ||
		    $manager_urn ne $sliver->aggregate_urn());

	    if (defined($virtualization_type) &&
		($virtualization_type eq "emulab-xen" ||
		 $virtualization_type eq "emulab-blockstore")) {
		$isvnode = 1;
	    }
	    my $client_id = GetVirtualId($ref);
	    my $node_id   = GetVnodeId($ref);
	    if (GeniHRN::IsValid($node_id)) {
		my $hrn = GeniHRN->new($node_id);
		if (!$hrn->IsNode()) {
		    print STDERR "$node_id is not a node\n";
		    return undef;
		}
		$node_id = $hrn->id();
	    }
	    $nodes->{$node_id} = {
		"client_id"  => $client_id,
		"isvirtnode" => $isvnode,
	    };
	}
	$rval->{$sliver->aggregate_urn()} = $nodes;
    }
    return $rval;
}

893
#
894
# Add an aggregate to an instance.
895
#
896
sub AddAggregate($$)
Leigh B Stoller's avatar
Leigh B Stoller committed
897
{
898 899 900
    my ($self, $aggregate_urn) = @_;

    my $aggobj = APT_Instance::Aggregate->Create($self, $aggregate_urn);
Leigh B Stoller's avatar
Leigh B Stoller committed
901
    return undef
902
	if (!defined($aggobj));
Leigh B Stoller's avatar
Leigh B Stoller committed
903

904
    $self->{'AGGREGATES'}->{$aggregate_urn} = $aggobj;
905 906 907 908 909 910 911 912 913
    return $aggobj;
}

#
# Locate the aggregate for a nodeid
#
sub FindAggregateByNodeId($$)
{
    my ($self, $node_id) = @_;
914
    my @slivers = $self->AggregateList();
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
    if (!@slivers) {
	return undef;
    }
    foreach my $sliver (@slivers) {
	my $manifest = GeniXML::Parse($sliver->manifest());
	if (! defined($manifest)) {
	    print STDERR "Could not parse manifest for $sliver\n";
	    return -1;
	}
	foreach my $ref (GeniXML::FindNodes("n:node",
					    $manifest)->get_nodelist()) {
	    my $client_id = GeniXML::GetVirtualId($ref);
	    if (defined($client_id) && $client_id eq $node_id) {
		if (wantarray) {
		    return ($sliver, $manifest);
		}
		return $sliver;
	    }
	}
    }
    return undef;
}

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
sub WriteCredentials($$)
{
    my ($self, $directory) = @_;
    my $geniuser  = $self->GetGeniUser();
    my $slice     = $self->GetGeniSlice();
    my $context   = APT_Geni::GeniContext();

    if (! (defined($geniuser) && defined($slice))) {
	fatal("Could not lookup geniuser or slice");
    }

    my ($slice_credential, $speaksfor_credential) =
	APT_Geni::GenCredentials($slice, $geniuser);
    if (! (defined($speaksfor_credential) &&
	   defined($slice_credential))) {
	fatal("Could not create credentials");
    }

    my $credfile = "$directory/slicecred.xml";
    unlink($credfile)
	if (-e $credfile);
    open(XML, ">$credfile") or
	fatal("Could not create $credfile");
    print XML $slice_credential->asString();
    close(XML);
    
    $credfile = "$directory/speaksforcred.xml";
    unlink($credfile)
	if (-e $credfile);
    open(XML, ">$credfile") or
	fatal("Could not create $credfile");
    print XML $speaksfor_credential->asString();
    close(XML);
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988

    #
    # We do not generally need this, but might as well generate it,
    # since we do not easily know if the stitcher is going to contact
    # the AL2S aggregate, which does not support speaksfor.
    #
    $credfile = "$directory/al2scred.xml";
    my $authcred = APT_Geni::GenAuthCredential($slice);
    if (!defined($authcred)) {
	print STDERR "Could not general auth cred!\n";
	return -1;
    }
    unlink($credfile)
	if (-e $credfile);
    open(XML, ">$credfile") or
	fatal("Could not create $credfile");
    print XML $authcred->asString();
    close(XML);
989 990 991 992
    
    return 0;
}

993 994 995
#
# Build a set of sshkeys.
#
996
sub GetSSHKeys($$;$)
997
{
998
    my ($self, $pref, $target_user) = @_;
999 1000 1001
    my $rval;
    my @keys;

1002
    my $geniuser = $target_user || $self->GetGeniUser();
1003 1004 1005 1006 1007
    return -1
	if (!defined($geniuser));
    my $project  = $self->GetProject();
    return -1
	if (!defined($project));
1008 1009 1010
    my $group = $self->GetGroup();
    return -1
	if (!defined($group));
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

    if ($geniuser->GetKeyBundle(\@keys, 1) < 0 || !@keys) {
	print STDERR "No ssh keys for $geniuser\n";
	return -1;
    }

    #
    # CM format.
    #
    $rval = [{'urn'   => $geniuser->urn(),
	      'login' => $geniuser->uid(),
1022 1023
	      # Creator clearly has root privs!
	      'privs' => 'root',
1024 1025 1026
	      'keys'  => [ @keys ]
	     }];

1027 1028 1029 1030
    # Want keys for single user.
    goto done
	if (defined($target_user));

1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
    if (! ($project->pid() eq $APT_HOLDINGPROJECT ||
	   $project->pid() eq $CLOUD_HOLDINGPROJECT)) {
	#
	# Get other users from the project. Real local users are easy,
	# nonlocal users from the GPO portal are messy.
	#
	my @members;
	if ($project->GetProjectGroup()->MemberList(\@members)) {
	    print STDERR "Error getting memberlist for $project\n";
	}
	else {
	    foreach my $member (@members) {
		next
		    if ($member->SameUser($geniuser->emulab_user()));
	    
		my $guser = GeniUser->CreateFromLocal($member);
		next
		    if (!defined($guser));

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
		#
		# Watch for local Emulab users with user privs.
		# Only Emulab based CMs will do anything with this,
		# but that is all we talk to.
		#
		my $privs = "root";

		if ($guser->IsLocal()) {
		    my $trust = $group->Trust($guser->emulab_user());
		    if (! TBMinTrust($trust, PROJMEMBERTRUST_LOCALROOT())) {
			$privs = "user";
		    }
		}

1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
		#
		# So, users coming in from the trusted signer have their keys
		# at their home portal. We download those keys whenever they
		# log in, and cache them in their local stub account, but they
		# could be out of date. But in order to refresh those keys, we
		# would need a valid (not expired) speaks-for credential, which
		# we might have, but typically not since they have short expire
		# times. So, lets not worry about this right now, just use the
		# cached keys and see who complains.
		#
		@keys = ();
		if ($guser->GetKeyBundle(\@keys, 1) < 0 || !@keys) {
		    print STDERR "No ssh keys for $guser\n";
		    next;
		}
		push(@{$rval}, {'urn'   => $guser->urn(),
				'login' => $guser->uid(),
1081
				'privs' => $privs,
1082 1083 1084 1085 1086
				'keys'  => [ @keys ]
		               });
	    }
	}
    }
1087
  done:
1088 1089 1090 1091
    $$pref = $rval;
    return 0;
}

1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
#
# Update the image status in the webtask for the instance. This is not
# clean at all, need a better way to do this.
#
sub UpdateImageStatus($$)
{
    my ($self, $details) = @_;

    DBQueryWarn("lock tables web_tasks write, apt_instances write, ".
		"            apt_instance_aggregates write")
	or return {};

    $self->Refresh();
    if ($self->status() ne "imaging") {
	goto done;
    }
1108
    my $webtask = $self->webtask();
1109 1110 1111
    if (!defined($webtask)) {
	goto done;
    }
1112 1113 1114
    # This could be really stale, must refresh.
    $webtask->Refresh();
    
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
    #
    # This will need to change; we can get updates from polling or
    # from the event stream. The events are processed out of band from
    # from the polling, so we have a consistency problem. In addition,
    # the event stream is sending status for just a single node since
    # events are bounded in size.
    #
    # In other words, the new image status has to be merged into the
    # existing status. We have use some kind of lock to avoid scrambling
    # the json data, and for now it is a table lock.
    #
    # Also, the blob has a timestamp in it, so we can sorta tell which
    # is most recent (not perfect, but does not really need to be).
    #
    if (defined($webtask->image_stamp()) &&
	defined($details->{'utc'}) &&
	$details->{'utc'} < $webtask->image_stamp()) {
	goto done;
    }
    $webtask->image_size($details->{'size'});
    $webtask->image_status($details->{'status'});
    $webtask->image_stamp($details->{'utc'});
    $webtask->Store();
  done:
    DBQueryWarn("unlock tables");
    return 0;
}

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
sub AddExtensionHistory($$)
{
    my ($self, $reason) = @_;
    my $uuid = $self->uuid();
    my $safe_text = DBQuoteSpecial($reason);

    return DBQueryWarn("update apt_instances set ".
		       "  extension_history=CONCAT($safe_text,".
		       "                     IFNULL(extension_history,'')) ".
		       "where uuid='$uuid'");
}

sub ExtensionRequested($$$)
{
    my ($self, $reason, $granted) = @_;
    my $uuid = $self->uuid();
    my $safe_text = DBQuoteSpecial($reason);

    return DBQueryWarn("update apt_instances set ".
		       "  extension_reason=$safe_text,".
		       "  extension_requested=1, ".
		       "  extension_count=extension_count+1, ".
1165
		       "  extension_hours=extension_hours+${granted} ".
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
		       "where uuid='$uuid'");
}

sub BumpExtensionCount($$)
{
    my ($self, $granted) = @_;
    my $uuid = $self->uuid();

    return DBQueryWarn("update apt_instances set ".
		       "  extension_count=extension_count+1, ".
1176
		       "  extension_hours=extension_hours+${granted} ".
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
		       "where uuid='$uuid'");
}

#
# Return the list APT_Aggregate objects for an instance.
#
sub AptAggregateList($)
{
    my ($self)  = @_;
    my @results = ();

    foreach my $agg ($self->AggregateList()) {
	my $aptagg = APT_Aggregate->Lookup($agg->aggregate_urn());
	if (!defined($aptagg)) {
	    print STDERR "Could not get APT_Aggregate object for $agg\n";
	    return ();
	}
	push(@results, $aptagg);
    }
    return @results;
}

Leigh B Stoller's avatar
Leigh B Stoller committed
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
#
# Apply extension policies.
#
sub ApplyExtensionPolicies($)
{
    my ($self)   = @_;
    my $uuid     = $self->uuid();
    my $pid      = $self->pid();
    my $name     = $self->name();
    my $pid_idx  = $self->pid_idx();
    my $gid_idx  = $self->gid_idx();
    my $uid_idx  = $self->creator_idx();
1211 1212
    my $current_disable = $self->extension_disabled();
    my $current_limit   = $self->extension_limit();
Leigh B Stoller's avatar
Leigh B Stoller committed
1213 1214
    my $policy;
    my $disabled = 0;
1215 1216
    my $limit    = 0;
    my $admin_after_limit = 0;
Leigh B Stoller's avatar
Leigh B Stoller committed
1217 1218 1219 1220 1221 1222
    my $reason;

    #
    # Apply in order project, group, then user.
    #
    my $query_result =
1223 1224
	DBQueryWarn("select disabled,reason,`limit`,admin_after_limit ".
		    " from apt_extension_group_policies ".
Leigh B Stoller's avatar
Leigh B Stoller committed
1225 1226 1227 1228
		    "where pid_idx='$pid_idx' and gid_idx=pid_idx");
    return -1
	if (!defined($query_result));
    if ($query_result->numrows) {
1229 1230 1231
	($disabled,$reason,
	 $limit,$admin_after_limit) = $query_result->fetchrow_array();
	if (($disabled || $limit) && !defined($reason)) {
Leigh B Stoller's avatar
Leigh B Stoller committed
1232
	    $reason = "because of a project restriction";
Leigh B Stoller's avatar
Leigh B Stoller committed
1233 1234 1235 1236
	}
	$policy = "Project";
    }
    $query_result =
1237 1238
	DBQueryWarn("select disabled,reason,`limit`,admin_after_limit ".
		    "  from apt_extension_group_policies ".
Leigh B Stoller's avatar
Leigh B Stoller committed
1239 1240 1241 1242
		    "where pid_idx='$pid_idx' and gid_idx='$gid_idx'");
    return -1
	if (!defined($query_result));
    if ($query_result->numrows) {
1243 1244 1245 1246 1247 1248
	my ($d,$r,$l,$admin_after_limit) = $query_result->fetchrow_array();
	$disabled = $d;
	$limit    = $l;
	$reason   = $r;
	$policy   = "Group";
	if (($disabled || $limit) && !defined($reason)) {
Leigh B Stoller's avatar
Leigh B Stoller committed
1249
	    $reason = "because of a group restriction";
Leigh B Stoller's avatar
Leigh B Stoller committed
1250 1251 1252
	}
    }
    $query_result =
1253 1254
	DBQueryWarn("select disabled,reason,`limit`,admin_after_limit ".
		    "  from apt_extension_user_policies ".
Leigh B Stoller's avatar
Leigh B Stoller committed
1255 1256 1257 1258
		    "where uid_idx='$uid_idx'");
    return -1
	if (!defined($query_result));
    if ($query_result->numrows) {
1259 1260 1261 1262 1263 1264
	my ($d,$r,$l,$admin_after_limit) = $query_result->fetchrow_array();
	$disabled = $d;
	$limit    = $l;
	$reason   = $r;
	$policy   = "User";
	if (($disabled || $limit) && !defined($reason)) {
Leigh B Stoller's avatar
Leigh B Stoller committed
1265
	    $reason = "because of user restriction";
Leigh B Stoller's avatar
Leigh B Stoller committed
1266 1267
	}
    }
1268 1269 1270 1271 1272 1273
    # Apply flags
    $self->Update({"extension_disabled" => $disabled,
		   "extension_limit"    => $limit,
		   "extension_admin_after_limit" => $admin_after_limit})
	== 0 or return -1;
    
Leigh B Stoller's avatar
Leigh B Stoller committed
1274
    # Set the reason only if disabled, clear otherwise.
1275 1276
    if ($disabled) {
	$self->Update({"extension_disabled_reason" => $reason}) == 0	
Leigh B Stoller's avatar
Leigh B Stoller committed
1277 1278 1279
	    or return -1;
    }
    else {
1280
	$self->Update({"extension_disabled_reason" => undef}) == 0	
Leigh B Stoller's avatar
Leigh B Stoller committed
1281 1282
	    or return -1;
    }
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
    # Ditto the limit.
    if ($limit) {
	$self->Update({"extension_limit_reason" => $reason}) == 0	
	    or return -1;
    }
    else {
	$self->Update({"extension_limit_reason" => undef}) == 0	
	    or return -1;
    }
    #
    # Basically, disable overrides limit when requesting an extension.
    #
    $current_limit = -1 if (!defined($current_limit));
    $limit         = -1 if (!defined($limit));
    
    if ($disabled != $current_disable || $limit != $current_limit) {
	my $message = "$policy policy applied to $pid/$name:\n";
Leigh B Stoller's avatar
Leigh B Stoller committed
1300

1301 1302 1303 1304 1305
	if ($disabled != $current_disable) {
	    my $which = ($disabled ? "disabled" : "enabled");
	    $message .= "Extensions have been ${which}.\n";
	}
	if ($limit != $current_limit) {
1306 1307
	    my $which = ($limit ?
			 "limited to " .  HoursToEnglish($limit) :
1308 1309 1310 1311 1312 1313 1314 1315
			 "set to unlimited");
	    $message .= "Extensions have been ${which}.\n";
	    if ($limit && $admin_after_limit) {
		$message .=
		    "Additional extension requests will require ".
		    "admin approval.\n";
	    }
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
1316
	SENDMAIL($TBAUDIT,
1317 1318
		 "Portal experiment $uuid extension policy changed",
		 $message . "\n" .
Leigh B Stoller's avatar
Leigh B Stoller committed
1319 1320 1321 1322 1323 1324 1325
		 (defined($reason) ? "Reason:\n$reason\n\n" : "").
		 $self->adminURL() . "\n",
		 $TBOPS);
    }
    return 0;
}

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
#
# Convert hours to days and hours string.
#
sub HoursToEnglish($)
{
    my ($hours) = @_;
    my $string;
    
    my $wdays  = int($hours / 24);
    my $whours = $hours % 24;
    if ($wdays) {
	$string = "$wdays day";
	$string .= "s" if ($wdays > 1);
	if ($whours) {
	    $string .= " ${whours} hour";
	    $string .= "s" if ($whours > 1);
	}
    }
    elsif ($whours) {
	$string = "$whours hour";
	$string .= "s" if ($whours > 1);
    }
    else {
	$string = "nothing";
    }
    return $string;
}

1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
#
# Check to see if all aggregates provisioned, and update node counts.
#
sub CheckProvisioned($)
{
    my ($self) = @_;
    my $provisioned = 1;

    DBQueryWarn("lock tables apt_instances write, ".
		"            apt_instance_aggregates write, ".
		"            web_tasks read")
	or return -1;

    #print STDERR "CheckProvisioned: " . $self->status() . "\n";

    # We only do this while we are provisioning.
    $self->Refresh();
    goto done
	if ($self->status() ne "provisioning");

    foreach my $agg ($self->AggregateList()) {
	$agg->Refresh();

	$provisioned = 0
	    if (!defined($agg->manifest()));
    }
    if ($provisioned) {
	#print STDERR "CheckProvisioned: provisioned\n";
	
	$self->SetStatus("provisioned");
	$self->ComputeNodeCounts();
    }
  done:
    DBQueryWarn("unlock tables");
    return 0;
}

1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430