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