GeniCM.pm.in 10.1 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
# Copyright (c) 2008 University of Utah and the Flux Group.
# All rights reserved.
#
package GeniCM;

#
# The server side of the CM interface on remote sites. Also communicates
# with the GMC interface at Geni Central as a client.
#
use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

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

# Must come after package declaration!
use lib '@prefix@/lib';
use GeniDB;
use Genixmlrpc;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
24 25
use GeniResponse;
use GeniTicket;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26
use GeniCredential;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
27
use GeniSlice;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
28
use GeniSliver;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
29
use GeniUser;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
30
use libtestbed;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31 32 33
# Hate to import all this crap; need a utility library.
use libdb qw(TBGetUniqueIndex TBcheck_dbslot TBDB_CHECKDBSLOT_ERROR);
use User;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
34
use Node;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
35 36
use English;
use Data::Dumper;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
37
use XML::Simple;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
38
use Experiment;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
39 40 41 42 43 44 45 46

# Configure variables
my $TB		   = "@prefix@";
my $TBOPS          = "@TBOPSEMAIL@";
my $TBAPPROVAL     = "@TBAPPROVALEMAIL@";
my $TBAUDIT   	   = "@TBAUDITEMAIL@";
my $BOSSNODE       = "@BOSSNODE@";
my $OURDOMAIN      = "@OURDOMAIN@";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
47 48
my $CREATEEXPT     = "$TB/bin/batchexp";
my $NALLOC	   = "$TB/bin/nalloc";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
49 50 51 52 53 54 55 56
my $AVAIL	   = "$TB/sbin/avail";

#
# Discover resources on this component, returning a resource availablity spec
#
sub DiscoverResources($)
{
    my ($argref) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
57
    my $slice      = $argref->{'slice'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
58 59
    my $credential = $argref->{'credential'};
    my $user_uuid  = $ENV{'GENIUSER'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
60
    my $slice_uuid;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
61

Leigh B. Stoller's avatar
Leigh B. Stoller committed
62
    if (! defined($slice)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
63 64 65 66 67 68 69 70
	return GeniResponse->MalformedArgsResponse();
    }

    $credential = GeniCredential->CreateFromSigned($credential);
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
71 72 73 74
    GeniCredential->CertificateInfo($slice, \$slice_uuid) == 0 or
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not get uuid from Certificate");
	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    # The credential owner/slice has to match what was provided.
    if (! ($user_uuid eq $credential->owner_uuid() &&
	   $slice_uuid eq $credential->this_uuid())) {
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "Invalid credentials for operation");
    }

    #
    # Eventually we will take an optional rspec, but for now just return
    # a list of free nodes using avail. 
    #
    if (! open(AVAIL, "$AVAIL type=pc aslist |")) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not start avail");
    }
    my @nodelist = ();
    while (<AVAIL>) {
	my $nodeid = $_;
	chomp($nodeid);
	my $node = Node->Lookup($nodeid);
	push(@nodelist, $node)
	    if (defined($node));
    }
    close(AVAIL);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
100
    my $xml = "<rspec xmlns=\"http://protogeni.net/resources/rspec/0.1\">\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
101 102 103 104 105 106 107 108 109 110 111
    foreach my $node (@nodelist) {
	my $uuid = $node->uuid();
	my $nodeid = $node->node_id();
	
	$xml .= "<node uuid=\"$uuid\" name=\"$nodeid\">".
	    "<available>true</available></node>\n";
    }
    $xml .= "</rspec>";

    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $xml);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
112

Leigh B. Stoller's avatar
Leigh B. Stoller committed
113
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
114
# Respond to a GetTicket request. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
115 116 117 118
#
sub GetTicket($)
{
    my ($argref) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
119
    my $slice_uuid = $argref->{'slice_uuid'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
120
    my $rspec      = $argref->{'rspec'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
121
    my $impotent   = $argref->{'impotent'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
122
    my $credential = $argref->{'credential'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
123
    my $owner_uuid = $ENV{'GENIUSER'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
124 125

    if (! (defined($slice_uuid) && ($slice_uuid =~ /^[-\w]+$/))) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
126 127
	GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
			     "Improper slice uuid");	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
128
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
129 130 131
    if (! (defined($rspec) && ($rspec =~ /^[-\w]+$/))) {
	GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
			     "Improper rspec");	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
132
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
133 134 135
    # Convert the rspec back to a structure.
    $rspec = XMLin($rspec, ForceArray => ["node"]);

Leigh B. Stoller's avatar
Leigh B. Stoller committed
136 137 138
    $impotent = 0
	if (!defined($impotent));

Leigh B. Stoller's avatar
Leigh B. Stoller committed
139
    $credential = GeniCredential->CreateFromSigned($credential);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
140 141 142 143 144 145 146 147 148 149
    if (!defined($credential)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniCredential object");
    }
    # The credential owner/slice has to match what was provided.
    if (! ($owner_uuid eq $credential->owner_uuid() &&
	   $slice_uuid eq $credential->this_uuid())) {
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "Invalid credentials for operation");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
150

Leigh B. Stoller's avatar
Leigh B. Stoller committed
151 152 153 154 155 156 157 158 159 160 161 162
    #
    # See if we have a record of this slice in the DB. If not, then we have
    # to go to the ClearingHouse to find its record, so that we can find out
    # who the SA for it is.
    #
    my $slice = GeniSlice->Lookup($slice_uuid);
    if (!defined($slice)) {

    }

    

Leigh B. Stoller's avatar
Leigh B. Stoller committed
163
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
164
    # XXX Should we create a local geni_slices record in the DB?
Leigh B. Stoller's avatar
Leigh B. Stoller committed
165
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
166 167 168 169
    # If the underlying experiment does not exist, need to create
    # a holding experiment. All these are going to go into the same
    # project for now. Generally, users for non-local slices do not
    # have local accounts or directories.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
170
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    my $experiment = Experiment->Lookup($slice_uuid);
    if (!defined($experiment)) {
	#
	# Form an eid for the experiment. 
	#
	my $eid = "slice" . TBGetUniqueIndex('next_sliceid', 1);

	# Note the -h option; allows experiment with no NS file.
	system("$CREATEEXPT -q -i -w -E 'Geni Slice Experiment' ".
	       "-h '$slice_uuid' -p genislices -e $eid");
	if ($?) {
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"Internal Error");
	}
	$experiment = Experiment->Lookup($slice_uuid);
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
188
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
189 190 191
    # An rspec is a structure that requests specific nodes. If those
    # nodes are available, then reserve it. Otherwise the ticket
    # cannot be granted. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
192
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
193
    my @nodeids = ();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
194 195 196
    my $pid     = $experiment->pid();
    my $eid     = $experiment->eid();

Leigh B. Stoller's avatar
Leigh B. Stoller committed
197 198 199 200 201 202 203 204 205
    foreach my $node_id (keys(%{$rspec->{'node'}})) {
	if ($node_id =~ /^(\w*)$/) {
	    $node_id = $1;
	}
	else {
	    return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
					"Improper node id: $node_id");
	}
	push(@nodeids, $node_id);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
206
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
207 208

    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
209
    # Create the ticket first, before allocating the node.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
210
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
211
    my $ticket = GeniTicket->Create($slice_uuid, $owner_uuid, $rspec);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
212 213 214 215
    if (!defined($ticket)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniTicket object");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
216
    # Nalloc might fail if the node gets picked up by someone else.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
217
    if (!$impotent) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
218
	system("$NALLOC $pid $eid @nodeids");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
219 220 221 222 223 224 225 226 227 228
	if (($? >> 8) < 0) {
	    $ticket->Delete();
	    return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
					"Allocation failure");
	}
	elsif (($? >> 8) > 0) {
	    $ticket->Delete();
	    return GeniResponse->Create(GENIRESPONSE_UNAVAILABLE, undef,
					"Could not allocate node\n");
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
229
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
230
    if ($ticket->Sign() != 0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
231
	# Release will free the nodes.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
232
	$ticket->Release();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
233 234 235
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not sign Ticket");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
236 237 238 239 240 241 242 243 244 245
    return GeniResponse->Create(GENIRESPONSE_SUCCESS,
				$ticket->asString());
}

#
# Create a sliver.
#
sub CreateSliver($)
{
    my ($argref) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
246 247
    my $owner_uuid = $ENV{'GENIUSER'};
    my $ticket     = $argref->{'ticket'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
248 249 250 251 252 253 254 255 256 257 258 259

    if (! (defined($ticket) &&
	   !TBcheck_dbslot($ticket, "default", "text",
			   TBDB_CHECKDBSLOT_ERROR))) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "ticket: ". TBFieldErrorString());
    }
    $ticket = GeniTicket->CreateFromSignedTicket($ticket);
    if (!defined($ticket)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniTicket object");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
260 261 262 263 264
    # The credential owner has to match what is in the ticket.
    if ($owner_uuid ne $ticket->owner_uuid()) {
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "Invalid credentials for operation");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
265

Leigh B. Stoller's avatar
Leigh B. Stoller committed
266 267 268 269 270 271
    my $experiment = Experiment->Lookup($ticket->slice_uuid());
    if (!defined($experiment)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "No local experiment for slice");
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
272 273 274 275 276
    my $sliver = GeniSliver->Create($ticket);
    if (!defined($sliver)) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not create GeniSliver object");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
277 278 279 280 281 282 283 284 285 286 287

    #
    # Provision the slice. Okay, we already allocated the node above,
    # so this should just work, unless the node has been released cause
    # it has been too long.
    #
    if ($sliver->Provision() != 0) {
	$sliver->Delete();
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not provision sliver");
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301
    #
    # The API states we return a credential to control the sliver.
    #
    my $credential = GeniCredential->Create($sliver->uuid(),
					    $owner_uuid);
    if (!defined($credential)) {
	print STDERR "Could not create a credential for $sliver!\n";
	return -1;
    }
    if ($credential->Sign()) {
	print STDERR "Could not sign sliver credential!\n";
	return -1;
    }
    return GeniResponse->Create(GENIRESPONSE_SUCCESS, $credential->asString());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
302 303 304
}

#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
305
# Destroy a sliver.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
306
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
307
sub DestroySliver($)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
308 309
{
    my ($argref) = @_;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
310
    my $owner_uuid  = $ENV{'GENIUSER'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
311
    my $sliver_uuid = $argref->{'uuid'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
312
    my $credential  = $argref->{'credential'};
Leigh B. Stoller's avatar
Leigh B. Stoller committed
313 314 315 316 317 318 319 320 321 322

    if (!defined($sliver_uuid)) {
	return GeniResponse->Create(GENIRESPONSE_BADARGS);
    }
    my $sliver = GeniSliver->Lookup($sliver_uuid);
    if (!defined($sliver)) {
	return GeniResponse->Create(GENIRESPONSE_BADARGS, undef,
				    "No such sliver $sliver_uuid");
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
323 324
    if (! (defined($credential) &&
	   !TBcheck_dbslot($credential, "default", "text",
Leigh B. Stoller's avatar
Leigh B. Stoller committed
325 326
			   TBDB_CHECKDBSLOT_ERROR))) {
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
Leigh B. Stoller's avatar
Leigh B. Stoller committed
327
				    "credential: ". TBFieldErrorString());
Leigh B. Stoller's avatar
Leigh B. Stoller committed
328
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
329 330
    $credential = GeniCredential->CreateFromSigned($credential);
    if (!defined($credential)) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
331
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
Leigh B. Stoller's avatar
Leigh B. Stoller committed
332
				    "Could not create GeniCredential object");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
333
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
334 335 336 337 338
    # The credential owner has to match what is in the ticket.
    if (! ($owner_uuid eq $credential->owner_uuid() &&
	   $sliver_uuid eq $credential->this_uuid())) {
	return GeniResponse->Create(GENIRESPONSE_FORBIDDEN, undef,
				    "Invalid credentials for operation");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
339
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
340

Leigh B. Stoller's avatar
Leigh B. Stoller committed
341 342 343 344 345 346 347 348
    $sliver->UnProvision() == 0 or
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not unprovision sliver");
    $sliver->Delete() == 0 or
	return GeniResponse->Create(GENIRESPONSE_ERROR, undef,
				    "Could not delete sliver");
    
    return GeniResponse->Create(GENIRESPONSE_SUCCESS);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
349
}