root / lib / luxi.py @ ef40c537
History | View | Annotate | Download (16 kB)
1 | c2a03789 | Iustin Pop | #
|
---|---|---|---|
2 | c2a03789 | Iustin Pop | #
|
3 | c2a03789 | Iustin Pop | |
4 | 83c046a2 | Iustin Pop | # Copyright (C) 2006, 2007, 2011, 2012 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 | 8d5b316c | Iustin Pop | This module implements the local unix socket protocol. 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 | 7577196d | Guido Trotter | The module is also 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 | 231db3a5 | Michael Hanselmann | import logging |
37 | c2a03789 | Iustin Pop | |
38 | fad50141 | Michael Hanselmann | from ganeti import serializer |
39 | ceab32dd | Iustin Pop | from ganeti import constants |
40 | 6797ec29 | Iustin Pop | from ganeti import errors |
41 | cb462b06 | Michael Hanselmann | from ganeti import utils |
42 | 28b71a76 | Michael Hanselmann | from ganeti import objects |
43 | c2a03789 | Iustin Pop | |
44 | c2a03789 | Iustin Pop | |
45 | 231db3a5 | Michael Hanselmann | KEY_METHOD = "method"
|
46 | 231db3a5 | Michael Hanselmann | KEY_ARGS = "args"
|
47 | 3d8548c4 | Michael Hanselmann | KEY_SUCCESS = "success"
|
48 | 3d8548c4 | Michael Hanselmann | KEY_RESULT = "result"
|
49 | e986f20c | Michael Hanselmann | KEY_VERSION = "version"
|
50 | 3d8548c4 | Michael Hanselmann | |
51 | 0bbe448c | Michael Hanselmann | REQ_SUBMIT_JOB = "SubmitJob"
|
52 | 2971c913 | Iustin Pop | REQ_SUBMIT_MANY_JOBS = "SubmitManyJobs"
|
53 | dfe57c22 | Michael Hanselmann | REQ_WAIT_FOR_JOB_CHANGE = "WaitForJobChange"
|
54 | 0bbe448c | Michael Hanselmann | REQ_CANCEL_JOB = "CancelJob"
|
55 | 0bbe448c | Michael Hanselmann | REQ_ARCHIVE_JOB = "ArchiveJob"
|
56 | 83c046a2 | Iustin Pop | REQ_AUTO_ARCHIVE_JOBS = "AutoArchiveJobs"
|
57 | 28b71a76 | Michael Hanselmann | REQ_QUERY = "Query"
|
58 | 28b71a76 | Michael Hanselmann | REQ_QUERY_FIELDS = "QueryFields"
|
59 | 0bbe448c | Michael Hanselmann | REQ_QUERY_JOBS = "QueryJobs"
|
60 | ee6c7b94 | Michael Hanselmann | REQ_QUERY_INSTANCES = "QueryInstances"
|
61 | 02f7fe54 | Michael Hanselmann | REQ_QUERY_NODES = "QueryNodes"
|
62 | a79ef2a5 | Adeodato Simo | REQ_QUERY_GROUPS = "QueryGroups"
|
63 | 32f93223 | Michael Hanselmann | REQ_QUERY_EXPORTS = "QueryExports"
|
64 | ae5849b5 | Michael Hanselmann | REQ_QUERY_CONFIG_VALUES = "QueryConfigValues"
|
65 | 66baeccc | Iustin Pop | REQ_QUERY_CLUSTER_INFO = "QueryClusterInfo"
|
66 | 7699c3af | Iustin Pop | REQ_QUERY_TAGS = "QueryTags"
|
67 | 83c046a2 | Iustin Pop | REQ_SET_DRAIN_FLAG = "SetDrainFlag"
|
68 | 05e50653 | Michael Hanselmann | REQ_SET_WATCHER_PAUSE = "SetWatcherPause"
|
69 | c2a03789 | Iustin Pop | |
70 | e3a25810 | Michael Hanselmann | #: List of all LUXI requests
|
71 | e3a25810 | Michael Hanselmann | REQ_ALL = frozenset([
|
72 | e3a25810 | Michael Hanselmann | REQ_ARCHIVE_JOB, |
73 | 83c046a2 | Iustin Pop | REQ_AUTO_ARCHIVE_JOBS, |
74 | e3a25810 | Michael Hanselmann | REQ_CANCEL_JOB, |
75 | e3a25810 | Michael Hanselmann | REQ_QUERY, |
76 | e3a25810 | Michael Hanselmann | REQ_QUERY_CLUSTER_INFO, |
77 | e3a25810 | Michael Hanselmann | REQ_QUERY_CONFIG_VALUES, |
78 | e3a25810 | Michael Hanselmann | REQ_QUERY_EXPORTS, |
79 | e3a25810 | Michael Hanselmann | REQ_QUERY_FIELDS, |
80 | e3a25810 | Michael Hanselmann | REQ_QUERY_GROUPS, |
81 | e3a25810 | Michael Hanselmann | REQ_QUERY_INSTANCES, |
82 | e3a25810 | Michael Hanselmann | REQ_QUERY_JOBS, |
83 | e3a25810 | Michael Hanselmann | REQ_QUERY_NODES, |
84 | e3a25810 | Michael Hanselmann | REQ_QUERY_TAGS, |
85 | 83c046a2 | Iustin Pop | REQ_SET_DRAIN_FLAG, |
86 | e3a25810 | Michael Hanselmann | REQ_SET_WATCHER_PAUSE, |
87 | e3a25810 | Michael Hanselmann | REQ_SUBMIT_JOB, |
88 | e3a25810 | Michael Hanselmann | REQ_SUBMIT_MANY_JOBS, |
89 | e3a25810 | Michael Hanselmann | REQ_WAIT_FOR_JOB_CHANGE, |
90 | e3a25810 | Michael Hanselmann | ]) |
91 | e3a25810 | Michael Hanselmann | |
92 | c2a03789 | Iustin Pop | DEF_CTMO = 10
|
93 | c2a03789 | Iustin Pop | DEF_RWTO = 60
|
94 | c2a03789 | Iustin Pop | |
95 | 793a8f7c | Michael Hanselmann | # WaitForJobChange timeout
|
96 | 793a8f7c | Michael Hanselmann | WFJC_TIMEOUT = (DEF_RWTO - 1) / 2 |
97 | 793a8f7c | Michael Hanselmann | |
98 | c2a03789 | Iustin Pop | |
99 | 7a8bda3f | Michael Hanselmann | class ProtocolError(errors.LuxiError): |
100 | 5a1c22fe | Iustin Pop | """Denotes an error in the LUXI protocol."""
|
101 | c2a03789 | Iustin Pop | |
102 | c2a03789 | Iustin Pop | |
103 | c2a03789 | Iustin Pop | class ConnectionClosedError(ProtocolError): |
104 | 5a1c22fe | Iustin Pop | """Connection closed error."""
|
105 | c2a03789 | Iustin Pop | |
106 | c2a03789 | Iustin Pop | |
107 | c2a03789 | Iustin Pop | class TimeoutError(ProtocolError): |
108 | 5a1c22fe | Iustin Pop | """Operation timeout error."""
|
109 | c2a03789 | Iustin Pop | |
110 | c2a03789 | Iustin Pop | |
111 | b77acb3e | Iustin Pop | class RequestError(ProtocolError): |
112 | 5a1c22fe | Iustin Pop | """Error on request.
|
113 | b77acb3e | Iustin Pop |
|
114 | b77acb3e | Iustin Pop | This signifies an error in the request format or request handling,
|
115 | b77acb3e | Iustin Pop | but not (e.g.) an error in starting up an instance.
|
116 | b77acb3e | Iustin Pop |
|
117 | b77acb3e | Iustin Pop | Some common conditions that can trigger this exception:
|
118 | b77acb3e | Iustin Pop | - job submission failed because the job data was wrong
|
119 | b77acb3e | Iustin Pop | - query failed because required fields were missing
|
120 | b77acb3e | Iustin Pop |
|
121 | b77acb3e | Iustin Pop | """
|
122 | b77acb3e | Iustin Pop | |
123 | 3d8548c4 | Michael Hanselmann | |
124 | 03a8dbdc | Iustin Pop | class NoMasterError(ProtocolError): |
125 | 5a1c22fe | Iustin Pop | """The master cannot be reached.
|
126 | 03a8dbdc | Iustin Pop |
|
127 | 03a8dbdc | Iustin Pop | This means that the master daemon is not running or the socket has
|
128 | 03a8dbdc | Iustin Pop | been removed.
|
129 | 03a8dbdc | Iustin Pop |
|
130 | 03a8dbdc | Iustin Pop | """
|
131 | 03a8dbdc | Iustin Pop | |
132 | b77acb3e | Iustin Pop | |
133 | 5a1c22fe | Iustin Pop | class PermissionError(ProtocolError): |
134 | 5a1c22fe | Iustin Pop | """Permission denied while connecting to the master socket.
|
135 | 5a1c22fe | Iustin Pop |
|
136 | 5a1c22fe | Iustin Pop | This means the user doesn't have the proper rights.
|
137 | 5a1c22fe | Iustin Pop |
|
138 | 5a1c22fe | Iustin Pop | """
|
139 | 5a1c22fe | Iustin Pop | |
140 | 5a1c22fe | Iustin Pop | |
141 | c2a03789 | Iustin Pop | class Transport: |
142 | c2a03789 | Iustin Pop | """Low-level transport class.
|
143 | c2a03789 | Iustin Pop |
|
144 | c2a03789 | Iustin Pop | This is used on the client side.
|
145 | c2a03789 | Iustin Pop |
|
146 | c2a03789 | Iustin Pop | This could be replace by any other class that provides the same
|
147 | c2a03789 | Iustin Pop | semantics to the Client. This means:
|
148 | c2a03789 | Iustin Pop | - can send messages and receive messages
|
149 | c2a03789 | Iustin Pop | - safe for multithreading
|
150 | c2a03789 | Iustin Pop |
|
151 | c2a03789 | Iustin Pop | """
|
152 | c2a03789 | Iustin Pop | |
153 | 25942a6c | Guido Trotter | def __init__(self, address, timeouts=None): |
154 | c2a03789 | Iustin Pop | """Constructor for the Client class.
|
155 | c2a03789 | Iustin Pop |
|
156 | c2a03789 | Iustin Pop | Arguments:
|
157 | c2a03789 | Iustin Pop | - address: a valid address the the used transport class
|
158 | c2a03789 | Iustin Pop | - timeout: a list of timeouts, to be used on connect and read/write
|
159 | c2a03789 | Iustin Pop |
|
160 | c2a03789 | Iustin Pop | There are two timeouts used since we might want to wait for a long
|
161 | c2a03789 | Iustin Pop | time for a response, but the connect timeout should be lower.
|
162 | c2a03789 | Iustin Pop |
|
163 | c2a03789 | Iustin Pop | If not passed, we use a default of 10 and respectively 60 seconds.
|
164 | c2a03789 | Iustin Pop |
|
165 | c2a03789 | Iustin Pop | Note that on reading data, since the timeout applies to an
|
166 | c2a03789 | Iustin Pop | invidual receive, it might be that the total duration is longer
|
167 | c2a03789 | Iustin Pop | than timeout value passed (we make a hard limit at twice the read
|
168 | c2a03789 | Iustin Pop | timeout).
|
169 | c2a03789 | Iustin Pop |
|
170 | c2a03789 | Iustin Pop | """
|
171 | c2a03789 | Iustin Pop | self.address = address
|
172 | c2a03789 | Iustin Pop | if timeouts is None: |
173 | c2a03789 | Iustin Pop | self._ctimeout, self._rwtimeout = DEF_CTMO, DEF_RWTO |
174 | c2a03789 | Iustin Pop | else:
|
175 | c2a03789 | Iustin Pop | self._ctimeout, self._rwtimeout = timeouts |
176 | c2a03789 | Iustin Pop | |
177 | c2a03789 | Iustin Pop | self.socket = None |
178 | c2a03789 | Iustin Pop | self._buffer = "" |
179 | c2a03789 | Iustin Pop | self._msgs = collections.deque()
|
180 | c2a03789 | Iustin Pop | |
181 | c2a03789 | Iustin Pop | try:
|
182 | c2a03789 | Iustin Pop | self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
183 | cb462b06 | Michael Hanselmann | |
184 | cb462b06 | Michael Hanselmann | # Try to connect
|
185 | c2a03789 | Iustin Pop | try:
|
186 | cb462b06 | Michael Hanselmann | utils.Retry(self._Connect, 1.0, self._ctimeout, |
187 | cb462b06 | Michael Hanselmann | args=(self.socket, address, self._ctimeout)) |
188 | cb462b06 | Michael Hanselmann | except utils.RetryTimeout:
|
189 | cb462b06 | Michael Hanselmann | raise TimeoutError("Connect timed out") |
190 | cb462b06 | Michael Hanselmann | |
191 | c2a03789 | Iustin Pop | self.socket.settimeout(self._rwtimeout) |
192 | 03a8dbdc | Iustin Pop | except (socket.error, NoMasterError):
|
193 | c2a03789 | Iustin Pop | if self.socket is not None: |
194 | c2a03789 | Iustin Pop | self.socket.close()
|
195 | c2a03789 | Iustin Pop | self.socket = None |
196 | c2a03789 | Iustin Pop | raise
|
197 | c2a03789 | Iustin Pop | |
198 | cb462b06 | Michael Hanselmann | @staticmethod
|
199 | cb462b06 | Michael Hanselmann | def _Connect(sock, address, timeout): |
200 | cb462b06 | Michael Hanselmann | sock.settimeout(timeout) |
201 | cb462b06 | Michael Hanselmann | try:
|
202 | cb462b06 | Michael Hanselmann | sock.connect(address) |
203 | cb462b06 | Michael Hanselmann | except socket.timeout, err:
|
204 | cb462b06 | Michael Hanselmann | raise TimeoutError("Connect timed out: %s" % str(err)) |
205 | cb462b06 | Michael Hanselmann | except socket.error, err:
|
206 | 5a1c22fe | Iustin Pop | error_code = err.args[0]
|
207 | 5a1c22fe | Iustin Pop | if error_code in (errno.ENOENT, errno.ECONNREFUSED): |
208 | cb462b06 | Michael Hanselmann | raise NoMasterError(address)
|
209 | 5a1c22fe | Iustin Pop | elif error_code in (errno.EPERM, errno.EACCES): |
210 | 5a1c22fe | Iustin Pop | raise PermissionError(address)
|
211 | 5a1c22fe | Iustin Pop | elif error_code == errno.EAGAIN:
|
212 | cb462b06 | Michael Hanselmann | # Server's socket backlog is full at the moment
|
213 | cb462b06 | Michael Hanselmann | raise utils.RetryAgain()
|
214 | cb462b06 | Michael Hanselmann | raise
|
215 | cb462b06 | Michael Hanselmann | |
216 | c2a03789 | Iustin Pop | def _CheckSocket(self): |
217 | c2a03789 | Iustin Pop | """Make sure we are connected.
|
218 | c2a03789 | Iustin Pop |
|
219 | c2a03789 | Iustin Pop | """
|
220 | c2a03789 | Iustin Pop | if self.socket is None: |
221 | c2a03789 | Iustin Pop | raise ProtocolError("Connection is closed") |
222 | c2a03789 | Iustin Pop | |
223 | c2a03789 | Iustin Pop | def Send(self, msg): |
224 | c2a03789 | Iustin Pop | """Send a message.
|
225 | c2a03789 | Iustin Pop |
|
226 | c2a03789 | Iustin Pop | This just sends a message and doesn't wait for the response.
|
227 | c2a03789 | Iustin Pop |
|
228 | c2a03789 | Iustin Pop | """
|
229 | 25942a6c | Guido Trotter | if constants.LUXI_EOM in msg: |
230 | 797506fc | Michael Hanselmann | raise ProtocolError("Message terminator found in payload") |
231 | 797506fc | Michael Hanselmann | |
232 | c2a03789 | Iustin Pop | self._CheckSocket()
|
233 | c2a03789 | Iustin Pop | try:
|
234 | 6096ee13 | Michael Hanselmann | # TODO: sendall is not guaranteed to send everything
|
235 | 25942a6c | Guido Trotter | self.socket.sendall(msg + constants.LUXI_EOM)
|
236 | c2a03789 | Iustin Pop | except socket.timeout, err:
|
237 | c2a03789 | Iustin Pop | raise TimeoutError("Sending timeout: %s" % str(err)) |
238 | c2a03789 | Iustin Pop | |
239 | c2a03789 | Iustin Pop | def Recv(self): |
240 | 5bbd3f7f | Michael Hanselmann | """Try to receive a message from the socket.
|
241 | c2a03789 | Iustin Pop |
|
242 | c2a03789 | Iustin Pop | In case we already have messages queued, we just return from the
|
243 | c2a03789 | Iustin Pop | queue. Otherwise, we try to read data with a _rwtimeout network
|
244 | c2a03789 | Iustin Pop | timeout, and making sure we don't go over 2x_rwtimeout as a global
|
245 | c2a03789 | Iustin Pop | limit.
|
246 | c2a03789 | Iustin Pop |
|
247 | c2a03789 | Iustin Pop | """
|
248 | c2a03789 | Iustin Pop | self._CheckSocket()
|
249 | c2a03789 | Iustin Pop | etime = time.time() + self._rwtimeout
|
250 | c2a03789 | Iustin Pop | while not self._msgs: |
251 | c2a03789 | Iustin Pop | if time.time() > etime:
|
252 | c2a03789 | Iustin Pop | raise TimeoutError("Extended receive timeout") |
253 | 6096ee13 | Michael Hanselmann | while True: |
254 | 6096ee13 | Michael Hanselmann | try:
|
255 | 6096ee13 | Michael Hanselmann | data = self.socket.recv(4096) |
256 | 28e3e216 | Michael Hanselmann | except socket.timeout, err:
|
257 | 28e3e216 | Michael Hanselmann | raise TimeoutError("Receive timeout: %s" % str(err)) |
258 | 6096ee13 | Michael Hanselmann | except socket.error, err:
|
259 | 6096ee13 | Michael Hanselmann | if err.args and err.args[0] == errno.EAGAIN: |
260 | 6096ee13 | Michael Hanselmann | continue
|
261 | 6096ee13 | Michael Hanselmann | raise
|
262 | 6096ee13 | Michael Hanselmann | break
|
263 | c2a03789 | Iustin Pop | if not data: |
264 | c2a03789 | Iustin Pop | raise ConnectionClosedError("Connection closed while reading") |
265 | 25942a6c | Guido Trotter | new_msgs = (self._buffer + data).split(constants.LUXI_EOM)
|
266 | c2a03789 | Iustin Pop | self._buffer = new_msgs.pop()
|
267 | c2a03789 | Iustin Pop | self._msgs.extend(new_msgs)
|
268 | c2a03789 | Iustin Pop | return self._msgs.popleft() |
269 | c2a03789 | Iustin Pop | |
270 | c2a03789 | Iustin Pop | def Call(self, msg): |
271 | c2a03789 | Iustin Pop | """Send a message and wait for the response.
|
272 | c2a03789 | Iustin Pop |
|
273 | c2a03789 | Iustin Pop | This is just a wrapper over Send and Recv.
|
274 | c2a03789 | Iustin Pop |
|
275 | c2a03789 | Iustin Pop | """
|
276 | c2a03789 | Iustin Pop | self.Send(msg)
|
277 | c2a03789 | Iustin Pop | return self.Recv() |
278 | c2a03789 | Iustin Pop | |
279 | c2a03789 | Iustin Pop | def Close(self): |
280 | c2a03789 | Iustin Pop | """Close the socket"""
|
281 | c2a03789 | Iustin Pop | if self.socket is not None: |
282 | c2a03789 | Iustin Pop | self.socket.close()
|
283 | c2a03789 | Iustin Pop | self.socket = None |
284 | c2a03789 | Iustin Pop | |
285 | c2a03789 | Iustin Pop | |
286 | 231db3a5 | Michael Hanselmann | def ParseRequest(msg): |
287 | 231db3a5 | Michael Hanselmann | """Parses a LUXI request message.
|
288 | 231db3a5 | Michael Hanselmann |
|
289 | 231db3a5 | Michael Hanselmann | """
|
290 | 231db3a5 | Michael Hanselmann | try:
|
291 | 231db3a5 | Michael Hanselmann | request = serializer.LoadJson(msg) |
292 | 231db3a5 | Michael Hanselmann | except ValueError, err: |
293 | 231db3a5 | Michael Hanselmann | raise ProtocolError("Invalid LUXI request (parsing error): %s" % err) |
294 | 231db3a5 | Michael Hanselmann | |
295 | 231db3a5 | Michael Hanselmann | logging.debug("LUXI request: %s", request)
|
296 | 231db3a5 | Michael Hanselmann | |
297 | 231db3a5 | Michael Hanselmann | if not isinstance(request, dict): |
298 | 231db3a5 | Michael Hanselmann | logging.error("LUXI request not a dict: %r", msg)
|
299 | 231db3a5 | Michael Hanselmann | raise ProtocolError("Invalid LUXI request (not a dict)") |
300 | 231db3a5 | Michael Hanselmann | |
301 | b459a848 | Andrea Spadaccini | method = request.get(KEY_METHOD, None) # pylint: disable=E1103 |
302 | b459a848 | Andrea Spadaccini | args = request.get(KEY_ARGS, None) # pylint: disable=E1103 |
303 | b459a848 | Andrea Spadaccini | version = request.get(KEY_VERSION, None) # pylint: disable=E1103 |
304 | e7a25b08 | Guido Trotter | |
305 | 231db3a5 | Michael Hanselmann | if method is None or args is None: |
306 | 231db3a5 | Michael Hanselmann | logging.error("LUXI request missing method or arguments: %r", msg)
|
307 | 231db3a5 | Michael Hanselmann | raise ProtocolError(("Invalid LUXI request (no method or arguments" |
308 | 231db3a5 | Michael Hanselmann | " in request): %r") % msg)
|
309 | 231db3a5 | Michael Hanselmann | |
310 | e986f20c | Michael Hanselmann | return (method, args, version)
|
311 | 231db3a5 | Michael Hanselmann | |
312 | 231db3a5 | Michael Hanselmann | |
313 | 231db3a5 | Michael Hanselmann | def ParseResponse(msg): |
314 | 231db3a5 | Michael Hanselmann | """Parses a LUXI response message.
|
315 | 231db3a5 | Michael Hanselmann |
|
316 | 231db3a5 | Michael Hanselmann | """
|
317 | 231db3a5 | Michael Hanselmann | # Parse the result
|
318 | 231db3a5 | Michael Hanselmann | try:
|
319 | 231db3a5 | Michael Hanselmann | data = serializer.LoadJson(msg) |
320 | d143f2c6 | Iustin Pop | except KeyboardInterrupt: |
321 | d143f2c6 | Iustin Pop | raise
|
322 | 231db3a5 | Michael Hanselmann | except Exception, err: |
323 | 231db3a5 | Michael Hanselmann | raise ProtocolError("Error while deserializing response: %s" % str(err)) |
324 | 231db3a5 | Michael Hanselmann | |
325 | 231db3a5 | Michael Hanselmann | # Validate response
|
326 | 231db3a5 | Michael Hanselmann | if not (isinstance(data, dict) and |
327 | 231db3a5 | Michael Hanselmann | KEY_SUCCESS in data and |
328 | 231db3a5 | Michael Hanselmann | KEY_RESULT in data):
|
329 | 231db3a5 | Michael Hanselmann | raise ProtocolError("Invalid response from server: %r" % data) |
330 | 231db3a5 | Michael Hanselmann | |
331 | 2317945a | Guido Trotter | return (data[KEY_SUCCESS], data[KEY_RESULT],
|
332 | b459a848 | Andrea Spadaccini | data.get(KEY_VERSION, None)) # pylint: disable=E1103 |
333 | 231db3a5 | Michael Hanselmann | |
334 | 231db3a5 | Michael Hanselmann | |
335 | e986f20c | Michael Hanselmann | def FormatResponse(success, result, version=None): |
336 | 231db3a5 | Michael Hanselmann | """Formats a LUXI response message.
|
337 | 231db3a5 | Michael Hanselmann |
|
338 | 231db3a5 | Michael Hanselmann | """
|
339 | 231db3a5 | Michael Hanselmann | response = { |
340 | 231db3a5 | Michael Hanselmann | KEY_SUCCESS: success, |
341 | 231db3a5 | Michael Hanselmann | KEY_RESULT: result, |
342 | 231db3a5 | Michael Hanselmann | } |
343 | 231db3a5 | Michael Hanselmann | |
344 | e986f20c | Michael Hanselmann | if version is not None: |
345 | e986f20c | Michael Hanselmann | response[KEY_VERSION] = version |
346 | e986f20c | Michael Hanselmann | |
347 | 231db3a5 | Michael Hanselmann | logging.debug("LUXI response: %s", response)
|
348 | 231db3a5 | Michael Hanselmann | |
349 | 231db3a5 | Michael Hanselmann | return serializer.DumpJson(response)
|
350 | 231db3a5 | Michael Hanselmann | |
351 | 231db3a5 | Michael Hanselmann | |
352 | e986f20c | Michael Hanselmann | def FormatRequest(method, args, version=None): |
353 | 231db3a5 | Michael Hanselmann | """Formats a LUXI request message.
|
354 | 231db3a5 | Michael Hanselmann |
|
355 | 231db3a5 | Michael Hanselmann | """
|
356 | 231db3a5 | Michael Hanselmann | # Build request
|
357 | 231db3a5 | Michael Hanselmann | request = { |
358 | 231db3a5 | Michael Hanselmann | KEY_METHOD: method, |
359 | 231db3a5 | Michael Hanselmann | KEY_ARGS: args, |
360 | 231db3a5 | Michael Hanselmann | } |
361 | 231db3a5 | Michael Hanselmann | |
362 | e986f20c | Michael Hanselmann | if version is not None: |
363 | e986f20c | Michael Hanselmann | request[KEY_VERSION] = version |
364 | e986f20c | Michael Hanselmann | |
365 | 231db3a5 | Michael Hanselmann | # Serialize the request
|
366 | a182a3ed | Michael Hanselmann | return serializer.DumpJson(request)
|
367 | 231db3a5 | Michael Hanselmann | |
368 | 231db3a5 | Michael Hanselmann | |
369 | e986f20c | Michael Hanselmann | def CallLuxiMethod(transport_cb, method, args, version=None): |
370 | 231db3a5 | Michael Hanselmann | """Send a LUXI request via a transport and return the response.
|
371 | 231db3a5 | Michael Hanselmann |
|
372 | 231db3a5 | Michael Hanselmann | """
|
373 | 231db3a5 | Michael Hanselmann | assert callable(transport_cb) |
374 | 231db3a5 | Michael Hanselmann | |
375 | e986f20c | Michael Hanselmann | request_msg = FormatRequest(method, args, version=version) |
376 | 231db3a5 | Michael Hanselmann | |
377 | 231db3a5 | Michael Hanselmann | # Send request and wait for response
|
378 | 231db3a5 | Michael Hanselmann | response_msg = transport_cb(request_msg) |
379 | 231db3a5 | Michael Hanselmann | |
380 | e986f20c | Michael Hanselmann | (success, result, resp_version) = ParseResponse(response_msg) |
381 | e986f20c | Michael Hanselmann | |
382 | e986f20c | Michael Hanselmann | # Verify version if there was one in the response
|
383 | e986f20c | Michael Hanselmann | if resp_version is not None and resp_version != version: |
384 | e986f20c | Michael Hanselmann | raise errors.LuxiError("LUXI version mismatch, client %s, response %s" % |
385 | e986f20c | Michael Hanselmann | (version, resp_version)) |
386 | 231db3a5 | Michael Hanselmann | |
387 | 231db3a5 | Michael Hanselmann | if success:
|
388 | 231db3a5 | Michael Hanselmann | return result
|
389 | 231db3a5 | Michael Hanselmann | |
390 | 231db3a5 | Michael Hanselmann | errors.MaybeRaise(result) |
391 | 231db3a5 | Michael Hanselmann | raise RequestError(result)
|
392 | 231db3a5 | Michael Hanselmann | |
393 | 231db3a5 | Michael Hanselmann | |
394 | c2a03789 | Iustin Pop | class Client(object): |
395 | c2a03789 | Iustin Pop | """High-level client implementation.
|
396 | c2a03789 | Iustin Pop |
|
397 | c2a03789 | Iustin Pop | This uses a backing Transport-like class on top of which it
|
398 | c2a03789 | Iustin Pop | implements data serialization/deserialization.
|
399 | c2a03789 | Iustin Pop |
|
400 | c2a03789 | Iustin Pop | """
|
401 | ceab32dd | Iustin Pop | def __init__(self, address=None, timeouts=None, transport=Transport): |
402 | c2a03789 | Iustin Pop | """Constructor for the Client class.
|
403 | c2a03789 | Iustin Pop |
|
404 | c2a03789 | Iustin Pop | Arguments:
|
405 | c2a03789 | Iustin Pop | - address: a valid address the the used transport class
|
406 | c2a03789 | Iustin Pop | - timeout: a list of timeouts, to be used on connect and read/write
|
407 | c2a03789 | Iustin Pop | - transport: a Transport-like class
|
408 | c2a03789 | Iustin Pop |
|
409 | c2a03789 | Iustin Pop |
|
410 | c2a03789 | Iustin Pop | If timeout is not passed, the default timeouts of the transport
|
411 | c2a03789 | Iustin Pop | class are used.
|
412 | c2a03789 | Iustin Pop |
|
413 | c2a03789 | Iustin Pop | """
|
414 | ceab32dd | Iustin Pop | if address is None: |
415 | ceab32dd | Iustin Pop | address = constants.MASTER_SOCKET |
416 | 8d5b316c | Iustin Pop | self.address = address
|
417 | 8d5b316c | Iustin Pop | self.timeouts = timeouts
|
418 | 8d5b316c | Iustin Pop | self.transport_class = transport
|
419 | 8d5b316c | Iustin Pop | self.transport = None |
420 | 8d5b316c | Iustin Pop | self._InitTransport()
|
421 | 8d5b316c | Iustin Pop | |
422 | 8d5b316c | Iustin Pop | def _InitTransport(self): |
423 | 8d5b316c | Iustin Pop | """(Re)initialize the transport if needed.
|
424 | 8d5b316c | Iustin Pop |
|
425 | 8d5b316c | Iustin Pop | """
|
426 | 8d5b316c | Iustin Pop | if self.transport is None: |
427 | 8d5b316c | Iustin Pop | self.transport = self.transport_class(self.address, |
428 | 8d5b316c | Iustin Pop | timeouts=self.timeouts)
|
429 | 8d5b316c | Iustin Pop | |
430 | 8d5b316c | Iustin Pop | def _CloseTransport(self): |
431 | 8d5b316c | Iustin Pop | """Close the transport, ignoring errors.
|
432 | 8d5b316c | Iustin Pop |
|
433 | 8d5b316c | Iustin Pop | """
|
434 | 8d5b316c | Iustin Pop | if self.transport is None: |
435 | 8d5b316c | Iustin Pop | return
|
436 | 8d5b316c | Iustin Pop | try:
|
437 | 8d5b316c | Iustin Pop | old_transp = self.transport
|
438 | 8d5b316c | Iustin Pop | self.transport = None |
439 | 8d5b316c | Iustin Pop | old_transp.Close() |
440 | b459a848 | Andrea Spadaccini | except Exception: # pylint: disable=W0703 |
441 | 8d5b316c | Iustin Pop | pass
|
442 | c2a03789 | Iustin Pop | |
443 | 231db3a5 | Michael Hanselmann | def _SendMethodCall(self, data): |
444 | 3d8548c4 | Michael Hanselmann | # Send request and wait for response
|
445 | 8d5b316c | Iustin Pop | try:
|
446 | 8d5b316c | Iustin Pop | self._InitTransport()
|
447 | 231db3a5 | Michael Hanselmann | return self.transport.Call(data) |
448 | 8d5b316c | Iustin Pop | except Exception: |
449 | 8d5b316c | Iustin Pop | self._CloseTransport()
|
450 | 8d5b316c | Iustin Pop | raise
|
451 | 8d5b316c | Iustin Pop | |
452 | 2a917701 | Michael Hanselmann | def Close(self): |
453 | 2a917701 | Michael Hanselmann | """Close the underlying connection.
|
454 | 2a917701 | Michael Hanselmann |
|
455 | 2a917701 | Michael Hanselmann | """
|
456 | 2a917701 | Michael Hanselmann | self._CloseTransport()
|
457 | 2a917701 | Michael Hanselmann | |
458 | 231db3a5 | Michael Hanselmann | def CallMethod(self, method, args): |
459 | 231db3a5 | Michael Hanselmann | """Send a generic request and return the response.
|
460 | 3d8548c4 | Michael Hanselmann |
|
461 | 231db3a5 | Michael Hanselmann | """
|
462 | a629ecb9 | Iustin Pop | if not isinstance(args, (list, tuple)): |
463 | a629ecb9 | Iustin Pop | raise errors.ProgrammerError("Invalid parameter passed to CallMethod:" |
464 | a629ecb9 | Iustin Pop | " expected list, got %s" % type(args)) |
465 | e986f20c | Michael Hanselmann | return CallLuxiMethod(self._SendMethodCall, method, args, |
466 | e986f20c | Michael Hanselmann | version=constants.LUXI_VERSION) |
467 | c2a03789 | Iustin Pop | |
468 | 3ccafd0e | Iustin Pop | def SetQueueDrainFlag(self, drain_flag): |
469 | 83c046a2 | Iustin Pop | return self.CallMethod(REQ_SET_DRAIN_FLAG, (drain_flag, )) |
470 | 3ccafd0e | Iustin Pop | |
471 | 05e50653 | Michael Hanselmann | def SetWatcherPause(self, until): |
472 | a629ecb9 | Iustin Pop | return self.CallMethod(REQ_SET_WATCHER_PAUSE, (until, )) |
473 | 05e50653 | Michael Hanselmann | |
474 | 0bbe448c | Michael Hanselmann | def SubmitJob(self, ops): |
475 | 0bbe448c | Michael Hanselmann | ops_state = map(lambda op: op.__getstate__(), ops) |
476 | 0bbe448c | Michael Hanselmann | return self.CallMethod(REQ_SUBMIT_JOB, ops_state) |
477 | 0bbe448c | Michael Hanselmann | |
478 | 2971c913 | Iustin Pop | def SubmitManyJobs(self, jobs): |
479 | 2971c913 | Iustin Pop | jobs_state = [] |
480 | 2971c913 | Iustin Pop | for ops in jobs: |
481 | 2971c913 | Iustin Pop | jobs_state.append([op.__getstate__() for op in ops]) |
482 | 2971c913 | Iustin Pop | return self.CallMethod(REQ_SUBMIT_MANY_JOBS, jobs_state) |
483 | 2971c913 | Iustin Pop | |
484 | 0bbe448c | Michael Hanselmann | def CancelJob(self, job_id): |
485 | a629ecb9 | Iustin Pop | return self.CallMethod(REQ_CANCEL_JOB, (job_id, )) |
486 | 0bbe448c | Michael Hanselmann | |
487 | 0bbe448c | Michael Hanselmann | def ArchiveJob(self, job_id): |
488 | a629ecb9 | Iustin Pop | return self.CallMethod(REQ_ARCHIVE_JOB, (job_id, )) |
489 | 0bbe448c | Michael Hanselmann | |
490 | 07cd723a | Iustin Pop | def AutoArchiveJobs(self, age): |
491 | f8ad5591 | Michael Hanselmann | timeout = (DEF_RWTO - 1) / 2 |
492 | 83c046a2 | Iustin Pop | return self.CallMethod(REQ_AUTO_ARCHIVE_JOBS, (age, timeout)) |
493 | 07cd723a | Iustin Pop | |
494 | f4484122 | Michael Hanselmann | def WaitForJobChangeOnce(self, job_id, fields, |
495 | 793a8f7c | Michael Hanselmann | prev_job_info, prev_log_serial, |
496 | 793a8f7c | Michael Hanselmann | timeout=WFJC_TIMEOUT): |
497 | 793a8f7c | Michael Hanselmann | """Waits for changes on a job.
|
498 | 793a8f7c | Michael Hanselmann |
|
499 | 793a8f7c | Michael Hanselmann | @param job_id: Job ID
|
500 | 793a8f7c | Michael Hanselmann | @type fields: list
|
501 | 793a8f7c | Michael Hanselmann | @param fields: List of field names to be observed
|
502 | 793a8f7c | Michael Hanselmann | @type prev_job_info: None or list
|
503 | 793a8f7c | Michael Hanselmann | @param prev_job_info: Previously received job information
|
504 | 793a8f7c | Michael Hanselmann | @type prev_log_serial: None or int/long
|
505 | 793a8f7c | Michael Hanselmann | @param prev_log_serial: Highest log serial number previously received
|
506 | 793a8f7c | Michael Hanselmann | @type timeout: int/float
|
507 | 793a8f7c | Michael Hanselmann | @param timeout: Timeout in seconds (values larger than L{WFJC_TIMEOUT} will
|
508 | 793a8f7c | Michael Hanselmann | be capped to that value)
|
509 | 793a8f7c | Michael Hanselmann |
|
510 | 793a8f7c | Michael Hanselmann | """
|
511 | 793a8f7c | Michael Hanselmann | assert timeout >= 0, "Timeout can not be negative" |
512 | f4484122 | Michael Hanselmann | return self.CallMethod(REQ_WAIT_FOR_JOB_CHANGE, |
513 | f4484122 | Michael Hanselmann | (job_id, fields, prev_job_info, |
514 | 793a8f7c | Michael Hanselmann | prev_log_serial, |
515 | 793a8f7c | Michael Hanselmann | min(WFJC_TIMEOUT, timeout)))
|
516 | f4484122 | Michael Hanselmann | |
517 | f4484122 | Michael Hanselmann | def WaitForJobChange(self, job_id, fields, prev_job_info, prev_log_serial): |
518 | 5c735209 | Iustin Pop | while True: |
519 | f4484122 | Michael Hanselmann | result = self.WaitForJobChangeOnce(job_id, fields,
|
520 | f4484122 | Michael Hanselmann | prev_job_info, prev_log_serial) |
521 | 5c735209 | Iustin Pop | if result != constants.JOB_NOTCHANGED:
|
522 | 5c735209 | Iustin Pop | break
|
523 | 5c735209 | Iustin Pop | return result
|
524 | dfe57c22 | Michael Hanselmann | |
525 | 2e5c33db | Iustin Pop | def Query(self, what, fields, qfilter): |
526 | 28b71a76 | Michael Hanselmann | """Query for resources/items.
|
527 | 28b71a76 | Michael Hanselmann |
|
528 | abd66bf8 | Michael Hanselmann | @param what: One of L{constants.QR_VIA_LUXI}
|
529 | 28b71a76 | Michael Hanselmann | @type fields: List of strings
|
530 | 28b71a76 | Michael Hanselmann | @param fields: List of requested fields
|
531 | 2e5c33db | Iustin Pop | @type qfilter: None or list
|
532 | 2e5c33db | Iustin Pop | @param qfilter: Query filter
|
533 | 28b71a76 | Michael Hanselmann | @rtype: L{objects.QueryResponse}
|
534 | 28b71a76 | Michael Hanselmann |
|
535 | 28b71a76 | Michael Hanselmann | """
|
536 | a629ecb9 | Iustin Pop | result = self.CallMethod(REQ_QUERY, (what, fields, qfilter))
|
537 | 28b71a76 | Michael Hanselmann | return objects.QueryResponse.FromDict(result)
|
538 | 28b71a76 | Michael Hanselmann | |
539 | 28b71a76 | Michael Hanselmann | def QueryFields(self, what, fields): |
540 | 28b71a76 | Michael Hanselmann | """Query for available fields.
|
541 | 28b71a76 | Michael Hanselmann |
|
542 | abd66bf8 | Michael Hanselmann | @param what: One of L{constants.QR_VIA_LUXI}
|
543 | 28b71a76 | Michael Hanselmann | @type fields: None or list of strings
|
544 | 28b71a76 | Michael Hanselmann | @param fields: List of requested fields
|
545 | 28b71a76 | Michael Hanselmann | @rtype: L{objects.QueryFieldsResponse}
|
546 | 28b71a76 | Michael Hanselmann |
|
547 | 28b71a76 | Michael Hanselmann | """
|
548 | a629ecb9 | Iustin Pop | result = self.CallMethod(REQ_QUERY_FIELDS, (what, fields))
|
549 | 28b71a76 | Michael Hanselmann | return objects.QueryFieldsResponse.FromDict(result)
|
550 | 28b71a76 | Michael Hanselmann | |
551 | 0bbe448c | Michael Hanselmann | def QueryJobs(self, job_ids, fields): |
552 | 0bbe448c | Michael Hanselmann | return self.CallMethod(REQ_QUERY_JOBS, (job_ids, fields)) |
553 | 3d8548c4 | Michael Hanselmann | |
554 | ec79568d | Iustin Pop | def QueryInstances(self, names, fields, use_locking): |
555 | ec79568d | Iustin Pop | return self.CallMethod(REQ_QUERY_INSTANCES, (names, fields, use_locking)) |
556 | ee6c7b94 | Michael Hanselmann | |
557 | ec79568d | Iustin Pop | def QueryNodes(self, names, fields, use_locking): |
558 | ec79568d | Iustin Pop | return self.CallMethod(REQ_QUERY_NODES, (names, fields, use_locking)) |
559 | 02f7fe54 | Michael Hanselmann | |
560 | a79ef2a5 | Adeodato Simo | def QueryGroups(self, names, fields, use_locking): |
561 | a79ef2a5 | Adeodato Simo | return self.CallMethod(REQ_QUERY_GROUPS, (names, fields, use_locking)) |
562 | a79ef2a5 | Adeodato Simo | |
563 | ec79568d | Iustin Pop | def QueryExports(self, nodes, use_locking): |
564 | ec79568d | Iustin Pop | return self.CallMethod(REQ_QUERY_EXPORTS, (nodes, use_locking)) |
565 | 32f93223 | Michael Hanselmann | |
566 | 66baeccc | Iustin Pop | def QueryClusterInfo(self): |
567 | 66baeccc | Iustin Pop | return self.CallMethod(REQ_QUERY_CLUSTER_INFO, ()) |
568 | 66baeccc | Iustin Pop | |
569 | ae5849b5 | Michael Hanselmann | def QueryConfigValues(self, fields): |
570 | a629ecb9 | Iustin Pop | return self.CallMethod(REQ_QUERY_CONFIG_VALUES, (fields, )) |
571 | ae5849b5 | Michael Hanselmann | |
572 | 7699c3af | Iustin Pop | def QueryTags(self, kind, name): |
573 | 7699c3af | Iustin Pop | return self.CallMethod(REQ_QUERY_TAGS, (kind, name)) |