import Euca2ool
[sfa.git] / sfa / openstack / euca_shell.py
1 try:
2     import boto
3     from boto.ec2.regioninfo import RegionInfo
4     from boto.exception import EC2ResponseError
5     has_boto=True
6 except:
7     has_boto=False    
8
9 try: 
10     from euca2ools import Euca2ool
11     has_euca = True
12 except:
13     has_euca = False
14
15 from sfa.util.sfalogging import logger
16 from sfa.openstack.nova_shell import NovaShell
17 from sfa.util.config import Config
18
19 class EucaShell:
20     """
21     A xmlrpc connection to the euca api. 
22     """    
23
24     def __init__(self, config):
25         self.config = config
26
27     def get_euca_connection(self):
28         if not has_boto:
29             logger.info('Unable to access EC2 API - boto library not found.')
30             return None
31         if not has_euca:
32             logger.info('euca2ools library not found.')
33             return None
34
35         self.euca2ool = Euca2ool()
36         nova = NovaShell(self.config)
37         admin_user = nova.auth_manager.get_user(self.config.SFA_NOVA_USER)
38         access_key = admin_user.access
39         secret_key = admin_user.secret
40         url = self.config.SFA_NOVA_API_URL
41         path = "/"
42         euca_port = self.config.SFA_NOVA_API_PORT        
43         use_ssl = False
44
45         # Split the url into parts 
46         if url.find('https://') >= 0:
47             use_ssl  = True
48             url = url.replace('https://', '')
49         elif url.find('http://') >= 0:
50             use_ssl  = False
51             url = url.replace('http://', '')
52         parts = url.split(':')
53         host = parts[0]
54         port = None 
55         if len(parts) > 1:
56             parts = parts.split('/')
57             port = int(parts[0])
58             parts = parts[1:]
59             path = '/'.join(parts)
60         
61         return boto.connect_ec2(aws_access_key_id=access_key,
62                                 aws_secret_access_key=secret_key,
63                                 is_secure=use_ssl,
64                                 region=RegionInfo(None, 'eucalyptus', host),
65                                 port=port,
66                                 path=path) 
67
68     def __getattr__(self, name):
69         def func(*args, **kwds):
70             conn = self.get_euca_connection()
71             return getattr(conn, name)(*args, **kwds)
72         return func