GeniSliver.pm.in 42.8 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1
2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2008-2016 University of Utah and the Flux Group.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 
# {{{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
33
34
35
36
37
38
39
40
#
package GeniSliver;

#
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

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

use GeniDB;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41
use GeniComponent;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
42
use GeniSlice;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
43
use GeniCredential;
44
use GeniCertificate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45
use GeniAggregate;
46
use GeniUsage;
47
use GeniHRN;
48
use GeniXML;
49
use GeniEvent;
50
use emutil;
51
use Node;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
52
use English;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
53
use XML::Simple;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
54
55
use Data::Dumper;
use File::Temp qw(tempfile);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
56
use overload ('""' => 'Stringify');
Leigh B. Stoller's avatar
Leigh B. Stoller committed
57
58
59
60
61
62
63
64

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
65
my $PGENIDOMAIN    = "@PROTOGENI_DOMAIN@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66
my $SIGNCRED	   = "$TB/sbin/signgenicred";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
67
68
69
my $AVAIL	   = "$TB/sbin/avail";
my $NALLOC	   = "$TB/bin/nalloc";
my $NFREE	   = "$TB/bin/nfree";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
70
my $NODEREBOOT	   = "$TB/bin/node_reboot";
71
my $NAMEDSETUP     = "$TB/sbin/named_setup";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
72
73
my $PLABNODE       = "$TB/sbin/plabnodewrapper";
my $VNODESETUP     = "$TB/sbin/vnode_setup";
74
my $GENTOPOFILE    = "$TB/libexec/gentopofile";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
75
my $POWER          = "$TB/bin/power";
76
my $OSLOAD         = "$TB/bin/os_load";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
77
78
79

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

#
83
# Lookup by URN, idx, or uuid.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
84
85
86
87
88
#
sub Lookup($$)
{
    my ($class, $token) = @_;
    my $query_result;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
89
    my $idx;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
90

91
92
93
    if (GeniHRN::IsValid($token)) {
	my ($authority, $type, $id) = GeniHRN::Parse($token);
	return undef if $type ne "sliver";
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

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

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

	    ($idx) = $query_result->fetchrow_array();
	}
112
113
    }
    elsif ($token =~ /^\d+$/) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
114
	$idx = $token;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
115
116
117
    }
    elsif ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	$query_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
118
	    DBQueryWarn("select idx from geni_slivers ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
119
			"where uuid='$token'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
120
121
122
123
	    return undef
		if (! $query_result || !$query_result->numrows);

	    ($idx) = $query_result->fetchrow_array();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
124
125
126
127
    }
    else {
	return undef;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
128
129
130
131
132
133
134
135

    # Look in cache first
    return $slivers{"$idx"}
        if (exists($slivers{"$idx"}));

    $query_result = DBQueryWarn("select * from geni_slivers ".
				"where idx='$idx'");

Leigh B. Stoller's avatar
Leigh B. Stoller committed
136
137
138
    return undef
	if (!$query_result || !$query_result->numrows);

139
140
141
142
143
144
    my $self               = {};
    $self->{'SLIVER'}      = $query_result->fetchrow_hashref();
    $self->{'SLICE'}       = undef;	# server
    $self->{'AGGREGATE'}   = undef;	# server
    $self->{'RSPEC'}       = undef;	# server
    $self->{'CERTIFICATE'} = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
145
146
147

    my $rspec_string = $self->{'SLIVER'}->{'rspec_string'};
    if (defined($rspec_string) && $rspec_string ne "") {
148
149
	my $rspec = GeniXML::Parse($rspec_string);
	if (!defined($rspec)) {
150
151
152
	    return undef;
	}
	$self->{'RSPEC'} = $rspec;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
153
    }
154
155
156
157
158

    #
    # Grab the certificate, since we will probably want it.
    #
    my $uuid = $self->{'SLIVER'}->{'uuid'};
159
    if (0) {
160
161
162
163
164
165
    my $certificate = GeniCertificate->Lookup($uuid);
    if (!defined($certificate)) {
	print STDERR "Could not find certificate for sliver $idx ($uuid)\n";
	return undef;
    }
    $self->{'CERTIFICATE'} = $certificate;
166
    }
167
168
169
170

    # Bless into sub package if called for.
    my $resource_type = $self->{'SLIVER'}->{'resource_type'};
    if (defined($resource_type) && $resource_type ne "") {
171
	bless($self, $class . "::" . $resource_type);
172
173
174
175
    }
    else {
	bless($self, $class);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    
    # Add to cache. 
    $slivers{$self->{'SLIVER'}->{'idx'}} = $self;
    
    return $self;
}

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

    return "[GeniSliver: $uuid, IDX: $idx]";
}

196
197
198
199
200
201
202
203
204
205
#
# Flush from our little cache, as for the expire daemon.
#
sub Flush($)
{
    my ($self) = @_;

    delete($slivers{$self->idx()});
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
206
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
207
# Create a sliver record in the DB. On the client side we save the credential
Leigh B. Stoller's avatar
Leigh B. Stoller committed
208
# that allows control of it, for later operations.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
209
#
210
sub Create($$$$$$$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
211
{
212
    my ($class, $slice, $owner, 
213
	$resource_uuid, $resource_type, $resource_id,
214
	$hrn, $nickname, $rspec) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
215
216
    my @insert_data = ();

Jonathon Duerig's avatar
Jonathon Duerig committed
217
218
219
220
221
222
223
224
225
226
227
228
    my $urn;
    # Get sliver id from the ticket if available.
    if (defined($rspec)) {
	$urn = GeniXML::GetSliverId($rspec);
    }
    # Otherwise, generate a fresh one.
    if (! defined($urn)) {
	# Every sliver gets a new unique index.
	my $sliver_idx = TBGetUniqueIndex('next_sliver', 1);
	$urn = GeniHRN::Generate("@OURDOMAIN@", "sliver", $sliver_idx);
    }
    my (undef, undef, $idx) = GeniHRN::Parse($urn);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
229

230
231
    # Sanity check.
    my $certificate = GeniCertificate->Lookup($urn);
232
    if (0) {
233
234
235
236
    if (defined($certificate)) {
	print STDERR "GeniSliver::Create: ".
	    "Already have a certificate for $hrn/$urn\n";
	return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
237
    }
238
239
240
    $certificate = GeniCertificate->Create({'urn'  => $urn,
					    'hrn'  => $hrn,
					    'email'=> $TBOPS});
241
242
    if (!defined($certificate)) {
	print STDERR "GeniSliver::Create: ".
243
	    "Could not generate new certificate for $hrn/$urn\n";
244
	return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
245
    }
246
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
247
    my $slice_uuid     = $slice->uuid();
248
    my $owner_uuid     = $owner->uuid();
249
250
    my $sliver_uuid    = (defined($certificate) ?
			  $certificate : GeniUtil::NewUUID());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
251
252
253
254

    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
    push(@insert_data, "idx='$idx'");
255
256
257
    push(@insert_data, "hrn=" . DBQuoteSpecial($hrn));
    push(@insert_data, "nickname=" . DBQuoteSpecial($nickname))
	if (defined($nickname));
258
    push(@insert_data, "uuid='$sliver_uuid'");
259
260
    push(@insert_data, "resource_uuid='$resource_uuid'");
    push(@insert_data, "resource_type='$resource_type'");
261
    push(@insert_data, "resource_id='$resource_id'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
262
263
    push(@insert_data, "creator_uuid='$owner_uuid'");
    push(@insert_data, "slice_uuid='$slice_uuid'");
264
265
    # Start out new slivers, as new.
    push(@insert_data, "state='new'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
266

Leigh B. Stoller's avatar
Leigh B. Stoller committed
267
    if (defined($rspec)) {
268
	my $rspec_string = GeniXML::Serialize($rspec);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
269
270
271
272
273
	my $safe_rspec   = DBQuoteSpecial($rspec_string);

	push(@insert_data, "rspec_string=$safe_rspec");
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
274
    # Insert into DB.
275
276
    if (!DBQueryWarn("insert into geni_slivers set " .
		     join(",", @insert_data))) {
277
278
	$certificate->Delete()
	    if (defined($certificate));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
279
280
281
	return undef;
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
282
283
284
    my $sliver = GeniSliver->Lookup($idx);
    return undef
	if (!defined($sliver));
285
286
287
288
289

    if (GeniUsage->NewSliver($sliver, $slice, $owner)) {
	print STDERR
	    "GeniSliver::Create: GeniUsage->NewSliver($sliver) failed\n";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
290
    $sliver->{'AGGREGATE'} = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
291
    $sliver->{'SLICE'}     = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
292
293

    return $sliver;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
294
295
296
297
298
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'SLIVER'}->{$_[1]}); }
sub idx($)		{ return field($_[0], "idx"); }
sub uuid($)		{ return field($_[0], "uuid"); }
299
300
sub hrn($)		{ return field($_[0], "hrn"); }
sub nickname($)		{ return field($_[0], "nickname"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
301
302
303
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
304
sub registered($)	{ return field($_[0], "registered"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
305
sub credential_idx($)	{ return field($_[0], "credential_idx"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
306
sub resource_uuid($)	{ return field($_[0], "resource_uuid"); }
307
sub resource_id($)	{ return field($_[0], "resource_id"); }
308
sub resource_type($)	{ return field($_[0], "resource_type"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
309
310
311
sub component_uuid($)	{ return field($_[0], "component_uuid"); }
sub aggregate_uuid($)	{ return field($_[0], "aggregate_uuid"); }
sub rspec_string($)     { return field($_[0], "rspec_string"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
312
sub status($)		{ return field($_[0], "status"); }
313
sub state($)		{ return field($_[0], "state"); }
314
sub ErrorLog($)		{ return field($_[0], "errorlog"); }
315
sub cert($)		{ return GetCertificate($_[0])->cert(); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
316
sub rspec($)            { return $_[0]->{'RSPEC'}; }
317
318
319
320
321
322
323
324
# Watch for slivers that no longer get a certificate.
sub GetCertificate($)
{
    if (!defined($_[0]->{'CERTIFICATE'})) {
	print STDERR "*** No certificate for sliver: " . $_[0] . "\n";
    }
    return $_[0]->{'CERTIFICATE'};
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
325

326
327
328
329
330
331
332
333
# Return the sliver URN. 
sub sliver_urn($)
{
    my ($self)  = @_;

    return GeniHRN::Generate("@OURDOMAIN@", "sliver", $self->idx());
}

334
335
336
337
338
339
340
341
342
343
344
345
346
347
#
# Equality test. Not strictly necessary in perl, but good form.
#
sub SameSliver($$)
{
    my ($self, $other) = @_;

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

    return $self->idx() == $other->idx();
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
348
349
350
#
# Delete the sliver. The sliver should not be provisioned when this done.
#
351
sub Delete($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
352
{
353
    my ($self, $purge) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
354
355
356
357

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

358
359
    my $idx  = $self->idx();
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
360

361
362
363
364
    if (GeniUsage->DestroySliver($self, $purge)) {
	print STDERR
	    "GeniSliver::Delete: GeniUsage->DestroySliver($self) failed\n";
    }
365
366
    DBQueryWarn("delete from geni_credentials where this_uuid='$uuid'")
	or return -1;
367
368
    DBQueryWarn("delete from geni_certificates where uuid='$uuid'")
	or return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
369
370
    DBQueryWarn("delete from geni_slivers where idx='$idx'")
	or return -1;
371

372
373
374
    # Delete from cache. 
    delete($slivers{$idx});
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
375
376
377
    return 0;
}

378
379
380
381
382
383
384
385
386
#
# Annotate the rspec with the proper information to become
# a manifest. Returns undef because type is unknown
#
sub AnnotateManifest($)
{
    return undef;
}

387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#
# Get the manifest for an aggregate. Returns the XML string.
#
sub GetManifest($$)
{
    my ($self, $asxml) = @_;

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

    my $manifest = $self->rspec();

    return $manifest
	if (!$asxml);
    
402
    my $xml = GeniXML::Serialize($self->rspec());
403
404
405
    return $xml;
}

406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#
# Store the rspec/manifest string.
#
sub UpdateRspec($$)
{
    my ($self, $rspec) = @_;

    my $idx          = $self->idx();
    my $rspec_string = GeniXML::Serialize($rspec);
    my $safe_rspec   = DBQuoteSpecial($rspec_string);

    return -1
	if (!DBQueryWarn("update geni_slivers set ".
			 "  rspec_string=$safe_rspec ".
			 "where idx='$idx'"));
    
    $self->{'RSPEC'} = $rspec;
423
    $self->{'SLIVER'}->{'rspec_string'} = $rspec_string;
424
425
426
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
427
428
429
430
431
432
433
434
435
436
#
# Set the aggregate for a sliver.
#
sub SetAggregate($$)
{
    my ($self, $aggregate) = @_;

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
437
438
    my $idx      = $self->idx();
    my $agg_uuid = $aggregate->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
439
440
441

    return -1
	if (!DBQueryWarn("update geni_slivers set ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
442
			 "  aggregate_uuid='$agg_uuid' ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
443
444
			 "where idx='$idx'"));
    
445
446
447
448
449
450
451
    if (!DBQueryWarn("update sliver_history set ".
		     "  aggregate_uuid='$agg_uuid' ".
		     "where idx='$idx'")) {
	print STDERR "GeniSliver::SetAggregate: ".
	    "Failed to update sliver_history for $self\n";
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
452
    $self->{'SLIVER'}->{'aggregate_uuid'} = $agg_uuid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
453
454
455
456
    $self->{'AGGREGATE'} = $aggregate;
    return 0;
}

457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#
# And clear the aggregate.
#
sub ClearAggregate($$)
{
    my ($self) = @_;

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

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

    return -1
	if (!DBQueryWarn("update geni_slivers set ".
			 "  aggregate_uuid=NULL ".
			 "where idx='$idx'"));
    
    $self->{'SLIVER'}->{'aggregate_uuid'} = undef;
    $self->{'AGGREGATE'} = undef;
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
479
480
481
482
483
484
485
486
487
488
489
490
#
# Get the aggregate for a sliver.
#
sub GetAggregate($)
{
    my ($self) = @_;

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

    return $self->{'AGGREGATE'} if (defined($self->{'AGGREGATE'}));
    return undef
Leigh B. Stoller's avatar
Leigh B. Stoller committed
491
	if (!defined($self->aggregate_uuid()));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
492

Leigh B. Stoller's avatar
Leigh B. Stoller committed
493
    my $aggregate = GeniAggregate->Lookup($self->aggregate_uuid());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
494
495
496
497
498
499
500
501
    if (!defined($aggregate)) {
	print STDERR "Could not get aggregate object associated with $self\n";
	return undef;
    }
    $self->{'AGGREGATE'} = $aggregate;
    return $aggregate;
}

502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#
# Set the status for the sliver.
#
sub SetStatus($$)
{
    my ($self, $status) = @_;

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

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

523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#
# Set the state for the sliver.
#
sub SetState($$)
{
    my ($self, $state) = @_;

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

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

544
545
546
547
548
549
550
551
#
# Generate the blob for status. Redefined below.
#
sub GenerateStatusBlob($)
{
    return undef;
}

552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#
# 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_slivers set ".
			 "  errorlog=$safe_log ".
			 "where idx='$idx'"));
    
    $self->{'SLIVER'}->{'errorlog'} = $log;
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
574
575
576
577
578
579
#
# Get the experiment for the slice this sliver belongs to.
#
sub GetExperiment($)
{
    my ($self) = @_;
580
    require Experiment;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
581
582
583
584
585
586
587

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

    return Experiment->Lookup($self->slice_uuid());
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#
# Get the slice for the sliver.
#
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;
}

613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#
# The expiration time for a sliver 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();
}

631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#
# Look up a list of slivers for a locally instantiated slice. 
# Used by the CM.
#
sub SliceSlivers($$$)
{
    my ($class, $slice, $pref) = @_;

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

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

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

658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#
# Find slivers "dependent" on this sliver, as for interfaces on nodes.
#
sub DependentSlivers($$)
{
    my ($self, $pref) = @_;

    return -1
	if (! (ref($self) && ref($pref)));
    @$pref = ();

    my $idx = $self->idx();
    my $resource_uuid = $self->resource_uuid();

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

    my @result = ();
    while (my ($idx) = $query_result->fetchrow_array()) {
	my $sliver = GeniSliver->Lookup($idx);
	if (!defined($sliver)) {
	    print STDERR "Could not get sliver object for $idx\n";
	    return -1;
	}
	push(@result, $sliver);
    }
    @$pref = @result;
    return 0;
}

691
692
693
694
695
696
697
698
699
700
701
702
703
############################################################################
#
# The server side methods are in packages which inherit from above.
#
package GeniSliver::Node;
use vars qw(@ISA);
@ISA = "GeniSliver";

use GeniDB;
use GeniComponent;
use GeniSlice;
use GeniCredential;
use GeniCertificate;
704
use GeniUtil;
705
use emutil;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
706
use XML::Simple;
707
use Data::Dumper;
708
use libdb qw(TBDB_ALLOCSTATE_RES_INIT_DIRTY TBDB_NODESTATE_SHUTDOWN
709
	     TBResolveNextOSID TBDB_NODESTATE_ISUP TBDB_NODESTATE_TBFAILED
710
             TBDB_NODESTATE_RELOADFAILED TBDB_NODESTATE_PXEWAIT);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
711

712
713
# Error log for local physical node. This overrides the default method above,
# since it is stored in the node.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
714
715
716
717
718
sub ErrorLog($)
{
    my ($self)  = @_;
    my $bootlog = "";

719
    my $node = Node->Lookup($self->resource_id());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
720
721
722
723
724
725
726
727
728
729
    if (!defined($node)) {
	print STDERR "Could not map node $self to its object\n";
	return "";
    }
    if ($node->GetBootLog(\$bootlog)) {
	print STDERR "Could not get bootlog for $self ($node)\n";
	return "";
    }
    return $bootlog;
}
730

731
732
733
# Return the resource URN. This is how a resource is resolved.
# Resource urns may differ from component_urns (pcvm201-1 rather than pc201).
sub resource_urn($)
734
735
736
737
738
739
{
    my ($self)  = @_;

    return GeniHRN::Generate("@OURDOMAIN@", "node", $self->resource_id())
}

740
sub Create($$$$$$)
741
{
742
    # $rspec is a LibXML element representing a single node.
Leigh B Stoller's avatar
Leigh B Stoller committed
743
    my ($class, $slice, $user, $node, $rspec) = @_;
Jonathon Duerig's avatar
Jonathon Duerig committed
744
    my $virtualization_type = GeniXML::GetVirtualizationType($rspec);
745
    if (!defined($virtualization_type)) {
746
747
748
	print STDERR "Node does not contain a virtualization_type\n";
	return undef;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
749
750
751
752

    my $experiment = $slice->GetExperiment();
    if (!defined($experiment)) {
	print STDERR "Could not map $slice to its experiment\n";
753
	return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
754
    }
755

756
757
    #
    # An artifact of Emulab is that for shared/remote nodes, the physical
758
    # node is already allocated, but not to the current experiment.
759
760
761
    #
    if (! ($node->sharing_mode() ||
	   ($node->isremotenode() && $node->isvirtnode()))) {
762
763
764
765
766
767
	my $reservation = $node->Reservation();
	if (!defined($reservation)) {
	    print STDERR "$node was already released from $slice\n";
	    return undef;
	}
	if (! $reservation->SameExperiment($experiment)) {
768
	    print STDERR "$node is reserved to another, not $experiment\n";
769
770
771
	    # Signal error so we can look at what happened.
	    return undef;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
772
    }
773
    my $nickname = GeniXML::GetVirtualId($rspec);
774
    if (!defined($nickname)) {
775
776
777
	print STDERR "Node does not contain a virtual_id\n";
	return undef;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
778
779
780
    my $resource_uuid = $node->uuid();
    my $resource_id   = $node->node_id();
    my $hrn           = "${PGENIDOMAIN}." . $node->node_id();
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811

    my $sliver = GeniSliver->Create($slice, $user, $resource_uuid, "Node",
				    $resource_id, $hrn, $nickname, $rspec);
    return undef
	if (!defined($sliver));

    return $sliver;
}

#
# Annotate the rspec of this node with the proper information to become
# a manifest. Returns LibXML tree or undef on failure.
#
sub AnnotateManifest($)
{
    my ($self) = @_;
    my $rspec = GeniXML::Parse($self->rspec_string());
    if (! defined($rspec)) {
	print STDERR "Could not parse manifest rspec for node $self\n";
	return undef;
    }
    my $node = Node->Lookup($self->resource_id());
    if (!defined($node)) {
	print STDERR "Could not map node $self to its object\n";
	return undef;
    }
    my $user = GeniUser->Lookup($self->creator_uuid(), 1);
    if (!defined($user)) {
	print STDERR "Could not map sliver $self to its creator\n";
	return undef;
    }
812
813
814
815
816
    my $experiment = $self->GetExperiment();
    if (!defined($experiment)) {
	print STDERR "Could not lookup experiment for sliver $self\n";
	return undef;
    }
817

Leigh B Stoller's avatar
Leigh B Stoller committed
818
819
    my $sshdport      = 22;
    my $hostname      = GeniUtil::FindHostname($node->node_id());
820
    my $sshdhost      = $hostname;
821
    my $vhostname     = GeniUtil::FindVHostname($experiment, $node);
822
    my $component_urn = GeniHRN::Generate("@OURDOMAIN@", "node",
823
					  $node->phys_nodeid());
824
    
Leigh B Stoller's avatar
Leigh B Stoller committed
825
    if ($node->isvirtnode()) {
826
827
828
829
830
831
832
833
834
835
836
837
838
	my $vname = $node->vname();
	my $public_control;
	$experiment->GetVirtNodeAttribute( $vname, "routable_control_ip",
					   \$public_control );
	if( defined( $public_control ) && $public_control eq "true" ) {
	    # we've assigned a public address to this one
	    $sshdhost = $hostname;
	} else {
	    # this node has no public control address of its own -- give the
	    # sshd port on the physical host's address
	    $sshdport = $node->sshdport();
	    $sshdhost = GeniUtil::FindHostname($node->phys_nodeid());
	}
839
840
841
842
843
    }

    #
    # Add this stuff to the rspec (which becomes the manifest).
    #
Jonathon Duerig's avatar
Jonathon Duerig committed
844
    if (GeniXML::IsVersion0($rspec)) {
845
	GeniXML::SetText("hostname", $rspec, $sshdhost);
Jonathon Duerig's avatar
Jonathon Duerig committed
846
847
	GeniXML::SetText("sshdport", $rspec, $sshdport)
	    if (defined($sshdport));
848
849
	GeniXML::SetText("sliver_uuid", $rspec, $self->uuid());
	GeniXML::SetText("sliver_urn", $rspec, $self->sliver_urn());
850
	GeniXML::SetText("component_urn", $rspec, $self->resource_urn());
851
852
    } else {
	GeniXML::SetText("sliver_id", $rspec, $self->sliver_urn());
853
	GeniXML::SetText("component_id", $rspec, $component_urn);
854
855
856
857
858
	my $host = GeniXML::FindFirst("n:host", $rspec);
	if (! defined($host)) {
	    $host = GeniXML::AddElement("host", $rspec);
	}
	GeniXML::SetText("name", $host, $vhostname);
Jonathon Duerig's avatar
Jonathon Duerig committed
859
    }
860
861
    my $services = GeniXML::FindFirst("n:services", $rspec);
    
862
863
864
865
866
    if ($experiment->HasNonLocalUsers()) {
	if (! defined($services)) {
	    $services = GeniXML::AddElement("services", $rspec);
	}
	my $login = GeniXML::FindFirst("n:login", $services);
867
	while (defined($login)) {
Leigh B Stoller's avatar
Leigh B Stoller committed
868
	    $services->removeChild($login);
869
870
871
872
873
	    $login = GeniXML::FindFirst("n:login", $services);	    
	}
	my $nonlocalusers;
	$experiment->NonLocalUsers(\$nonlocalusers);
	foreach my $localuser (@$nonlocalusers) {
874
	    $login = GeniXML::AddElement("login", $services);
875
876
877
878
879

	    GeniXML::SetText("authentication", $login, "ssh-keys");
	    GeniXML::SetText("hostname", $login, $sshdhost);
	    GeniXML::SetText("port", $login, $sshdport);
	    GeniXML::SetText("username", $login, $localuser->{"login"});
880
881
	}
    }
882
    my $tipserver = undef;
883
884
885
886
887
888
889
    #
    # The tipline for a virtual node will not exist till its booted,
    # since the capture runs on the physical host. But thats too late,
    # it needs to be in the manifest now. So lets just say we have
    # one, expecting it to be available by the time the user might
    # want to use it.
    #
890
891
892
    if (!$node->IsTainted() &&
	(($node->TipServer(\$tipserver) == 0 && defined($tipserver)) ||
	 $node->isvirtnode())) {
893
894
895
896
897
898
899
900
901
	if (! defined($services)) {
	    $services = GeniXML::AddElement("services", $rspec);
	}
	my $console = GeniXML::FindNodesNS("n:console", $services,
					   $GeniXML::EMULAB_NS)->pop();
	if (defined($console)) {
	    $services->removeChild($console);
	}
	$console = GeniXML::AddElement("console",$services,$GeniXML::EMULAB_NS);
902
903
	GeniXML::SetText("server", $console,
			 (defined($tipserver) ? $tipserver : $sshdhost));
904
    }
905

906
907
908
909
    if ($self->UpdateRspec($rspec)) {
	print STDERR "Could not insert annotated manifest for node $self ".
	    "into database";
	return undef;
Jonathon Duerig's avatar
Jonathon Duerig committed
910
    }
911
912

    return $rspec;
913
914
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
915
916
#
# Provision a slice. We actually did this when the ticket was requested.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
917
# We fill in some virt table stuff so that tbswap will work.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
918
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
919
sub Provision($;$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
920
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
921
    my ($self, $extraargs) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
922
923
924
925
926
927
928

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

    #
    # the node is already allocated to the sliver, but still need to enter
    # a virt_nodes entry, and possibly more virt table entries, so that the
Leigh B. Stoller's avatar
Leigh B. Stoller committed
929
    # node will boot properly, and is otherwise controllable.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
930
    #
931
    my $experiment = $self->GetExperiment();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
932
933
934
935
    if (!defined($experiment)) {
	print STDERR "Could not map $self to its experiment\n";
	return -1;
    }
936
    my $node_id = $self->resource_id();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
937
    return 0
938
939
	if (!defined($node_id));
    my $node       = Node->Lookup($node_id);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
940
    if (!defined($node)) {
941
	print STDERR "Could not map node $node_id to its object\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
942
943
944
945
	return -1;
    }
    my $reservation = $node->Reservation();
    if (!defined($reservation)) {
946
	print STDERR "$node was already released from slice\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
947
948
	return -1;
    }
949
    if (! $reservation->SameExperiment($experiment)) {
950
	print STDERR "$node is reserved to another, not $experiment\n";
951
952
953
954
955
	# Signal error so we can look at what happened.
	return -1;
    }
    my $pid = $experiment->pid();
    my $eid = $experiment->eid();
956

Jonathon Duerig's avatar
Jonathon Duerig committed
957
    my $virt_type = GeniXML::GetVirtualizationType($self->rspec());
958
    if (!$node->isremotenode() &&
959
	defined($virt_type) &&
960
	$virt_type eq "emulab-vnode") {
961
962
963
964
	my $pnode = Node->Lookup($node->phys_nodeid());
	if (!defined($pnode)) {
	    print STDERR "Could not get pnode object for $node\n";
	    return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
965
	}
966

967
968
	# Mark
	$node->ModifyReservation({"genisliver_idx" => $self->idx()});
969

Jonathon Duerig's avatar
Jonathon Duerig committed
970
	my $subtype = GeniXML::GetVirtualizationSubtype($self->rspec());
971
	if (defined($subtype)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
972
973
974
	    if (!$pnode->sharing_mode()) {
		$pnode->ModifyReservation({"genisliver_idx" => $self->idx()});
	    }
975
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
976
977
    }
    else {
978
979
980
981
982
983
984
985
	#
	# For a "raw" node, there is no vnode, so this is the pnode
	# we need to mark for tmcd.
	#
	if ($node->ModifyReservation({"genisliver_idx" => $self->idx()})) {
	    return -1;
	}
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
986
987
988
989
990
991
    return 0;
}

#
# Unprovision a sliver. 
#
992
sub UnProvision($;$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
993
{
994
    my ($self, $nophysfree) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
995
996
997

    return -1
	if (! ref($self));
998
999
    $nophysfree = 0
	if (!defined($nophysfree));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1000

For faster browsing, not all history is shown. View entire blame