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