c67d60ecaf5c70dbcc763cc89f059a2b8efbd42e
[sfa.git] / sfa / openstack / euca_shell.py
1 import boto
2 from boto.ec2.regioninfo import RegionInfo
3 from boto.exception import EC2ResponseError
4 from sfa.util.sfalogging import logger
5
6
7 class EucaShell:
8     """
9     A xmlrpc connection to the euca api. 
10     """    
11
12     def __init__(self, config):
13         self.config = Config
14
15     def get_euca_connection(self):
16
17         access_key = self.config.SFA_EUCA_ACCESS_KEY
18         secret_key = self.config.SFA_EUCA_SECRET_KEY
19         url = self.config.SFA_EUCA_URL
20         path = "/"
21         euca_port = self.config.SFA_EUCA_PORT        
22         use_ssl = False
23
24         # Split the url into parts 
25         if url.find('https://') >= 0:
26             use_ssl  = True
27             url = url.replace('https://', '')
28         elif url.find('http://') >= 0:
29             use_ssl  = False
30             url = url.replace('http://', '')
31         (host, parts) = url.split(':')
32         if len(parts) > 1:
33             parts = parts.split('/')
34             port = int(parts[0])
35             parts = parts[1:]
36             path = '/'.join(parts)
37         
38         if not access_key or not secret_key or not url:
39             logger.error('Please set ALL of the required environment ' \
40                          'variables by sourcing the eucarc file.')
41             return None
42         return boto.connect_ec2(aws_access_key_id=access_key,
43                                 aws_secret_access_key=secret_key,
44                                 is_secure=use_ssl,
45                                 region=RegionInfo(None, 'eucalyptus', host),
46                                 port=port,
47                                 path=path) 
48
49     def __getattr__(self, name):
50         def func(*args, **kwds):
51             conn = self.get_euca_connection()
52             return getattr(conn, name)(*args, **kwds)
53         return func