cmdlib: Drop SSH runner from LU base class
[ganeti-local] / lib / errors.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007, 2008, 2009, 2010 Google Inc.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21
22 """Ganeti exception handling"""
23
24
25 # OpPrereqError failure types
26
27 # resolver errors
28 ECODE_RESOLVER = "resolver_error"
29 # not enough resources (iallocator failure, disk space, memory, etc.)
30 ECODE_NORES = "insufficient_resources"
31 # wrong arguments (at syntax level)
32 ECODE_INVAL = "wrong_input"
33 # wrong entity state
34 ECODE_STATE = "wrong_state"
35 # entity not found
36 ECODE_NOENT = "unknown_entity"
37 # entity already exists
38 ECODE_EXISTS = "already_exists"
39 # resource not unique (e.g. MAC or IP duplication)
40 ECODE_NOTUNIQUE = "resource_not_unique"
41 # internal cluster error
42 ECODE_FAULT = "internal_error"
43 # environment error (e.g. node disk error)
44 ECODE_ENVIRON = "environment_error"
45
46 #: List of all failure types
47 ECODE_ALL = frozenset([
48   ECODE_RESOLVER,
49   ECODE_NORES,
50   ECODE_INVAL,
51   ECODE_STATE,
52   ECODE_NOENT,
53   ECODE_EXISTS,
54   ECODE_NOTUNIQUE,
55   ECODE_FAULT,
56   ECODE_ENVIRON,
57   ])
58
59
60 class GenericError(Exception):
61   """Base exception for Ganeti.
62
63   """
64   pass
65
66
67 class LVMError(GenericError):
68   """LVM-related exception.
69
70   This exception codifies problems with LVM setup.
71
72   """
73   pass
74
75
76 class LockError(GenericError):
77   """Lock error exception.
78
79   This signifies problems in the locking subsystem.
80
81   """
82   pass
83
84
85 class HypervisorError(GenericError):
86   """Hypervisor-related exception.
87
88   This is raised in case we can't communicate with the hypervisor
89   properly.
90
91   """
92   pass
93
94
95 class ProgrammerError(GenericError):
96   """Programming-related error.
97
98   This is raised in cases we determine that the calling conventions
99   have been violated, meaning we got some desynchronisation between
100   parts of our code. It signifies a real programming bug.
101
102   """
103   pass
104
105
106 class BlockDeviceError(GenericError):
107   """Block-device related exception.
108
109   This is raised in case we can't setup the instance's block devices
110   properly.
111
112   """
113   pass
114
115
116 class ConfigurationError(GenericError):
117   """Configuration related exception.
118
119   Things like having an instance with a primary node that doesn't
120   exist in the config or such raise this exception.
121
122   """
123   pass
124
125
126 class ConfigVersionMismatch(ConfigurationError):
127   """Version mismatch in the configuration file.
128
129   The error has two arguments: the expected and the actual found
130   version.
131
132   """
133   pass
134
135
136 class ReservationError(GenericError):
137   """Errors reserving a resource.
138
139   """
140
141
142 class RemoteError(GenericError):
143   """Programming-related error on remote call.
144
145   This is raised when an unhandled error occurs in a call to a
146   remote node.  It usually signifies a real programming bug.
147
148   """
149   pass
150
151
152 class SignatureError(GenericError):
153   """Error authenticating a remote message.
154
155   This is raised when the hmac signature on a message doesn't verify correctly
156   to the message itself. It can happen because of network unreliability or
157   because of spurious traffic.
158
159   """
160   pass
161
162
163 class ParameterError(GenericError):
164   """A passed parameter to a command is invalid.
165
166   This is raised when the parameter passed to a request function is
167   invalid. Correct code should have verified this before passing the
168   request structure.
169
170   The argument to this exception should be the parameter name.
171
172   """
173   pass
174
175
176 class OpPrereqError(GenericError):
177   """Prerequisites for the OpCode are not fulfilled.
178
179   This exception will have either one or two arguments. For the
180   two-argument construction, the second argument should be one of the
181   ECODE_* codes.
182
183   """
184
185
186 class OpExecError(GenericError):
187   """Error during OpCode execution.
188
189   """
190
191
192 class OpCodeUnknown(GenericError):
193   """Unknown opcode submitted.
194
195   This signifies a mismatch between the definitions on the client and
196   server side.
197
198   """
199
200
201 class JobLost(GenericError):
202   """Submitted job lost.
203
204   The job was submitted but it cannot be found in the current job
205   list.
206
207   """
208
209
210 class JobFileCorrupted(GenericError):
211   """Job file could not be properly decoded/restored.
212
213   """
214
215
216 class ResolverError(GenericError):
217   """Host name cannot be resolved.
218
219   This is not a normal situation for Ganeti, as we rely on having a
220   working resolver.
221
222   The non-resolvable hostname is available as the first element of the
223   args tuple; the other two elements of the tuple are the first two
224   args of the socket.gaierror exception (error code and description).
225
226   """
227
228
229 class HooksFailure(GenericError):
230   """A generic hook failure.
231
232   This signifies usually a setup misconfiguration.
233
234   """
235
236
237 class HooksAbort(HooksFailure):
238   """A required hook has failed.
239
240   This caused an abort of the operation in the initial phase. This
241   exception always has an attribute args which is a list of tuples of:
242     - node: the source node on which this hooks has failed
243     - script: the name of the script which aborted the run
244
245   """
246
247
248 class UnitParseError(GenericError):
249   """Unable to parse size unit.
250
251   """
252
253
254 class ParseError(GenericError):
255   """Generic parse error.
256
257   Raised when unable to parse user input.
258
259   """
260
261
262 class TypeEnforcementError(GenericError):
263   """Unable to enforce data type.
264
265   """
266
267
268 class SshKeyError(GenericError):
269   """Invalid SSH key.
270
271   """
272
273
274 class TagError(GenericError):
275   """Generic tag error.
276
277   The argument to this exception will show the exact error.
278
279   """
280
281
282 class CommandError(GenericError):
283   """External command error.
284
285   """
286
287
288 class StorageError(GenericError):
289   """Storage-related exception.
290
291   """
292
293
294 class InotifyError(GenericError):
295   """Error raised when there is a failure setting up an inotify watcher.
296
297   """
298
299
300 class QuitGanetiException(Exception):
301   """Signal Ganeti that it must quit.
302
303   This is not necessarily an error (and thus not a subclass of
304   GenericError), but it's an exceptional circumstance and it is thus
305   treated. This instance should be instantiated with two values. The
306   first one will specify the return code to the caller, and the second
307   one will be the returned result (either as an error or as a normal
308   result). Usually only the leave cluster rpc call should return
309   status True (as there it's expected we quit), every other call will
310   return status False (as a critical error was encountered).
311
312   Examples::
313
314     # Return a result of "True" to the caller, but quit ganeti afterwards
315     raise QuitGanetiException(True, None)
316     # Send an error to the caller, and quit ganeti
317     raise QuitGanetiException(False, "Fatal safety violation, shutting down")
318
319   """
320
321
322 class JobQueueError(GenericError):
323   """Job queue error.
324
325   """
326
327
328 class JobQueueDrainError(JobQueueError):
329   """Job queue is marked for drain error.
330
331   This is raised when a job submission attempt is made but the queue
332   is marked for drain.
333
334   """
335
336
337 class JobQueueFull(JobQueueError):
338   """Job queue full error.
339
340   Raised when job queue size reached its hard limit.
341
342   """
343
344
345 class ConfdRequestError(GenericError):
346   """A request error in Ganeti confd.
347
348   Events that should make confd abort the current request and proceed serving
349   different ones.
350
351   """
352
353
354 class ConfdMagicError(GenericError):
355   """A magic fourcc error in Ganeti confd.
356
357   Errors processing the fourcc in ganeti confd datagrams.
358
359   """
360
361
362 class ConfdClientError(GenericError):
363   """A magic fourcc error in Ganeti confd.
364
365   Errors in the confd client library.
366
367   """
368
369
370 class UdpDataSizeError(GenericError):
371   """UDP payload too big.
372
373   """
374
375
376 class NoCtypesError(GenericError):
377   """python ctypes module is not found in the system.
378
379   """
380
381
382 class IPAddressError(GenericError):
383   """Generic IP address error.
384
385   """
386
387
388 class LuxiError(GenericError):
389   """LUXI error.
390
391   """
392
393
394 class QueryFilterParseError(ParseError):
395   """Error while parsing query filter.
396
397   """
398   def GetDetails(self):
399     """Returns a list of strings with details about the error.
400
401     """
402     try:
403       (_, inner) = self.args
404     except IndexError:
405       return None
406
407     return [str(inner.line),
408             (" " * (inner.column - 1)) + "^",
409             str(inner)]
410
411
412 # errors should be added above
413
414
415 def GetErrorClass(name):
416   """Return the class of an exception.
417
418   Given the class name, return the class itself.
419
420   @type name: str
421   @param name: the exception name
422   @rtype: class
423   @return: the actual class, or None if not found
424
425   """
426   item = globals().get(name, None)
427   if item is not None:
428     if not (isinstance(item, type(Exception)) and
429             issubclass(item, GenericError)):
430       item = None
431   return item
432
433
434 def EncodeException(err):
435   """Encodes an exception into a format that L{MaybeRaise} will recognise.
436
437   The passed L{err} argument will be formatted as a tuple (exception
438   name, arguments) that the MaybeRaise function will recognise.
439
440   @type err: GenericError child
441   @param err: usually a child of GenericError (but any exception
442       will be accepted)
443   @rtype: tuple
444   @return: tuple of (exception name, exception arguments)
445
446   """
447   return (err.__class__.__name__, err.args)
448
449
450 def GetEncodedError(result):
451   """If this looks like an encoded Ganeti exception, return it.
452
453   This function tries to parse the passed argument and if it looks
454   like an encoding done by EncodeException, it will return the class
455   object and arguments.
456
457   """
458   tlt = (tuple, list)
459   if (isinstance(result, tlt) and len(result) == 2 and
460       isinstance(result[1], tlt)):
461     # custom ganeti errors
462     errcls = GetErrorClass(result[0])
463     if errcls:
464       return (errcls, tuple(result[1]))
465
466   return None
467
468
469 def MaybeRaise(result):
470   """If this looks like an encoded Ganeti exception, raise it.
471
472   This function tries to parse the passed argument and if it looks
473   like an encoding done by EncodeException, it will re-raise it.
474
475   """
476   error = GetEncodedError(result)
477   if error:
478     (errcls, args) = error
479     raise errcls, args