fix calls to glance
[sfa.git] / sfa / openstack / nova_shell.py
1 import sys
2 import xmlrpclib
3 import socket
4 import gettext
5 from urlparse import urlparse
6 from sfa.util.sfalogging import logger
7 from sfa.util.config import Config
8
9 try:
10     from nova import db
11     from nova import flags
12     from nova import context
13     from nova.auth.manager import AuthManager
14     from nova.compute.manager import ComputeManager
15     from nova.network.manager import NetworkManager
16     from nova.scheduler.manager import SchedulerManager
17     from sfa.openstack.client import GlanceClient
18     has_nova = True
19 except:
20     has_nova = False
21
22
23 class InjectContext:
24     """
25     Wraps the module and injects the context when executing methods 
26     """     
27     def __init__(self, proxy, context):
28         self.proxy = proxy
29         self.context = context
30     
31     def __getattr__(self, name):
32         def func(*args, **kwds):
33             result=getattr(self.proxy, name)(self.context, *args, **kwds)
34             return result
35         return func
36
37 class NovaShell:
38     """
39     A simple native shell to a nova backend. 
40     This class can receive all nova calls to the underlying testbed
41     """
42     
43     # dont care about limiting calls yet 
44     direct_calls = []
45     alias_calls = {}
46
47
48     # use the 'capability' auth mechanism for higher performance when the PLC db is local    
49     def __init__ ( self, config ) :
50         if not config:
51             config = Config()
52         self.auth_manager = None
53         self.compute_manager = None
54         self.network_manager = None
55         self.scheduler_manager = None
56         self.db = None
57         self.image_manager = None
58
59         if has_nova:
60             logger.debug('nova access - native')
61             # load the config
62             flags.FLAGS(['foo', '--flagfile=/etc/nova/nova.conf', 'foo', 'foo'])
63             # instantiate managers 
64             self.auth_manager = AuthManager()
65             self.compute_manager = ComputeManager()
66             self.network_manager = NetworkManager()
67             self.scheduler_manager = SchedulerManager()
68             self.db = InjectContext(db, context.get_admin_context())
69             self.image_manager = GlanceClient(config)
70         else:
71             logger.debug('nova access - REST')
72             raise SfaNotImplemented('nova access - Rest')