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