root / lib / luxi.py @ 64381ad7
History | View | Annotate | Download (7.8 kB)
1 | c2a03789 | Iustin Pop | #
|
---|---|---|---|
2 | c2a03789 | Iustin Pop | #
|
3 | c2a03789 | Iustin Pop | |
4 | c2a03789 | Iustin Pop | # Copyright (C) 2006, 2007 Google Inc.
|
5 | c2a03789 | Iustin Pop | #
|
6 | c2a03789 | Iustin Pop | # This program is free software; you can redistribute it and/or modify
|
7 | c2a03789 | Iustin Pop | # it under the terms of the GNU General Public License as published by
|
8 | c2a03789 | Iustin Pop | # the Free Software Foundation; either version 2 of the License, or
|
9 | c2a03789 | Iustin Pop | # (at your option) any later version.
|
10 | c2a03789 | Iustin Pop | #
|
11 | c2a03789 | Iustin Pop | # This program is distributed in the hope that it will be useful, but
|
12 | c2a03789 | Iustin Pop | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | c2a03789 | Iustin Pop | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | c2a03789 | Iustin Pop | # General Public License for more details.
|
15 | c2a03789 | Iustin Pop | #
|
16 | c2a03789 | Iustin Pop | # You should have received a copy of the GNU General Public License
|
17 | c2a03789 | Iustin Pop | # along with this program; if not, write to the Free Software
|
18 | c2a03789 | Iustin Pop | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | c2a03789 | Iustin Pop | # 02110-1301, USA.
|
20 | c2a03789 | Iustin Pop | |
21 | c2a03789 | Iustin Pop | |
22 | c2a03789 | Iustin Pop | """Module for the unix socket protocol
|
23 | c2a03789 | Iustin Pop |
|
24 | c2a03789 | Iustin Pop | This module implements the local unix socket protocl. You only need
|
25 | c2a03789 | Iustin Pop | this module and the opcodes module in the client program in order to
|
26 | c2a03789 | Iustin Pop | communicate with the master.
|
27 | c2a03789 | Iustin Pop |
|
28 | c2a03789 | Iustin Pop | The module is also be used by the master daemon.
|
29 | c2a03789 | Iustin Pop |
|
30 | c2a03789 | Iustin Pop | """
|
31 | c2a03789 | Iustin Pop | |
32 | c2a03789 | Iustin Pop | import socket |
33 | c2a03789 | Iustin Pop | import collections |
34 | c2a03789 | Iustin Pop | import time |
35 | 03a8dbdc | Iustin Pop | import errno |
36 | c2a03789 | Iustin Pop | |
37 | fad50141 | Michael Hanselmann | from ganeti import serializer |
38 | ceab32dd | Iustin Pop | from ganeti import constants |
39 | c2a03789 | Iustin Pop | |
40 | c2a03789 | Iustin Pop | |
41 | 3d8548c4 | Michael Hanselmann | KEY_METHOD = 'method'
|
42 | 3d8548c4 | Michael Hanselmann | KEY_ARGS = 'args'
|
43 | 3d8548c4 | Michael Hanselmann | KEY_SUCCESS = "success"
|
44 | 3d8548c4 | Michael Hanselmann | KEY_RESULT = "result"
|
45 | 3d8548c4 | Michael Hanselmann | |
46 | 0bbe448c | Michael Hanselmann | REQ_SUBMIT_JOB = "SubmitJob"
|
47 | 0bbe448c | Michael Hanselmann | REQ_CANCEL_JOB = "CancelJob"
|
48 | 0bbe448c | Michael Hanselmann | REQ_ARCHIVE_JOB = "ArchiveJob"
|
49 | 0bbe448c | Michael Hanselmann | REQ_QUERY_JOBS = "QueryJobs"
|
50 | c2a03789 | Iustin Pop | |
51 | c2a03789 | Iustin Pop | DEF_CTMO = 10
|
52 | c2a03789 | Iustin Pop | DEF_RWTO = 60
|
53 | c2a03789 | Iustin Pop | |
54 | c2a03789 | Iustin Pop | |
55 | c2a03789 | Iustin Pop | class ProtocolError(Exception): |
56 | c2a03789 | Iustin Pop | """Denotes an error in the server communication"""
|
57 | c2a03789 | Iustin Pop | |
58 | c2a03789 | Iustin Pop | |
59 | c2a03789 | Iustin Pop | class ConnectionClosedError(ProtocolError): |
60 | c2a03789 | Iustin Pop | """Connection closed error"""
|
61 | c2a03789 | Iustin Pop | |
62 | c2a03789 | Iustin Pop | |
63 | c2a03789 | Iustin Pop | class TimeoutError(ProtocolError): |
64 | c2a03789 | Iustin Pop | """Operation timeout error"""
|
65 | c2a03789 | Iustin Pop | |
66 | c2a03789 | Iustin Pop | |
67 | c2a03789 | Iustin Pop | class EncodingError(ProtocolError): |
68 | c2a03789 | Iustin Pop | """Encoding failure on the sending side"""
|
69 | c2a03789 | Iustin Pop | |
70 | c2a03789 | Iustin Pop | |
71 | c2a03789 | Iustin Pop | class DecodingError(ProtocolError): |
72 | c2a03789 | Iustin Pop | """Decoding failure on the receiving side"""
|
73 | c2a03789 | Iustin Pop | |
74 | c2a03789 | Iustin Pop | |
75 | b77acb3e | Iustin Pop | class RequestError(ProtocolError): |
76 | b77acb3e | Iustin Pop | """Error on request
|
77 | b77acb3e | Iustin Pop |
|
78 | b77acb3e | Iustin Pop | This signifies an error in the request format or request handling,
|
79 | b77acb3e | Iustin Pop | but not (e.g.) an error in starting up an instance.
|
80 | b77acb3e | Iustin Pop |
|
81 | b77acb3e | Iustin Pop | Some common conditions that can trigger this exception:
|
82 | b77acb3e | Iustin Pop | - job submission failed because the job data was wrong
|
83 | b77acb3e | Iustin Pop | - query failed because required fields were missing
|
84 | b77acb3e | Iustin Pop |
|
85 | b77acb3e | Iustin Pop | """
|
86 | b77acb3e | Iustin Pop | |
87 | 3d8548c4 | Michael Hanselmann | |
88 | 03a8dbdc | Iustin Pop | class NoMasterError(ProtocolError): |
89 | 03a8dbdc | Iustin Pop | """The master cannot be reached
|
90 | 03a8dbdc | Iustin Pop |
|
91 | 03a8dbdc | Iustin Pop | This means that the master daemon is not running or the socket has
|
92 | 03a8dbdc | Iustin Pop | been removed.
|
93 | 03a8dbdc | Iustin Pop |
|
94 | 03a8dbdc | Iustin Pop | """
|
95 | 03a8dbdc | Iustin Pop | |
96 | b77acb3e | Iustin Pop | |
97 | c2a03789 | Iustin Pop | class Transport: |
98 | c2a03789 | Iustin Pop | """Low-level transport class.
|
99 | c2a03789 | Iustin Pop |
|
100 | c2a03789 | Iustin Pop | This is used on the client side.
|
101 | c2a03789 | Iustin Pop |
|
102 | c2a03789 | Iustin Pop | This could be replace by any other class that provides the same
|
103 | c2a03789 | Iustin Pop | semantics to the Client. This means:
|
104 | c2a03789 | Iustin Pop | - can send messages and receive messages
|
105 | c2a03789 | Iustin Pop | - safe for multithreading
|
106 | c2a03789 | Iustin Pop |
|
107 | c2a03789 | Iustin Pop | """
|
108 | c2a03789 | Iustin Pop | |
109 | c2a03789 | Iustin Pop | def __init__(self, address, timeouts=None, eom=None): |
110 | c2a03789 | Iustin Pop | """Constructor for the Client class.
|
111 | c2a03789 | Iustin Pop |
|
112 | c2a03789 | Iustin Pop | Arguments:
|
113 | c2a03789 | Iustin Pop | - address: a valid address the the used transport class
|
114 | c2a03789 | Iustin Pop | - timeout: a list of timeouts, to be used on connect and read/write
|
115 | c2a03789 | Iustin Pop | - eom: an identifier to be used as end-of-message which the
|
116 | c2a03789 | Iustin Pop | upper-layer will guarantee that this identifier will not appear
|
117 | c2a03789 | Iustin Pop | in any message
|
118 | c2a03789 | Iustin Pop |
|
119 | c2a03789 | Iustin Pop | There are two timeouts used since we might want to wait for a long
|
120 | c2a03789 | Iustin Pop | time for a response, but the connect timeout should be lower.
|
121 | c2a03789 | Iustin Pop |
|
122 | c2a03789 | Iustin Pop | If not passed, we use a default of 10 and respectively 60 seconds.
|
123 | c2a03789 | Iustin Pop |
|
124 | c2a03789 | Iustin Pop | Note that on reading data, since the timeout applies to an
|
125 | c2a03789 | Iustin Pop | invidual receive, it might be that the total duration is longer
|
126 | c2a03789 | Iustin Pop | than timeout value passed (we make a hard limit at twice the read
|
127 | c2a03789 | Iustin Pop | timeout).
|
128 | c2a03789 | Iustin Pop |
|
129 | c2a03789 | Iustin Pop | """
|
130 | c2a03789 | Iustin Pop | self.address = address
|
131 | c2a03789 | Iustin Pop | if timeouts is None: |
132 | c2a03789 | Iustin Pop | self._ctimeout, self._rwtimeout = DEF_CTMO, DEF_RWTO |
133 | c2a03789 | Iustin Pop | else:
|
134 | c2a03789 | Iustin Pop | self._ctimeout, self._rwtimeout = timeouts |
135 | c2a03789 | Iustin Pop | |
136 | c2a03789 | Iustin Pop | self.socket = None |
137 | c2a03789 | Iustin Pop | self._buffer = "" |
138 | c2a03789 | Iustin Pop | self._msgs = collections.deque()
|
139 | c2a03789 | Iustin Pop | |
140 | c2a03789 | Iustin Pop | if eom is None: |
141 | c2a03789 | Iustin Pop | self.eom = '\3' |
142 | c2a03789 | Iustin Pop | else:
|
143 | c2a03789 | Iustin Pop | self.eom = eom
|
144 | c2a03789 | Iustin Pop | |
145 | c2a03789 | Iustin Pop | try:
|
146 | c2a03789 | Iustin Pop | self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
147 | c2a03789 | Iustin Pop | self.socket.settimeout(self._ctimeout) |
148 | c2a03789 | Iustin Pop | try:
|
149 | c2a03789 | Iustin Pop | self.socket.connect(address)
|
150 | c2a03789 | Iustin Pop | except socket.timeout, err:
|
151 | 03a8dbdc | Iustin Pop | raise TimeoutError("Connect timed out: %s" % str(err)) |
152 | 03a8dbdc | Iustin Pop | except socket.error, err:
|
153 | 03a8dbdc | Iustin Pop | if err.args[0] == errno.ENOENT: |
154 | 03a8dbdc | Iustin Pop | raise NoMasterError((address,))
|
155 | 03a8dbdc | Iustin Pop | raise
|
156 | c2a03789 | Iustin Pop | self.socket.settimeout(self._rwtimeout) |
157 | 03a8dbdc | Iustin Pop | except (socket.error, NoMasterError):
|
158 | c2a03789 | Iustin Pop | if self.socket is not None: |
159 | c2a03789 | Iustin Pop | self.socket.close()
|
160 | c2a03789 | Iustin Pop | self.socket = None |
161 | c2a03789 | Iustin Pop | raise
|
162 | c2a03789 | Iustin Pop | |
163 | c2a03789 | Iustin Pop | def _CheckSocket(self): |
164 | c2a03789 | Iustin Pop | """Make sure we are connected.
|
165 | c2a03789 | Iustin Pop |
|
166 | c2a03789 | Iustin Pop | """
|
167 | c2a03789 | Iustin Pop | if self.socket is None: |
168 | c2a03789 | Iustin Pop | raise ProtocolError("Connection is closed") |
169 | c2a03789 | Iustin Pop | |
170 | c2a03789 | Iustin Pop | def Send(self, msg): |
171 | c2a03789 | Iustin Pop | """Send a message.
|
172 | c2a03789 | Iustin Pop |
|
173 | c2a03789 | Iustin Pop | This just sends a message and doesn't wait for the response.
|
174 | c2a03789 | Iustin Pop |
|
175 | c2a03789 | Iustin Pop | """
|
176 | c2a03789 | Iustin Pop | if self.eom in msg: |
177 | c2a03789 | Iustin Pop | raise EncodingError("Message terminator found in payload") |
178 | c2a03789 | Iustin Pop | self._CheckSocket()
|
179 | c2a03789 | Iustin Pop | try:
|
180 | c2a03789 | Iustin Pop | self.socket.sendall(msg + self.eom) |
181 | c2a03789 | Iustin Pop | except socket.timeout, err:
|
182 | c2a03789 | Iustin Pop | raise TimeoutError("Sending timeout: %s" % str(err)) |
183 | c2a03789 | Iustin Pop | |
184 | c2a03789 | Iustin Pop | def Recv(self): |
185 | c2a03789 | Iustin Pop | """Try to receive a messae from the socket.
|
186 | c2a03789 | Iustin Pop |
|
187 | c2a03789 | Iustin Pop | In case we already have messages queued, we just return from the
|
188 | c2a03789 | Iustin Pop | queue. Otherwise, we try to read data with a _rwtimeout network
|
189 | c2a03789 | Iustin Pop | timeout, and making sure we don't go over 2x_rwtimeout as a global
|
190 | c2a03789 | Iustin Pop | limit.
|
191 | c2a03789 | Iustin Pop |
|
192 | c2a03789 | Iustin Pop | """
|
193 | c2a03789 | Iustin Pop | self._CheckSocket()
|
194 | c2a03789 | Iustin Pop | etime = time.time() + self._rwtimeout
|
195 | c2a03789 | Iustin Pop | while not self._msgs: |
196 | c2a03789 | Iustin Pop | if time.time() > etime:
|
197 | c2a03789 | Iustin Pop | raise TimeoutError("Extended receive timeout") |
198 | c2a03789 | Iustin Pop | try:
|
199 | c2a03789 | Iustin Pop | data = self.socket.recv(4096) |
200 | c2a03789 | Iustin Pop | except socket.timeout, err:
|
201 | c2a03789 | Iustin Pop | raise TimeoutError("Receive timeout: %s" % str(err)) |
202 | c2a03789 | Iustin Pop | if not data: |
203 | c2a03789 | Iustin Pop | raise ConnectionClosedError("Connection closed while reading") |
204 | c2a03789 | Iustin Pop | new_msgs = (self._buffer + data).split(self.eom) |
205 | c2a03789 | Iustin Pop | self._buffer = new_msgs.pop()
|
206 | c2a03789 | Iustin Pop | self._msgs.extend(new_msgs)
|
207 | c2a03789 | Iustin Pop | return self._msgs.popleft() |
208 | c2a03789 | Iustin Pop | |
209 | c2a03789 | Iustin Pop | def Call(self, msg): |
210 | c2a03789 | Iustin Pop | """Send a message and wait for the response.
|
211 | c2a03789 | Iustin Pop |
|
212 | c2a03789 | Iustin Pop | This is just a wrapper over Send and Recv.
|
213 | c2a03789 | Iustin Pop |
|
214 | c2a03789 | Iustin Pop | """
|
215 | c2a03789 | Iustin Pop | self.Send(msg)
|
216 | c2a03789 | Iustin Pop | return self.Recv() |
217 | c2a03789 | Iustin Pop | |
218 | c2a03789 | Iustin Pop | def Close(self): |
219 | c2a03789 | Iustin Pop | """Close the socket"""
|
220 | c2a03789 | Iustin Pop | if self.socket is not None: |
221 | c2a03789 | Iustin Pop | self.socket.close()
|
222 | c2a03789 | Iustin Pop | self.socket = None |
223 | c2a03789 | Iustin Pop | |
224 | c2a03789 | Iustin Pop | |
225 | c2a03789 | Iustin Pop | class Client(object): |
226 | c2a03789 | Iustin Pop | """High-level client implementation.
|
227 | c2a03789 | Iustin Pop |
|
228 | c2a03789 | Iustin Pop | This uses a backing Transport-like class on top of which it
|
229 | c2a03789 | Iustin Pop | implements data serialization/deserialization.
|
230 | c2a03789 | Iustin Pop |
|
231 | c2a03789 | Iustin Pop | """
|
232 | ceab32dd | Iustin Pop | def __init__(self, address=None, timeouts=None, transport=Transport): |
233 | c2a03789 | Iustin Pop | """Constructor for the Client class.
|
234 | c2a03789 | Iustin Pop |
|
235 | c2a03789 | Iustin Pop | Arguments:
|
236 | c2a03789 | Iustin Pop | - address: a valid address the the used transport class
|
237 | c2a03789 | Iustin Pop | - timeout: a list of timeouts, to be used on connect and read/write
|
238 | c2a03789 | Iustin Pop | - transport: a Transport-like class
|
239 | c2a03789 | Iustin Pop |
|
240 | c2a03789 | Iustin Pop |
|
241 | c2a03789 | Iustin Pop | If timeout is not passed, the default timeouts of the transport
|
242 | c2a03789 | Iustin Pop | class are used.
|
243 | c2a03789 | Iustin Pop |
|
244 | c2a03789 | Iustin Pop | """
|
245 | ceab32dd | Iustin Pop | if address is None: |
246 | ceab32dd | Iustin Pop | address = constants.MASTER_SOCKET |
247 | c2a03789 | Iustin Pop | self.transport = transport(address, timeouts=timeouts)
|
248 | c2a03789 | Iustin Pop | |
249 | 3d8548c4 | Michael Hanselmann | def CallMethod(self, method, args): |
250 | c2a03789 | Iustin Pop | """Send a generic request and return the response.
|
251 | c2a03789 | Iustin Pop |
|
252 | c2a03789 | Iustin Pop | """
|
253 | 3d8548c4 | Michael Hanselmann | # Build request
|
254 | 3d8548c4 | Michael Hanselmann | request = { |
255 | 3d8548c4 | Michael Hanselmann | KEY_METHOD: method, |
256 | 3d8548c4 | Michael Hanselmann | KEY_ARGS: args, |
257 | 3d8548c4 | Michael Hanselmann | } |
258 | 3d8548c4 | Michael Hanselmann | |
259 | 3d8548c4 | Michael Hanselmann | # Send request and wait for response
|
260 | 3d8548c4 | Michael Hanselmann | result = self.transport.Call(serializer.DumpJson(request, indent=False)) |
261 | c2a03789 | Iustin Pop | try:
|
262 | fad50141 | Michael Hanselmann | data = serializer.LoadJson(result) |
263 | c2a03789 | Iustin Pop | except Exception, err: |
264 | c2a03789 | Iustin Pop | raise ProtocolError("Error while deserializing response: %s" % str(err)) |
265 | 3d8548c4 | Michael Hanselmann | |
266 | 3d8548c4 | Michael Hanselmann | # Validate response
|
267 | a14a17fc | Iustin Pop | if (not isinstance(data, dict) or |
268 | 3d8548c4 | Michael Hanselmann | KEY_SUCCESS not in data or |
269 | 3d8548c4 | Michael Hanselmann | KEY_RESULT not in data): |
270 | a14a17fc | Iustin Pop | raise DecodingError("Invalid response from server: %s" % str(data)) |
271 | 3d8548c4 | Michael Hanselmann | |
272 | 3d8548c4 | Michael Hanselmann | if not data[KEY_SUCCESS]: |
273 | 3d8548c4 | Michael Hanselmann | # TODO: decide on a standard exception
|
274 | 3d8548c4 | Michael Hanselmann | raise RequestError(data[KEY_RESULT])
|
275 | 3d8548c4 | Michael Hanselmann | |
276 | 3d8548c4 | Michael Hanselmann | return data[KEY_RESULT]
|
277 | c2a03789 | Iustin Pop | |
278 | 0bbe448c | Michael Hanselmann | def SubmitJob(self, ops): |
279 | 0bbe448c | Michael Hanselmann | ops_state = map(lambda op: op.__getstate__(), ops) |
280 | 0bbe448c | Michael Hanselmann | return self.CallMethod(REQ_SUBMIT_JOB, ops_state) |
281 | 0bbe448c | Michael Hanselmann | |
282 | 0bbe448c | Michael Hanselmann | def CancelJob(self, job_id): |
283 | 0bbe448c | Michael Hanselmann | return self.CallMethod(REQ_CANCEL_JOB, job_id) |
284 | 0bbe448c | Michael Hanselmann | |
285 | 0bbe448c | Michael Hanselmann | def ArchiveJob(self, job_id): |
286 | 0bbe448c | Michael Hanselmann | return self.CallMethod(REQ_ARCHIVE_JOB, job_id) |
287 | 0bbe448c | Michael Hanselmann | |
288 | 0bbe448c | Michael Hanselmann | def QueryJobs(self, job_ids, fields): |
289 | 0bbe448c | Michael Hanselmann | return self.CallMethod(REQ_QUERY_JOBS, (job_ids, fields)) |
290 | 3d8548c4 | Michael Hanselmann | |
291 | 3d8548c4 | Michael Hanselmann | # TODO: class Server(object) |