1 from __future__ import with_statement
9 from boto.ec2.regioninfo import RegionInfo
10 from boto.exception import EC2ResponseError
11 from ConfigParser import ConfigParser
12 from xmlbuilder import XMLBuilder
13 from lxml import etree as ET
14 from sqlobject import *
16 from sfa.util.faults import *
17 from sfa.util.xrn import urn_to_hrn
18 from sfa.util.rspec import RSpec
19 from sfa.server.registry import Registries
20 from sfa.trust.credential import Credential
21 from sfa.plc.api import SfaAPI
22 from sfa.util.plxrn import hrn_to_pl_slicename, slicename_to_hrn
23 from sfa.util.callids import Callids
25 from threading import Thread
26 from time import sleep
29 # The data structure used to represent a cloud.
30 # It contains the cloud name, its ip address, image information,
31 # key pairs, and clusters information.
36 # The location of the RelaxNG schema.
38 EUCALYPTUS_RSPEC_SCHEMA='/etc/sfa/eucalyptus.rng'
43 # Meta data of an instance.
45 class Meta(SQLObject):
46 instance = SingleJoin('EucaInstance')
47 state = StringCol(default = 'new')
48 pub_addr = StringCol(default = None)
49 pri_addr = StringCol(default = None)
50 start_time = DateTimeCol(default = None)
53 # A representation of an Eucalyptus instance. This is a support class
54 # for instance <-> slice mapping.
56 class EucaInstance(SQLObject):
57 instance_id = StringCol(unique=True, default=None)
58 kernel_id = StringCol()
59 image_id = StringCol()
60 ramdisk_id = StringCol()
61 inst_type = StringCol()
62 key_pair = StringCol()
63 slice = ForeignKey('Slice')
64 meta = ForeignKey('Meta')
67 # Contacts Eucalyptus and tries to reserve this instance.
69 # @param botoConn A connection to Eucalyptus.
70 # @param pubKeys A list of public keys for the instance.
72 def reserveInstance(self, botoConn, pubKeys):
73 logger = logging.getLogger('EucaAggregate')
74 logger.info('Reserving an instance: image: %s, kernel: ' \
75 '%s, ramdisk: %s, type: %s, key: %s' % \
76 (self.image_id, self.kernel_id, self.ramdisk_id,
77 self.inst_type, self.key_pair))
79 # XXX The return statement is for testing. REMOVE in production
83 reservation = botoConn.run_instances(self.image_id,
84 kernel_id = self.kernel_id,
85 ramdisk_id = self.ramdisk_id,
86 instance_type = self.inst_type,
87 key_name = self.key_pair,
89 for instance in reservation.instances:
90 self.instance_id = instance.id
92 # If there is an error, destroy itself.
93 except EC2ResponseError, ec2RespErr:
94 errTree = ET.fromstring(ec2RespErr.body)
95 msg = errTree.find('.//Message')
96 logger.error(msg.text)
100 # A representation of a PlanetLab slice. This is a support class
101 # for instance <-> slice mapping.
103 class Slice(SQLObject):
104 slice_hrn = StringCol()
105 #slice_index = DatabaseIndex('slice_hrn')
106 instances = MultipleJoin('EucaInstance')
109 # Initialize the aggregate manager by reading a configuration file.
112 logger = logging.getLogger('EucaAggregate')
113 fileHandler = logging.FileHandler('/var/log/euca.log')
114 fileHandler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
115 logger.addHandler(fileHandler)
116 logger.setLevel(logging.DEBUG)
118 configParser = ConfigParser()
119 configParser.read(['/etc/sfa/eucalyptus_aggregate.conf', 'eucalyptus_aggregate.conf'])
120 if len(configParser.sections()) < 1:
121 logger.error('No cloud defined in the config file')
122 raise Exception('Cannot find cloud definition in configuration file.')
124 # Only read the first section.
125 cloudSec = configParser.sections()[0]
126 cloud['name'] = cloudSec
127 cloud['access_key'] = configParser.get(cloudSec, 'access_key')
128 cloud['secret_key'] = configParser.get(cloudSec, 'secret_key')
129 cloud['cloud_url'] = configParser.get(cloudSec, 'cloud_url')
130 cloudURL = cloud['cloud_url']
131 if cloudURL.find('https://') >= 0:
132 cloudURL = cloudURL.replace('https://', '')
133 elif cloudURL.find('http://') >= 0:
134 cloudURL = cloudURL.replace('http://', '')
135 (cloud['ip'], parts) = cloudURL.split(':')
137 # Create image bundles
138 images = getEucaConnection().get_all_images()
139 cloud['images'] = images
140 cloud['imageBundles'] = {}
142 if i.type != 'machine' or i.kernel_id is None: continue
143 name = os.path.dirname(i.location)
144 detail = {'imageID' : i.id, 'kernelID' : i.kernel_id, 'ramdiskID' : i.ramdisk_id}
145 cloud['imageBundles'][name] = detail
147 # Initialize sqlite3 database and tables.
148 dbPath = '/etc/sfa/db'
149 dbName = 'euca_aggregate.db'
151 if not os.path.isdir(dbPath):
152 logger.info('%s not found. Creating directory ...' % dbPath)
155 conn = connectionForURI('sqlite://%s/%s' % (dbPath, dbName))
156 sqlhub.processConnection = conn
157 Slice.createTable(ifNotExists=True)
158 EucaInstance.createTable(ifNotExists=True)
159 IP.createTable(ifNotExists=True)
161 # Start the update thread to keep track of the meta data
162 # about Eucalyptus instance.
163 Thread(target=updateMeta).start()
165 # Make sure the schema exists.
166 if not os.path.exists(EUCALYPTUS_RSPEC_SCHEMA):
167 err = 'Cannot location schema at %s' % EUCALYPTUS_RSPEC_SCHEMA
172 # Creates a connection to Eucalytpus. This function is inspired by
173 # the make_connection() in Euca2ools.
175 # @return A connection object or None
177 def getEucaConnection():
179 accessKey = cloud['access_key']
180 secretKey = cloud['secret_key']
181 eucaURL = cloud['cloud_url']
185 logger = logging.getLogger('EucaAggregate')
187 if not accessKey or not secretKey or not eucaURL:
188 logger.error('Please set ALL of the required environment ' \
189 'variables by sourcing the eucarc file.')
192 # Split the url into parts
193 if eucaURL.find('https://') >= 0:
195 eucaURL = eucaURL.replace('https://', '')
196 elif eucaURL.find('http://') >= 0:
198 eucaURL = eucaURL.replace('http://', '')
199 (eucaHost, parts) = eucaURL.split(':')
201 parts = parts.split('/')
202 eucaPort = int(parts[0])
204 srvPath = '/'.join(parts)
206 return boto.connect_ec2(aws_access_key_id=accessKey,
207 aws_secret_access_key=secretKey,
209 region=RegionInfo(None, 'eucalyptus', eucaHost),
214 # Returns a string of keys that belong to the users of the given slice.
215 # @param sliceHRN The hunman readable name of the slice.
218 def getKeysForSlice(sliceHRN):
219 logger = logging.getLogger('EucaAggregate')
221 # convert hrn to slice name
222 plSliceName = hrn_to_pl_slicename(sliceHRN)
223 except IndexError, e:
224 logger.error('Invalid slice name (%s)' % sliceHRN)
227 # Get the slice's information
228 sliceData = api.plshell.GetSlices(api.plauth, {'name':plSliceName})
230 logger.warn('Cannot get any data for slice %s' % plSliceName)
233 # It should only return a list with len = 1
234 sliceData = sliceData[0]
237 person_ids = sliceData['person_ids']
239 logger.warn('No users in slice %s' % sliceHRN)
242 persons = api.plshell.GetPersons(api.plauth, person_ids)
243 for person in persons:
244 pkeys = api.plshell.GetKeys(api.plauth, person['key_ids'])
246 keys.append(key['key'])
251 # A class that builds the RSpec for Eucalyptus.
253 class EucaRSpecBuilder(object):
255 # Initizes a RSpec builder
257 # @param cloud A dictionary containing data about a
258 # cloud (ex. clusters, ip)
259 def __init__(self, cloud):
260 self.eucaRSpec = XMLBuilder(format = True, tab_step = " ")
261 self.cloudInfo = cloud
264 # Creates a request stanza.
266 # @param num The number of instances to create.
267 # @param image The disk image id.
268 # @param kernel The kernel image id.
269 # @param keypair Key pair to embed.
270 # @param ramdisk Ramdisk id (optional).
272 def __requestXML(self, num, image, kernel, keypair, ramdisk = ''):
277 with xml.kernel_image(id=kernel):
283 with xml.ramdisk(id=ramdisk):
285 with xml.disk_image(id=image):
291 # Creates the cluster stanza.
293 # @param clusters Clusters information.
295 def __clustersXML(self, clusters):
296 cloud = self.cloudInfo
299 for cluster in clusters:
300 instances = cluster['instances']
301 with xml.cluster(id=cluster['name']):
305 for inst in instances:
306 with xml.vm_type(name=inst[0]):
309 with xml.max_instances:
313 with xml.memory(unit='MB'):
315 with xml.disk_space(unit='GB'):
317 if 'instances' in cloud and inst[0] in cloud['instances']:
318 existingEucaInstances = cloud['instances'][inst[0]]
319 with xml.euca_instances:
320 for eucaInst in existingEucaInstances:
321 with xml.euca_instance(id=eucaInst['id']):
323 xml << eucaInst['state']
325 xml << eucaInst['public_dns']
327 def __imageBundleXML(self, bundles):
330 for bundle in bundles.keys():
331 with xml.bundle(id=bundle):
335 # Creates the Images stanza.
337 # @param images A list of images in Eucalyptus.
339 def __imagesXML(self, images):
343 with xml.image(id=image.id):
347 xml << image.architecture
351 xml << image.location
354 # Creates the KeyPairs stanza.
356 # @param keypairs A list of key pairs in Eucalyptus.
358 def __keyPairsXML(self, keypairs):
366 # Generates the RSpec.
369 logger = logging.getLogger('EucaAggregate')
370 if not self.cloudInfo:
371 logger.error('No cloud information')
375 cloud = self.cloudInfo
376 with xml.RSpec(type='eucalyptus'):
377 with xml.network(id=cloud['name']):
380 #self.__keyPairsXML(cloud['keypairs'])
381 #self.__imagesXML(cloud['images'])
382 self.__imageBundleXML(cloud['imageBundles'])
383 self.__clustersXML(cloud['clusters'])
387 # A parser to parse the output of availability-zones.
389 # Note: Only one cluster is supported. If more than one, this will
392 class ZoneResultParser(object):
393 def __init__(self, zones):
397 if len(self.zones) < 3:
403 cluster['name'] = self.zones[0].name
404 cluster['ip'] = self.zones[0].state
406 for i in range(2, len(self.zones)):
407 currZone = self.zones[i]
408 instType = currZone.name.split()[1]
410 stateString = currZone.state.split('/')
411 rscString = stateString[1].split()
413 instFree = int(stateString[0])
414 instMax = int(rscString[0])
415 instNumCpu = int(rscString[1])
416 instRam = int(rscString[2])
417 instDiskSpace = int(rscString[3])
419 instTuple = (instType, instFree, instMax, instNumCpu, instRam, instDiskSpace)
420 instList.append(instTuple)
421 cluster['instances'] = instList
422 clusterList.append(cluster)
426 def ListResources(api, creds, options, call_id):
427 if Callids().already_handled(call_id): return ""
429 # get slice's hrn from options
430 xrn = options.get('geni_slice_urn', '')
431 hrn, type = urn_to_hrn(xrn)
432 logger = logging.getLogger('EucaAggregate')
434 # get hrn of the original caller
435 origin_hrn = options.get('origin_hrn', None)
437 origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
439 conn = getEucaConnection()
442 logger.error('Cannot create a connection to Eucalyptus')
443 return 'Cannot create a connection to Eucalyptus'
447 zones = conn.get_all_zones(['verbose'])
448 p = ZoneResultParser(zones)
450 cloud['clusters'] = clusters
453 images = conn.get_all_images()
454 cloud['images'] = images
455 cloud['imageBundles'] = {}
457 if i.type != 'machine' or i.kernel_id is None: continue
458 name = os.path.dirname(i.location)
459 detail = {'imageID' : i.id, 'kernelID' : i.kernel_id, 'ramdiskID' : i.ramdisk_id}
460 cloud['imageBundles'][name] = detail
463 keyPairs = conn.get_all_key_pairs()
464 cloud['keypairs'] = keyPairs
470 # Get the instances that belong to the given slice from sqlite3
471 # XXX use getOne() in production because the slice's hrn is supposed
472 # to be unique. For testing, uniqueness is turned off in the db.
473 # If the slice isn't found in the database, create a record for the
475 matchedSlices = list(Slice.select(Slice.q.slice_hrn == hrn))
477 theSlice = matchedSlices[-1]
479 theSlice = Slice(slice_hrn = hrn)
480 for instance in theSlice.instances:
481 instanceId.append(instance.instance_id)
483 # Get the information about those instances using their ids.
484 if len(instanceId) > 0:
485 reservations = conn.get_all_instances(instanceId)
488 for reservation in reservations:
489 for instance in reservation.instances:
490 instances.append(instance)
492 # Construct a dictionary for the EucaRSpecBuilder
494 for instance in instances:
495 instList = instancesDict.setdefault(instance.instance_type, [])
498 instInfoDict['id'] = instance.id
499 instInfoDict['public_dns'] = instance.public_dns_name
500 instInfoDict['state'] = instance.state
501 instInfoDict['key'] = instance.key_name
503 instList.append(instInfoDict)
504 cloud['instances'] = instancesDict
506 except EC2ResponseError, ec2RespErr:
507 errTree = ET.fromstring(ec2RespErr.body)
508 errMsgE = errTree.find('.//Message')
509 logger.error(errMsgE.text)
511 rspec = EucaRSpecBuilder(cloud).toXML()
513 # Remove the instances records so next time they won't
515 if 'instances' in cloud:
516 del cloud['instances']
521 Hook called via 'sfi.py create'
523 def CreateSliver(api, xrn, creds, xml, users, call_id):
524 if Callids().already_handled(call_id): return ""
527 hrn = urn_to_hrn(xrn)[0]
528 logger = logging.getLogger('EucaAggregate')
530 conn = getEucaConnection()
532 logger.error('Cannot create a connection to Eucalyptus')
536 schemaXML = ET.parse(EUCALYPTUS_RSPEC_SCHEMA)
537 rspecValidator = ET.RelaxNG(schemaXML)
538 rspecXML = ET.XML(xml)
539 if not rspecValidator(rspecXML):
540 error = rspecValidator.error_log.last_error
541 message = '%s (line %s)' % (error.message, error.line)
542 # XXX: InvalidRSpec is new. Currently, I am not working with Trunk code.
543 #raise InvalidRSpec(message)
544 raise Exception(message)
546 # Get the slice from db or create one.
547 s = Slice.select(Slice.q.slice_hrn == hrn).getOne(None)
549 s = Slice(slice_hrn = hrn)
551 # Process any changes in existing instance allocation
553 for sliceInst in s.instances:
554 pendingRmInst.append(sliceInst.instance_id)
555 existingInstGroup = rspecXML.findall('.//euca_instances')
556 for instGroup in existingInstGroup:
557 for existingInst in instGroup:
558 if existingInst.get('id') in pendingRmInst:
559 pendingRmInst.remove(existingInst.get('id'))
560 for inst in pendingRmInst:
561 logger.debug('Instance %s will be terminated' % inst)
562 dbInst = EucaInstance.select(EucaInstance.q.instance_id == inst).getOne(None)
563 # Only change the state but do not remove the entry from the DB.
564 dbInst.meta.state = 'deleted'
565 #dbInst.destroySelf()
566 conn.terminate_instances(pendingRmInst)
568 # Process new instance requests
569 requests = rspecXML.findall('.//request')
571 # Get all the public keys associate with slice.
572 pubKeys = getKeysForSlice(s.slice_hrn)
573 logger.debug('Passing the following keys to the instance:\n%s' % pubKeys)
575 vmTypeElement = req.getparent()
576 instType = vmTypeElement.get('name')
577 numInst = int(req.find('instances').text)
579 bundleName = req.find('bundle').text
580 if not cloud['imageBundles'][bundleName]:
581 logger.error('Cannot find bundle %s' % bundleName)
582 bundleInfo = cloud['imageBundles'][bundleName]
583 instKernel = bundleInfo['kernelID']
584 instDiskImg = bundleInfo['imageID']
585 instRamDisk = bundleInfo['ramdiskID']
588 # Create the instances
589 for i in range(0, numInst):
590 eucaInst = EucaInstance(slice = s,
591 kernel_id = instKernel,
592 image_id = instDiskImg,
593 ramdisk_id = instRamDisk,
595 inst_type = instType,
596 meta = Meta(start_time=datetime.datetime.now()))
597 eucaInst.reserveInstance(conn, pubKeys)
599 # xxx - should return altered rspec
600 # with enough data for the client to understand what's happened
604 # A thread that will update the meta data.
607 logger = logging.getLogger('EucaAggregate')
611 # Get IDs of the instances that don't have IPs yet.
612 dbResults = Meta.select(
613 AND(Meta.q.pri_addr == None,
614 Meta.q.state != 'deleted')
616 dbResults = list(dbResults)
617 logger.debug('[update thread] dbResults: %s' % dbResults)
620 instids.append(r.instance.instance_id)
621 logger.debug('[update thread] Instance Id: %s' % ', '.join(instids))
623 # Get instance information from Eucalyptus
624 conn = getEucaConnection()
626 reservations = conn.get_all_instances(instids)
627 for reservation in reservations:
628 vmInstances += reservation.instances
631 instIPs = [ {'id':i.id, 'pri_addr':i.private_dns_name, 'pub_addr':i.public_dns_name}
632 for i in vmInstances if i.private_dns_name != '0.0.0.0' ]
633 logger.debug('[update thread] IP dict: %s' % str(instIPs))
635 # Update the local DB
636 for ipData in instIPs:
637 dbInst = EucaInstance.select(EucaInstance.q.instance_id == ipData['id']).getOne(None)
639 logger.info('[update thread] Could not find %s in DB' % ipData['id'])
641 dbInst.meta.pri_addr = ipData['pri_addr']
642 dbInst.meta.pub_addr = ipData['pub_addr']
643 dbInst.meta.state = 'running'
649 #with open(sys.argv[1]) as xml:
650 # theRSpec = xml.read()
651 #CreateSliver(None, 'planetcloud.pc.test', theRSpec, 'call-id-cloudtest')
653 #rspec = ListResources('euca', 'planetcloud.pc.test', 'planetcloud.pc.marcoy', 'test_euca')
655 print getKeysForSlice('gc.gc.test1')
657 if __name__ == "__main__":