4 # Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Google Inc.
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.
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.
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
22 """Ganeti exception handling.
26 from ganeti import constants
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
42 class GenericError(Exception):
43 """Base exception for Ganeti.
48 class LockError(GenericError):
49 """Lock error exception.
51 This signifies problems in the locking subsystem.
56 class PidFileLockError(LockError):
57 """PID file is already locked by another process.
62 class HypervisorError(GenericError):
63 """Hypervisor-related exception.
65 This is raised in case we can't communicate with the hypervisor
71 class HotplugError(HypervisorError):
72 """Hotplug-related exception.
74 This is raised in case a hotplug action fails or is not supported.
75 It is currently used only by KVM hypervisor.
80 class ProgrammerError(GenericError):
81 """Programming-related error.
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.
90 class BlockDeviceError(GenericError):
91 """Block-device related exception.
93 This is raised in case we can't setup the instance's block devices
99 class ConfigurationError(GenericError):
100 """Configuration related exception.
102 Things like having an instance with a primary node that doesn't
103 exist in the config or such raise this exception.
108 class ConfigVersionMismatch(ConfigurationError):
109 """Version mismatch in the configuration file.
111 The error has two arguments: the expected and the actual found
117 class AddressPoolError(GenericError):
118 """Errors related to IP address pools.
123 class ReservationError(GenericError):
124 """Errors reserving a resource.
129 class RemoteError(GenericError):
130 """Programming-related error on remote call.
132 This is raised when an unhandled error occurs in a call to a
133 remote node. It usually signifies a real programming bug.
138 class SignatureError(GenericError):
139 """Error authenticating a remote message.
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.
148 class ParameterError(GenericError):
149 """A passed parameter to a command is invalid.
151 This is raised when the parameter passed to a request function is
152 invalid. Correct code should have verified this before passing the
155 The argument to this exception should be the parameter name.
160 class ResultValidationError(GenericError):
161 """The iallocation results fails validation.
166 class OpPrereqError(GenericError):
167 """Prerequisites for the OpCode are not fulfilled.
169 This exception has two arguments: an error message, and one of the
175 class OpExecError(GenericError):
176 """Error during OpCode execution.
181 class OpResultError(GenericError):
182 """Issue with OpCode result.
187 class DeviceCreationError(GenericError):
188 """Error during the creation of a device.
190 This exception should contain the list of the devices actually created
191 up to now, in the form of pairs (node, device)
194 def __init__(self, message, created_devices):
195 GenericError.__init__(self)
196 self.message = message
197 self.created_devices = created_devices
203 class OpCodeUnknown(GenericError):
204 """Unknown opcode submitted.
206 This signifies a mismatch between the definitions on the client and
212 class JobLost(GenericError):
213 """Submitted job lost.
215 The job was submitted but it cannot be found in the current job
221 class JobFileCorrupted(GenericError):
222 """Job file could not be properly decoded/restored.
227 class ResolverError(GenericError):
228 """Host name cannot be resolved.
230 This is not a normal situation for Ganeti, as we rely on having a
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).
240 class HooksFailure(GenericError):
241 """A generic hook failure.
243 This signifies usually a setup misconfiguration.
248 class HooksAbort(HooksFailure):
249 """A required hook has failed.
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
259 class UnitParseError(GenericError):
260 """Unable to parse size unit.
265 class ParseError(GenericError):
266 """Generic parse error.
268 Raised when unable to parse user input.
273 class TypeEnforcementError(GenericError):
274 """Unable to enforce data type.
279 class X509CertError(GenericError):
280 """Invalid X509 certificate.
282 This error has two arguments: the certificate filename and the error cause.
287 class TagError(GenericError):
288 """Generic tag error.
290 The argument to this exception will show the exact error.
295 class CommandError(GenericError):
296 """External command error.
301 class StorageError(GenericError):
302 """Storage-related exception.
307 class InotifyError(GenericError):
308 """Error raised when there is a failure setting up an inotify watcher.
313 class QuitGanetiException(Exception):
314 """Signal Ganeti that it must quit.
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).
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")
335 class JobQueueError(GenericError):
341 class JobQueueDrainError(JobQueueError):
342 """Job queue is marked for drain error.
344 This is raised when a job submission attempt is made but the queue
350 class JobQueueFull(JobQueueError):
351 """Job queue full error.
353 Raised when job queue size reached its hard limit.
358 class ConfdMagicError(GenericError):
359 """A magic fourcc error in Ganeti confd.
361 Errors processing the fourcc in ganeti confd datagrams.
366 class ConfdClientError(GenericError):
367 """A magic fourcc error in Ganeti confd.
369 Errors in the confd client library.
374 class UdpDataSizeError(GenericError):
375 """UDP payload too big.
380 class NoCtypesError(GenericError):
381 """python ctypes module is not found in the system.
386 class IPAddressError(GenericError):
387 """Generic IP address error.
392 class LuxiError(GenericError):
398 class QueryFilterParseError(ParseError):
399 """Error while parsing query filter.
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
407 def GetDetails(self):
408 """Returns a list of strings with details about the error.
412 (_, inner) = self.args
416 return [str(inner.line),
417 (" " * (inner.column - 1)) + "^",
421 class RapiTestResult(GenericError):
422 """Exception containing results from RAPI test utilities.
427 class FileStoragePathError(GenericError):
428 """Error from file storage path validation.
433 # errors should be added above
436 def GetErrorClass(name):
437 """Return the class of an exception.
439 Given the class name, return the class itself.
442 @param name: the exception name
444 @return: the actual class, or None if not found
447 item = globals().get(name, None)
449 if not (isinstance(item, type(Exception)) and
450 issubclass(item, GenericError)):
455 def EncodeException(err):
456 """Encodes an exception into a format that L{MaybeRaise} will recognise.
458 The passed L{err} argument will be formatted as a tuple (exception
459 name, arguments) that the MaybeRaise function will recognise.
461 @type err: GenericError child
462 @param err: usually a child of GenericError (but any exception
465 @return: tuple of (exception name, exception arguments)
468 return (err.__class__.__name__, err.args)
471 def GetEncodedError(result):
472 """If this looks like an encoded Ganeti exception, return it.
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.
480 if (isinstance(result, tlt) and len(result) == 2 and
481 isinstance(result[1], tlt)):
482 # custom ganeti errors
483 errcls = GetErrorClass(result[0])
485 return (errcls, tuple(result[1]))
490 def MaybeRaise(result):
491 """If this looks like an encoded Ganeti exception, raise it.
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.
497 error = GetEncodedError(result)
499 (errcls, args) = error
500 # pylint: disable=W0142