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