1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
'''XMLRPC/SSL Communication Library (following the ssslib API)'''
__revision__ = '$Revision:$'
from elementtree.ElementTree import XML, tostring
from ConfigParser import ConfigParser
from M2Crypto.m2xmlrpclib import ServerProxy, SSL_Transport
from xmlrpclib import Fault
class CommunicationError(Exception):
'''Duplicate the sss.ssslib error API'''
pass
class comm_lib(object):
'''This sets up the communication for XMLRPC Bcfg2'''
def __init__(self):
self.cf = ConfigParser()
self.cf.read('/etc/bcfg2.conf')
location = self.cf.get("communication", "url")
self.proxy = ServerProxy(location, SSL_Transport())
self.user = 'root'
self.password = self.cf.get("communication", "password")
def ClientInit(self, component):
'''Return a single dummy handle'''
return "handle"
def SendMessage(self, handle, msg):
'''Encode the XML msg as an XML-RPC request'''
data = XML(msg)
args = (self.user, self.password)
if data.tag == 'get-probes':
funcname = "GetProbes"
elif data.tag == 'probe-data':
funcname = "RecvProbeData"
args = (self.user, self.password, data.getchildren())
elif data.tag == 'get-config':
funcname = 'GetConfig'
elif data.tag == 'upload-statistics':
funcname = "RecvStats"
args = (self.user, self.password, msg)
else:
print "unsupported function call"
raise CommunicationError, "foo"
func = getattr(self.proxy, funcname)
try:
self.response = apply(func, args)
except Fault, msg:
raise CommunicationError, msg
def RecvMessage(self, handle):
'''Return cached response'''
return self.response
def ClientClose(self, handle):
'''This is a noop for xmlrpc'''
pass
|