Merge branch 'master' into eucalyptus-devel
[sfa.git] / sfa / managers / aggregate_manager_eucalyptus.py
1 from __future__ import with_statement 
2
3 import sys
4 import os
5 import logging
6 import datetime
7
8 import boto
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 *
15
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
24
25 from threading import Thread
26 from time import sleep
27
28 ##
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.
32 #
33 cloud = {}
34
35 ##
36 # The location of the RelaxNG schema.
37 #
38 EUCALYPTUS_RSPEC_SCHEMA='/etc/sfa/eucalyptus.rng'
39
40 api = SfaAPI()
41
42 ##
43 # Meta data of an instance.
44 #
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)
51
52 ##
53 # A representation of an Eucalyptus instance. This is a support class
54 # for instance <-> slice mapping.
55 #
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')
65
66     ##
67     # Contacts Eucalyptus and tries to reserve this instance.
68     # 
69     # @param botoConn A connection to Eucalyptus.
70     # @param pubKeys A list of public keys for the instance.
71     #
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))
78
79         # XXX The return statement is for testing. REMOVE in production
80         #return
81
82         try:
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,
88                                                  user_data = pubKeys)
89             for instance in reservation.instances:
90                 self.instance_id = instance.id
91
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)
97             self.destroySelf()
98
99 ##
100 # A representation of a PlanetLab slice. This is a support class
101 # for instance <-> slice mapping.
102 #
103 class Slice(SQLObject):
104     slice_hrn = StringCol()
105     #slice_index = DatabaseIndex('slice_hrn')
106     instances = MultipleJoin('EucaInstance')
107
108 ##
109 # Initialize the aggregate manager by reading a configuration file.
110 #
111 def init_server():
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)
117
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.')
123
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(':')
136
137     # Create image bundles
138     images = getEucaConnection().get_all_images()
139     cloud['images'] = images
140     cloud['imageBundles'] = {}
141     for i in images:
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
146
147     # Initialize sqlite3 database and tables.
148     dbPath = '/etc/sfa/db'
149     dbName = 'euca_aggregate.db'
150
151     if not os.path.isdir(dbPath):
152         logger.info('%s not found. Creating directory ...' % dbPath)
153         os.mkdir(dbPath)
154
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)
160
161     # Start the update thread to keep track of the meta data
162     # about Eucalyptus instance.
163     Thread(target=updateMeta).start()
164
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
168         logger.error(err)
169         raise Exception(err)
170
171 ##
172 # Creates a connection to Eucalytpus. This function is inspired by 
173 # the make_connection() in Euca2ools.
174 #
175 # @return A connection object or None
176 #
177 def getEucaConnection():
178     global cloud
179     accessKey = cloud['access_key']
180     secretKey = cloud['secret_key']
181     eucaURL   = cloud['cloud_url']
182     useSSL    = False
183     srvPath   = '/'
184     eucaPort  = 8773
185     logger    = logging.getLogger('EucaAggregate')
186
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.')
190         return None
191     
192     # Split the url into parts
193     if eucaURL.find('https://') >= 0:
194         useSSL  = True
195         eucaURL = eucaURL.replace('https://', '')
196     elif eucaURL.find('http://') >= 0:
197         useSSL  = False
198         eucaURL = eucaURL.replace('http://', '')
199     (eucaHost, parts) = eucaURL.split(':')
200     if len(parts) > 1:
201         parts = parts.split('/')
202         eucaPort = int(parts[0])
203         parts = parts[1:]
204         srvPath = '/'.join(parts)
205
206     return boto.connect_ec2(aws_access_key_id=accessKey,
207                             aws_secret_access_key=secretKey,
208                             is_secure=useSSL,
209                             region=RegionInfo(None, 'eucalyptus', eucaHost), 
210                             port=eucaPort,
211                             path=srvPath)
212
213 ##
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.
216 # @return sting()
217 #
218 def getKeysForSlice(sliceHRN):
219     logger = logging.getLogger('EucaAggregate')
220     try:
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)
225         return []
226
227     # Get the slice's information
228     sliceData = api.plshell.GetSlices(api.plauth, {'name':plSliceName})
229     if not sliceData:
230         logger.warn('Cannot get any data for slice %s' % plSliceName)
231         return []
232
233     # It should only return a list with len = 1
234     sliceData = sliceData[0]
235
236     keys = []
237     person_ids = sliceData['person_ids']
238     if not person_ids: 
239         logger.warn('No users in slice %s' % sliceHRN)
240         return []
241
242     persons = api.plshell.GetPersons(api.plauth, person_ids)
243     for person in persons:
244         pkeys = api.plshell.GetKeys(api.plauth, person['key_ids'])
245         for key in pkeys:
246             keys.append(key['key'])
247  
248     return ''.join(keys)
249
250 ##
251 # A class that builds the RSpec for Eucalyptus.
252 #
253 class EucaRSpecBuilder(object):
254     ##
255     # Initizes a RSpec builder
256     #
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
262
263     ##
264     # Creates a request stanza.
265     # 
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).
271     #
272     def __requestXML(self, num, image, kernel, keypair, ramdisk = ''):
273         xml = self.eucaRSpec
274         with xml.request:
275             with xml.instances:
276                 xml << str(num)
277             with xml.kernel_image(id=kernel):
278                 xml << ''
279             if ramdisk == '':
280                 with xml.ramdisk:
281                     xml << ''
282             else:
283                 with xml.ramdisk(id=ramdisk):
284                     xml << ''
285             with xml.disk_image(id=image):
286                 xml << ''
287             with xml.keypair:
288                 xml << keypair
289
290     ##
291     # Creates the cluster stanza.
292     #
293     # @param clusters Clusters information.
294     #
295     def __clustersXML(self, clusters):
296         cloud = self.cloudInfo
297         xml = self.eucaRSpec
298
299         for cluster in clusters:
300             instances = cluster['instances']
301             with xml.cluster(id=cluster['name']):
302                 with xml.ipv4:
303                     xml << cluster['ip']
304                 with xml.vm_types:
305                     for inst in instances:
306                         with xml.vm_type(name=inst[0]):
307                             with xml.free_slots:
308                                 xml << str(inst[1])
309                             with xml.max_instances:
310                                 xml << str(inst[2])
311                             with xml.cores:
312                                 xml << str(inst[3])
313                             with xml.memory(unit='MB'):
314                                 xml << str(inst[4])
315                             with xml.disk_space(unit='GB'):
316                                 xml << str(inst[5])
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']):
322                                             with xml.state:
323                                                 xml << eucaInst['state']
324                                             with xml.public_dns:
325                                                 xml << eucaInst['public_dns']
326
327     def __imageBundleXML(self, bundles):
328         xml = self.eucaRSpec
329         with xml.bundles:
330             for bundle in bundles.keys():
331                 with xml.bundle(id=bundle):
332                     xml << ''
333
334     ##
335     # Creates the Images stanza.
336     #
337     # @param images A list of images in Eucalyptus.
338     #
339     def __imagesXML(self, images):
340         xml = self.eucaRSpec
341         with xml.images:
342             for image in images:
343                 with xml.image(id=image.id):
344                     with xml.type:
345                         xml << image.type
346                     with xml.arch:
347                         xml << image.architecture
348                     with xml.state:
349                         xml << image.state
350                     with xml.location:
351                         xml << image.location
352
353     ##
354     # Creates the KeyPairs stanza.
355     #
356     # @param keypairs A list of key pairs in Eucalyptus.
357     #
358     def __keyPairsXML(self, keypairs):
359         xml = self.eucaRSpec
360         with xml.keypairs:
361             for key in keypairs:
362                 with xml.keypair:
363                     xml << key.name
364
365     ##
366     # Generates the RSpec.
367     #
368     def toXML(self):
369         logger = logging.getLogger('EucaAggregate')
370         if not self.cloudInfo:
371             logger.error('No cloud information')
372             return ''
373
374         xml = self.eucaRSpec
375         cloud = self.cloudInfo
376         with xml.RSpec(type='eucalyptus'):
377             with xml.network(id=cloud['name']):
378                 with xml.ipv4:
379                     xml << cloud['ip']
380                 #self.__keyPairsXML(cloud['keypairs'])
381                 #self.__imagesXML(cloud['images'])
382                 self.__imageBundleXML(cloud['imageBundles'])
383                 self.__clustersXML(cloud['clusters'])
384         return str(xml)
385
386 ##
387 # A parser to parse the output of availability-zones.
388 #
389 # Note: Only one cluster is supported. If more than one, this will
390 #       not work.
391 #
392 class ZoneResultParser(object):
393     def __init__(self, zones):
394         self.zones = zones
395
396     def parse(self):
397         if len(self.zones) < 3:
398             return
399         clusterList = []
400         cluster = {} 
401         instList = []
402
403         cluster['name'] = self.zones[0].name
404         cluster['ip']   = self.zones[0].state
405
406         for i in range(2, len(self.zones)):
407             currZone = self.zones[i]
408             instType = currZone.name.split()[1]
409
410             stateString = currZone.state.split('/')
411             rscString   = stateString[1].split()
412
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])
418
419             instTuple = (instType, instFree, instMax, instNumCpu, instRam, instDiskSpace)
420             instList.append(instTuple)
421         cluster['instances'] = instList
422         clusterList.append(cluster)
423
424         return clusterList
425
426 def ListResources(api, creds, options, call_id): 
427     if Callids().already_handled(call_id): return ""
428     global cloud
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')
433
434     # get hrn of the original caller
435     origin_hrn = options.get('origin_hrn', None)
436     if not origin_hrn:
437         origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
438
439     conn = getEucaConnection()
440
441     if not conn:
442         logger.error('Cannot create a connection to Eucalyptus')
443         return 'Cannot create a connection to Eucalyptus'
444
445     try:
446         # Zones
447         zones = conn.get_all_zones(['verbose'])
448         p = ZoneResultParser(zones)
449         clusters = p.parse()
450         cloud['clusters'] = clusters
451         
452         # Images
453         images = conn.get_all_images()
454         cloud['images'] = images
455         cloud['imageBundles'] = {}
456         for i in images:
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
461
462         # Key Pairs
463         keyPairs = conn.get_all_key_pairs()
464         cloud['keypairs'] = keyPairs
465
466         if hrn:
467             instanceId = []
468             instances  = []
469
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 
474             # slice.
475             matchedSlices = list(Slice.select(Slice.q.slice_hrn == hrn))
476             if matchedSlices:
477                 theSlice = matchedSlices[-1]
478             else:
479                 theSlice = Slice(slice_hrn = hrn)
480             for instance in theSlice.instances:
481                 instanceId.append(instance.instance_id)
482
483             # Get the information about those instances using their ids.
484             if len(instanceId) > 0:
485                 reservations = conn.get_all_instances(instanceId)
486             else:
487                 reservations = []
488             for reservation in reservations:
489                 for instance in reservation.instances:
490                     instances.append(instance)
491
492             # Construct a dictionary for the EucaRSpecBuilder
493             instancesDict = {}
494             for instance in instances:
495                 instList = instancesDict.setdefault(instance.instance_type, [])
496                 instInfoDict = {} 
497
498                 instInfoDict['id'] = instance.id
499                 instInfoDict['public_dns'] = instance.public_dns_name
500                 instInfoDict['state'] = instance.state
501                 instInfoDict['key'] = instance.key_name
502
503                 instList.append(instInfoDict)
504             cloud['instances'] = instancesDict
505
506     except EC2ResponseError, ec2RespErr:
507         errTree = ET.fromstring(ec2RespErr.body)
508         errMsgE = errTree.find('.//Message')
509         logger.error(errMsgE.text)
510
511     rspec = EucaRSpecBuilder(cloud).toXML()
512
513     # Remove the instances records so next time they won't 
514     # show up.
515     if 'instances' in cloud:
516         del cloud['instances']
517
518     return rspec
519
520 """
521 Hook called via 'sfi.py create'
522 """
523 def CreateSliver(api, xrn, creds, xml, users, call_id):
524     if Callids().already_handled(call_id): return ""
525
526     global cloud
527     hrn = urn_to_hrn(xrn)[0]
528     logger = logging.getLogger('EucaAggregate')
529
530     conn = getEucaConnection()
531     if not conn:
532         logger.error('Cannot create a connection to Eucalyptus')
533         return ""
534
535     # Validate RSpec
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)
545
546     # Get the slice from db or create one.
547     s = Slice.select(Slice.q.slice_hrn == hrn).getOne(None)
548     if s is None:
549         s = Slice(slice_hrn = hrn)
550
551     # Process any changes in existing instance allocation
552     pendingRmInst = []
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)
567
568     # Process new instance requests
569     requests = rspecXML.findall('.//request')
570     if requests:
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)
574     for req in requests:
575         vmTypeElement = req.getparent()
576         instType = vmTypeElement.get('name')
577         numInst  = int(req.find('instances').text)
578         
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']
586         instKey     = None
587
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,
594                                     key_pair   = instKey,
595                                     inst_type  = instType,
596                                     meta       = Meta(start_time=datetime.datetime.now()))
597             eucaInst.reserveInstance(conn, pubKeys)
598
599     # xxx - should return altered rspec 
600     # with enough data for the client to understand what's happened
601     return xml
602
603 ##
604 # A thread that will update the meta data.
605 #
606 def updateMeta():
607     logger = logging.getLogger('EucaAggregate')
608     while True:
609         sleep(120)
610
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')
615                     )
616         dbResults = list(dbResults)
617         logger.debug('[update thread] dbResults: %s' % dbResults)
618         instids = []
619         for r in dbResults:
620             instids.append(r.instance.instance_id)
621         logger.debug('[update thread] Instance Id: %s' % ', '.join(instids))
622
623         # Get instance information from Eucalyptus
624         conn = getEucaConnection()
625         vmInstances = []
626         reservations = conn.get_all_instances(instids)
627         for reservation in reservations:
628             vmInstances += reservation.instances
629
630         # Check the IPs
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))
634
635         # Update the local DB
636         for ipData in instIPs:
637             dbInst = EucaInstance.select(EucaInstance.q.instance_id == ipData['id']).getOne(None)
638             if not dbInst:
639                 logger.info('[update thread] Could not find %s in DB' % ipData['id'])
640                 continue
641             dbInst.meta.pri_addr = ipData['pri_addr']
642             dbInst.meta.pub_addr = ipData['pub_addr']
643             dbInst.meta.state    = 'running'
644
645 def main():
646     init_server()
647
648     #theRSpec = None
649     #with open(sys.argv[1]) as xml:
650     #    theRSpec = xml.read()
651     #CreateSliver(None, 'planetcloud.pc.test', theRSpec, 'call-id-cloudtest')
652
653     #rspec = ListResources('euca', 'planetcloud.pc.test', 'planetcloud.pc.marcoy', 'test_euca')
654     #print rspec
655     print getKeysForSlice('gc.gc.test1')
656
657 if __name__ == "__main__":
658     main()
659