Update RSpec, and create_slice()
[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                 print >>sys.stderr, 'bundle: %r' % bundle
303                 sys.stderr.flush()
304                 with xml.bundle(id=bundle):
305                     xml << ''
306
307     ##
308     # Creates the Images stanza.
309     #
310     # @param images A list of images in Eucalyptus.
311     #
312     def __imagesXML(self, images):
313         xml = self.eucaRSpec
314         with xml.images:
315             for image in images:
316                 with xml.image(id=image.id):
317                     with xml.type:
318                         xml << image.type
319                     with xml.arch:
320                         xml << image.architecture
321                     with xml.state:
322                         xml << image.state
323                     with xml.location:
324                         xml << image.location
325
326     ##
327     # Creates the KeyPairs stanza.
328     #
329     # @param keypairs A list of key pairs in Eucalyptus.
330     #
331     def __keyPairsXML(self, keypairs):
332         xml = self.eucaRSpec
333         with xml.keypairs:
334             for key in keypairs:
335                 with xml.keypair:
336                     xml << key.name
337
338     ##
339     # Generates the RSpec.
340     #
341     def toXML(self):
342         if not self.cloudInfo:
343             print >>sys.stderr, 'No cloud information'
344             return ''
345
346         xml = self.eucaRSpec
347         cloud = self.cloudInfo
348         with xml.RSpec(type='eucalyptus'):
349             with xml.cloud(id=cloud['name']):
350                 with xml.ipv4:
351                     xml << cloud['ip']
352                 #self.__keyPairsXML(cloud['keypairs'])
353                 #self.__imagesXML(cloud['images'])
354                 self.__imageBundleXML(cloud['imageBundles'])
355                 self.__clustersXML(cloud['clusters'])
356         return str(xml)
357
358 ##
359 # A parser to parse the output of availability-zones.
360 #
361 # Note: Only one cluster is supported. If more than one, this will
362 #       not work.
363 #
364 class ZoneResultParser(object):
365     def __init__(self, zones):
366         self.zones = zones
367
368     def parse(self):
369         if len(self.zones) < 3:
370             return
371         clusterList = []
372         cluster = {} 
373         instList = []
374
375         cluster['name'] = self.zones[0].name
376         cluster['ip']   = self.zones[0].state
377
378         for i in range(2, len(self.zones)):
379             currZone = self.zones[i]
380             instType = currZone.name.split()[1]
381
382             stateString = currZone.state.split('/')
383             rscString   = stateString[1].split()
384
385             instFree      = int(stateString[0])
386             instMax       = int(rscString[0])
387             instNumCpu    = int(rscString[1])
388             instRam       = int(rscString[2])
389             instDiskSpace = int(rscString[3])
390
391             instTuple = (instType, instFree, instMax, instNumCpu, instRam, instDiskSpace)
392             instList.append(instTuple)
393         cluster['instances'] = instList
394         clusterList.append(cluster)
395
396         return clusterList
397
398 def get_rspec(api, creds, options): 
399     global cloud
400     # get slice's hrn from options
401     xrn = options.get('geni_slice_urn', '')
402     hrn, type = urn_to_hrn(xrn)
403
404     # get hrn of the original caller
405     origin_hrn = options.get('origin_hrn', None)
406     if not origin_hrn:
407         origin_hrn = Credential(string=creds[0]).get_gid_caller().get_hrn()
408
409     conn = getEucaConnection()
410
411     if not conn:
412         print >>sys.stderr, 'Error: Cannot create a connection to Eucalyptus'
413         return 'Cannot create a connection to Eucalyptus'
414
415     try:
416         # Zones
417         zones = conn.get_all_zones(['verbose'])
418         p = ZoneResultParser(zones)
419         clusters = p.parse()
420         cloud['clusters'] = clusters
421         
422         # Images
423         images = conn.get_all_images()
424         cloud['images'] = images
425         cloud['imageBundles'] = {}
426         for i in images:
427             if i.type != 'machine' or i.kernel_id is None: continue
428             name = os.path.dirname(i.location)
429             detail = {'imageID' : i.id, 'kernelID' : i.kernel_id, 'ramdiskID' : i.ramdisk_id}
430             cloud['imageBundles'][name] = detail
431
432         # Key Pairs
433         keyPairs = conn.get_all_key_pairs()
434         cloud['keypairs'] = keyPairs
435
436         if hrn:
437             instanceId = []
438             instances  = []
439
440             # Get the instances that belong to the given slice from sqlite3
441             # XXX use getOne() in production because the slice's hrn is supposed
442             # to be unique. For testing, uniqueness is turned off in the db.
443             # If the slice isn't found in the database, create a record for the 
444             # slice.
445             matchedSlices = list(Slice.select(Slice.q.slice_hrn == hrn))
446             if matchedSlices:
447                 theSlice = matchedSlices[-1]
448             else:
449                 theSlice = Slice(slice_hrn = hrn)
450             for instance in theSlice.instances:
451                 instanceId.append(instance.instance_id)
452
453             # Get the information about those instances using their ids.
454             if len(instanceId) > 0:
455                 reservations = conn.get_all_instances(instanceId)
456             else:
457                 reservations = []
458             for reservation in reservations:
459                 for instance in reservation.instances:
460                     instances.append(instance)
461
462             # Construct a dictionary for the EucaRSpecBuilder
463             instancesDict = {}
464             for instance in instances:
465                 instList = instancesDict.setdefault(instance.instance_type, [])
466                 instInfoDict = {} 
467
468                 instInfoDict['id'] = instance.id
469                 instInfoDict['public_dns'] = instance.public_dns_name
470                 instInfoDict['state'] = instance.state
471                 instInfoDict['key'] = instance.key_name
472
473                 instList.append(instInfoDict)
474             cloud['instances'] = instancesDict
475
476     except EC2ResponseError, ec2RespErr:
477         errTree = ET.fromstring(ec2RespErr.body)
478         errMsgE = errTree.find('.//Message')
479         print >>sys.stderr, errMsgE.text
480
481     rspec = EucaRSpecBuilder(cloud).toXML()
482
483     # Remove the instances records so next time they won't 
484     # show up.
485     if 'instances' in cloud:
486         del cloud['instances']
487
488     return rspec
489
490 """
491 Hook called via 'sfi.py create'
492 """
493 def create_slice(api, xrn, creds, xml, users):
494     global cloud
495     hrn = urn_to_hrn(xrn)[0]
496
497     conn = getEucaConnection()
498     if not conn:
499         print >>sys.stderr, 'Error: Cannot create a connection to Eucalyptus'
500         return False
501
502     # Validate RSpec
503     schemaXML = ET.parse(EUCALYPTUS_RSPEC_SCHEMA)
504     rspecValidator = ET.RelaxNG(schemaXML)
505     rspecXML = ET.XML(xml)
506     if not rspecValidator(rspecXML):
507         error = rspecValidator.error_log.last_error
508         message = '%s (line %s)' % (error.message, error.line) 
509         # XXX: InvalidRSpec is new. Currently, I am not working with Trunk code.
510         #raise InvalidRSpec(message)
511         raise Exception(message)
512
513     # Get the slice from db or create one.
514     s = Slice.select(Slice.q.slice_hrn == hrn).getOne(None)
515     if s is None:
516         s = Slice(slice_hrn = hrn)
517
518     # Process any changes in existing instance allocation
519     pendingRmInst = []
520     for sliceInst in s.instances:
521         pendingRmInst.append(sliceInst.instance_id)
522     existingInstGroup = rspecXML.findall('.//euca_instances')
523     for instGroup in existingInstGroup:
524         for existingInst in instGroup:
525             if existingInst.get('id') in pendingRmInst:
526                 pendingRmInst.remove(existingInst.get('id'))
527     for inst in pendingRmInst:
528         print >>sys.stderr, 'Instance %s will be terminated' % inst
529         dbInst = EucaInstance.select(EucaInstance.q.instance_id == inst).getOne(None)
530         dbInst.destroySelf()
531     conn.terminate_instances(pendingRmInst)
532
533     # Process new instance requests
534     requests = rspecXML.findall('.//request')
535     if requests:
536         # Get all the public keys associate with slice.
537         pubKeys = getKeysForSlice(s.slice_hrn)
538         print >>sys.stderr, "Passing the following keys to the instance:\n%s" % pubKeys
539     for req in requests:
540         vmTypeElement = req.getparent()
541         instType = vmTypeElement.get('name')
542         numInst  = int(req.find('instances').text)
543         
544         bundleName = req.find('bundle').text
545         if not cloud['imageBundles'][bundleName]:
546             print >>sys.stderr, 'Cannot find bundle %s' % bundleName
547         bundleInfo = cloud['imageBundles'][bundleName]
548         instKernel  = bundleInfo['kernelID']
549         instDiskImg = bundleInfo['imageID']
550         instRamDisk = bundleInfo['ramdiskID']
551         instKey     = None
552
553         # Create the instances
554         for i in range(0, numInst):
555             eucaInst = EucaInstance(slice = s, 
556                                     kernel_id = instKernel,
557                                     image_id = instDiskImg,
558                                     ramdisk_id = instRamDisk,
559                                     key_pair = instKey,
560                                     inst_type = instType)
561             eucaInst.reserveInstance(conn, pubKeys)
562
563     return True
564
565 def main():
566     init_server()
567
568     #theRSpec = None
569     #with open(sys.argv[1]) as xml:
570     #    theRSpec = xml.read()
571     #create_slice(None, 'planetcloud.pc.test', theRSpec)
572
573     #rspec = get_rspec('euca', 'planetcloud.pc.test', 'planetcloud.pc.marcoy')
574     #print rspec
575     print getKeysForSlice('gc.gc.test1')
576
577 if __name__ == "__main__":
578     main()
579