constants: Stop using wildcard import for pathutils
[ganeti-local] / lib / errors.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 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
65
66 class LVMError(GenericError):
67   """LVM-related exception.
68
69   This exception codifies problems with LVM setup.
70
71   """
72
73
74 class LockError(GenericError):
75   """Lock error exception.
76
77   This signifies problems in the locking subsystem.
78
79   """
80
81
82 class PidFileLockError(LockError):
83   """PID file is already locked by another process.
84
85   """
86
87
88 class HypervisorError(GenericError):
89   """Hypervisor-related exception.
90
91   This is raised in case we can't communicate with the hypervisor
92   properly.
93
94   """
95
96
97 class ProgrammerError(GenericError):
98   """Programming-related error.
99
100   This is raised in cases we determine that the calling conventions
101   have been violated, meaning we got some desynchronisation between
102   parts of our code. It signifies a real programming bug.
103
104   """
105
106
107 class BlockDeviceError(GenericError):
108   """Block-device related exception.
109
110   This is raised in case we can't setup the instance's block devices
111   properly.
112
113   """
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
124
125 class ConfigVersionMismatch(ConfigurationError):
126   """Version mismatch in the configuration file.
127
128   The error has two arguments: the expected and the actual found
129   version.
130
131   """
132
133
134 class ReservationError(GenericError):
135   """Errors reserving a resource.
136
137   """
138
139
140 class RemoteError(GenericError):
141   """Programming-related error on remote call.
142
143   This is raised when an unhandled error occurs in a call to a
144   remote node.  It usually signifies a real programming bug.
145
146   """
147
148
149 class SignatureError(GenericError):
150   """Error authenticating a remote message.
151
152   This is raised when the hmac signature on a message doesn't verify correctly
153   to the message itself. It can happen because of network unreliability or
154   because of spurious traffic.
155
156   """
157
158
159 class ParameterError(GenericError):
160   """A passed parameter to a command is invalid.
161
162   This is raised when the parameter passed to a request function is
163   invalid. Correct code should have verified this before passing the
164   request structure.
165
166   The argument to this exception should be the parameter name.
167
168   """
169
170
171 class ResultValidationError(GenericError):
172   """The iallocation results fails validation.
173
174   """
175
176
177 class OpPrereqError(GenericError):
178   """Prerequisites for the OpCode are not fulfilled.
179
180   This exception will have either one or two arguments. For the
181   two-argument construction, the second argument should be one of the
182   ECODE_* codes.
183
184   """
185
186
187 class OpExecError(GenericError):
188   """Error during OpCode execution.
189
190   """
191
192
193 class OpResultError(GenericError):
194   """Issue with OpCode result.
195
196   """
197
198
199 class OpCodeUnknown(GenericError):
200   """Unknown opcode submitted.
201
202   This signifies a mismatch between the definitions on the client and
203   server side.
204
205   """
206
207
208 class JobLost(GenericError):
209   """Submitted job lost.
210
211   The job was submitted but it cannot be found in the current job
212   list.
213
214   """
215
216
217 class JobFileCorrupted(GenericError):
218   """Job file could not be properly decoded/restored.
219
220   """
221
222
223 class ResolverError(GenericError):
224   """Host name cannot be resolved.
225
226   This is not a normal situation for Ganeti, as we rely on having a
227   working resolver.
228
229   The non-resolvable hostname is available as the first element of the
230   args tuple; the other two elements of the tuple are the first two
231   args of the socket.gaierror exception (error code and description).
232
233   """
234
235
236 class HooksFailure(GenericError):
237   """A generic hook failure.
238
239   This signifies usually a setup misconfiguration.
240
241   """
242
243
244 class HooksAbort(HooksFailure):
245   """A required hook has failed.
246
247   This caused an abort of the operation in the initial phase. This
248   exception always has an attribute args which is a list of tuples of:
249     - node: the source node on which this hooks has failed
250     - script: the name of the script which aborted the run
251
252   """
253
254
255 class UnitParseError(GenericError):
256   """Unable to parse size unit.
257
258   """
259
260
261 class ParseError(GenericError):
262   """Generic parse error.
263
264   Raised when unable to parse user input.
265
266   """
267
268
269 class TypeEnforcementError(GenericError):
270   """Unable to enforce data type.
271
272   """
273
274
275 class SshKeyError(GenericError):
276   """Invalid SSH key.
277
278   """
279
280
281 class X509CertError(GenericError):
282   """Invalid X509 certificate.
283
284   This error has two arguments: the certificate filename and the error cause.
285
286   """
287
288
289 class TagError(GenericError):
290   """Generic tag error.
291
292   The argument to this exception will show the exact error.
293
294   """
295
296
297 class CommandError(GenericError):
298   """External command error.
299
300   """
301
302
303 class StorageError(GenericError):
304   """Storage-related exception.
305
306   """
307
308
309 class InotifyError(GenericError):
310   """Error raised when there is a failure setting up an inotify watcher.
311
312   """
313
314
315 class QuitGanetiException(Exception):
316   """Signal Ganeti that it must quit.
317
318   This is not necessarily an error (and thus not a subclass of
319   GenericError), but it's an exceptional circumstance and it is thus
320   treated. This instance should be instantiated with two values. The
321   first one will specify the return code to the caller, and the second
322   one will be the returned result (either as an error or as a normal
323   result). Usually only the leave cluster rpc call should return
324   status True (as there it's expected we quit), every other call will
325   return status False (as a critical error was encountered).
326
327   Examples::
328
329     # Return a result of "True" to the caller, but quit ganeti afterwards
330     raise QuitGanetiException(True, None)
331     # Send an error to the caller, and quit ganeti
332     raise QuitGanetiException(False, "Fatal safety violation, shutting down")
333
334   """
335
336
337 class JobQueueError(GenericError):
338   """Job queue error.
339
340   """
341
342
343 class JobQueueDrainError(JobQueueError):
344   """Job queue is marked for drain error.
345
346   This is raised when a job submission attempt is made but the queue
347   is marked for drain.
348
349   """
350
351
352 class JobQueueFull(JobQueueError):
353   """Job queue full error.
354
355   Raised when job queue size reached its hard limit.
356
357   """
358
359
360 class ConfdRequestError(GenericError):
361   """A request error in Ganeti confd.
362
363   Events that should make confd abort the current request and proceed serving
364   different ones.
365
366   """
367
368
369 class ConfdMagicError(GenericError):
370   """A magic fourcc error in Ganeti confd.
371
372   Errors processing the fourcc in ganeti confd datagrams.
373
374   """
375
376
377 class ConfdClientError(GenericError):
378   """A magic fourcc error in Ganeti confd.
379
380   Errors in the confd client library.
381
382   """
383
384
385 class UdpDataSizeError(GenericError):
386   """UDP payload too big.
387
388   """
389
390
391 class NoCtypesError(GenericError):
392   """python ctypes module is not found in the system.
393
394   """
395
396
397 class IPAddressError(GenericError):
398   """Generic IP address error.
399
400   """
401
402
403 class LuxiError(GenericError):
404   """LUXI error.
405
406   """
407
408
409 class QueryFilterParseError(ParseError):
410   """Error while parsing query filter.
411
412   """
413   def GetDetails(self):
414     """Returns a list of strings with details about the error.
415
416     """
417     try:
418       (_, inner) = self.args
419     except IndexError:
420       return None
421
422     return [str(inner.line),
423             (" " * (inner.column - 1)) + "^",
424             str(inner)]
425
426
427 class RapiTestResult(GenericError):
428   """Exception containing results from RAPI test utilities.
429
430   """
431
432
433 # errors should be added above
434
435
436 def GetErrorClass(name):
437   """Return the class of an exception.
438
439   Given the class name, return the class itself.
440
441   @type name: str
442   @param name: the exception name
443   @rtype: class
444   @return: the actual class, or None if not found
445
446   """
447   item = globals().get(name, None)
448   if item is not None:
449     if not (isinstance(item, type(Exception)) and
450             issubclass(item, GenericError)):
451       item = None
452   return item
453
454
455 def EncodeException(err):
456   """Encodes an exception into a format that L{MaybeRaise} will recognise.
457
458   The passed L{err} argument will be formatted as a tuple (exception
459   name, arguments) that the MaybeRaise function will recognise.
460
461   @type err: GenericError child
462   @param err: usually a child of GenericError (but any exception
463       will be accepted)
464   @rtype: tuple
465   @return: tuple of (exception name, exception arguments)
466
467   """
468   return (err.__class__.__name__, err.args)
469
470
471 def GetEncodedError(result):
472   """If this looks like an encoded Ganeti exception, return it.
473
474   This function tries to parse the passed argument and if it looks
475   like an encoding done by EncodeException, it will return the class
476   object and arguments.
477
478   """
479   tlt = (tuple, list)
480   if (isinstance(result, tlt) and len(result) == 2 and
481       isinstance(result[1], tlt)):
482     # custom ganeti errors
483     errcls = GetErrorClass(result[0])
484     if errcls:
485       return (errcls, tuple(result[1]))
486
487   return None
488
489
490 def MaybeRaise(result):
491   """If this looks like an encoded Ganeti exception, raise it.
492
493   This function tries to parse the passed argument and if it looks
494   like an encoding done by EncodeException, it will re-raise it.
495
496   """
497   error = GetEncodedError(result)
498   if error:
499     (errcls, args) = error
500     # pylint: disable=W0142
501     raise errcls(*args)