Merge branch 'master' of ssh://git.planet-lab.org/git/sfa
[sfa.git] / sfa / managers / aggregate_manager_eucalyptus.py
1 from __future__ import with_statement 
2
3 import sys
4 import os
5
6 import boto
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 *
13
14 from sfa.util.faults import *
15 from sfa.util.xrn import urn_to_hrn, Xrn
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
22 from sfa.util.sfalogging import sfa_logger
23 from sfa.rspecs.sfa_rspec import sfa_rspec_version
24 from sfa.util.version import version_core
25
26 ##
27 # The data structure used to represent a cloud.
28 # It contains the cloud name, its ip address, image information,
29 # key pairs, and clusters information.
30 #
31 cloud = {}
32
33 ##
34 # The location of the RelaxNG schema.
35 #
36 EUCALYPTUS_RSPEC_SCHEMA='/etc/sfa/eucalyptus.rng'
37
38 # Quick hack
39 sys.stderr = file('/var/log/euca_agg.log', 'a+')
40 api = SfaAPI()
41
42 ##
43 # A representation of an Eucalyptus instance. This is a support class
44 # for instance <-> slice mapping.
45 #
46 class EucaInstance(SQLObject):
47     instance_id = StringCol(unique=True, default=None)
48     kernel_id   = StringCol()
49     image_id    = StringCol()
50     ramdisk_id  = StringCol()
51     inst_type   = StringCol()
52     key_pair    = StringCol()
53     slice = ForeignKey('Slice')
54
55     ##
56     # Contacts Eucalyptus and tries to reserve this instance.
57     # 
58     # @param botoConn A connection to Eucalyptus.
59     # @param pubKeys A list of public keys for the instance.
60     #
61     def reserveInstance(self, botoConn, pubKeys):
62         print >>sys.stderr, 'Reserving an instance: image: %s, kernel: ' \
63                             '%s, ramdisk: %s, type: %s, key: %s' % \
64                             (self.image_id, self.kernel_id, self.ramdisk_id, 
65                              self.inst_type, self.key_pair)
66
67         # XXX The return statement is for testing. REMOVE in production
68         #return
69
70         try:
71             reservation = botoConn.run_instances(self.image_id,
72                                                  kernel_id = self.kernel_id,
73                                                  ramdisk_id = self.ramdisk_id,
74                                                  instance_type = self.inst_type,
75                                                  key_name  = self.key_pair,
76                                                  user_data = pubKeys)
77             for instance in reservation.instances:
78                 self.instance_id = instance.id
79
80         # If there is an error, destroy itself.
81         except EC2ResponseError, ec2RespErr:
82             errTree = ET.fromstring(ec2RespErr.body)
83             msg = errTree.find('.//Message')
84             print >>sys.stderr, msg.text
85             self.destroySelf()
86
87 ##
88 # A representation of a PlanetLab slice. This is a support class
89 # for instance <-> slice mapping.
90 #
91 class Slice(SQLObject):
92     slice_hrn = StringCol()
93     #slice_index = DatabaseIndex('slice_hrn')
94     instances = MultipleJoin('EucaInstance')
95
96 ##
97 # Initialize the aggregate manager by reading a configuration file.
98 #
99 def init_server():
100     configParser = ConfigParser()
101     configParser.read(['/etc/sfa/eucalyptus_aggregate.conf', 'eucalyptus_aggregate.conf'])
102     if len(configParser.sections()) < 1:
103         print >>sys.stderr, 'No cloud defined in the config file'
104         raise Exception('Cannot find cloud definition in configuration file.')
105
106     # Only read the first section.
107     cloudSec = configParser.sections()[0]
108     cloud['name'] = cloudSec
109     cloud['access_key'] = configParser.get(cloudSec, 'access_key')
110     cloud['secret_key'] = configParser.get(cloudSec, 'secret_key')
111     cloud['cloud_url']  = configParser.get(cloudSec, 'cloud_url')
112     cloudURL = cloud['cloud_url']
113     if cloudURL.find('https://') >= 0:
114         cloudURL = cloudURL.replace('https://', '')
115     elif cloudURL.find('http://') >= 0:
116         cloudURL = cloudURL.replace('http://', '')
117     (cloud['ip'], parts) = cloudURL.split(':')
118
119     # Create image bundles
120     images = getEucaConnection().get_all_images()
121     cloud['images'] = images
122     cloud['imageBundles'] = {}
123     for i in images:
124         if i.type != 'machine' or i.kernel_id is None: continue
125         name = os.path.dirname(i.location)
126         detail = {'imageID' : i.id, 'kernelID' : i.kernel_id, 'ramdiskID' : i.ramdisk_id}
127         cloud['imageBundles'][name] = detail
128
129     # Initialize sqlite3 database.
130     dbPath = '/etc/sfa/db'
131     dbName = 'euca_aggregate.db'
132
133     if not os.path.isdir(dbPath):
134         print >>sys.stderr, '%s not found. Creating directory ...' % dbPath
135         os.mkdir(dbPath)
136
137     conn = connectionForURI('sqlite://%s/%s' % (dbPath, dbName))
138     sqlhub.processConnection = conn
139     Slice.createTable(ifNotExists=True)
140     EucaInstance.createTable(ifNotExists=True)
141
142     # Make sure the schema exists.
143     if not os.path.exists(EUCALYPTUS_RSPEC_SCHEMA):
144         err = 'Cannot location schema at %s' % EUCALYPTUS_RSPEC_SCHEMA
145         print >>sys.stderr, err
146         raise Exception(err)
147
148 ##
149 # Creates a connection to Eucalytpus. This function is inspired by 
150 # the make_connection() in Euca2ools.
151 #
152 # @return A connection object or None
153 #
154 def getEucaConnection():
155     global cloud
156     accessKey = cloud['access_key']
157     secretKey = cloud['secret_key']
158     eucaURL   = cloud['cloud_url']
159     useSSL    = False
160     srvPath   = '/'
161     eucaPort  = 8773
162
163     if not accessKey or not secretKey or not eucaURL:
164         print >>sys.stderr, 'Please set ALL of the required environment ' \
165                             'variables by sourcing the eucarc file.'
166         return None
167     
168     # Split the url into parts
169     if eucaURL.find('https://') >= 0:
170         useSSL  = True
171         eucaURL = eucaURL.replace('https://', '')
172     elif eucaURL.find('http://') >= 0:
173         useSSL  = False
174         eucaURL = eucaURL.replace('http://', '')
175     (eucaHost, parts) = eucaURL.split(':')
176     if len(parts) > 1:
177         parts = parts.split('/')
178         eucaPort = int(parts[0])
179         parts = parts[1:]
180         srvPath = '/'.join(parts)
181
182     return boto.connect_ec2(aws_access_key_id=accessKey,
183                             aws_secret_access_key=secretKey,
184                             is_secure=useSSL,
185                             region=RegionInfo(None, 'eucalyptus', eucaHost), 
186                             port=eucaPort,
187                             path=srvPath)
188
189 ##
190 # Returns a string of keys that belong to the users of the given slice.
191 # @param sliceHRN The hunman readable name of the slice.
192 # @return sting()
193 #
194 def getKeysForSlice(sliceHRN):
195     try:
196         # convert hrn to slice name
197         plSliceName = hrn_to_pl_slicename(sliceHRN)
198     except IndexError, e:
199         print >>sys.stderr, 'Invalid slice name (%s)' % sliceHRN
200         return []
201
202     # Get the slice's information
203     sliceData = api.plshell.GetSlices(api.plauth, {'name':plSliceName})
204     if not sliceData:
205         print >>sys.stderr, 'Cannot get any data for slice %s' % plSliceName
206         return []
207
208     # It should only return a list with len = 1
209     sliceData = sliceData[0]
210
211     keys = []
212     person_ids = sliceData['person_ids']
213     if not person_ids: 
214         print >>sys.stderr, 'No users in slice %s' % sliceHRN
215         return []
216
217     persons = api.plshell.GetPersons(api.plauth, person_ids)
218     for person in persons:
219         pkeys = api.plshell.GetKeys(api.plauth, person['key_ids'])
220         for key in pkeys:
221             keys.append(key['key'])
222  
223     return ''.join(keys)
224
225 ##
226 # A class that builds the RSpec for Eucalyptus.
227 #
228 class EucaRSpecBuilder(object):
229     ##
230     # Initizes a RSpec builder
231     #
232     # @param cloud A dictionary containing data about a 
233     #              cloud (ex. clusters, ip)
234     def __init__(self, cloud):
235         self.eucaRSpec = XMLBuilder(format = True, tab_step = "  ")
236         self.cloudInfo = cloud
237
238     ##
239     # Creates a request stanza.
240     # 
241     # @param num The number of instances to create.
242     # @param image The disk image id.
243     # @param kernel The kernel image id.
244     # @param keypair Key pair to embed.
245     # @param ramdisk Ramdisk id (optional).
246     #
247     def __requestXML(self, num, image, kernel, keypair, ramdisk = ''):
248         xml = self.eucaRSpec
249         with xml.request:
250             with xml.instances:
251                 xml << str(num)
252             with xml.kernel_image(id=kernel):
253                 xml << ''
254             if ramdisk == '':
255                 with xml.ramdisk:
256                     xml << ''
257             else:
258                 with xml.ramdisk(id=ramdisk):
259                     xml << ''
260             with xml.disk_image(id=image):
261                 xml << ''
262             with xml.keypair:
263                 xml << keypair
264
265     ##
266     # Creates the cluster stanza.
267     #
268     # @param clusters Clusters information.
269     #
270     def __clustersXML(self, clusters):
271         cloud = self.cloudInfo
272         xml = self.eucaRSpec
273
274         for cluster in clusters:
275             instances = cluster['instances']
276             with xml.cluster(id=cluster['name']):
277                 with xml.ipv4:
278                     xml << cluster['ip']
279                 with xml.vm_types:
280                     for inst in instances:
281                         with xml.vm_type(name=inst[0]):
282                             with xml.free_slots:
283                                 xml << str(inst[1])
284                             with xml.max_instances:
285                                 xml << str(inst[2])
286                             with xml.cores:
287                                 xml << str(inst[3])
288                             with xml.memory(unit='MB'):
289                                 xml << str(inst[4])
290                             with xml.disk_space(unit='GB'):
291                                 xml << str(inst[5])
292                             if 'instances' in cloud and inst[0] in cloud['instances']:
293                                 existingEucaInstances = cloud['instances'][inst[0]]
294                                 with xml.euca_instances:
295                                     for eucaInst in existingEucaInstances:
296                                         with xml.euca_instance(id=eucaInst['id']):
297                                             with xml.state:
298                                                 xml << eucaInst['state']
299                                             with xml.public_dns:
300                                                 xml << eucaInst['public_dns']
301
302     def __imageBundleXML(self, bundles):
303         xml = self.eucaRSpec
304         with xml.bundles:
305             for bundle in bundles.keys():
306                 with xml.bundle(id=bundle):
307                     xml << ''
308
309     ##
310     # Creates the Images stanza.
311     #
312     # @param images A list of images in Eucalyptus.
313     #
314     def __imagesXML(self, images):
315         xml = self.eucaRSpec
316         with xml.images:
317             for image in images:
318                 with xml.image(id=image.id):
319                     with xml.type:
320                         xml << image.type
321                     with xml.arch:
322                         xml << image.architecture
323                     with xml.state:
324                         xml << image.state
325                     with xml.location:
326                         xml << image.location
327
328     ##
329     # Creates the KeyPairs stanza.
330     #
331     # @param keypairs A list of key pairs in Eucalyptus.
332     #
333     def __keyPairsXML(self, keypairs):
334         xml = self.eucaRSpec
335         with xml.keypairs:
336             for key in keypairs:
337                 with xml.keypair:
338                     xml << key.name
339
340     ##
341     # Generates the RSpec.
342     #
343     def toXML(self):
344         if not self.cloudInfo:
345             print >>sys.stderr, 'No cloud information'
346             return ''
347
348         xml = self.eucaRSpec
349         cloud = self.cloudInfo
350         with xml.RSpec(type='eucalyptus'):
351             with xml.network(id=cloud['name']):
352                 with xml.ipv4:
353                     xml << cloud['ip']
354                 #self.__keyPairsXML(cloud['keypairs'])
355                 #self.__imagesXML(cloud['images'])
356                 self.__imageBundleXML(cloud['imageBundles'])
357                 self.__clustersXML(cloud['clusters'])
358         return str(xml)
359
360 ##
361 # A parser to parse the output of availability-zones.
362 #
363 # Note: Only one cluster is supported. If more than one, this will
364 #       not work.
365 #
366 class ZoneResultParser(object):
367     def __init__(self, zones):
368         self.zones = zones
369
370     def parse(self):
371         if len(self.zones) < 3:
372             return
373         clusterList = []
374         cluster = {} 
375         instList = []
376
377         cluster['name'] = self.zones[0].name
378         cluster['ip']   = self.zones[0].state
379
380         for i in range(2, len(self.zones)):
381             currZone = self.zones[i]
382             instType = currZone.name.split()[1]
383
384             stateString = currZone.state.split('/')
385             rscString   = stateString[1].split()
386
387             instFree      = int(stateString[0])
388             instMax       = int(rscString[0])
389             instNumCpu    = int(rscString[1])
390             instRam       = int(rscString[2])
391             instDiskSpace = int(rscString[3])
392
393             instTuple = (instType, instFree, instMax, instNumCpu, instRam, instDiskSpace)
394             instList.append(instTuple)
395         cluster['instances'] = instList
396         clusterList.append(cluster)
397
398         return clusterList
399
400 def ListResources(api, creds, options, call_id): 
401     if Callids().already_handled(call_id): return ""
402     global cloud
403     # get slice's hrn from options
404     xrn = options.get('geni_slice_urn', '')
405     hrn, type = urn_to_hrn(xrn)
406
407     # get hrn of the original caller
408     origin_hrn = options.get('origin_hrn', None)
409     if not origin_hrn:
410         origin_hrn = Credential(string=creds).get_gid_caller().get_hrn()
411         # origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
412
413     conn = getEucaConnection()
414
415     if not conn:
416         print >>sys.stderr, 'Error: Cannot create a connection to Eucalyptus'
417         return 'Cannot create a connection to Eucalyptus'
418
419     try:
420         # Zones
421         zones = conn.get_all_zones(['verbose'])
422         p = ZoneResultParser(zones)
423         clusters = p.parse()
424         cloud['clusters'] = clusters
425         
426         # Images
427         images = conn.get_all_images()
428         cloud['images'] = images
429         cloud['imageBundles'] = {}
430         for i in images:
431             if i.type != 'machine' or i.kernel_id is None: continue
432             name = os.path.dirname(i.location)
433             detail = {'imageID' : i.id, 'kernelID' : i.kernel_id, 'ramdiskID' : i.ramdisk_id}
434             cloud['imageBundles'][name] = detail
435
436         # Key Pairs
437         keyPairs = conn.get_all_key_pairs()
438         cloud['keypairs'] = keyPairs
439
440         if hrn:
441             instanceId = []
442             instances  = []
443
444             # Get the instances that belong to the given slice from sqlite3
445             # XXX use getOne() in production because the slice's hrn is supposed
446             # to be unique. For testing, uniqueness is turned off in the db.
447             # If the slice isn't found in the database, create a record for the 
448             # slice.
449             matchedSlices = list(Slice.select(Slice.q.slice_hrn == hrn))
450             if matchedSlices:
451                 theSlice = matchedSlices[-1]
452             else:
453                 theSlice = Slice(slice_hrn = hrn)
454             for instance in theSlice.instances:
455                 instanceId.append(instance.instance_id)
456
457             # Get the information about those instances using their ids.
458             if len(instanceId) > 0:
459                 reservations = conn.get_all_instances(instanceId)
460             else:
461                 reservations = []
462             for reservation in reservations:
463                 for instance in reservation.instances:
464                     instances.append(instance)
465
466             # Construct a dictionary for the EucaRSpecBuilder
467             instancesDict = {}
468             for instance in instances:
469                 instList = instancesDict.setdefault(instance.instance_type, [])
470                 instInfoDict = {} 
471
472                 instInfoDict['id'] = instance.id
473                 instInfoDict['public_dns'] = instance.public_dns_name
474                 instInfoDict['state'] = instance.state
475                 instInfoDict['key'] = instance.key_name
476
477                 instList.append(instInfoDict)
478             cloud['instances'] = instancesDict
479
480     except EC2ResponseError, ec2RespErr:
481         errTree = ET.fromstring(ec2RespErr.body)
482         errMsgE = errTree.find('.//Message')
483         print >>sys.stderr, errMsgE.text
484
485     rspec = EucaRSpecBuilder(cloud).toXML()
486
487     # Remove the instances records so next time they won't 
488     # show up.
489     if 'instances' in cloud:
490         del cloud['instances']
491
492     return rspec
493
494 """
495 Hook called via 'sfi.py create'
496 """
497 def CreateSliver(api, xrn, creds, xml, users, call_id):
498     if Callids().already_handled(call_id): return ""
499
500     global cloud
501     hrn = urn_to_hrn(xrn)[0]
502
503     conn = getEucaConnection()
504     if not conn:
505         print >>sys.stderr, 'Error: Cannot create a connection to Eucalyptus'
506         return ""
507
508     # Validate RSpec
509     schemaXML = ET.parse(EUCALYPTUS_RSPEC_SCHEMA)
510     rspecValidator = ET.RelaxNG(schemaXML)
511     rspecXML = ET.XML(xml)
512     for network in rspecXML.iterfind("./network"):
513         if network.get('id') != cloud['name']:
514             # Throw away everything except my own RSpec
515             # sfa_logger().error("CreateSliver: deleting %s from rspec"%network.get('id'))
516             network.getparent().remove(network)
517     if not rspecValidator(rspecXML):
518         error = rspecValidator.error_log.last_error
519         message = '%s (line %s)' % (error.message, error.line) 
520         # XXX: InvalidRSpec is new. Currently, I am not working with Trunk code.
521         #raise InvalidRSpec(message)
522         raise Exception(message)
523
524     # Get the slice from db or create one.
525     s = Slice.select(Slice.q.slice_hrn == hrn).getOne(None)
526     if s is None:
527         s = Slice(slice_hrn = hrn)
528
529     # Process any changes in existing instance allocation
530     pendingRmInst = []
531     for sliceInst in s.instances:
532         pendingRmInst.append(sliceInst.instance_id)
533     existingInstGroup = rspecXML.findall(".//euca_instances")
534     for instGroup in existingInstGroup:
535         for existingInst in instGroup:
536             if existingInst.get('id') in pendingRmInst:
537                 pendingRmInst.remove(existingInst.get('id'))
538     for inst in pendingRmInst:
539         print >>sys.stderr, 'Instance %s will be terminated' % inst
540         dbInst = EucaInstance.select(EucaInstance.q.instance_id == inst).getOne(None)
541         dbInst.destroySelf()
542     conn.terminate_instances(pendingRmInst)
543
544     # Process new instance requests
545     requests = rspecXML.findall(".//request")
546     if requests:
547         # Get all the public keys associate with slice.
548         pubKeys = getKeysForSlice(s.slice_hrn)
549         print >>sys.stderr, "Passing the following keys to the instance:\n%s" % pubKeys
550         sys.stderr.flush()
551     for req in requests:
552         vmTypeElement = req.getparent()
553         instType = vmTypeElement.get('name')
554         numInst  = int(req.find('instances').text)
555         
556         bundleName = req.find('bundle').text
557         if not cloud['imageBundles'][bundleName]:
558             print >>sys.stderr, 'Cannot find bundle %s' % bundleName
559         bundleInfo = cloud['imageBundles'][bundleName]
560         instKernel  = bundleInfo['kernelID']
561         instDiskImg = bundleInfo['imageID']
562         instRamDisk = bundleInfo['ramdiskID']
563         instKey     = None
564
565         # Create the instances
566         for i in range(0, numInst):
567             eucaInst = EucaInstance(slice = s, 
568                                     kernel_id = instKernel,
569                                     image_id = instDiskImg,
570                                     ramdisk_id = instRamDisk,
571                                     key_pair = instKey,
572                                     inst_type = instType)
573             eucaInst.reserveInstance(conn, pubKeys)
574
575     # xxx - should return altered rspec 
576     # with enough data for the client to understand what's happened
577     return xml
578
579 def GetVersion(api):
580     xrn=Xrn(api.hrn)
581     request_rspec_versions = [dict(sfa_rspec_version)]
582     ad_rspec_versions = [dict(sfa_rspec_version)]
583     version_more = {'interface':'aggregate',
584                     'testbed':'myplc',
585                     'hrn':xrn.get_hrn(),
586                     'request_rspec_versions': request_rspec_versions,
587                     'ad_rspec_versions': ad_rspec_versions,
588                     'default_ad_rspec': dict(sfa_rspec_version)
589                     }
590     return version_core(version_more)
591
592 def main():
593     init_server()
594
595     #theRSpec = None
596     #with open(sys.argv[1]) as xml:
597     #    theRSpec = xml.read()
598     #CreateSliver(None, 'planetcloud.pc.test', theRSpec, 'call-id-cloudtest')
599
600     #rspec = ListResources('euca', 'planetcloud.pc.test', 'planetcloud.pc.marcoy', 'test_euca')
601     #print rspec
602     print getKeysForSlice('gc.gc.test1')
603
604 if __name__ == "__main__":
605     main()
606