(2.11) Make disk.name and disk.uuid available in bdev
[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
26 from ganeti import constants
27
28
29 ECODE_RESOLVER = constants.ERRORS_ECODE_RESOLVER
30 ECODE_NORES = constants.ERRORS_ECODE_NORES
31 ECODE_TEMP_NORES = constants.ERRORS_ECODE_TEMP_NORES
32 ECODE_INVAL = constants.ERRORS_ECODE_INVAL
33 ECODE_STATE = constants.ERRORS_ECODE_STATE
34 ECODE_NOENT = constants.ERRORS_ECODE_NOENT
35 ECODE_EXISTS = constants.ERRORS_ECODE_EXISTS
36 ECODE_NOTUNIQUE = constants.ERRORS_ECODE_NOTUNIQUE
37 ECODE_FAULT = constants.ERRORS_ECODE_FAULT
38 ECODE_ENVIRON = constants.ERRORS_ECODE_ENVIRON
39 ECODE_ALL = constants.ERRORS_ECODE_ALL
40
41
42 class GenericError(Exception):
43   """Base exception for Ganeti.
44
45   """
46
47
48 class LockError(GenericError):
49   """Lock error exception.
50
51   This signifies problems in the locking subsystem.
52
53   """
54
55
56 class PidFileLockError(LockError):
57   """PID file is already locked by another process.
58
59   """
60
61
62 class HypervisorError(GenericError):
63   """Hypervisor-related exception.
64
65   This is raised in case we can't communicate with the hypervisor
66   properly.
67
68   """
69
70
71 class HotplugError(HypervisorError):
72   """Hotplug-related exception.
73
74   This is raised in case a hotplug action fails or is not supported.
75   It is currently used only by KVM hypervisor.
76
77   """
78
79
80 class ProgrammerError(GenericError):
81   """Programming-related error.
82
83   This is raised in cases we determine that the calling conventions
84   have been violated, meaning we got some desynchronisation between
85   parts of our code. It signifies a real programming bug.
86
87   """
88
89
90 class BlockDeviceError(GenericError):
91   """Block-device related exception.
92
93   This is raised in case we can't setup the instance's block devices
94   properly.
95
96   """
97
98
99 class ConfigurationError(GenericError):
100   """Configuration related exception.
101
102   Things like having an instance with a primary node that doesn't
103   exist in the config or such raise this exception.
104
105   """
106
107
108 class ConfigVersionMismatch(ConfigurationError):
109   """Version mismatch in the configuration file.
110
111   The error has two arguments: the expected and the actual found
112   version.
113
114   """
115
116
117 class AddressPoolError(GenericError):
118   """Errors related to IP address pools.
119
120   """
121
122
123 class ReservationError(GenericError):
124   """Errors reserving a resource.
125
126   """
127
128
129 class RemoteError(GenericError):
130   """Programming-related error on remote call.
131
132   This is raised when an unhandled error occurs in a call to a
133   remote node.  It usually signifies a real programming bug.
134
135   """
136
137
138 class SignatureError(GenericError):
139   """Error authenticating a remote message.
140
141   This is raised when the hmac signature on a message doesn't verify correctly
142   to the message itself. It can happen because of network unreliability or
143   because of spurious traffic.
144
145   """
146
147
148 class ParameterError(GenericError):
149   """A passed parameter to a command is invalid.
150
151   This is raised when the parameter passed to a request function is
152   invalid. Correct code should have verified this before passing the
153   request structure.
154
155   The argument to this exception should be the parameter name.
156
157   """
158
159
160 class ResultValidationError(GenericError):
161   """The iallocation results fails validation.
162
163   """
164
165
166 class OpPrereqError(GenericError):
167   """Prerequisites for the OpCode are not fulfilled.
168
169   This exception has two arguments: an error message, and one of the
170   ECODE_* codes.
171
172   """
173
174
175 class OpExecError(GenericError):
176   """Error during OpCode execution.
177
178   """
179
180
181 class OpResultError(GenericError):
182   """Issue with OpCode result.
183
184   """
185
186
187 class DeviceCreationError(GenericError):
188   """Error during the creation of a device.
189
190   This exception should contain the list of the devices actually created
191   up to now, in the form of pairs (node, device)
192
193   """
194   def __init__(self, message, created_devices):
195     GenericError.__init__(self)
196     self.message = message
197     self.created_devices = created_devices
198
199   def __str__(self):
200     return self.message
201
202
203 class OpCodeUnknown(GenericError):
204   """Unknown opcode submitted.
205
206   This signifies a mismatch between the definitions on the client and
207   server side.
208
209   """
210
211
212 class JobLost(GenericError):
213   """Submitted job lost.
214
215   The job was submitted but it cannot be found in the current job
216   list.
217
218   """
219
220
221 class JobFileCorrupted(GenericError):
222   """Job file could not be properly decoded/restored.
223
224   """
225
226
227 class ResolverError(GenericError):
228   """Host name cannot be resolved.
229
230   This is not a normal situation for Ganeti, as we rely on having a
231   working resolver.
232
233   The non-resolvable hostname is available as the first element of the
234   args tuple; the other two elements of the tuple are the first two
235   args of the socket.gaierror exception (error code and description).
236
237   """
238
239
240 class HooksFailure(GenericError):
241   """A generic hook failure.
242
243   This signifies usually a setup misconfiguration.
244
245   """
246
247
248 class HooksAbort(HooksFailure):
249   """A required hook has failed.
250
251   This caused an abort of the operation in the initial phase. This
252   exception always has an attribute args which is a list of tuples of:
253     - node: the source node on which this hooks has failed
254     - script: the name of the script which aborted the run
255
256   """
257
258
259 class UnitParseError(GenericError):
260   """Unable to parse size unit.
261
262   """
263
264
265 class ParseError(GenericError):
266   """Generic parse error.
267
268   Raised when unable to parse user input.
269
270   """
271
272
273 class TypeEnforcementError(GenericError):
274   """Unable to enforce data type.
275
276   """
277
278
279 class X509CertError(GenericError):
280   """Invalid X509 certificate.
281
282   This error has two arguments: the certificate filename and the error cause.
283
284   """
285
286
287 class TagError(GenericError):
288   """Generic tag error.
289
290   The argument to this exception will show the exact error.
291
292   """
293
294
295 class CommandError(GenericError):
296   """External command error.
297
298   """
299
300
301 class StorageError(GenericError):
302   """Storage-related exception.
303
304   """
305
306
307 class InotifyError(GenericError):
308   """Error raised when there is a failure setting up an inotify watcher.
309
310   """
311
312
313 class QuitGanetiException(Exception):
314   """Signal Ganeti that it must quit.
315
316   This is not necessarily an error (and thus not a subclass of
317   GenericError), but it's an exceptional circumstance and it is thus
318   treated. This exception should be instantiated with two values. The
319   first one will specify the return code to the caller, and the second
320   one will be the returned result (either as an error or as a normal
321   result). Usually only the leave cluster rpc call should return
322   status True (as there it's expected we quit), every other call will
323   return status False (as a critical error was encountered).
324
325   Examples::
326
327     # Return a result of "True" to the caller, but quit ganeti afterwards
328     raise QuitGanetiException(True, None)
329     # Send an error to the caller, and quit ganeti
330     raise QuitGanetiException(False, "Fatal safety violation, shutting down")
331
332   """
333
334
335 class JobQueueError(GenericError):
336   """Job queue error.
337
338   """
339
340
341 class JobQueueDrainError(JobQueueError):
342   """Job queue is marked for drain error.
343
344   This is raised when a job submission attempt is made but the queue
345   is marked for drain.
346
347   """
348
349
350 class JobQueueFull(JobQueueError):
351   """Job queue full error.
352
353   Raised when job queue size reached its hard limit.
354
355   """
356
357
358 class ConfdMagicError(GenericError):
359   """A magic fourcc error in Ganeti confd.
360
361   Errors processing the fourcc in ganeti confd datagrams.
362
363   """
364
365
366 class ConfdClientError(GenericError):
367   """A magic fourcc error in Ganeti confd.
368
369   Errors in the confd client library.
370
371   """
372
373
374 class UdpDataSizeError(GenericError):
375   """UDP payload too big.
376
377   """
378
379
380 class NoCtypesError(GenericError):
381   """python ctypes module is not found in the system.
382
383   """
384
385
386 class IPAddressError(GenericError):
387   """Generic IP address error.
388
389   """
390
391
392 class LuxiError(GenericError):
393   """LUXI error.
394
395   """
396
397
398 class QueryFilterParseError(ParseError):
399   """Error while parsing query filter.
400
401   This exception must be instantiated with two values. The first one is a
402   string with an error description, the second one is an instance of a subclass
403   of C{pyparsing.ParseBaseException} (used to display the exact error
404   location).
405
406   """
407   def GetDetails(self):
408     """Returns a list of strings with details about the error.
409
410     """
411     try:
412       (_, inner) = self.args
413     except IndexError:
414       return None
415
416     return [str(inner.line),
417             (" " * (inner.column - 1)) + "^",
418             str(inner)]
419
420
421 class RapiTestResult(GenericError):
422   """Exception containing results from RAPI test utilities.
423
424   """
425
426
427 class FileStoragePathError(GenericError):
428   """Error from file storage path validation.
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)