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