GeniSliver.pm.in 26.4 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1
2
3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2008-2009 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5
6
7
8
9
10
11
12
13
14
15
16
17
# All rights reserved.
#
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
18
use GeniComponent;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
19
use GeniSlice;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
20
use GeniCredential;
21
use GeniCertificate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
22
use GeniAggregate;
23
use GeniUsage;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
24
# Hate to import all this crap; need a utility library.
25
use emutil qw(TBGetUniqueIndex);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26
use Experiment;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
27
use English;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
28
use XML::Simple;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29
30
use Data::Dumper;
use File::Temp qw(tempfile);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31
use overload ('""' => 'Stringify');
Leigh B. Stoller's avatar
Leigh B. Stoller committed
32
33
34
35
36
37
38
39

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
40
my $PGENIDOMAIN    = "@PROTOGENI_DOMAIN@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41
my $SIGNCRED	   = "$TB/sbin/signgenicred";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
42
43
44
my $AVAIL	   = "$TB/sbin/avail";
my $NALLOC	   = "$TB/bin/nalloc";
my $NFREE	   = "$TB/bin/nfree";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45
my $NODEREBOOT	   = "$TB/bin/node_reboot";
46
my $NAMEDSETUP     = "$TB/sbin/named_setup";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
47
48
my $PLABNODE       = "$TB/sbin/plabnodewrapper";
my $VNODESETUP     = "$TB/sbin/vnode_setup";
49
my $GENTOPOFILE    = "$TB/libexec/gentopofile";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
50
51
52

# Cache of instances to avoid regenerating them.
my %slivers      = ();
53
BEGIN { use GeniUtil; GeniUtil::AddCache(\%slivers); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
54
55
56
57
58
59
60
61

#
# Lookup by idx, or uuid.
#
sub Lookup($$)
{
    my ($class, $token) = @_;
    my $query_result;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
62
    my $idx;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
63
64

    if ($token =~ /^\d+$/) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
65
	$idx = $token;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
66
67
68
    }
    elsif ($token =~ /^\w+\-\w+\-\w+\-\w+\-\w+$/) {
	$query_result =
Leigh B. Stoller's avatar
Leigh B. Stoller committed
69
	    DBQueryWarn("select idx from geni_slivers ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
70
			"where uuid='$token'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
71
72
73
74
	    return undef
		if (! $query_result || !$query_result->numrows);

	    ($idx) = $query_result->fetchrow_array();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
75
76
77
78
    }
    else {
	return undef;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
79
80
81
82
83
84
85
86

    # 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
87
88
89
    return undef
	if (!$query_result || !$query_result->numrows);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
90
91
    my $self              = {};
    $self->{'SLIVER'}     = $query_result->fetchrow_hashref();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
92
93
94
95
96
97
98
99
100
101
102
    $self->{'COMPONENT'}  = undef;	# client
    $self->{'SLICE'}      = undef;	# client/server
    $self->{'CREDENTIAL'} = undef;	# client
    $self->{'AGGREGATE'}  = undef;	# server
    $self->{'RSPEC'}      = undef;	# client/server

    my $rspec_string = $self->{'SLIVER'}->{'rspec_string'};
    if (defined($rspec_string) && $rspec_string ne "") {
	$self->{'RSPEC'} = XMLin($rspec_string,
				 ForceArray => ["node", "link"]);
    }
103
104
105
106
107
108
109
110
111
112
113

    #
    # Grab the certificate, since we will probably want it.
    #
    my $uuid = $self->{'SLIVER'}->{'uuid'};
    my $certificate = GeniCertificate->Lookup($uuid);
    if (!defined($certificate)) {
	print STDERR "Could not find certificate for sliver $idx ($uuid)\n";
	return undef;
    }
    $self->{'CERTIFICATE'} = $certificate;
114
115
116
117

    # Bless into sub package if called for.
    my $resource_type = $self->{'SLIVER'}->{'resource_type'};
    if (defined($resource_type) && $resource_type ne "") {
118
	bless($self, $class . "::" . $resource_type);
119
120
121
122
    }
    else {
	bless($self, $class);
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    
    # 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]";
}

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
144
# Create a sliver record in the DB. On the client side we save the credential
Leigh B. Stoller's avatar
Leigh B. Stoller committed
145
# that allows control of it, for later operations.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
146
#
147
sub Create($$$$$$$$;$$$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
148
{
149
    my ($class, $slice, $owner, $uuid, $resource_uuid, $resource_type,
150
	$hrn, $nickname,
Leigh B. Stoller's avatar
Leigh B. Stoller committed
151
	$rspec, $credential, $component) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
152
    my @insert_data = ();
153
    my $certificate;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
154
155
156

    # Every sliver gets a new unique index.
    my $idx = TBGetUniqueIndex('next_sliver', 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
157
    if (defined($credential)) {
158
	$certificate = $credential->target_cert();
159
	# Store the certificate for later use.
160
	if ($certificate->Store() != 0) {
161
162
163
	    print STDERR "Could not store certificate\n";
	    return undef;
	}
164
	$resource_uuid = $uuid = $credential->this_uuid();
165
166
167
168
169
	$hrn = $credential->hrn();
	if (defined($credential->extensions()) &&
	    exists($credential->extensions()->{'nickname'})) {
	    $nickname = $credential->extensions()->{'nickname'};
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
170
171
172
173
174
	# Store the credential
	return undef
	    if ($credential->Store() != 0);
    }
    else {
175
	# Create a cert pair, for this resource uuid. 
176
	$certificate = GeniCertificate->Create("sliver", $hrn, $TBOPS, $uuid);
177
178
	if (!defined($certificate)) {
	    print STDERR "Could not generate new certificate and UUID!\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
179
180
	    return undef;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
181
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
182
    my $slice_uuid     = $slice->uuid();
183
    my $owner_uuid     = $owner->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
184
185
186
187

    # Now tack on other stuff we need.
    push(@insert_data, "created=now()");
    push(@insert_data, "idx='$idx'");
188
189
190
    push(@insert_data, "hrn=" . DBQuoteSpecial($hrn));
    push(@insert_data, "nickname=" . DBQuoteSpecial($nickname))
	if (defined($nickname));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
191
    push(@insert_data, "uuid='$uuid'");
192
193
    push(@insert_data, "resource_uuid='$resource_uuid'");
    push(@insert_data, "resource_type='$resource_type'");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
194
195
196
    push(@insert_data, "creator_uuid='$owner_uuid'");
    push(@insert_data, "slice_uuid='$slice_uuid'");

Leigh B. Stoller's avatar
Leigh B. Stoller committed
197
198
199
200
    # Only on the client side.
    push(@insert_data, "credential_idx=" . $credential->idx())
	if (defined($credential));
    # Only on the client side.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
201
    push(@insert_data, "component_uuid='" . $component->uuid() . "'")
Leigh B. Stoller's avatar
Leigh B. Stoller committed
202
	if (defined($component));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
203

Leigh B. Stoller's avatar
Leigh B. Stoller committed
204
205
206
207
208
209
210
    if (defined($rspec)) {
	my $rspec_string = XMLout($rspec, RootName => "rspec");
	my $safe_rspec   = DBQuoteSpecial($rspec_string);

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
211
    # Insert into DB.
212
213
214
    if (!DBQueryWarn("insert into geni_slivers set " .
		     join(",", @insert_data))) {
	$certificate->Delete();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
215
216
217
	return undef;
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
218
219
220
    my $sliver = GeniSliver->Lookup($idx);
    return undef
	if (!defined($sliver));
221
222
223
224
225

    if (GeniUsage->NewSliver($sliver, $slice, $owner)) {
	print STDERR
	    "GeniSliver::Create: GeniUsage->NewSliver($sliver) failed\n";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
226
227
228
    
    $sliver->{'CREDENTIAL'} = $credential
	if (defined($credential));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
229
230
231
    $sliver->{'COMPONENT'} = $component
	if (defined($component));
    $sliver->{'AGGREGATE'} = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
232
    $sliver->{'SLICE'}     = undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
233
234

    return $sliver;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
235
236
237
238
239
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'SLIVER'}->{$_[1]}); }
sub idx($)		{ return field($_[0], "idx"); }
sub uuid($)		{ return field($_[0], "uuid"); }
240
241
sub hrn($)		{ return field($_[0], "hrn"); }
sub nickname($)		{ return field($_[0], "nickname"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
242
243
244
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
245
sub credential_idx($)	{ return field($_[0], "credential_idx"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
246
sub resource_uuid($)	{ return field($_[0], "resource_uuid"); }
247
sub resource_type($)	{ return field($_[0], "resource_type"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
248
249
250
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
251
sub status($)		{ return field($_[0], "status"); }
252
253
sub cert($)		{ return $_[0]->{'CERTIFICATE'}->cert(); }
sub GetCertificate($)   { return $_[0]->{'CERTIFICATE'}; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
254
sub rspec($)            { return $_[0]->{'RSPEC'}; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
255
256
257
258

#
# Delete the sliver. The sliver should not be provisioned when this done.
#
259
sub Delete($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
260
{
261
    my ($self, $purge) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
262
263
264
265

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

266
267
    my $idx  = $self->idx();
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
268

269
270
271
272
    if (GeniUsage->DestroySliver($self, $purge)) {
	print STDERR
	    "GeniSliver::Delete: GeniUsage->DestroySliver($self) failed\n";
    }
273
274
275
276
    DBQueryWarn("delete from geni_credentials where this_uuid='$uuid'")
	or return -1;
    DBQueryWarn("delete from geni_certificates where uuid='$uuid'")
	or return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
277
278
    DBQueryWarn("delete from geni_slivers where idx='$idx'")
	or return -1;
279

280
281
282
    # Delete from cache. 
    delete($slivers{$idx});
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
283
284
285
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
286
287
288
289
290
291
292
293
294
295
#
# 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
296
297
    my $idx      = $self->idx();
    my $agg_uuid = $aggregate->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
298
299
300

    return -1
	if (!DBQueryWarn("update geni_slivers set ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
301
			 "  aggregate_uuid='$agg_uuid' ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
302
303
			 "where idx='$idx'"));
    
304
305
306
307
308
309
310
    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
311
    $self->{'SLIVER'}->{'aggregate_uuid'} = $agg_uuid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
312
313
314
315
    $self->{'AGGREGATE'} = $aggregate;
    return 0;
}

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#
# 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
338
339
340
341
342
343
344
345
346
347
348
349
#
# 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
350
	if (!defined($self->aggregate_uuid()));
Leigh B. Stoller's avatar
Leigh B. Stoller committed
351

Leigh B. Stoller's avatar
Leigh B. Stoller committed
352
    my $aggregate = GeniAggregate->Lookup($self->aggregate_uuid());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
353
354
355
356
357
358
359
360
    if (!defined($aggregate)) {
	print STDERR "Could not get aggregate object associated with $self\n";
	return undef;
    }
    $self->{'AGGREGATE'} = $aggregate;
    return $aggregate;
}

361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#
# 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;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
382
383
384
385
386
387
388
389
390
391
392
393
394
#
# Get the experiment for the slice this sliver belongs to.
#
sub GetExperiment($)
{
    my ($self) = @_;

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

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
395
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
396
# Get the credential for the sliver.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
397
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
398
sub GetCredential($$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
399
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
400
    my ($self, $user) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
401
402
403
404

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
405
406
    return $self->{'CREDENTIAL'} if (defined($self->{'CREDENTIAL'}));

Leigh B. Stoller's avatar
Leigh B. Stoller committed
407
    my $credential = GeniCredential->Lookup($self, $user);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
408
409
    if (!defined($credential)) {
	print STDERR "Could not get credential object associated with $self\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
410
411
	return undef;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
    $self->{'CREDENTIAL'} = $credential;
    return $credential;
}

#
# Get the component for the sliver.
#
sub GetComponent($)
{
    my ($self) = @_;

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

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
428
    if (!defined($self->component_uuid())) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
429
430
431
	print STDERR "No component associated with $self\n";
	return undef;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
432
    my $component = GeniComponent->Lookup($self->component_uuid());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
433
434
435
436
437
438
    if (!defined($component)) {
	print STDERR "Could not get component object associated with $self\n";
	return undef;
    }
    $self->{'COMPONENT'} = $component;
    return $component;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
439
440
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#
# 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;
}

466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#
# 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;
}

493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#
# 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;
}

526
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
527
528
# Create a signed credential for this sliver, issued to the provided user.
# The credential will grant all permissions for now.
529
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
530
531
532
# Should we store these credentials in the DB, recording what we hand out?
#
sub NewCredential($$)
533
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
534
    my ($self, $owner) = @_;
535

Leigh B. Stoller's avatar
Leigh B. Stoller committed
536
537
    return undef
	if (! (ref($self) && ref($owner)));
538

Leigh B. Stoller's avatar
Leigh B. Stoller committed
539
540
541
542
543
    my $credential = GeniCredential->Create($self, $owner);
    if (!defined($credential)) {
	print STDERR "Could not create credential for $self, $owner\n";
	return undef;
    }
544
545
546
    if (defined($self->nickname())) {
	$credential->AddExtension("nickname", $self->nickname());
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
547
548
549
550
551
552
    if ($credential->Sign($self->GetCertificate()) != 0) {
	print STDERR "Could not sign credential for $self, $owner\n";
	return undef;
    }
    return $credential;
}
553

Leigh B. Stoller's avatar
Leigh B. Stoller committed
554
555
556
557
558
559
560
############################################################################
#
# The client side methods are in packages which inherit from above.
#
package GeniSliver::Client;
use vars qw(@ISA);
@ISA = "GeniSliver";
561

Leigh B. Stoller's avatar
Leigh B. Stoller committed
562
563
564
565
566
567
use GeniDB;
use GeniComponent;
use GeniSlice;
use GeniCredential;
use GeniCertificate;
use libdb qw(TBDB_ALLOCSTATE_RES_INIT_DIRTY);
568

Leigh B. Stoller's avatar
Leigh B. Stoller committed
569
570
sub Create()
{
571
    my ($class, $slice, $user, $rspec, $credential, $component) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
572

573
    return GeniSliver->Create($slice, $user, undef, undef,
574
			      "Client", undef, undef, $rspec, 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
575
			      $credential, $component);
576
577
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
#
# Client side method to contact the sliver component and start it.
#
sub Start($$)
{
    my ($self, $user) = @_;

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

    my $component = $self->GetComponent();
    return -1
	if (!defined($component));

    return -1
	if ($component->StartSliver($self, $user) != 0);

    return 0;
}

598
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
599
# Client side method to contact the sliver component and destroy it.
600
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
601
sub Destroy($$)
602
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
603
    my ($self, $user) = @_;
604

Leigh B. Stoller's avatar
Leigh B. Stoller committed
605
606
    return -1
	if (! ref($self));
607

Leigh B. Stoller's avatar
Leigh B. Stoller committed
608
609
610
611
612
613
614
615
    my $component = $self->GetComponent();
    return -1
	if (!defined($component));

    return -1
	if ($component->DestroySliver($self, $user) != 0);

    # Delete the local object from the DB.
616
    $self->Delete(1) == 0
Leigh B. Stoller's avatar
Leigh B. Stoller committed
617
618
619
	or return -1;

    return 0;
620
621
}

622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
#
# On the client side, the aggregate points to the parent sliver when it
# contains multiple resources.
#
sub SetAggregate($$)
{
    my ($self, $parent) = @_;

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

    my $idx         = $self->idx();
    my $parent_uuid = $parent->uuid();

    return -1
	if (!DBQueryWarn("update geni_slivers set ".
			 "  aggregate_uuid='$parent_uuid' ".
			 "where idx='$idx'"));

    return 0;
}
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
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'"));
    return 0;
}
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
sub GetAggregate($)
{
    my ($self) = @_;

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

    return undef
	if (!defined($self->aggregate_uuid()));

    my $parent = GeniSliver->Lookup($self->aggregate_uuid());
    if (!defined($parent)) {
	print STDERR "Could not get parent object associated with $self\n";
	return undef;
    }
    return $parent;
}

676
677
678
679
680
681
682
683
684
685
686
687
688
############################################################################
#
# 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;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
689
use Experiment;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
690
use XML::Simple;
691
692
use libdb qw(TBDB_ALLOCSTATE_RES_INIT_DIRTY);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
693
sub Create($$$$$)
694
{
695
    my ($class, $slice, $user, $resource_uuid, $rspec) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
696
    my $virtualization_type = $rspec->{'virtualization_type'};
697
    my $uuid = $resource_uuid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
698
699
700
701

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
705
706
707
708
709
710
711
712
    #
    # 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
    # node will boot properly, and is otherwise controllable.
    #
    my $node = Node->Lookup($resource_uuid);
    if (!defined($node)) {
	print STDERR "Could not map node $resource_uuid to its object\n";
713
	return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
714
    }
715
716
717
718
719
720
721
722
723
724
725
    if (! $node->isremotenode()) {
	my $reservation = $node->Reservation();
	if (!defined($reservation)) {
	    print STDERR "$node was already released from $slice\n";
	    return undef;
	}
	if (! $reservation->SameExperiment($experiment)) {
	    print STDERR "$node is reserved to another, not $reservation\n";
	    # Signal error so we can look at what happened.
	    return undef;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
726
    }
727
    my $hrn = "${PGENIDOMAIN}." . $node->node_id();
728
    my $nickname = $rspec->{'nickname'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
729
730
731
732
733
734
    
    #
    # The resource UUID refers to the physical node, but the virtualization
    # type might require a vnode.
    # 
    if ($virtualization_type eq "emulab-vnode") {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
735
736
737
738
739
740
741
742
743
744
745
	my $vtype = "pcfake";

	if ($node->isplabphysnode()) {
	    if ($node->type() =~ /^(\w*)phys$/) {
		$vtype = $1;
	    }
	    else {
		print STDERR "Could not determine vtype for $node\n";
		return undef;
	    }
	}
746
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
747
	#
748
	# Create a virtual node on the physnode. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
749
750
751
	#
	my @vnodes;
	if (Node::CreateVnodes(\@vnodes,
752
753
			       {"pid"      => $experiment->pid(),
				"eid"      => $experiment->eid(),
Leigh B. Stoller's avatar
Leigh B. Stoller committed
754
				"count"    => 1,
755
				"vtype"    => "$vtype",
Leigh B. Stoller's avatar
Leigh B. Stoller committed
756
				"nodeid"   => $node->node_id(),
Leigh B. Stoller's avatar
Leigh B. Stoller committed
757
				"verbose"  => 0 }) < 0) {
758
	    print STDERR "Could not create new virtual node on $node\n";
759
	    return undef;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
760
761
	}
	my $vnode = Node->Lookup($vnodes[0]);
762
	$uuid = $vnode->uuid();
763
	$hrn = "${PGENIDOMAIN}." . $vnode->node_id()
Leigh B. Stoller's avatar
Leigh B. Stoller committed
764
    }
765
    return GeniSliver->Create($slice, $user, $uuid, $resource_uuid,
766
			      "Node", $hrn, $nickname, $rspec);
767
768
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
769
770
#
# Provision a slice. We actually did this when the ticket was requested.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
771
# We fill in some virt table stuff so that tbswap will work.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
772
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
773
sub Provision($;$)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
774
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
775
    my ($self, $extraargs) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
776
777
778
779
780
781
782

    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
783
    # node will boot properly, and is otherwise controllable.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
784
785
786
787
788
789
    #
    my $experiment = Experiment->Lookup($self->slice_uuid());
    if (!defined($experiment)) {
	print STDERR "Could not map $self to its experiment\n";
	return -1;
    }
790
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
791
    return 0
792
793
	if (!defined($uuid));
    my $node       = Node->Lookup($uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
794
    if (!defined($node)) {
795
	print STDERR "Could not map node $uuid to its object\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
796
797
	return -1;
    }
798
    my $node_id     = $node->node_id();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
799
800
    my $reservation = $node->Reservation();
    if (!defined($reservation)) {
801
	print STDERR "$node was already released from slice\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
802
803
	return -1;
    }
804
805
806
807
808
809
810
    if (! $reservation->SameExperiment($experiment)) {
	print STDERR "$node is reserved to another, not $reservation\n";
	# Signal error so we can look at what happened.
	return -1;
    }
    my $pid = $experiment->pid();
    my $eid = $experiment->eid();
811
	
812
813
814
815
    if ($experiment->InsertVirtNode($node) != 0) {
	print STDERR "Could not add virtnode entry for $node to $self\n";
	return -1;
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
816

817
    my $redirected = 0;
818
819
820
821
822
    if (exists($self->rspec()->{'tmcd_server'}) &&
	exists($self->rspec()->{'tmcd_nodeid'})) {
	my $tmcd_redirect =
	    $self->rspec()->{'tmcd_server'} . ":" .
	    $self->rspec()->{'tmcd_nodeid'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
823

824
825
	if ($node->ModifyReservation({"tmcd_redirect" => $tmcd_redirect})){
	    return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
826
	}
827
	$redirected = 1;
828
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
829

830
831
832
833
834
835
836
837
838
839
840
841
842
    #
    # An emulab cluster node uses a vnode on the physnode, even for a
    # dedicated physical nodes. We need to tell tmcd about the pnode,
    # but not the vnode since it is going to redirect to tmcd on the
    # remote Emulab controlling the experiment.
    #
    if (!$node->isremotenode() &&
	exists($self->rspec()->{'virtualization_type'}) &&
	$self->rspec()->{'virtualization_type'} eq "emulab-vnode") {
	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
843
	}
844
845
846
	    
	if ($experiment->InsertVirtNode($pnode) != 0) {
	    print STDERR "Could not add virtnode entry for $pnode to $self\n";
847
848
	    return -1;
	}
849
	$pnode->ModifyReservation({"genisliver_idx" => $self->idx()});
850

851
852
853
854
	# Not redirected. Use local tmcd anyway.
	$node->ModifyReservation({"genisliver_idx" => $self->idx()})
	    if (!$redirected);

855
856
	# Set it to boot the default OS.
	if ($pnode->SelectOS() != 0) {
857
858
	    return -1;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
859
860
    }
    else {
861
862
863
864
865
866
867
868
	#
	# 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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
    return 0;
}

#
# Unprovision a sliver. 
#
sub UnProvision($)
{
    my ($self) = @_;

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

    my $experiment = Experiment->Lookup($self->slice_uuid());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
883
884
885
886
    if (!defined($experiment)) {
	print STDERR "Could not map $self to its experiment\n";
	return -1;
    }
887
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
888
    return 0
889
890
	if (!defined($uuid));
    my $node       = Node->Lookup($uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
891
    if (!defined($node)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
892
	# Lets call this nonfatal since it might be a virtnode that 
893
	print STDERR "Could not map node $uuid to its object\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
894
	return 0;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
895
896
897
898
899
900
901
    }
    my $reservation = $node->Reservation();
    if (!defined($reservation)) {
	print STDERR "$node was already released from $self\n";
	return 0;
    }
    if ($reservation->SameExperiment($experiment)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
902
	my $node_id = $node->node_id();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
903
904
	my $pid = $experiment->pid();
	my $eid = $experiment->eid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
905

906
	if ($node->isremotenode()) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
907
	    system("$VNODESETUP -p -q -m -k $pid $eid $node_id");
908
	    if ($?) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
909
		print STDERR "$VNODESETUP failed\n";
910
911
912
913
		return -1;
	    }
	}

914
915
916
917
	if ($experiment->DeleteVirtNode($node) != 0) {
	    print STDERR "Could remove virtnode entry for $node from $self\n";
	    return -1;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
918

919
920
	if (!$node->isremotenode() &&
	    $self->rspec()->{'virtualization_type'} eq "emulab-vnode") {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
921
922
923
924
925
926
927
928
929
930
931
	    my $pnode_id = $node->phys_nodeid();
	    my $pnode = Node->Lookup($pnode_id);
	    if (!defined($pnode)) {
		print STDERR "Could not get pnode object for $pnode_id\n";
		return -1;
	    }
	    if ($experiment->DeleteVirtNode($pnode) != 0) {
		print STDERR
		    "Could remove virtnode entry for $pnode from $self\n";
		return -1;
	    }
932
	    system("$NFREE -x -q $pid $eid $pnode_id");
933
	    $pnode->Refresh();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
934
935
	}
	else {
936
	    system("$NFREE -q $pid $eid $node_id");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
937
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
938
939
	if ($?) {
	    print STDERR "Could not deallocate $node from $self\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
940
	    $node->Refresh();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
941
942
	    return -1;
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
943
	$node->Refresh();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
944
945
946
947
948
949
950
951
952
953
    }
    else {
	print STDERR "$node is reserved to another, not $self\n";
	# Signal error so we can look at what happened.
	return -1;
    }
    return 0;
}

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
954
# Start a sliver, which means what?
Leigh B. Stoller's avatar
Leigh B. Stoller committed
955
#
956
sub Start($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
957
958
959
960
961
962
963
964
965
966
967
{
    my ($self) = @_;

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

    my $experiment = Experiment->Lookup($self->slice_uuid());
    if (!defined($experiment)) {
	print STDERR "Could not map $self to its experiment\n";
	return -1;
    }
968
    my $uuid = $self->uuid();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
969
    return 0
970
971
	if (!defined($uuid));
    my $node       = Node->Lookup($uuid);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
972
    if (!defined($node)) {
973
	print STDERR "Could not map node $uuid to its object\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
974
975
976
977
978
979
980
981
	return -1;
    }
    my $reservation = $node->Reservation();
    if (!defined($reservation)) {
	print STDERR "$node was already released from $self\n";
	return -1;
    }
    if ($reservation->SameExperiment($experiment)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
982
	my $node_id = $node->node_id();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
983

984
985
986
	# XXX Need to fix up this status stuff.
	if ($node->isremotenode() && $self->status() eq "created") {
	    $self->SetStatus("ready");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
987
	    goto done;
988
989
	}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
990
991
992
993
994
995
996
	#
	# Reboot pnode if not already running. 
	#
	if ($self->rspec()->{'virtualization_type'} eq "emulab-vnode" &&
	    $self->status() eq "created") {
	    $node_id = $node->phys_nodeid();
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
997
998
999
	#
	# Reboot and wait?
	#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1000
1001
1002
1003
1004
	system("$NODEREBOOT -s $node_id");
	$self->SetStatus("ready")
	    if (!$?);
	return -1
	    if ($?);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1005
1006
1007
1008
1009
    }
    else {
	print STDERR "$node is reserved to another, not $self\n";
	# Signal error so we can look at what happened.
	return -1;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1010
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1011
  done:
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1012
1013
    return 0;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1014

1015
##########################################################################
1016
#
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
package GeniSliver::Interface;
use vars qw(@ISA);
@ISA = "GeniSliver";

use GeniDB;
use GeniComponent;
use GeniSlice;
use GeniCredential;
use GeniCertificate;

sub Create()
{
1029
    my ($class, $slice, $user,
1030
1031
1032
	$interface_uuid, $node, $rspec) = @_;

    my $nickname = $rspec->{'nickname'};
1033
1034
    my $hrn = "${PGENIDOMAIN}." .
	$node->node_id() . "." . $rspec->{'iface_name'};
1035

1036
    return GeniSliver->Create($slice, $user, $interface_uuid,
1037
1038
			      $node->uuid(), "Interface",
			      $hrn, $nickname, $rspec);
1039
1040
1041
1042
1043
1044
}

sub Provision($)
{
    my ($self) = @_;

1045
1046
1047
1048
    #
    # This is actually implemented in GeniAggregate since currently "link"
    # is the smallest entity; you cannot operate on an individual interface.
    #
1049
1050
1051
1052
1053
1054
1055
    return -1
	if (! ref($self));

    return 0;
}

#
1056
# Unprovision a single interface from a link/lan. 
1057
1058
1059
1060
1061
1062
1063
1064
#
sub UnProvision($)
{
    my ($self) = @_;

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

1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
    my $aggregate = $self->GetAggregate();
    if (!defined($aggregate)) {
	print STDERR "Could not find aggregate for $self\n";
	return -1;
    }

    #
    # This is terrible; we need an interface to remove ports form vlans,
    # so that we do not have to tear down the entire vlan and recreate.
    #
    if ($aggregate->UnProvision() != 0) {
	print STDERR "Could not unprovision $aggregate\n";
	return -1;
    }
    $self->ClearAggregate();
    if ($aggregate->Provision() != 0) {
	print STDERR "Could not provision $aggregate\n";
	return -1;
    }
1084
1085
1086
1087
    return 0;
}

#
1088
# Start a sliver.
1089
#
1090
sub Start($)
1091
1092
1093
{
    my ($self) = @_;

1094
1095
1096
1097
    #
    # This is actually implemented in GeniAggregate since currently "link"
    # is the smallest entity; you cannot operate on an individual interface.
    #
1098
1099
1100
1101
1102
    return -1
	if (! ref($self));

    return 0;
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1103

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1104
1105
# _Always_ make sure that this 1 is at the end of the file...
1;