this is required for the federica driver as well
[sfa.git] / sfa / openstack / openstack_shell.py
1 import sys
2 import xmlrpclib
3 import socket
4 from urlparse import urlparse
5 from sfa.util.sfalogging import logger
6 try:
7     from nova import flags
8     from nova import context 
9     from nova import db
10     has_nova = True  
11 except:
12     has_nova = False
13  
14 class OpenstackShell:
15     """
16     A simple xmlrpc shell to a myplc instance
17     This class can receive all Openstack calls to the underlying testbed
18     """
19     
20     # dont care about limiting calls yet 
21     direct_calls = []
22     alias_calls = {}
23
24
25     # use the 'capability' auth mechanism for higher performance when the PLC db is local    
26     def __init__ ( self, config ) :
27         url = config.SFA_PLC_URL
28         # try to figure if the url is local
29         hostname=urlparse(url).hostname
30         if hostname == 'localhost': is_local=True
31         # otherwise compare IP addresses; 
32         # this might fail for any number of reasons, so let's harden that
33         try:
34             # xxx todo this seems to result in a DNS request for each incoming request to the AM
35             # should be cached or improved
36             url_ip=socket.gethostbyname(hostname)
37             local_ip=socket.gethostbyname(socket.gethostname())
38             if url_ip==local_ip: is_local=True
39         except:
40             pass
41
42
43         if is_local and has_nova:
44             logger.debug('openstack access - native')
45             # load the config
46             flags.FLAGS(['foo', '--flagfile=/etc/nova/nova.conf', 'foo', 'foo'])
47             self.auth = context.get_admin_context()
48             self.proxy = db
49         else:
50             self.auth = None
51             self.proxy = None
52             logger.debug('openstack access - REST')
53             raise SfaNotImplemented('openstack access - Rest')
54
55     def __getattr__(self, name):
56         def func(*args, **kwds):
57             result=getattr(self.proxy, name)(self.auth, *args, **kwds)
58             return result
59         return func