1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import socket
23 import sys
24 from xmlrpclib import Fault
25
26
27 from spacewalk.common.rhnLog import log_debug, log_error
28 from spacewalk.common.rhnTB import Traceback
29 from spacewalk.common.rhnException import rhnFault
30 from spacewalk.common.rhnTranslate import _
31 from rhnAuthProtocol import CommunicationError, send, recv
32
33
34
35
36
37
38
39
40
41
43
44 """ Bind XML-RPC to an RPC Server
45
46 Some magic to bind an XML-RPC method to an RPC server.
47 Supports "nested" methods (e.g. examples.getStateName).
48 """
49
50
52 self.__send = msend
53 self.__name = name
54
56 return _Method(self.__send, "%s.%s" % (self.__name, name))
57
59 return self.__send(self.__name, args)
60
62 return "<_Method instance at %s>" % id(self)
63
64 __repr__ = __str__
65
66
68
69 """ Client authenication temp. db.
70
71 Main class that the client side (client to the caching daemon) has to
72 instantiate to expose the proper API. Basically, the API is a dictionary.
73 """
74
75
77 log_debug(6, server_addr)
78 self.serverAddr = server_addr
79
81
82 log_debug(6, methodname, params)
83
84 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
85
86 try:
87 sock.connect(self.serverAddr)
88 except socket.error, e:
89 sock.close()
90 methodname = None
91 log_error("Error connecting to the auth cache: %s" % str(e))
92 Traceback("Shelf.__request", extra="""
93 Error connecting to the the authentication cache daemon.
94 Make sure it is started on %s""" % str(self.serverAddr))
95
96 raise rhnFault(1000,
97 _("Spacewalk Proxy error (issues connecting to auth cache). "
98 "Please contact your system administrator")), None, sys.exc_info()[2]
99
100 wfile = sock.makefile("w")
101
102 try:
103 send(wfile, methodname, None, *params)
104 except CommunicationError:
105 wfile.close()
106 sock.close()
107 Traceback("Shelf.__request",
108 extra="Encountered a CommunicationError")
109 raise
110 except socket.error:
111 wfile.close()
112 sock.close()
113 log_error("Error communicating to the auth cache: %s" % str(e))
114 Traceback("Shelf.__request", extra="""\
115 Error sending to the authentication cache daemon.
116 Make sure the authentication cache daemon is started""")
117
118 raise rhnFault(1000,
119 _("Spacewalk Proxy error (issues connecting to auth cache). "
120 "Please contact your system administrator")), None, sys.exc_info()[2]
121
122 wfile.close()
123
124 rfile = sock.makefile("r")
125 try:
126 params, methodname = recv(rfile)
127 except CommunicationError, e:
128 log_error(e.faultString)
129 rfile.close()
130 sock.close()
131 log_error("Error communicating to the auth cache: %s" % str(e))
132 Traceback("Shelf.__request", extra="""\
133 Error receiving from the authentication cache daemon.
134 Make sure the authentication cache daemon is started""")
135
136 raise rhnFault(1000,
137 _("Spacewalk Proxy error (issues communicating to auth cache). "
138 "Please contact your system administrator")), None, sys.exc_info()[2]
139 except Fault, e:
140 rfile.close()
141 sock.close()
142
143 if e.faultCode != 0:
144
145 raise
146
147 _dict = e.faultString
148 if not isinstance(_dict, type({})):
149
150 raise
151
152 if not _dict.has_key('name'):
153
154 raise
155
156 name = _dict['name']
157 args = _dict.get('args')
158
159 if not hasattr(__builtins__, name):
160
161 raise
162
163
164 import new
165 _dict = {'args': args}
166
167 raise new.instance(getattr(__builtins__, name), _dict), None, sys.exc_info()[2]
168
169 return params[0]
170
174
176 return "<Remote-Shelf instance at %s>" % id(self)
177
178
179
180
181
182
183 if __name__ == '__main__':
184 from spacewalk.common.rhnConfig import initCFG
185 initCFG("proxy.broker")
186 s = Shelf(('localhost', 9999))
187 s['1234'] = [1, 2, 3, 4, None, None]
188 s['blah'] = 'testing 1 2 3'
189 print 'Cached object s["1234"] = %s' % str(s['1234'])
190 print 'Cached object s["blah"] = %s' % str(s['blah'])
191 print s.has_key("asdfrasdf")
192
193
194
195
196
197