set config flags before instantiating nova service managers
[sfa.git] / sfa / openstack / nova_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 db
8     from nova import flags
9     from nova import context
10     from nova.auth.manager import AuthManager
11     from nova.compute.manager import ComputeManager
12     from nova.network.manager import NetworkManager
13     from nova.scheduler.manager import SchedulerManager
14     has_nova = True
15 except:
16     has_nova = False
17
18
19 def wrap_context(wrapped, context): 
20     """
21     Supplies the wrapped object with the specified context 
22     when executing callables. 
23     """ 
24     def wrapper(*args, **kwds):
25         return wrapped(context, *args, **kwds)
26     return wrap_context   
27  
28 class NovaShell:
29     """
30     A simple xmlrpc shell to a myplc instance
31     This class can receive all Openstack calls to the underlying testbed
32     """
33     
34     # dont care about limiting calls yet 
35     direct_calls = []
36     alias_calls = {}
37
38
39     # use the 'capability' auth mechanism for higher performance when the PLC db is local    
40     def __init__ ( self, config ) :
41         url = config.SFA_PLC_URL
42         # try to figure if the url is local
43         hostname=urlparse(url).hostname
44         if hostname == 'localhost': is_local=True
45         # otherwise compare IP addresses; 
46         # this might fail for any number of reasons, so let's harden that
47         try:
48             # xxx todo this seems to result in a DNS request for each incoming request to the AM
49             # should be cached or improved
50             url_ip=socket.gethostbyname(hostname)
51             local_ip=socket.gethostbyname(socket.gethostname())
52             if url_ip==local_ip: is_local=True
53         except:
54             pass
55
56
57         if is_local and has_nova:
58             logger.debug('nova access - native')
59             # load the config
60             flags.FLAGS(['foo', '--flagfile=/etc/nova/nova.conf', 'foo', 'foo'])
61             # instantiate managers 
62             self.auth_manager = AuthManager()
63             self.compute_manager = ComputeManager()
64             self.network_manager = NetworkManager()
65             self.scheduler_manager = SchedulerManager()
66             self.context = context.get_admin_context()
67             self.db = wrap_context(db, self.context)
68         else:
69             self.auth = None
70             self.proxy = None
71             logger.debug('nova access - REST')
72             raise SfaNotImplemented('nova access - Rest')
73
74     def __getattr__(self, name):
75         def func(*args, **kwds):
76             result=getattr(self.proxy, name)(self.auth, *args, **kwds)
77             return result
78         return func