1 from __future__ import with_statement
7 from boto.ec2.regioninfo import RegionInfo
8 from boto.exception import EC2ResponseError
9 from ConfigParser import ConfigParser
10 from xmlbuilder import XMLBuilder
11 from lxml import etree as ET
12 from sqlobject import *
14 from sfa.util.faults import *
15 from sfa.util.xrn import urn_to_hrn
16 from sfa.util.rspec import RSpec
17 from sfa.server.registry import Registries
18 from sfa.trust.credential import Credential
19 from sfa.plc.api import SfaAPI
20 from sfa.util.plxrn import hrn_to_pl_slicename, slicename_to_hrn
21 from sfa.util.callids import Callids
24 # The data structure used to represent a cloud.
25 # It contains the cloud name, its ip address, image information,
26 # key pairs, and clusters information.
31 # The location of the RelaxNG schema.
33 EUCALYPTUS_RSPEC_SCHEMA='/etc/sfa/eucalyptus.rng'
36 sys.stderr = file('/var/log/euca_agg.log', 'a+')
40 # A representation of an Eucalyptus instance. This is a support class
41 # for instance <-> slice mapping.
43 class EucaInstance(SQLObject):
44 instance_id = StringCol(unique=True, default=None)
45 kernel_id = StringCol()
46 image_id = StringCol()
47 ramdisk_id = StringCol()
48 inst_type = StringCol()
49 key_pair = StringCol()
50 slice = ForeignKey('Slice')
53 # Contacts Eucalyptus and tries to reserve this instance.
55 # @param botoConn A connection to Eucalyptus.
56 # @param pubKeys A list of public keys for the instance.
58 def reserveInstance(self, botoConn, pubKeys):
59 print >>sys.stderr, 'Reserving an instance: image: %s, kernel: ' \
60 '%s, ramdisk: %s, type: %s, key: %s' % \
61 (self.image_id, self.kernel_id, self.ramdisk_id,
62 self.inst_type, self.key_pair)
64 # XXX The return statement is for testing. REMOVE in production
68 reservation = botoConn.run_instances(self.image_id,
69 kernel_id = self.kernel_id,
70 ramdisk_id = self.ramdisk_id,
71 instance_type = self.inst_type,
72 key_name = self.key_pair,
74 for instance in reservation.instances:
75 self.instance_id = instance.id
77 # If there is an error, destroy itself.
78 except EC2ResponseError, ec2RespErr:
79 errTree = ET.fromstring(ec2RespErr.body)
80 msg = errTree.find('.//Message')
81 print >>sys.stderr, msg.text
85 # A representation of a PlanetLab slice. This is a support class
86 # for instance <-> slice mapping.
88 class Slice(SQLObject):
89 slice_hrn = StringCol()
90 #slice_index = DatabaseIndex('slice_hrn')
91 instances = MultipleJoin('EucaInstance')
94 # Initialize the aggregate manager by reading a configuration file.
97 configParser = ConfigParser()
98 configParser.read(['/etc/sfa/eucalyptus_aggregate.conf', 'eucalyptus_aggregate.conf'])
99 if len(configParser.sections()) < 1:
100 print >>sys.stderr, 'No cloud defined in the config file'
101 raise Exception('Cannot find cloud definition in configuration file.')
103 # Only read the first section.
104 cloudSec = configParser.sections()[0]
105 cloud['name'] = cloudSec
106 cloud['access_key'] = configParser.get(cloudSec, 'access_key')
107 cloud['secret_key'] = configParser.get(cloudSec, 'secret_key')
108 cloud['cloud_url'] = configParser.get(cloudSec, 'cloud_url')
109 cloudURL = cloud['cloud_url']
110 if cloudURL.find('https://') >= 0:
111 cloudURL = cloudURL.replace('https://', '')
112 elif cloudURL.find('http://') >= 0:
113 cloudURL = cloudURL.replace('http://', '')
114 (cloud['ip'], parts) = cloudURL.split(':')
116 # Create image bundles
117 images = getEucaConnection().get_all_images()
118 cloud['images'] = images
119 cloud['imageBundles'] = {}
121 if i.type != 'machine' or i.kernel_id is None: continue
122 name = os.path.dirname(i.location)
123 detail = {'imageID' : i.id, 'kernelID' : i.kernel_id, 'ramdiskID' : i.ramdisk_id}
124 cloud['imageBundles'][name] = detail
126 # Initialize sqlite3 database.
127 dbPath = '/etc/sfa/db'
128 dbName = 'euca_aggregate.db'
130 if not os.path.isdir(dbPath):
131 print >>sys.stderr, '%s not found. Creating directory ...' % dbPath
134 conn = connectionForURI('sqlite://%s/%s' % (dbPath, dbName))
135 sqlhub.processConnection = conn
136 Slice.createTable(ifNotExists=True)
137 EucaInstance.createTable(ifNotExists=True)
139 # Make sure the schema exists.
140 if not os.path.exists(EUCALYPTUS_RSPEC_SCHEMA):
141 err = 'Cannot location schema at %s' % EUCALYPTUS_RSPEC_SCHEMA
142 print >>sys.stderr, err
146 # Creates a connection to Eucalytpus. This function is inspired by
147 # the make_connection() in Euca2ools.
149 # @return A connection object or None
151 def getEucaConnection():
153 accessKey = cloud['access_key']
154 secretKey = cloud['secret_key']
155 eucaURL = cloud['cloud_url']
160 if not accessKey or not secretKey or not eucaURL:
161 print >>sys.stderr, 'Please set ALL of the required environment ' \
162 'variables by sourcing the eucarc file.'
165 # Split the url into parts
166 if eucaURL.find('https://') >= 0:
168 eucaURL = eucaURL.replace('https://', '')
169 elif eucaURL.find('http://') >= 0:
171 eucaURL = eucaURL.replace('http://', '')
172 (eucaHost, parts) = eucaURL.split(':')
174 parts = parts.split('/')
175 eucaPort = int(parts[0])
177 srvPath = '/'.join(parts)
179 return boto.connect_ec2(aws_access_key_id=accessKey,
180 aws_secret_access_key=secretKey,
182 region=RegionInfo(None, 'eucalyptus', eucaHost),
187 # Returns a string of keys that belong to the users of the given slice.
188 # @param sliceHRN The hunman readable name of the slice.
191 def getKeysForSlice(sliceHRN):
193 # convert hrn to slice name
194 plSliceName = hrn_to_pl_slicename(sliceHRN)
195 except IndexError, e:
196 print >>sys.stderr, 'Invalid slice name (%s)' % sliceHRN
199 # Get the slice's information
200 sliceData = api.plshell.GetSlices(api.plauth, {'name':plSliceName})
202 print >>sys.stderr, 'Cannot get any data for slice %s' % plSliceName
205 # It should only return a list with len = 1
206 sliceData = sliceData[0]
209 person_ids = sliceData['person_ids']
211 print >>sys.stderr, 'No users in slice %s' % sliceHRN
214 persons = api.plshell.GetPersons(api.plauth, person_ids)
215 for person in persons:
216 pkeys = api.plshell.GetKeys(api.plauth, person['key_ids'])
218 keys.append(key['key'])
223 # A class that builds the RSpec for Eucalyptus.
225 class EucaRSpecBuilder(object):
227 # Initizes a RSpec builder
229 # @param cloud A dictionary containing data about a
230 # cloud (ex. clusters, ip)
231 def __init__(self, cloud):
232 self.eucaRSpec = XMLBuilder(format = True, tab_step = " ")
233 self.cloudInfo = cloud
236 # Creates a request stanza.
238 # @param num The number of instances to create.
239 # @param image The disk image id.
240 # @param kernel The kernel image id.
241 # @param keypair Key pair to embed.
242 # @param ramdisk Ramdisk id (optional).
244 def __requestXML(self, num, image, kernel, keypair, ramdisk = ''):
249 with xml.kernel_image(id=kernel):
255 with xml.ramdisk(id=ramdisk):
257 with xml.disk_image(id=image):
263 # Creates the cluster stanza.
265 # @param clusters Clusters information.
267 def __clustersXML(self, clusters):
268 cloud = self.cloudInfo
271 for cluster in clusters:
272 instances = cluster['instances']
273 with xml.cluster(id=cluster['name']):
277 for inst in instances:
278 with xml.vm_type(name=inst[0]):
281 with xml.max_instances:
285 with xml.memory(unit='MB'):
287 with xml.disk_space(unit='GB'):
289 if 'instances' in cloud and inst[0] in cloud['instances']:
290 existingEucaInstances = cloud['instances'][inst[0]]
291 with xml.euca_instances:
292 for eucaInst in existingEucaInstances:
293 with xml.euca_instance(id=eucaInst['id']):
295 xml << eucaInst['state']
297 xml << eucaInst['public_dns']
299 def __imageBundleXML(self, bundles):
302 for bundle in bundles.keys():
303 with xml.bundle(id=bundle):
307 # Creates the Images stanza.
309 # @param images A list of images in Eucalyptus.
311 def __imagesXML(self, images):
315 with xml.image(id=image.id):
319 xml << image.architecture
323 xml << image.location
326 # Creates the KeyPairs stanza.
328 # @param keypairs A list of key pairs in Eucalyptus.
330 def __keyPairsXML(self, keypairs):
338 # Generates the RSpec.
341 if not self.cloudInfo:
342 print >>sys.stderr, 'No cloud information'
346 cloud = self.cloudInfo
347 with xml.RSpec(type='eucalyptus'):
348 with xml.cloud(id=cloud['name']):
351 #self.__keyPairsXML(cloud['keypairs'])
352 #self.__imagesXML(cloud['images'])
353 self.__imageBundleXML(cloud['imageBundles'])
354 self.__clustersXML(cloud['clusters'])
358 # A parser to parse the output of availability-zones.
360 # Note: Only one cluster is supported. If more than one, this will
363 class ZoneResultParser(object):
364 def __init__(self, zones):
368 if len(self.zones) < 3:
374 cluster['name'] = self.zones[0].name
375 cluster['ip'] = self.zones[0].state
377 for i in range(2, len(self.zones)):
378 currZone = self.zones[i]
379 instType = currZone.name.split()[1]
381 stateString = currZone.state.split('/')
382 rscString = stateString[1].split()
384 instFree = int(stateString[0])
385 instMax = int(rscString[0])
386 instNumCpu = int(rscString[1])
387 instRam = int(rscString[2])
388 instDiskSpace = int(rscString[3])
390 instTuple = (instType, instFree, instMax, instNumCpu, instRam, instDiskSpace)
391 instList.append(instTuple)
392 cluster['instances'] = instList
393 clusterList.append(cluster)
397 def ListResources(api, creds, options, call_id):
398 if Callids().already_handled(call_id): return ""
400 # get slice's hrn from options
401 xrn = options.get('geni_slice_urn', '')
402 hrn, type = urn_to_hrn(xrn)
404 # get hrn of the original caller
405 origin_hrn = options.get('origin_hrn', None)
407 origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
409 conn = getEucaConnection()
412 print >>sys.stderr, 'Error: Cannot create a connection to Eucalyptus'
413 return 'Cannot create a connection to Eucalyptus'
417 zones = conn.get_all_zones(['verbose'])
418 p = ZoneResultParser(zones)
420 cloud['clusters'] = clusters
423 images = conn.get_all_images()
424 cloud['images'] = images
425 cloud['imageBundles'] = {}
427 if i.type != 'machine' or i.kernel_id is None: continue
428 name = os.path.dirname(i.location)
429 detail = {'imageID' : i.id, 'kernelID' : i.kernel_id, 'ramdiskID' : i.ramdisk_id}
430 cloud['imageBundles'][name] = detail
433 keyPairs = conn.get_all_key_pairs()
434 cloud['keypairs'] = keyPairs
440 # Get the instances that belong to the given slice from sqlite3
441 # XXX use getOne() in production because the slice's hrn is supposed
442 # to be unique. For testing, uniqueness is turned off in the db.
443 # If the slice isn't found in the database, create a record for the
445 matchedSlices = list(Slice.select(Slice.q.slice_hrn == hrn))
447 theSlice = matchedSlices[-1]
449 theSlice = Slice(slice_hrn = hrn)
450 for instance in theSlice.instances:
451 instanceId.append(instance.instance_id)
453 # Get the information about those instances using their ids.
454 if len(instanceId) > 0:
455 reservations = conn.get_all_instances(instanceId)
458 for reservation in reservations:
459 for instance in reservation.instances:
460 instances.append(instance)
462 # Construct a dictionary for the EucaRSpecBuilder
464 for instance in instances:
465 instList = instancesDict.setdefault(instance.instance_type, [])
468 instInfoDict['id'] = instance.id
469 instInfoDict['public_dns'] = instance.public_dns_name
470 instInfoDict['state'] = instance.state
471 instInfoDict['key'] = instance.key_name
473 instList.append(instInfoDict)
474 cloud['instances'] = instancesDict
476 except EC2ResponseError, ec2RespErr:
477 errTree = ET.fromstring(ec2RespErr.body)
478 errMsgE = errTree.find('.//Message')
479 print >>sys.stderr, errMsgE.text
481 rspec = EucaRSpecBuilder(cloud).toXML()
483 # Remove the instances records so next time they won't
485 if 'instances' in cloud:
486 del cloud['instances']
491 Hook called via 'sfi.py create'
493 def CreateSliver(api, xrn, creds, xml, users, call_id):
494 if Callids().already_handled(call_id): return False
497 hrn = urn_to_hrn(xrn)[0]
499 conn = getEucaConnection()
501 print >>sys.stderr, 'Error: Cannot create a connection to Eucalyptus'
505 schemaXML = ET.parse(EUCALYPTUS_RSPEC_SCHEMA)
506 rspecValidator = ET.RelaxNG(schemaXML)
507 rspecXML = ET.XML(xml)
508 if not rspecValidator(rspecXML):
509 error = rspecValidator.error_log.last_error
510 message = '%s (line %s)' % (error.message, error.line)
511 # XXX: InvalidRSpec is new. Currently, I am not working with Trunk code.
512 #raise InvalidRSpec(message)
513 raise Exception(message)
515 # Get the slice from db or create one.
516 s = Slice.select(Slice.q.slice_hrn == hrn).getOne(None)
518 s = Slice(slice_hrn = hrn)
520 # Process any changes in existing instance allocation
522 for sliceInst in s.instances:
523 pendingRmInst.append(sliceInst.instance_id)
524 existingInstGroup = rspecXML.findall('.//euca_instances')
525 for instGroup in existingInstGroup:
526 for existingInst in instGroup:
527 if existingInst.get('id') in pendingRmInst:
528 pendingRmInst.remove(existingInst.get('id'))
529 for inst in pendingRmInst:
530 print >>sys.stderr, 'Instance %s will be terminated' % inst
531 dbInst = EucaInstance.select(EucaInstance.q.instance_id == inst).getOne(None)
533 conn.terminate_instances(pendingRmInst)
535 # Process new instance requests
536 requests = rspecXML.findall('.//request')
538 # Get all the public keys associate with slice.
539 pubKeys = getKeysForSlice(s.slice_hrn)
540 print >>sys.stderr, "Passing the following keys to the instance:\n%s" % pubKeys
543 vmTypeElement = req.getparent()
544 instType = vmTypeElement.get('name')
545 numInst = int(req.find('instances').text)
547 bundleName = req.find('bundle').text
548 if not cloud['imageBundles'][bundleName]:
549 print >>sys.stderr, 'Cannot find bundle %s' % bundleName
550 bundleInfo = cloud['imageBundles'][bundleName]
551 instKernel = bundleInfo['kernelID']
552 instDiskImg = bundleInfo['imageID']
553 instRamDisk = bundleInfo['ramdiskID']
556 # Create the instances
557 for i in range(0, numInst):
558 eucaInst = EucaInstance(slice = s,
559 kernel_id = instKernel,
560 image_id = instDiskImg,
561 ramdisk_id = instRamDisk,
563 inst_type = instType)
564 eucaInst.reserveInstance(conn, pubKeys)
572 #with open(sys.argv[1]) as xml:
573 # theRSpec = xml.read()
574 #CreateSliver(None, 'planetcloud.pc.test', theRSpec, 'call-id-cloudtest')
576 #rspec = ListResources('euca', 'planetcloud.pc.test', 'planetcloud.pc.marcoy', 'test_euca')
578 print getKeysForSlice('gc.gc.test1')
580 if __name__ == "__main__":