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, Xrn
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
24 from sfa.util.sfalogging import sfa_logger
25 from sfa.rspecs.sfa_rspec import sfa_rspec_version
26 from sfa.util.version import version_core
28 from multiprocessing import Process
29 from time import sleep
32 # The data structure used to represent a cloud.
33 # It contains the cloud name, its ip address, image information,
34 # key pairs, and clusters information.
39 # The location of the RelaxNG schema.
41 EUCALYPTUS_RSPEC_SCHEMA='/etc/sfa/eucalyptus.rng'
46 # Meta data of an instance.
48 class Meta(SQLObject):
49 instance = SingleJoin('EucaInstance')
50 state = StringCol(default = 'new')
51 pub_addr = StringCol(default = None)
52 pri_addr = StringCol(default = None)
53 start_time = DateTimeCol(default = None)
56 # A representation of an Eucalyptus instance. This is a support class
57 # for instance <-> slice mapping.
59 class EucaInstance(SQLObject):
60 instance_id = StringCol(unique=True, default=None)
61 kernel_id = StringCol()
62 image_id = StringCol()
63 ramdisk_id = StringCol()
64 inst_type = StringCol()
65 key_pair = StringCol()
66 slice = ForeignKey('Slice')
67 meta = ForeignKey('Meta')
70 # Contacts Eucalyptus and tries to reserve this instance.
72 # @param botoConn A connection to Eucalyptus.
73 # @param pubKeys A list of public keys for the instance.
75 def reserveInstance(self, botoConn, pubKeys):
76 logger = logging.getLogger('EucaAggregate')
77 logger.info('Reserving an instance: image: %s, kernel: ' \
78 '%s, ramdisk: %s, type: %s, key: %s' % \
79 (self.image_id, self.kernel_id, self.ramdisk_id,
80 self.inst_type, self.key_pair))
82 # XXX The return statement is for testing. REMOVE in production
86 reservation = botoConn.run_instances(self.image_id,
87 kernel_id = self.kernel_id,
88 ramdisk_id = self.ramdisk_id,
89 instance_type = self.inst_type,
90 key_name = self.key_pair,
92 for instance in reservation.instances:
93 self.instance_id = instance.id
95 # If there is an error, destroy itself.
96 except EC2ResponseError, ec2RespErr:
97 errTree = ET.fromstring(ec2RespErr.body)
98 msg = errTree.find('.//Message')
99 logger.error(msg.text)
103 # A representation of a PlanetLab slice. This is a support class
104 # for instance <-> slice mapping.
106 class Slice(SQLObject):
107 slice_hrn = StringCol()
108 #slice_index = DatabaseIndex('slice_hrn')
109 instances = MultipleJoin('EucaInstance')
112 # Initialize the aggregate manager by reading a configuration file.
115 logger = logging.getLogger('EucaAggregate')
116 fileHandler = logging.FileHandler('/var/log/euca.log')
117 fileHandler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
118 logger.addHandler(fileHandler)
119 fileHandler.setLevel(logging.DEBUG)
120 logger.setLevel(logging.DEBUG)
122 configParser = ConfigParser()
123 configParser.read(['/etc/sfa/eucalyptus_aggregate.conf', 'eucalyptus_aggregate.conf'])
124 if len(configParser.sections()) < 1:
125 logger.error('No cloud defined in the config file')
126 raise Exception('Cannot find cloud definition in configuration file.')
128 # Only read the first section.
129 cloudSec = configParser.sections()[0]
130 cloud['name'] = cloudSec
131 cloud['access_key'] = configParser.get(cloudSec, 'access_key')
132 cloud['secret_key'] = configParser.get(cloudSec, 'secret_key')
133 cloud['cloud_url'] = configParser.get(cloudSec, 'cloud_url')
134 cloudURL = cloud['cloud_url']
135 if cloudURL.find('https://') >= 0:
136 cloudURL = cloudURL.replace('https://', '')
137 elif cloudURL.find('http://') >= 0:
138 cloudURL = cloudURL.replace('http://', '')
139 (cloud['ip'], parts) = cloudURL.split(':')
141 # Create image bundles
142 images = getEucaConnection().get_all_images()
143 cloud['images'] = images
144 cloud['imageBundles'] = {}
146 if i.type != 'machine' or i.kernel_id is None: continue
147 name = os.path.dirname(i.location)
148 detail = {'imageID' : i.id, 'kernelID' : i.kernel_id, 'ramdiskID' : i.ramdisk_id}
149 cloud['imageBundles'][name] = detail
151 # Initialize sqlite3 database and tables.
152 dbPath = '/etc/sfa/db'
153 dbName = 'euca_aggregate.db'
155 if not os.path.isdir(dbPath):
156 logger.info('%s not found. Creating directory ...' % dbPath)
159 conn = connectionForURI('sqlite://%s/%s' % (dbPath, dbName))
160 sqlhub.processConnection = conn
161 Slice.createTable(ifNotExists=True)
162 EucaInstance.createTable(ifNotExists=True)
163 Meta.createTable(ifNotExists=True)
165 # Start the update process to keep track of the meta data
166 # about Eucalyptus instance.
167 Process(target=updateMeta).start()
169 # Make sure the schema exists.
170 if not os.path.exists(EUCALYPTUS_RSPEC_SCHEMA):
171 err = 'Cannot location schema at %s' % EUCALYPTUS_RSPEC_SCHEMA
176 # Creates a connection to Eucalytpus. This function is inspired by
177 # the make_connection() in Euca2ools.
179 # @return A connection object or None
181 def getEucaConnection():
183 accessKey = cloud['access_key']
184 secretKey = cloud['secret_key']
185 eucaURL = cloud['cloud_url']
189 logger = logging.getLogger('EucaAggregate')
191 if not accessKey or not secretKey or not eucaURL:
192 logger.error('Please set ALL of the required environment ' \
193 'variables by sourcing the eucarc file.')
196 # Split the url into parts
197 if eucaURL.find('https://') >= 0:
199 eucaURL = eucaURL.replace('https://', '')
200 elif eucaURL.find('http://') >= 0:
202 eucaURL = eucaURL.replace('http://', '')
203 (eucaHost, parts) = eucaURL.split(':')
205 parts = parts.split('/')
206 eucaPort = int(parts[0])
208 srvPath = '/'.join(parts)
210 return boto.connect_ec2(aws_access_key_id=accessKey,
211 aws_secret_access_key=secretKey,
213 region=RegionInfo(None, 'eucalyptus', eucaHost),
218 # Returns a string of keys that belong to the users of the given slice.
219 # @param sliceHRN The hunman readable name of the slice.
222 def getKeysForSlice(api, sliceHRN):
223 logger = logging.getLogger('EucaAggregate')
224 cred = api.getCredential()
225 registry = api.registries[api.hrn]
228 # Get the slice record
229 records = registry.Resolve(sliceHRN, cred)
231 logging.warn('Cannot find any record for slice %s' % sliceHRN)
234 # Find who can log into this slice
235 persons = records[0]['persons']
237 # Extract the keys from persons records
239 sliceUser = registry.Resolve(p, cred)
240 userKeys = sliceUser[0]['keys']
246 # A class that builds the RSpec for Eucalyptus.
248 class EucaRSpecBuilder(object):
250 # Initizes a RSpec builder
252 # @param cloud A dictionary containing data about a
253 # cloud (ex. clusters, ip)
254 def __init__(self, cloud):
255 self.eucaRSpec = XMLBuilder(format = True, tab_step = " ")
256 self.cloudInfo = cloud
259 # Creates a request stanza.
261 # @param num The number of instances to create.
262 # @param image The disk image id.
263 # @param kernel The kernel image id.
264 # @param keypair Key pair to embed.
265 # @param ramdisk Ramdisk id (optional).
267 def __requestXML(self, num, image, kernel, keypair, ramdisk = ''):
272 with xml.kernel_image(id=kernel):
278 with xml.ramdisk(id=ramdisk):
280 with xml.disk_image(id=image):
286 # Creates the cluster stanza.
288 # @param clusters Clusters information.
290 def __clustersXML(self, clusters):
291 cloud = self.cloudInfo
294 for cluster in clusters:
295 instances = cluster['instances']
296 with xml.cluster(id=cluster['name']):
300 for inst in instances:
301 with xml.vm_type(name=inst[0]):
304 with xml.max_instances:
308 with xml.memory(unit='MB'):
310 with xml.disk_space(unit='GB'):
312 if 'instances' in cloud and inst[0] in cloud['instances']:
313 existingEucaInstances = cloud['instances'][inst[0]]
314 with xml.euca_instances:
315 for eucaInst in existingEucaInstances:
316 with xml.euca_instance(id=eucaInst['id']):
318 xml << eucaInst['state']
320 xml << eucaInst['public_dns']
322 def __imageBundleXML(self, bundles):
325 for bundle in bundles.keys():
326 with xml.bundle(id=bundle):
330 # Creates the Images stanza.
332 # @param images A list of images in Eucalyptus.
334 def __imagesXML(self, images):
338 with xml.image(id=image.id):
342 xml << image.architecture
346 xml << image.location
349 # Creates the KeyPairs stanza.
351 # @param keypairs A list of key pairs in Eucalyptus.
353 def __keyPairsXML(self, keypairs):
361 # Generates the RSpec.
364 logger = logging.getLogger('EucaAggregate')
365 if not self.cloudInfo:
366 logger.error('No cloud information')
370 cloud = self.cloudInfo
371 with xml.RSpec(type='eucalyptus'):
372 with xml.network(id=cloud['name']):
375 #self.__keyPairsXML(cloud['keypairs'])
376 #self.__imagesXML(cloud['images'])
377 self.__imageBundleXML(cloud['imageBundles'])
378 self.__clustersXML(cloud['clusters'])
382 # A parser to parse the output of availability-zones.
384 # Note: Only one cluster is supported. If more than one, this will
387 class ZoneResultParser(object):
388 def __init__(self, zones):
392 if len(self.zones) < 3:
398 cluster['name'] = self.zones[0].name
399 cluster['ip'] = self.zones[0].state
401 for i in range(2, len(self.zones)):
402 currZone = self.zones[i]
403 instType = currZone.name.split()[1]
405 stateString = currZone.state.split('/')
406 rscString = stateString[1].split()
408 instFree = int(stateString[0])
409 instMax = int(rscString[0])
410 instNumCpu = int(rscString[1])
411 instRam = int(rscString[2])
412 instDiskSpace = int(rscString[3])
414 instTuple = (instType, instFree, instMax, instNumCpu, instRam, instDiskSpace)
415 instList.append(instTuple)
416 cluster['instances'] = instList
417 clusterList.append(cluster)
421 def ListResources(api, creds, options, call_id):
422 if Callids().already_handled(call_id): return ""
424 # get slice's hrn from options
425 xrn = options.get('geni_slice_urn', '')
426 hrn, type = urn_to_hrn(xrn)
427 logger = logging.getLogger('EucaAggregate')
429 # get hrn of the original caller
430 origin_hrn = options.get('origin_hrn', None)
432 origin_hrn = Credential(string=creds).get_gid_caller().get_hrn()
433 # origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
435 conn = getEucaConnection()
438 logger.error('Cannot create a connection to Eucalyptus')
439 return 'Cannot create a connection to Eucalyptus'
443 zones = conn.get_all_zones(['verbose'])
444 p = ZoneResultParser(zones)
446 cloud['clusters'] = clusters
449 images = conn.get_all_images()
450 cloud['images'] = images
451 cloud['imageBundles'] = {}
453 if i.type != 'machine' or i.kernel_id is None: continue
454 name = os.path.dirname(i.location)
455 detail = {'imageID' : i.id, 'kernelID' : i.kernel_id, 'ramdiskID' : i.ramdisk_id}
456 cloud['imageBundles'][name] = detail
459 keyPairs = conn.get_all_key_pairs()
460 cloud['keypairs'] = keyPairs
466 # Get the instances that belong to the given slice from sqlite3
467 # XXX use getOne() in production because the slice's hrn is supposed
468 # to be unique. For testing, uniqueness is turned off in the db.
469 # If the slice isn't found in the database, create a record for the
471 matchedSlices = list(Slice.select(Slice.q.slice_hrn == hrn))
473 theSlice = matchedSlices[-1]
475 theSlice = Slice(slice_hrn = hrn)
476 for instance in theSlice.instances:
477 instanceId.append(instance.instance_id)
479 # Get the information about those instances using their ids.
480 if len(instanceId) > 0:
481 reservations = conn.get_all_instances(instanceId)
484 for reservation in reservations:
485 for instance in reservation.instances:
486 instances.append(instance)
488 # Construct a dictionary for the EucaRSpecBuilder
490 for instance in instances:
491 instList = instancesDict.setdefault(instance.instance_type, [])
494 instInfoDict['id'] = instance.id
495 instInfoDict['public_dns'] = instance.public_dns_name
496 instInfoDict['state'] = instance.state
497 instInfoDict['key'] = instance.key_name
499 instList.append(instInfoDict)
500 cloud['instances'] = instancesDict
502 except EC2ResponseError, ec2RespErr:
503 errTree = ET.fromstring(ec2RespErr.body)
504 errMsgE = errTree.find('.//Message')
505 logger.error(errMsgE.text)
507 rspec = EucaRSpecBuilder(cloud).toXML()
509 # Remove the instances records so next time they won't
511 if 'instances' in cloud:
512 del cloud['instances']
517 Hook called via 'sfi.py create'
519 def CreateSliver(api, xrn, creds, xml, users, call_id):
520 if Callids().already_handled(call_id): return ""
523 hrn = urn_to_hrn(xrn)[0]
524 logger = logging.getLogger('EucaAggregate')
526 conn = getEucaConnection()
528 logger.error('Cannot create a connection to Eucalyptus')
532 schemaXML = ET.parse(EUCALYPTUS_RSPEC_SCHEMA)
533 rspecValidator = ET.RelaxNG(schemaXML)
534 rspecXML = ET.XML(xml)
535 for network in rspecXML.iterfind("./network"):
536 if network.get('id') != cloud['name']:
537 # Throw away everything except my own RSpec
538 # sfa_logger().error("CreateSliver: deleting %s from rspec"%network.get('id'))
539 network.getparent().remove(network)
540 if not rspecValidator(rspecXML):
541 error = rspecValidator.error_log.last_error
542 message = '%s (line %s)' % (error.message, error.line)
543 # XXX: InvalidRSpec is new. Currently, I am not working with Trunk code.
544 #raise InvalidRSpec(message)
545 raise Exception(message)
547 # Get the slice from db or create one.
548 s = Slice.select(Slice.q.slice_hrn == hrn).getOne(None)
550 s = Slice(slice_hrn = hrn)
552 # Process any changes in existing instance allocation
554 for sliceInst in s.instances:
555 pendingRmInst.append(sliceInst.instance_id)
556 existingInstGroup = rspecXML.findall(".//euca_instances")
557 for instGroup in existingInstGroup:
558 for existingInst in instGroup:
559 if existingInst.get('id') in pendingRmInst:
560 pendingRmInst.remove(existingInst.get('id'))
561 for inst in pendingRmInst:
562 logger.debug('Instance %s will be terminated' % inst)
563 dbInst = EucaInstance.select(EucaInstance.q.instance_id == inst).getOne(None)
564 # Only change the state but do not remove the entry from the DB.
565 dbInst.meta.state = 'deleted'
566 #dbInst.destroySelf()
567 conn.terminate_instances(pendingRmInst)
569 # Process new instance requests
570 requests = rspecXML.findall(".//request")
572 # Get all the public keys associate with slice.
573 pubKeys = getKeysForSlice(api, s.slice_hrn)
574 logger.debug('Passing the following keys to the instance:\n%s' % pubKeys)
576 vmTypeElement = req.getparent()
577 instType = vmTypeElement.get('name')
578 numInst = int(req.find('instances').text)
580 bundleName = req.find('bundle').text
581 if not cloud['imageBundles'][bundleName]:
582 logger.error('Cannot find bundle %s' % bundleName)
583 bundleInfo = cloud['imageBundles'][bundleName]
584 instKernel = bundleInfo['kernelID']
585 instDiskImg = bundleInfo['imageID']
586 instRamDisk = bundleInfo['ramdiskID']
589 # Create the instances
590 for i in range(0, numInst):
591 eucaInst = EucaInstance(slice = s,
592 kernel_id = instKernel,
593 image_id = instDiskImg,
594 ramdisk_id = instRamDisk,
596 inst_type = instType,
597 meta = Meta(start_time=datetime.datetime.now()))
598 eucaInst.reserveInstance(conn, pubKeys)
600 # xxx - should return altered rspec
601 # with enough data for the client to understand what's happened
605 # A separate process that will update the meta data.
608 logger = logging.getLogger('EucaMeta')
609 fileHandler = logging.FileHandler('/var/log/euca_meta.log')
610 fileHandler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
611 logger.addHandler(fileHandler)
612 fileHandler.setLevel(logging.DEBUG)
613 logger.setLevel(logging.DEBUG)
618 # Get IDs of the instances that don't have IPs yet.
619 dbResults = Meta.select(
620 AND(Meta.q.pri_addr == None,
621 Meta.q.state != 'deleted')
623 dbResults = list(dbResults)
624 logger.debug('[update process] dbResults: %s' % dbResults)
627 instids.append(r.instance.instance_id)
628 logger.debug('[update process] Instance Id: %s' % ', '.join(instids))
630 # Get instance information from Eucalyptus
631 conn = getEucaConnection()
633 reservations = conn.get_all_instances(instids)
634 for reservation in reservations:
635 vmInstances += reservation.instances
638 instIPs = [ {'id':i.id, 'pri_addr':i.private_dns_name, 'pub_addr':i.public_dns_name}
639 for i in vmInstances if i.private_dns_name != '0.0.0.0' ]
640 logger.debug('[update process] IP dict: %s' % str(instIPs))
642 # Update the local DB
643 for ipData in instIPs:
644 dbInst = EucaInstance.select(EucaInstance.q.instance_id == ipData['id']).getOne(None)
646 logger.info('[update process] Could not find %s in DB' % ipData['id'])
648 dbInst.meta.pri_addr = ipData['pri_addr']
649 dbInst.meta.pub_addr = ipData['pub_addr']
650 dbInst.meta.state = 'running'
654 request_rspec_versions = [dict(sfa_rspec_version)]
655 ad_rspec_versions = [dict(sfa_rspec_version)]
656 version_more = {'interface':'aggregate',
659 'request_rspec_versions': request_rspec_versions,
660 'ad_rspec_versions': ad_rspec_versions,
661 'default_ad_rspec': dict(sfa_rspec_version)
663 return version_core(version_more)
669 #with open(sys.argv[1]) as xml:
670 # theRSpec = xml.read()
671 #CreateSliver(None, 'planetcloud.pc.test', theRSpec, 'call-id-cloudtest')
673 #rspec = ListResources('euca', 'planetcloud.pc.test', 'planetcloud.pc.marcoy', 'test_euca')
676 server_key_file = '/var/lib/sfa/authorities/server.key'
677 server_cert_file = '/var/lib/sfa/authorities/server.cert'
678 api = SfaAPI(key_file = server_key_file, cert_file = server_cert_file, interface='aggregate')
679 print getKeysForSlice(api, 'gc.gc.test1')
681 if __name__ == "__main__":