trying to hunt down verbose messages that started recently to appear
[sfa.git] / sfa / plc / plshell.py
1 import sys
2 import xmlrpclib
3 import socket
4 from urlparse import urlparse
5
6 from sfa.util.sfalogging import logger
7
8 class PlShell:
9     """
10     A simple xmlrpc shell to a myplc instance
11     This class can receive all PLCAPI calls to the underlying testbed
12     For safety this is limited to a set of hard-coded calls
13     """
14     
15     direct_calls = ['AddNode', 'AddPerson', 'AddPersonKey', 'AddPersonToSite',
16                     'AddPersonToSlice', 'AddRoleToPerson', 'AddSite', 'AddSiteTag', 'AddSlice',
17                     'AddSliceTag', 'AddSliceToNodes', 'BindObjectToPeer', 'DeleteKey',
18                     'DeleteNode', 'DeletePerson', 'DeletePersonFromSlice', 'DeleteSite',
19                     'DeleteSlice', 'DeleteSliceFromNodes', 'DeleteSliceTag', 'GetInitScripts',
20                     'GetInterfaces', 'GetKeys', 'GetNodeTags', 'GetPeers',
21                     'GetPersons', 'GetSlices', 'GetSliceTags', 'GetTagTypes',
22                     'UnBindObjectFromPeer', 'UpdateNode', 'UpdatePerson', 'UpdateSite',
23                     'UpdateSlice', 'UpdateSliceTag',
24                     # also used as-is in importer
25                     'GetSites','GetNodes',
26                     ]
27     # support for other names - this is experimental
28     alias_calls = { 'get_authorities':'GetSites',
29                     'get_nodes':'GetNodes',
30                     }
31
32
33     # use the 'capability' auth mechanism for higher performance when the PLC db is local    
34     def __init__ ( self, config ) :
35         url = config.SFA_PLC_URL
36         # try to figure if the url is local
37         hostname=urlparse(url).hostname
38         is_local=False
39         if hostname == 'localhost': is_local=True
40         # otherwise compare IP addresses; 
41         # this might fail for any number of reasons, so let's harden that
42         try:
43             url_ip=socket.gethostbyname(hostname)
44             local_ip=socket.gethostbyname(socket.gethostname())
45             if url_ip==local_ip: is_local=True
46         except:
47             pass
48
49         if is_local:
50             try:
51                 # too bad this is not installed properly
52                 plcapi_path="/usr/share/plc_api"
53                 if plcapi_path not in sys.path: sys.path.append(plcapi_path)
54                 import PLC.Shell
55                 plc_direct_access=True
56             except:
57                 plc_direct_access=False
58         if is_local and plc_direct_access:
59             logger.debug('plshell access - capability')
60             self.plauth = { 'AuthMethod': 'capability',
61                             'Username':   config.SFA_PLC_USER,
62                             'AuthString': config.SFA_PLC_PASSWORD,
63                             }
64             self.proxy = PLC.Shell.Shell ()
65
66         else:
67             logger.debug('plshell access - xmlrpc')
68             self.plauth = { 'AuthMethod': 'password',
69                             'Username':   config.SFA_PLC_USER,
70                             'AuthString': config.SFA_PLC_PASSWORD,
71                             }
72             self.proxy = xmlrpclib.Server(url, verbose = False, allow_none = True)
73
74     def __getattr__(self, name):
75         def func(*args, **kwds):
76             actual_name=None
77             if name in PlShell.direct_calls: actual_name=name
78             if name in PlShell.alias_calls: actual_name=PlShell.alias_calls[name]
79             if not actual_name:
80                 raise Exception, "Illegal method call %s for PL driver"%(name)
81             result=getattr(self.proxy, actual_name)(self.plauth, *args, **kwds)
82             logger.debug('%s (%s) returned ... %s'%(name,actual_name,result))
83             return result
84         return func