X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Fopenstack%2Feuca_shell.py;h=90c222258ba02b9058336960a0e7d95044164614;hb=1505f3a42ea06ef30959e9e54fa7298eb55d83ee;hp=80f9f524766d2739bf85164ccc71c42de5c34286;hpb=3d51e29695f79b143974f5cf7b2e104d89626ba4;p=sfa.git diff --git a/sfa/openstack/euca_shell.py b/sfa/openstack/euca_shell.py index 80f9f524..90c22225 100644 --- a/sfa/openstack/euca_shell.py +++ b/sfa/openstack/euca_shell.py @@ -2,58 +2,78 @@ try: import boto from boto.ec2.regioninfo import RegionInfo from boto.exception import EC2ResponseError - has_boto=True + has_boto = True except: - has_boto=False + has_boto = False from sfa.util.sfalogging import logger from sfa.openstack.nova_shell import NovaShell from sfa.util.config import Config + class EucaShell: """ A xmlrpc connection to the euca api. - """ + """ def __init__(self, config): - self.config = Config + self.config = config + self.nova_shell = NovaShell(config) + self.access_key = None + self.secret_key = None + + def init_context(self, project_name=None): + + # use the context of the specified project's project + # manager. + if project_name: + project = self.nova_shell.auth_manager.get_project(project_name) + self.access_key = "%s:%s" % ( + project.project_manager.name, project_name) + self.secret_key = project.project_manager.secret + else: + # use admin user's context + admin_user = self.nova_shell.auth_manager.get_user( + self.config.SFA_NOVA_USER) + #access_key = admin_user.access + self.access_key = '%s' % admin_user.name + self.secret_key = admin_user.secret - def get_euca_connection(self): + def get_euca_connection(self, project_name=None): if not has_boto: logger.info('Unable to access EC2 API - boto library not found.') return None - nova = NovaShell(self.config) - admin_user = nova.auth_manager.get_user(self.config.SFA_NOVA_USER) - access_key = admin_user.access - secret_key = admin_user.secret + + if not self.access_key or not self.secret_key: + self.init_context(project_name) + url = self.config.SFA_NOVA_API_URL + host = None + port = None path = "/" - euca_port = self.config.SFA_NOVA_API_PORT use_ssl = False - - # Split the url into parts + # Split the url into parts if url.find('https://') >= 0: - use_ssl = True + use_ssl = True url = url.replace('https://', '') elif url.find('http://') >= 0: - use_ssl = False + use_ssl = False url = url.replace('http://', '') - (host, parts) = url.split(':') + parts = url.split(':') + host = parts[0] if len(parts) > 1: - parts = parts.split('/') + parts = parts[1].split('/') port = int(parts[0]) parts = parts[1:] - path = '/'.join(parts) - - return boto.connect_ec2(aws_access_key_id=access_key, - aws_secret_access_key=secret_key, + path = '/' + '/'.join(parts) + return boto.connect_ec2(aws_access_key_id=self.access_key, + aws_secret_access_key=self.secret_key, is_secure=use_ssl, region=RegionInfo(None, 'eucalyptus', host), + host=host, port=port, - path=path) + path=path) def __getattr__(self, name): def func(*args, **kwds): conn = self.get_euca_connection() - return getattr(conn, name)(*args, **kwds) - return func