GeniAggregate.pm.in 65.8 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1
2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2008-2013 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
# 
# {{{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.
# 
# }}}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29
30
31
32
#
package GeniAggregate;

#
33
# Some simple aggregate stuff.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
34
35
36
37
38
39
40
41
42
43
44
45
46
#
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

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

# Must come after package declaration!
use GeniDB;
use GeniCredential;
use GeniCertificate;
use GeniSliver;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
47
use GeniSlice;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
48
use GeniRegistry;
49
use GeniUtil;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
50
use GeniUser;
51
use GeniComponent;
52
use GeniHRN;
53
use GeniXML;
54
use emutil;
55
use EmulabConstants;
56
use Node;
57
58
use Logfile;
use libtestbed;
59
use Data::Dumper;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
60
61
62
use English;
use overload ('""' => 'Stringify');
use XML::Simple;
63
64
65
use POSIX qw(strftime);
use Time::Local;
use Date::Parse;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66
67
68
69
70
71
72
73

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
74
75
my $MAINSITE       = @TBMAINSITE@;
my $ELABINELAB     = @ELABINELAB@;
76
my $PGENIDOMAIN    = "@PROTOGENI_DOMAIN@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
77
78
my $SIGNCRED	   = "$TB/sbin/signgenicred";
my $VERIFYCRED	   = "$TB/sbin/verifygenicred";
79
my $NODEREBOOT	   = "$TB/bin/node_reboot";
80
my $EVENTSYS	   = "$TB/bin/eventsys_control";
81
my $VNODESETUP     = "$TB/sbin/vnode_setup";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
82
my $POWER          = "$TB/bin/power";
83
my $OSLOAD         = "$TB/bin/os_load";
84
my $SNMPIT         = "$TB/bin/snmpit_test";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
85
86
87
my $NAMEDSETUP     = "$TB/sbin/named_setup";
my $EXPORTS_SETUP  = "$TB/sbin/exports_setup";
my $GENTOPOFILE    = "$TB/libexec/gentopofile";
88
my $IMAGE_SETUP    = "$TB/sbin/image_setup";
Leigh B Stoller's avatar
Leigh B Stoller committed
89
my $ARPLOCKDOWN    = "$TB/sbin/arplockdown";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
90
91
92

# Cache of instances to avoid regenerating them.
my %aggregates     = ();
93
BEGIN { use GeniUtil; GeniUtil::AddCache(\%aggregates); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
94
95

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

104
105
106
    if (GeniHRN::IsValid($token)) {
	my ($authority, $type, $id) = GeniHRN::Parse($token);
	return undef if $type ne "sliver";
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

	if( GeniHRN::Authoritative($token, "@OURDOMAIN@") ) {
	    # Very simple: we put the index of our own aggregates right
	    # in the name.
	    $idx = $id;
	} else {
	    # Look up the aggregate's certificate.
	    $token = GeniHRN::Normalise( $token );
	    $query_result = DBQueryWarn(
		"SELECT geni_aggregates.idx FROM geni_aggregates, " .
		"geni_certificates WHERE geni_aggregates.uuid = " .
		"geni_certificates.uuid AND " .
		"geni_certificates.urn='$token';" );

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

	    ($idx) = $query_result->fetchrow_array();
	}
125
126
    }
    elsif ($token =~ /^\d+$/) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
	$idx = $token;
    }
    elsif ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	$query_result =
	    DBQueryWarn("select idx from geni_aggregates ".
			"where uuid='$token'");
	    return undef
		if (! $query_result || !$query_result->numrows);

	    ($idx) = $query_result->fetchrow_array();
    }
    else {
	return undef;
    }
    # Look in cache first
    return $aggregates{"$idx"}
        if (exists($aggregates{"$idx"}));

    $query_result =
	DBQueryWarn("select * from geni_aggregates where idx='$idx'");
    
    return undef
	if (!$query_result || !$query_result->numrows);

151
152
153
154
155
156
    my $self               = {};
    $self->{'AGGREGATE'}   = $query_result->fetchrow_hashref();
    $self->{'CREDENTIAL'}  = undef;
    $self->{'SLICE'}       = undef;
    $self->{'PARENT'}      = undef;
    $self->{'CERTIFICATE'} = undef;
157
158
159

    # Bless into sub package if called for.
    my $type = $self->{'AGGREGATE'}->{'type'};
160
    if (defined($type) && $type ne "" && $type ne "Aggregate") {
161
162
163
164
165
	bless($self, $class . "::" . $type);
    }
    else {
	bless($self, $class);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
166
167
168
169
170
171
172

    #
    # Grab the certificate, since we will probably want it.
    #
    my $uuid = $self->{'AGGREGATE'}->{'uuid'};
    my $certificate = GeniCertificate->Lookup($uuid);
    if (!defined($certificate)) {
173
174
175
176
177
178
179
180
	if ($type eq "Aggregate") {
	    print STDERR
		"Could not find certificate for aggregate $idx ($uuid)\n";
	    return undef;
	}
    }
    else {
	$self->{'CERTIFICATE'} = $certificate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    }
    
    # Add to cache. 
    $aggregates{$self->{'AGGREGATE'}->{'idx'}} = $self;
    
    return $self;
}

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
197
    my $hrn  = $self->hrn();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
198
199
    my $idx  = $self->idx();

Leigh B. Stoller's avatar
Leigh B. Stoller committed
200
    return "[GeniAggregate: $hrn, IDX: $idx]";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
201
202
203
204
205
206
207
}

#
# Create a Geni aggregate in the DB. This happens on the server side only
# for now. The client side does not actually know its an aggregate, at
# least not yet.
#
208
sub Create($$$$$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
209
{
210
    my ($class, $slice, $owner, $aggregate_type, $hrn, $nickname) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
211
    my @insert_data = ();
212
    my $certificate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
213
214

    # Every aggregate gets a new unique index.
215
    my $idx = TBGetUniqueIndex('next_sliver', 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
216
217

    # Create a cert pair, which gives us a new uuid.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
218
    my $urn = GeniHRN::Generate( "@OURDOMAIN@", "sliver", $idx );
219
220
221
222
223
224
225
226
227
228
229

    if ($aggregate_type eq "Aggregate") {
	$certificate = GeniCertificate->Create({'urn'   => $urn,
						'hrn'   => $hrn,
						'email' => $TBOPS});

	if (!defined($certificate)) {
	    print STDERR "GeniAggregate::Create: ".
		"Could not generate new certificate and UUID for $hrn\n";
	    return undef;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
230
    }
231
232
    my $uuid        = (defined($certificate) ?
		       $certificate->uuid() : GeniUtil::NewUUID());
233
234
    my $slice_uuid  = $slice->uuid();
    my $owner_uuid  = $owner->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
235
236
237
    $aggregate_type = "Aggregate"
    	if (! defined($aggregate_type));

Leigh B. Stoller's avatar
Leigh B. Stoller committed
238
239
240
    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
    push(@insert_data, "idx='$idx'");
241
    push(@insert_data, "hrn=" . DBQuoteSpecial($hrn));
242
    push(@insert_data, "nickname=" . DBQuoteSpecial($nickname));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
243
244
245
    push(@insert_data, "uuid='$uuid'");
    push(@insert_data, "creator_uuid='$owner_uuid'");
    push(@insert_data, "slice_uuid='$slice_uuid'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
246
    push(@insert_data, "type='$aggregate_type'");
247
248
    # Start out new aggregates, as new.
    push(@insert_data, "state='new'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
249
250
251
252

    # Insert into DB.
    if (!DBQueryWarn("insert into geni_aggregates set " .
		     join(",", @insert_data))) {
253
254
	$certificate->Delete()
	    if (defined($certificate));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
255
256
	return undef;
    }
257
258
259
260
261
    my $aggregate = GeniAggregate->Lookup($idx);
    return undef
	if (!defined($aggregate));

    return $aggregate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
262
263
264
265
266
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'AGGREGATE'}->{$_[1]}); }
sub idx($)		{ return field($_[0], "idx"); }
sub uuid($)		{ return field($_[0], "uuid"); }
267
sub nickname($)		{ return field($_[0], "nickname"); }
268
sub resource_id($)      { return $_[0]->nickname(); }
269
sub type($)		{ return field($_[0], "type"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
270
271
272
sub slice_uuid($)	{ return field($_[0], "slice_uuid"); }
sub creator_uuid($)	{ return field($_[0], "creator_uuid"); }
sub created($)		{ return field($_[0], "created"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
273
sub registered($)	{ return field($_[0], "registered"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
274
sub credential_idx($)	{ return field($_[0], "credential_idx"); }
275
sub aggregate_idx($)	{ return field($_[0], "aggregate_idx"); }
276
277
sub speaksfor_uuid($)	{ return field($_[0], "speaksfor_uuid"); }
sub speaksfor_urn($)	{ return field($_[0], "speaksfor_urn"); }
278
sub status($)		{ return field($_[0], "status"); }
279
sub state($)		{ return field($_[0], "state"); }
280
sub ErrorLog($)		{ return field($_[0], "errorlog"); }
281
282
283
284
285
286
287
288
289
sub cert($)		{ return GetCertificate($_[0])->cert(); }
# Watch for aggregates that no longer get a certificate.
sub GetCertificate($)
{
    if (!defined($_[0]->{'CERTIFICATE'})) {
	print STDERR "*** No certificate for aggregate: " . $_[0] . "\n";
    }
    return $_[0]->{'CERTIFICATE'};
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
290

291
292
293
# An alias so that slivers look like aggregates.
sub resource_type($)	{ return field($_[0], "type"); }

294
295
296
297
298
299
300
301
302
303
304
# A place to stash a temporary rspec.
sub rspec($;$)
{
    my ($self, $rspec) = @_;

    if (defined($rspec)) {
	$self->{'RSPEC'} = $rspec;
    }
    return $self->{'RSPEC'};
}

305
306
307
308
309
310
311
# Return the URN.
sub urn($)
{
    my ($self) = @_;

    return GeniHRN::Generate("@OURDOMAIN@", "sliver", $self->idx());
}
312
313
# Name compat.
sub sliver_urn($)	{ return $_[0]->urn(); }
314

315
316
317
318
#
# Destroy all the slivers in the aggregate, and then the aggregate if there
# is nothing in it. Leave it around if something goes wrong.
#
319
sub Delete($$)
320
{
321
    my ($self, $purge) = @_;
322
323
324
325
326
327
328
329
330
331
332
333
    my $broken = 0;

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

    my @slivers = ();
    if ($self->SliverList(\@slivers) != 0) {
	print STDERR "Could not get sliver list for $self\n";
	return -1;
    }
    foreach my $sliver (@slivers) {
	if ($sliver->status() eq "broken") {
334
	    print STDERR "Could not delete 'broken' $sliver from $self\n";
335
	    $broken++;
336
	    last;
337
	}
338
	if ($sliver->Delete($purge) != 0) {
339
340
341
	    print STDERR "Could not delete $sliver from $self\n";
	    $sliver->SetStatus("broken");
	    $broken++;
342
	    last;
343
344
345
346
347
	}
    }
    return -1
	if ($broken);
    
348
349
350
351
    if (GeniUsage->DestroyAggregate($self, $purge)) {
	print STDERR "GeniAggregate::Delete: ".
	    "GeniUsage->DestroyAggregate($self) failed\n";
    }
352
353
354
355
356
357
358
359
360
361
    my $idx  = $self->idx();
    my $uuid = $self->uuid();

    DBQueryWarn("delete from geni_credentials where this_uuid='$uuid'")
	or return -1;
    DBQueryWarn("delete from geni_certificates where uuid='$uuid'")
	or return -1;
    DBQueryWarn("delete from geni_aggregates where idx='$idx'")
	or return -1;
    
362
363
364
    # Delete from cache. 
    delete($aggregates{$idx});

365
366
367
    return 0;
}

368
369
370
371
372
373
374
#
# Cons up an hrn.
#
sub hrn($)
{
    my ($self) = @_;

375
376
377
378
379
    my $hrn = field($self, "hrn");

    if (defined($hrn) && $hrn ne "") {
	return $hrn;
    }
380
    return "${PGENIDOMAIN}.aggregate_" . $self->idx();
381
382
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#
# Look up toplevel aggregate for a locally instantiated slice. 
#
sub SliceAggregate($$)
{
    my ($class, $slice) = @_;

    my $slice_uuid = $slice->uuid();
    my @result = ();

    my $query_result =
	DBQueryWarn("select idx from geni_aggregates ".
		    "where slice_uuid='$slice_uuid' and type='Aggregate'");
    return undef
	if (!$query_result);
    return undef
	if ($query_result->numrows != 1);

    my ($idx) = $query_result->fetchrow_array();
    my $aggregate = GeniAggregate->Lookup($idx);
    return undef
	if (!defined($aggregate));

    return $aggregate;
}

409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#
# Look up a list of aggregates for a locally instantiated slice. 
# Used by the CM.
#
sub SliceAggregates($$$)
{
    my ($class, $slice, $pref) = @_;

    my $slice_uuid = $slice->uuid();
    my @result = ();

    my $query_result =
	DBQueryWarn("select idx from geni_aggregates ".
		    "where slice_uuid='$slice_uuid'");
    return -1
	if (!$query_result);

    while (my ($idx) = $query_result->fetchrow_array()) {
	my $aggregate = GeniAggregate->Lookup($idx);
	return -1
	    if (!defined($aggregate));
	push(@result, $aggregate);
    }
    @$pref = @result;
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
436
437
438
439
440
441
442
443
444
445
446
#
# List of slivers for this aggregate.
#
sub SliverList($$)
{
    my ($self, $pref) = @_;
    my @result = ();
    
    return -1
	if (! (ref($self) && ref($pref)));

447
448
    my $idx  = $self->idx();
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
449
    my $query_result =
450
451
	DBQueryWarn("select idx from geni_slivers ".
		    "where aggregate_uuid='$uuid'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
452
453
454
455
456
457
458
459
460
461
462
    return -1
	if (!$query_result);

    while (my ($sliver_idx) = $query_result->fetchrow_array()) {
	my $sliver = GeniSliver->Lookup($sliver_idx);
	if (!defined($sliver)) {
	    print STDERR "Could not find sliver object for $sliver_idx\n";
	    return -1;
	}
	push(@result, $sliver);
    }
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481

    #
    # And any aggregates that are children.
    #
    $query_result =
	DBQueryWarn("select idx from geni_aggregates ".
		    "where aggregate_idx='$idx'");
    return -1
	if (!$query_result);

    while (my ($aggregate_idx) = $query_result->fetchrow_array()) {
	my $aggregate = GeniAggregate->Lookup($aggregate_idx);
	if (!defined($aggregate_idx)) {
	    print STDERR
		"Could not find aggregate object for $aggregate_idx\n";
	    return -1;
	}
	push(@result, $aggregate);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
482
483
484
485
486
    @$pref = @result;
    return 0;
    
}

487
488
489
490
491
492
493
494
495
496
#
# Set the aggregate for an aggregate.
#
sub SetAggregate($$)
{
    my ($self, $aggregate) = @_;

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

497
498
499
    my $idx      = $self->idx();
    my $agg_idx  = $aggregate->idx();
    my $agg_uuid = $aggregate->uuid();
500
501
502
503
504
505
506
507
508
509
510
511

    return -1
	if (!DBQueryWarn("update geni_aggregates set ".
			 "  aggregate_idx='$agg_idx' ".
			 "where idx='$idx'"));
    
    $self->{'AGGREGATE'}->{'aggregate_idx'} = $agg_idx;
    $self->{'PARENT'} = $aggregate;
    return 0;
}

#
512
# Get the aggregate for an aggregate.
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#
sub GetAggregate($)
{
    my ($self) = @_;

    return undef
	if (! ref($self));

    return $self->{'PARENT'} if (defined($self->{'PARENT'}));
    return undef
	if (!defined($self->aggregate_idx()));

    my $aggregate = GeniAggregate->Lookup($self->aggregate_idx());
    if (!defined($aggregate)) {
	print STDERR "Could not get aggregate object associated with $self\n";
	return undef;
    }
    $self->{'PARENT'} = $aggregate;
    return $aggregate;
}

534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#
# Is object in the aggregate.
#
sub IsMember($$)
{
    my ($self, $object) = @_;

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

    my $aggregate = $object->GetAggregate();
    return 0
	if (!$aggregate);
    return -1
	if ($self->idx() != $aggregate->idx());
    return 1;
}

552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#
# Set the status for the aggregate
#
sub SetStatus($$)
{
    my ($self, $status) = @_;

    return undef
	if (! ref($self));

    my $idx = $self->idx();
    
    return -1
	if (!DBQueryWarn("update geni_aggregates set ".
			 "  status='$status' ".
			 "where idx='$idx'"));
    
    $self->{'AGGREGATE'}->{'status'} = $status;
    return 0;
}

573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#
# Set the state for the aggregate
#
sub SetState($$)
{
    my ($self, $state) = @_;

    return undef
	if (! ref($self));

    my $idx = $self->idx();
    
    return -1
	if (!DBQueryWarn("update geni_aggregates set ".
			 "  state='$state' ".
			 "where idx='$idx'"));
    
    $self->{'AGGREGATE'}->{'state'} = $state;
    return 0;
}

594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
#
# And the ErrorLog. These are intended to be short ...
#
sub SetErrorLog($$)
{
    my ($self, $log) = @_;
    my $safe_log = DBQuoteSpecial($log);

    return undef
	if (! ref($self));

    my $idx = $self->idx();
    
    return -1
	if (!DBQueryWarn("update geni_aggregates set ".
			 "  errorlog=$safe_log ".
			 "where idx='$idx'"));
    
    $self->{'AGGREGATE'}->{'errorlog'} = $log;
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#
# Set the registered datetime for the aggregate
#
sub SetRegistered($$)
{
    my ($self, $yesno) = @_;

    return undef
	if (! ref($self));

    my $idx = $self->idx();
    my $val = ($yesno ? "now()" : "NULL");
    
    return -1
	if (!DBQueryWarn("update geni_aggregates set ".
			 "  registered=$val ".
			 "where idx='$idx'"));
    
    return 0;
}

637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#
# Set the speaksfor stuff.
#
sub SetSpeaksFor($$)
{
    my ($self, $speaksfor) = @_;
    my $idx = $self->idx();
    my $safe_speaksfor_uuid = DBQuoteSpecial($speaksfor->owner_uuid());
    my $safe_speaksfor_urn  = DBQuoteSpecial($speaksfor->owner_urn());

    print "GeniAggregate->SetSpeaksFor($self, $speaksfor)\n";

    return -1
	if (!DBQueryWarn("update geni_aggregates set ".
			 "  speaksfor_uuid=$safe_speaksfor_uuid ".
			 "where idx='$idx'"));

    $self->{'AGGREGATE'}->{'speaksfor_urn'}  = $speaksfor->owner_urn();
    $self->{'AGGREGATE'}->{'speaksfor_uuid'} = $speaksfor->owner_uuid();

    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
#
# Get the slice for the aggregate.
#
sub GetSlice($)
{
    my ($self) = @_;

    return undef
	if (! ref($self));

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

    if (!defined($self->slice_uuid())) {
	print STDERR "No slice associated with $self\n";
	return undef;
    }
    my $slice = GeniSlice->Lookup($self->slice_uuid());
    if (!defined($slice)) {
	print STDERR "Could not get slice object associated with $self\n";
	return undef;
    }
    $self->{'SLICE'} = $slice;
    return $slice;
}

685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#
# The expiration time for an aggregate is when the slice expires.
# The DB field is ignored.
#
sub expires($)
{
    my ($self) = @_;

    return undef
	if (! ref($self));

    my $slice = $self->GetSlice();
    return undef
	if (!defined($slice));

    return $slice->expires();
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
#
# Get the creator for the aggregate.
#
sub GetCreator($)
{
    my ($self) = @_;

    return undef
	if (! ref($self));

    if (!defined($self->creator_uuid())) {
	print STDERR "No creator associated with $self\n";
	return undef;
    }
    return GeniUser->Lookup($self->creator_uuid(), 1);
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
#
# Create a signed credential for this aggregate, issued to the provided user.
# The credential will grant all permissions for now.
#
# Should we store these credentials in the DB, recording what we hand out?
#
sub NewCredential($$)
{
    my ($self, $owner) = @_;

    return undef
	if (! (ref($self) && ref($owner)));

    my $credential = GeniCredential->Create($self, $owner);
    if (!defined($credential)) {
	print STDERR "Could not create credential for $self, $owner\n";
	return undef;
    }
738
    # Bump expiration to avoid race with aggregate about to expire.
739
    $credential->SetExpiration(time() + (24 * 3600));
740
	
741
742
743
    if (defined($self->nickname())) {
	$credential->AddExtension("nickname", $self->nickname());
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
744
745
746
747
748
749
750
    if ($credential->Sign($self->GetCertificate()) != 0) {
	print STDERR "Could not sign credential for $self, $owner\n";
	return undef;
    }
    return $credential;
}

751
752
753
#
# Get the manifest for an aggregate. Returns the XML string.
#
754
sub GetManifest($$)
755
{
756
    my ($self, $asxml) = @_;
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775

    return undef
	if (! ref($self));

    my $slice      = $self->GetSlice();
    return undef
	if (!defined($slice));
    my $slice_uuid = $slice->uuid();

    my $query_result =
	DBQueryWarn("select manifest from geni_manifests ".
		    "where slice_uuid='$slice_uuid'");
    
    if (!$query_result || !$query_result->numrows) {
	print STDERR "GetManifest: Could not locate manifest for $self\n";
	return undef;
    }
    my ($xml) = $query_result->fetchrow_array();

776
777
    my $manifest = GeniXML::Parse($xml);
    if (!defined($manifest)) {
778
779
	return undef;
    }
780

781
782
783
    #
    # Update the manifest ticket to reflect the current expiration time.
    #
784
    my $valid_date = POSIX::strftime("20%y-%m-%dT%H:%M:%SZ",
785
				     gmtime(str2time($slice->expires())));
Jonathon Duerig's avatar
Jonathon Duerig committed
786
787
788
789
790
    if (GeniXML::IsVersion0($manifest)) {
	GeniXML::SetText("valid_until", $manifest, $valid_date);
    } else {
	GeniXML::SetText("expires", $manifest, $valid_date);
    }
791

792
793
794
    return $manifest
	if (!$asxml);
    
795
    $xml = GeniXML::Serialize($manifest);
796
797
798
    return $xml;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
799
#
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
# Process the manifest. Just hand off to the slivers.
#
sub ProcessManifest($$)
{
    my ($self, $manifest) = @_;

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

    my @slivers = ();
    if ($self->SliverList(\@slivers) != 0) {
	print STDERR "Could not get sliver list for $self\n";
	return -1;
    }

    foreach my $sliver (@slivers) {
	next
	    if (ref($sliver) ne "GeniSliver::Node");

	if ($sliver->ProcessManifest($manifest) != 0) {
	    return -1;
	}
    }
    return 0;
}

#
827
828
829
830
831
832
833
# Start all the slivers in the aggregate. Start is special since it
# sorta means reboot, and the only thing we reboot are nodes. And,
# since we might have multiple vnodes on a pnode, we want to be efficient
# about it.
#
# XXX Is is assumed that there is a single toplevel aggregate for the
# slice, so we can get all the nodes.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
834
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
835
sub Start($$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
836
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
837
    my ($self, $version, $restart) = @_;
838
    my $msg = "Internal Error: ";
Leigh B Stoller's avatar
Leigh B Stoller committed
839
840
    require Lan;
    require OSinfo;
841
    require Image;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
842
843
844

    return -1
	if (! ref($self));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
845
846
    $restart = 0
	if (!defined($restart));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
847

848
849
850
    # Clear last error.
    $self->SetErrorLog("");    

851
852
    my $experiment = Experiment->Lookup($self->slice_uuid());
    if (!defined($experiment)) {
853
854
	$msg .= "Could not map $self to its experiment";
	goto bad;
855
856
857
858
    }
    my $pid = $experiment->pid();
    my $eid = $experiment->eid();

859
860
861
862
863
864
865
866
867
868
    #
    # Look for a firewall that needs to be setup first.
    #
    my ($firewall, $firewall_sliver);
    my $firewalled = $experiment->IsFirewalled(\$firewall);
    if ($firewalled && !defined($firewall)) {
	$msg .= "Could not determine firewall for experiment";
	goto bad;
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
869
870
    my @slivers = ();
    if ($self->SliverList(\@slivers) != 0) {
871
872
	$msg .= "Could not get sliver list for $self";
	goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
873
    }
874
875
876
    my %reboots  = ();
    my %vnodes   = ();
    my %poweron  = ();
877
    my %reloads  = ();
878
879
    my %vnodekills = ();
    my %imageinfo  = ();
880
881
    # See "bad" label below; want to know what sliver failed (if any).
    my $sliver;
882

883
884
885
886
887
888
889
890
891
892
893
894
    #
    # Download the images. If this fails, we have wasted our time,
    # but we want to do this after we have forked off from the parent
    # and we have returned to the client (rpc). 
    #
    my $output = GeniUtil::ExecQuiet("$IMAGE_SETUP -g $pid,$eid");
    if ($?) {
	$msg = "Could not setup images:\n$output";
	goto bad;
    }
    print STDERR $output;

895
    foreach $sliver (@slivers) {
896
897
898
899
900
901
902
903
	if (ref($sliver) ne "GeniSliver::Node") {
	    next
		if ($sliver->state() eq "started" && !$restart);

	    $sliver->Start($version) == 0
		or return -1;
	    next;
	}
904
	# Remember which sliver is the firewall. 
Leigh B Stoller's avatar
Leigh B Stoller committed
905
	if ($firewalled && $firewall eq $sliver->resource_id()) {
906
907
	    $firewall_sliver = $sliver;
	}
908

909
	my $node = Node->Lookup($sliver->resource_id());
910
	if (!defined($node)) {
911
912
	    $msg .= "Could not map $sliver to a node";
	    goto bad;
913
	}
914
915
	my $reservation = $node->Reservation();
	if (!defined($reservation)) {
916
917
	    $msg .= "$node no longer belongs to $self";
	    goto bad;
918
	}
919
920
921
922
	$node->_reloaded(0);
	# Backpointer used in WaitForNodes().
	$node->_sliver($sliver);
	
923
	if ($reservation->SameExperiment($experiment)) {
924
925
	    my $vnode;
	    
926
927
928
929
930
931
932
	    #
	    # Since this is an aggregate, some slivers may already be
	    # in the started state. Skip those, unless doing a restart.
	    #
	    next
		if ($sliver->state() eq "started" && !$restart);
	    
933
	    if ($node->isvirtnode()) {
934
935
936
937
938
939
940
941
942
943
944
		# A virtnode on a shared physical node needs reboot or setup
		if ($node->sharing_mode()) {
		    if ($restart && $sliver->state() eq "started") {
			$reboots{$node->node_id} = $node;
		    }
		    else {
			$vnodes{$node->node_id} = $node;
		    }
		}
		# See below.
		$vnode = $node;
945
		$vnode->_parent(undef);
946
947
948
949
950
951
952
953
954
955
956
957
958

		#
		# We now allow the user to specify the OS for vnodes.
		#
		my $osinfo = OSinfo->Lookup($vnode->def_boot_osid());
		if (!defined($osinfo)) {
		    $msg .= "Could not get osinfo for $vnode";
		    goto bad;
		}
		print STDERR "$vnode wants to boot $osinfo.\n";

		#
		# If there is an image defined for this os on pcvm,
959
		# we need to setup a reload if it is not loaded.
960
		#
961
962
963
964
965
966
967
968
969
		my $isloaded = $vnode->IsOSLoaded($osinfo);
		if ($isloaded < 0) {
		    $msg .= "Error determining if $osinfo is loaded on $vnode";
		    goto bad;
		}
		if (! $isloaded) {
		    my $image = $osinfo->MapToImage("pcvm");
		    if (defined($image)) {
			print STDERR "Setting $vnode to load $image\n";
970
		    
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
			if (!exists($reloads{$image->imageid()})) {
			    $reloads{$image->imageid()} = [ ];
			} 
			push(@{ $reloads{$image->imageid()} }, $vnode);
			$imageinfo{$vnode->node_id()} = [$osinfo->osid(),
							 $image->imageid()];
			$vnode->_reloaded(1);

			#
			# Remove from reboots; we no longer want to do this,
			# but instead force them to be "killed" so that they
			# will go through reload when created again. The
			# client side should probably try to figure this out
			# instead.
			#
			if (exists($reboots{$vnode->node_id()})) {
			    delete($reboots{$vnode->node_id()});
			    $vnodekills{$vnode->node_id()} = $vnode;
			    $vnodes{$node->node_id()} = $vnode;
			}
		    }
992
		}
993

994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
		my $physnodeid = $vnode->phys_nodeid();
		$node = Node->Lookup($physnodeid);
		if (!defined($node)) {
		    $msg .= "Could not lookup $physnodeid";
		    goto bad;
		}
		# Possibly reset below.
		$node->_reloaded(0);
		# There is no sliver. 
		$node->_sliver(undef);
		# Signal that vnode depends on parent.
		$vnode->_parent($node);

1007
1008
		# No more to do.
		next
1009
		    if ($vnode->sharing_mode());
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
		
		#
		# Now it gets messy. Do not want to mess with the physnode
		# if its running other vnodes, and we just need to fire up
		# a new one. But if the physnode is going to get rebooted,
		# then there is no need to do anything with the vnodes; they
		# will boot up with the physnode. 
		#
		# But, have to make sure that the phys node gets setup.
		#
1020
1021
1022
1023
		next
		    if (exists($poweron{$physnodeid}) ||
			exists($reboots{$physnodeid}) ||
			exists($reloads{$physnodeid}));
1024
1025
	    }
	    #
1026
1027
1028
	    # If the node is not imageable, then there is not much to
	    # do except turn it on or reboot it. I am assuming that a
	    # a non imageable node is always in raw mode.
1029
	    #
1030
1031
1032
1033
1034
1035
1036
1037
	    if (!$node->imageable()) {
		if ($sliver->state() eq "stopped") {
		    $poweron{$node->node_id} = $node;
		}
		else  {
		    $reboots{$node->node_id} = $node;
		}
		next;
1038
	    }
1039
1040
1041
	    #
	    # See if the node is running the requested OS.
	    #
1042
1043
	    my $osinfo = OSinfo->Lookup($node->def_boot_osid());
	    if (!defined($osinfo)) {
1044
1045
		$msg .= "Could not get osinfo for $node";
		goto bad;
1046
1047
1048
	    }
	    print STDERR "$node wants to boot $osinfo.\n";
	    if ($osinfo->IsGeneric()) {
1049
		#
1050
		# Map generic OSID to the specific one.
1051
		#
1052
1053
		my $tmp = $osinfo->ResolveNextOSID($experiment);
		if (!defined($tmp)) {
1054
		    $msg .= "No next mapping for $osinfo on $node!";
1055
		    goto bad;
1056
1057
1058
1059
1060
1061
1062
1063
1064
		}
		print STDERR "  Mapping $osinfo on $node to $tmp\n";
		$osinfo = $tmp;
	    }
	    #
	    # Make sure this OSID is actually loaded on the machine.
	    #
	    my $isloaded = $node->IsOSLoaded($osinfo);
	    if ($isloaded < 0) {
1065
1066
		$msg .= "Error determining if $osinfo is loaded on $node";
		goto bad;
1067
1068
1069
1070
1071
1072
	    }
	    if (! $isloaded) {
		print STDERR "  Setting up a reload for $node\n";
		
		my $image = $osinfo->MapToImage($node->type());
		if (!defined($image)) {
1073
1074
		    $msg .= "  No image for $osinfo on $node";
		    goto bad;
1075
		}
1076
1077
1078
1079
		if (!exists($reloads{$image->imageid()})) {
		    $reloads{$image->imageid()} = [ ];
		} 
		push(@{ $reloads{$image->imageid()} }, $node);
1080
		$node->_reloaded(1);
1081
1082
1083
1084
1085
1086

		# As with os_setup, we do not count images unless
		# they are actually reloaded. I have no idea why.
		$imageinfo{$node->node_id()} = [$osinfo->osid(),
						$image->imageid()];
		
1087
1088
		
		# Reload means reboot or power on.
1089
1090
		# But skip the firewall; that is done specially since
		# it has to come up before everything else.
1091
1092
1093
		if (!defined($vnode) && $sliver->state() eq "stopped") {
		    $poweron{$node->node_id} = $node;
		}
1094
		else {
1095
1096
		    $reboots{$node->node_id} = $node;
		}
1097
1098
1099
1100
1101
1102
1103
	    }
	    else {
		#
		# Make sure boot is set correctly.
		#
		if ($node->OSSelect($osinfo, "def_boot_osid", 0)) {
		    print STDERR "  Could not os_select $node to $osinfo\n";
1104
		    goto bad;
1105
		}
1106
1107
1108
1109
1110
1111
1112
		#
		# If the node is going to get rebooted, then do not need
		# to worry about the vnodes on it. But if the node is ready
		# to go, then we have to do the vnodes. Remember, we do not
		# reboot the physnode since we might only be adding a new node
		# in which case, a full reboot is wrong.
		#
1113
1114
		if (! $node->IsUp() ||
		    # This catches the pg nodes which are in ISUP while free.
1115
		    ($sliver->state() eq "new" && !defined($vnode))) {
1116
		    # We should be using allocstatus. 
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
		    if ($sliver->state() eq "stopped" && !defined($vnode)) {
			$poweron{$node->node_id} = $node;
		    }
		    else {
			$reboots{$node->node_id} = $node;
		    }
		}
		elsif ($restart && !defined($vnode)) {
		    # Just a physnode that needs restarting.
		    $reboots{$node->node_id} = $node;
		}
		elsif (defined($vnode)) {
		    if ($sliver->state() eq "started") {
			$reboots{$vnode->node_id} = $vnode;
		    }
		    else {
			$vnodes{$vnode->node_id} = $vnode;
		    }
		}
1136
1137
1138
	    }
	}
	else {
1139
1140
	    $msg .= "$node is not reserved to $self";
	    goto bad;
1141
1142
	}
    }
1143
1144
1145
    # Record image stats
    Image->RecordImageHistory($experiment, 'os_setup', undef, 0, \%imageinfo);
    
1146
1147
    # See "bad" label below.
    $sliver = undef;
1148
1149
1150
1151
1152
1153
1154
1155

    my @waitvnodes = values(%vnodes);
    my @waitpnodes = (values(%poweron), values(%reboots));

    # Want to make sure we see fresh logs (and do not store the same log).
    foreach my $node (@waitpnodes, @waitpnodes) {
	$node->ClearBootLog();
    }
1156
    
1157
1158
1159
1160
1161
1162
    #
    # Cull out vnodes that are going to get rebooted cause the
    # physnode is getting rebooted. 
    #
    my %tmp = %vnodes;
    foreach my $vnode (values(%vnodes)) {
1163
1164
	if (! (exists($reboots{$vnode->phys_nodeid()}) ||
	       exists($poweron{$vnode->phys_nodeid()}))) {
1165
	    $tmp{$vnode->node_id()} = $vnode;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1166
	}
1167
    }
1168
1169
    %vnodes = %tmp;

1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
    #
    # Setup the reloads. We do not reboot the nodes until below.
    #
    if (keys(%reloads)) {
	foreach my $imageid (keys(%reloads)) {
	    my @nodes = @{ $reloads{$imageid} };
	    my @node_ids = map { $_->node_id() } @nodes;

	    # No wait, no reboot. reload runs completely in the background.
	    system("$OSLOAD -s -r -m $imageid @node_ids");
1180
1181
1182
1183
	    if ($?) {
		$msg .= "Failed to setup reload: $imageid on @node_ids";
		goto bad;
	    }
1184
1185
1186
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1187
    if ($version >= 2) {
1188
1189
1190
1191
1192
1193
	#
	# Dump the manifest into the experiment directory.
	#
	my $userdir       = $experiment->UserDir();
	my $manifest_file = "$userdir/tbdata/geni_manifest";
	my $manifest      = $self->GetManifest(1);
Leigh B Stoller's avatar
Leigh B Stoller committed
1194
	if ($manifest && open(MAN, ">$manifest_file")) {
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
	    print MAN $manifest;
	    close(MAN);
	}

	#
	# Now we need a mapping of node_id to sliver_urn.
	#
	my $mapping_file = "$userdir/tbdata/geni_mapping";
	if (open(MAP, ">$mapping_file")) {
	    foreach my $sliver (@slivers) {
		next
		    if (ref($sliver) ne "GeniSliver::Node");

		print MAP $sliver->resource_id();
		print MAP " ";
		print MAP $sliver->sliver_urn();
		print MAP "\n";
	    }
	    close(MAP);
	}
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1216
	if (system("$GENTOPOFILE $pid $eid")) {
1217
	    $msg .= "$GENTOPOFILE failed\n";
1218
	    goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1219
1220
	}
	if (system("$EXPORTS_SETUP")) {
1221
	    $msg .= "$EXPORTS_SETUP failed\n";
1222
	    goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1223
	}
1224
	if (system("$ARPLOCKDOWN ")) {
Leigh B Stoller's avatar
Leigh B Stoller committed
1225
1226
1227
	    $msg .= "$ARPLOCKDOWN failed\n";
	    goto bad;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1228
1229
	# The nodes will not boot locally unless there is a DNS record.
	if (system("$NAMEDSETUP")) {
1230
	    $msg .= "$NAMEDSETUP failed\n";
1231
	    goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1232
	}
1233
1234
	my @diff = ();
	my @same = ();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1235
	    
1236
1237
	if (Lan->CompareVlansWithSwitches($experiment, \@diff, \@same)) {
	    print STDERR "CompareVlansWithSwitches failed!\n";
1238
	    goto bad;
1239
1240
1241
1242
	}
	if (@diff) {
	    system("$SNMPIT -f ". join(" ", map("-o $_", @diff)));
	    if ($?) {
1243
1244
		$msg .= "Failed to remove obsolete VLANs.";
		goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1245
1246
	    }
	}
1247
	system("$SNMPIT -t $pid $eid");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1248
	if ($?) {
1249
1250
	    $msg .= "Failed to setup vlans";
	    goto bad;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1251
	}
1252
1253
1254
1255
	if ($experiment->SetupPortLans()) {
	    $msg .= "Failed to setup shared vlan ports";
	    goto bad;
	}
1256
1257
1258
1259
	if ($experiment->SyncPortLans()) {
	    $msg .= "Failed to add ports to shared vlans";
	    goto bad;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1260
1261
    }

1262
    #
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
    # Before anything, the firewall has to be turned on or rebooted.
    # Then we have to wait for it to come up before we can let the
    # rest of the nodes go. 
    #
    if ($firewalled &&
	(exists($poweron{$firewall}) || exists($reboots{$firewall}))) {
	my $node_id;
	require StateWait;
	require EmulabConstants;

	if (exists($poweron{$firewall})) {
	    my $node = $poweron{$firewall};
	    $node_id = $node->node_id();
	    
	    print STDERR "Powering on the firewall: $node_id\n";
	
	    system("$POWER on $node_id");
	    if ($?) {
		$msg .= "Failed to power on firewall: $node_id";
		$firewall_sliver->SetStatus("failed");
		goto bad;
	    }
	    delete($poweron{$firewall});
	}
	else {
	    my $node = $reboots{$firewall};
	    $node_id = $node->node_id();
	    
	    print STDERR "Rebooting the firewall: $node_id\n";
	    
	    system("$NODEREBOOT $node_id");
	    if ($?) {
		$msg .= "Failed to reboot firewall: $node_id";
		$firewall_sliver->SetStatus("failed");
		goto bad;
	    }
	    delete($reboots{$firewall});
	}
	$StateWait::debug = 0;
	my @states = (EmulabConstants::TBDB_NODESTATE_ISUP());

	if (StateWait::initStateWait(\@states, $node_id)) {
	    $msg .=  "Failed to initialize the statewait library!";
	    $firewall_sliver->SetStatus("failed");
	    goto bad;
	}
	my @finished = ();
	my @failed   = ();
	
	# Now we can statewait.
	print STDERR "Waiting for firewall ($node_id) to boot\n";
	
	if (StateWait::waitForState(\@finished, \@failed, (15 * 60))) {
	    $msg .= "Failed in waitForState for firewall: $node_id!";
	    $firewall_sliver->SetStatus("failed");
	    goto bad;
	}
	StateWait::endStateWait();

	#
	# Note that waitForState does not view timeout as failure,
	# so if both @finished and @failure are empty, we timed out.
	# Timeout is failure in this case.
	#
	@failed = ($node_id)
	    if (! (@finished || @failed));

	if (@failed) {
	    $msg .= "Firewall failed to boot properly: $node_id!";
	    $firewall_sliver->SetStatus("failed");
	    goto bad;
	}
    }

    #
    # Then power on any physical nodes that had been stopped.
1339
    # Then reboot the physical nodes, then any leftover virtual nodes.
1340
    #
1341
1342
1343
1344
1345
1346
1347
    if (keys(%poweron)) {
	my @node_ids = keys(%poweron);

	#
	# Should waiting be an option?
	#
	system("$POWER on @node_ids");
1348
1349
1350
1351
	if ($?) {
	    $msg .= "Failed to power on @node_ids";
	    goto bad;
	}
1352
    }
1353
1354
1355
1356
1357
1358
1359
1360
1361
    if (keys(%vnodekills)) {
	my @node_ids = keys(%vnodekills);

	system("$VNODESETUP -jk -m $pid $eid @node_ids");
	if ($?) {
	    $msg .= "Failed to kill vnodes @node_ids";
	    goto bad;
	}
    }
1362
1363
    if (keys(%reboots)) {
	my @node_ids = keys(%reboots);
1364
1365
1366
1367
1368

	#
	# Should waiting be an option?
	#
	system("$NODEREBOOT @node_ids");
1369
1370
1371
1372
	if ($?) {
	    $msg .= "Failed to reboot @node_ids";
	    goto bad;
	}
1373
1374
1375
    }
    if (keys(%vnodes)) {
	my @node_ids = keys(%vnodes);
1376
1377
1378
1379

	#
	# Should waiting be an option?
	#
1380
	system("$VNODESETUP -j -m $pid $eid @node_ids");
1381
1382
1383
1384
	if ($?) {
	    $msg .= "Failed to set up vnodes @node_ids";
	    goto bad;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1385
    }
1386
1387
1388
1389
1390
1391
1392
    #
    # Start the event scheduler.
    #
    my $action = ($restart ? "replay" : "start");
    system("$EVENTSYS $action $pid,$eid");
    if ($?) {
	$msg .= "Failed to (re)start the event system";
1393
1394
1395
1396
1397
1398
1399
	if ($TB ne "/usr/testbed") {
	    # Not sure why this is failing.
	    print STDERR "$msg\n";
	}
	else {
	    goto bad;
	}
1400
1401
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1402
1403
1404
1405
1406
1407
1408
    #
    # Worked? Set the new state. Needs more thought ...
    #
    foreach my $sliver (@slivers) {
	$sliver->SetState("started")
	    if (ref($sliver) eq "GeniSliver::Node");
    }
1409
    if (1) {
1410
1411
	$self->WaitForNodes(@waitpnodes, @waitvnodes);
    }
1412
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1413
    return 0;
1414
1415
1416
1417
1418
1419

  bad:
    if (defined($msg)) {
	$self->SetErrorLog($msg);
	print STDERR "$msg\n";
    }
1420
1421
1422
1423
    # Mark the offending sliver as failed.
    if (defined($sliver)) {
	$sliver->SetStatus("failed");
    }
1424
    return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1425
1426
}

1427
1428
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
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
#
# Wait for nodes
#
sub WaitForNodes($@)
{
    my ($self, @nodes) = @_;
    my %nodes = ();
    my @waitstates = (TBDB_NODESTATE_TBFAILED, TBDB_NODESTATE_ISUP);

    return 0
	if (!@nodes);
    
    my $slice = $self->GetSlice();
    if (!defined($slice)) {
	print STDERR "WaitForNodes: Could not map $self to its slice\n";
	return -1;
    }
    
    my $experiment = Experiment->Lookup($self->slice_uuid());
    if (!defined($experiment)) {
	print STDERR "Could not map $self to its experiment\n";
	return -1;
    }

    my $group = $experiment->GetGroup();
    if (!defined($group)) {
	print STDERR "Could not map $self to its experiment group\n";
	return -1;
    }

    my $creator = $self->GetCreator();
    if (!defined($creator)) {
	print STDERR "Could not map $self to its creator\n";
	return -1;
    }

    #
    # At this point we want to return and let the startsliver proceed
    # in the background. 
    #
    my $mypid = main::WrapperFork();
    if ($mypid) {
	return 0;
    }
    $slice->SetMonitorPid($PID);

    #
    # This is essentially what libossetup (os_setup) does. I want to
    # eventually use that code directly, but that will require some
    # restructuring in that code.
    #
    my %childcounts = ();
    
    # Array from the list.
    foreach my $node (@nodes) {
	$nodes{$node->node_id()} = $node;
	$node->_waitstart(time());
	$node->_waitend(undef);
	$node->Refresh();
	#
	# Count up number of virtnodes on each physnode.
	#
	if ($node->isvirtnode()) {
	    if (!exists($childcounts{$node->phys_nodeid()})) {
		$childcounts{$node->phys_nodeid()} = 0;
	    }
	    $childcounts{$node->phys_nodeid()} += 1;
	}
    }
    # Set the waitmax time for each node. 
    foreach my $node (@nodes) {
1498
	$node->_maxwait(1000 + ($node->_reloaded() ? 600 : 0));
1499
1500
	
	if ($node->isvirtnode()) {
1501
1502
1503
	    #
	    # Bump waitime according to number of virtnodes on each physnode.
	    #
1504
	    $node->_maxwait($node->_maxwait() +
1505
			    ($childcounts{$node->phys_nodeid()} * 150));
1506
1507
1508
1509
1510
1511
1512
1513
1514
	    
	    #
	    # If the parent of a virtnode is not in the list, it
	    # is not going to be rebooted; it is ready. Set the waitend
	    # for the parent to now, for the loop below. 
	    #
	    if (!exists($nodes{$node->_parent()})) {
		$node->_parent()->_waitend(time());
	    }
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
	}
    }

    #
    # Start a counter going, relative to the time we rebooted the first
    # node.
    #
    my $waittime  = 0;
    my $minutes   = 0;
    my $canceled  = $experiment->canceled();

    #
    # Wait for the nodes to finish booting, as recorded in database.
    #
    while (keys(%nodes)) {
	#
	# Check for cancelation. We quit the monitor.
	#
	$canceled = $experiment->canceled();
	if ($canceled) {
	    print STDERR "WaitForNodes canceled; terminating early!\n";
	    # Reset before return; do not want it left.
	    $slice->LockTables();
	    $experiment->SetCancelFlag(0);
	    $slice->ClearMonitorPid();
	    $slice->UnLockTables();
	    return -1;
	}
	
	#
	# We want to do this in order the nodes were passed in, so do not
	# use the array for the list.
	#
	foreach my $node (@nodes) {
	    my $node_id = $node->node_id();

	    # Already done?
	    next
		if (!exists($nodes{$node_id}));

	    #
	    # If this is a virtnode, check to see if the parent node
	    # failed to boot. No point in going on. Also reset the
	    # start time to the time that the parent came ready.
	    #
	    if ($node->isvirtnode() && defined($node->_parent())) {
		my $parent = $node->_parent();

		# Skip if still waiting on the parent.
		next
		    if (!defined($parent->_waitend()));
Leigh B Stoller's avatar
Leigh B Stoller committed
1566
1567
1568
1569

		if (defined($parent->_sliver()) &&
		    $parent->_sliver()->status() eq "failed") {
		    $node->_sliver()->SetStatus("failed");
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
		    $node->_waitend(time());
		    delete($nodes{$node_id});
		    next;
		}
		$node->_waitstart($parent->_waitend());
	    }
	    my $state;

	    if ($node->GetEventState(\$state)) {
		print STDERR "*** Error getting event state for $node_id.\n";
1580
1581
		$node->_sliver()->SetStatus("failed")
		    if (defined($node->_sliver()));
1582
1583
1584
1585
1586
1587
		$node->_waitend(time());
		delete($nodes{$node_id});
		next;
	    }
	    if (grep {$_ eq $state} @waitstates) {
		print "$node_id has reported state $state\n";
1588
1589
		$node->_sliver()->ComputeStatus()
		    if (defined($node->_sliver()));
1590
1591
1592
1593
1594
1595
1596
1597
1598
		$node->_waitend(time());
		delete($nodes{$node_id});
		next;
	    }
	    $waittime = time() - $node->_waitstart();
	    if ($waittime > $node->_maxwait()) {
		$minutes = int($waittime / 60);
		print STDERR "*** Giving up on $node_id ($state) - ".
		    "it's been $minutes minute(s).\n";
1599
1600
		$node->_sliver()->SetStatus("failed")
		    if (defined($node->_sliver()));
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
		$node->_waitend(time());
		delete($nodes{$node_id});
		next;
	    }
	    if (int($waittime / 60) > $minutes) {
		# Changing minutes is why we get this print for just
		# a single node each time. 
		$minutes = int($waittime / 60);
		print STDERR "Still waiting for $node_id ($state) - ".
		    "it's been $minutes minute(s).\n";
	    }
	}
	sleep(5);
    }

    #
    # Go through nodes and see what failed. 
    #
    my @failed = ();
    
    foreach my $node (@nodes) {
	my $node_id = $node->node_id();

	push(@failed, $node)
1625
1626
	    if (defined($node->_sliver()) &&
		$node->_sliver()->status() eq "failed");
1627

1628
1629
	$node->_bootlog(undef);
	
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
	#
	# Create a logfile from the boot log.
	#
	if (grep {$_ eq $node->eventstate()} @waitstates) {
	    my $bootlog;
	    if ($node->GetBootLog(\$bootlog) == 0 && $bootlog ne "") {
		my $logfile = Logfile->CreateFromString($group, $bootlog);
		if (defined($logfile)) {
		    $logfile->SetMetadata([["bootlog"   , $node->node_id()],
					   ["Method",     "reboot $node_id"],
					   ["slice_idx" , $slice->idx()],
					   ["slice_urn" , $slice->urn()],
					   ["slice_uuid", $slice->uuid()]], 1);
1643
1644
                     # Anon users can view the log if they know the secret id. 
		    $logfile->SetPublic(1);
1645
		    $logfile->Store();
1646
		    $node->_bootlog($logfile);
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
		}
	    }
	}
    }
    #
    # Notify.
    #
    if (@failed) {
	my $name   = $creator->name();
	my $email  = $creator->email();
	my $count  = scalar(@failed);
	my $urn    = $slice->urn();
1659
1660
1661
1662
1663
1664
1665
1666
1667
	my $logs   = "";

	foreach my $node (@failed) {
	    next
		if (!defined($node->_bootlog()));

	    $logs .= sprintf("%-15s : %s\n",
			     $node->node_id(), $node->_bootlog()->URL());
	}
1668
1669
1670

	SENDMAIL("$name <$email>", "$count nodes failed to boot",
		 "Nodes:\n".
1671
		 "  " . join(" ", @failed) . "\n".
1672
1673
		 "in $urn failed.\n\n" .
		 "$logs\n\n",