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