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