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