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