Update gitignore rules
[ganeti-local] / lib / errors.py
1 #
2 #
3
4 # Copyright (C) 2006, 2007 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
47 class GenericError(Exception):
48   """Base exception for Ganeti.
49
50   """
51   pass
52
53
54 class LVMError(GenericError):
55   """LVM-related exception.
56
57   This exception codifies problems with LVM setup.
58
59   """
60   pass
61
62
63 class LockError(GenericError):
64   """Lock error exception.
65
66   This signifies problems in the locking subsystem.
67
68   """
69   pass
70
71
72 class HypervisorError(GenericError):
73   """Hypervisor-related exception.
74
75   This is raised in case we can't communicate with the hypervisor
76   properly.
77
78   """
79   pass
80
81
82 class ProgrammerError(GenericError):
83   """Programming-related error.
84
85   This is raised in cases we determine that the calling conventions
86   have been violated, meaning we got some desynchronisation between
87   parts of our code. It signifies a real programming bug.
88
89   """
90   pass
91
92
93 class BlockDeviceError(GenericError):
94   """Block-device related exception.
95
96   This is raised in case we can't setup the instance's block devices
97   properly.
98
99   """
100   pass
101
102
103 class ConfigurationError(GenericError):
104   """Configuration related exception.
105
106   Things like having an instance with a primary node that doesn't
107   exist in the config or such raise this exception.
108
109   """
110   pass
111
112
113 class RemoteError(GenericError):
114   """Programming-related error on remote call.
115
116   This is raised when an unhandled error occurs in a call to a
117   remote node.  It usually signifies a real programming bug.
118
119   """
120   pass
121
122
123 class SignatureError(GenericError):
124   """Error authenticating a remote message.
125
126   This is raised when the hmac signature on a message doesn't verify correctly
127   to the message itself. It can happen because of network unreliability or
128   because of spurious traffic.
129
130   """
131   pass
132
133
134 class ParameterError(GenericError):
135   """A passed parameter to a command is invalid.
136
137   This is raised when the parameter passed to a request function is
138   invalid. Correct code should have verified this before passing the
139   request structure.
140
141   The argument to this exception should be the parameter name.
142
143   """
144   pass
145
146
147 class OpPrereqError(GenericError):
148   """Prerequisites for the OpCode are not fulfilled.
149
150   This exception will have either one or two arguments. For the
151   two-argument construction, the second argument should be one of the
152   ECODE_* codes.
153
154   """
155
156
157 class OpExecError(GenericError):
158   """Error during OpCode execution.
159
160   """
161
162
163 class OpCodeUnknown(GenericError):
164   """Unknown opcode submitted.
165
166   This signifies a mismatch between the definitions on the client and
167   server side.
168
169   """
170
171
172 class JobLost(GenericError):
173   """Submitted job lost.
174
175   The job was submitted but it cannot be found in the current job
176   list.
177
178   """
179
180
181 class ResolverError(GenericError):
182   """Host name cannot be resolved.
183
184   This is not a normal situation for Ganeti, as we rely on having a
185   working resolver.
186
187   The non-resolvable hostname is available as the first element of the
188   args tuple; the other two elements of the tuple are the first two
189   args of the socket.gaierror exception (error code and description).
190
191   """
192
193
194 class HooksFailure(GenericError):
195   """A generic hook failure.
196
197   This signifies usually a setup misconfiguration.
198
199   """
200
201
202 class HooksAbort(HooksFailure):
203   """A required hook has failed.
204
205   This caused an abort of the operation in the initial phase. This
206   exception always has an attribute args which is a list of tuples of:
207     - node: the source node on which this hooks has failed
208     - script: the name of the script which aborted the run
209
210   """
211
212
213 class UnitParseError(GenericError):
214   """Unable to parse size unit.
215
216   """
217
218
219 class TypeEnforcementError(GenericError):
220   """Unable to enforce data type.
221
222   """
223
224
225 class SshKeyError(GenericError):
226   """Invalid SSH key.
227
228   """
229
230
231 class TagError(GenericError):
232   """Generic tag error.
233
234   The argument to this exception will show the exact error.
235
236   """
237
238
239 class CommandError(GenericError):
240   """External command error.
241
242   """
243
244
245 class StorageError(GenericError):
246   """Storage-related exception.
247
248   """
249
250
251 class InotifyError(GenericError):
252   """Error raised when there is a failure setting up an inotify watcher.
253
254   """
255
256
257 class QuitGanetiException(Exception):
258   """Signal that Ganeti that it must quit.
259
260   This is not necessarily an error (and thus not a subclass of
261   GenericError), but it's an exceptional circumstance and it is thus
262   treated. This instance should be instantiated with two values. The
263   first one will specify the return code to the caller, and the second
264   one will be the returned result (either as an error or as a normal
265   result). Usually only the leave cluster rpc call should return
266   status True (as there it's expected we quit), every other call will
267   return status False (as a critical error was encountered).
268
269   Examples::
270
271     # Return a result of "True" to the caller, but quit ganeti afterwards
272     raise QuitGanetiException(True, None)
273     # Send an error to the caller, and quit ganeti
274     raise QuitGanetiException(False, "Fatal safety violation, shutting down")
275
276   """
277
278
279 class JobQueueError(GenericError):
280   """Job queue error.
281
282   """
283
284
285 class JobQueueDrainError(JobQueueError):
286   """Job queue is marked for drain error.
287
288   This is raised when a job submission attempt is made but the queue
289   is marked for drain.
290
291   """
292
293
294 class JobQueueFull(JobQueueError):
295   """Job queue full error.
296
297   Raised when job queue size reached its hard limit.
298
299   """
300
301
302 class ConfdFatalError(GenericError):
303   """A fatal failure in Ganeti confd.
304
305   Events that compromise the ability of confd to proceed further.
306   (for example: inability to load the config file)
307
308   """
309
310
311 class ConfdRequestError(GenericError):
312   """A request error in Ganeti confd.
313
314   Events that should make confd abort the current request and proceed serving
315   different ones.
316
317   """
318
319
320 class ConfdMagicError(GenericError):
321   """A magic fourcc error in Ganeti confd.
322
323   Errors processing the fourcc in ganeti confd datagrams.
324
325   """
326
327
328 class ConfdClientError(GenericError):
329   """A magic fourcc error in Ganeti confd.
330
331   Errors in the confd client library.
332
333   """
334
335
336 class UdpDataSizeError(GenericError):
337   """UDP payload too big.
338
339   """
340
341
342 # errors should be added above
343
344
345 def GetErrorClass(name):
346   """Return the class of an exception.
347
348   Given the class name, return the class itself.
349
350   @type name: str
351   @param name: the exception name
352   @rtype: class
353   @return: the actual class, or None if not found
354
355   """
356   item = globals().get(name, None)
357   if item is not None:
358     if not (isinstance(item, type(Exception)) and
359             issubclass(item, GenericError)):
360       item = None
361   return item
362
363
364 def EncodeException(err):
365   """Encodes an exception into a format that L{MaybeRaise} will recognise.
366
367   The passed L{err} argument will be formatted as a tuple (exception
368   name, arguments) that the MaybeRaise function will recognise.
369
370   @type err: GenericError child
371   @param err: usually a child of GenericError (but any exception
372       will be accepted)
373   @rtype: tuple
374   @return: tuple of (exception name, exception arguments)
375
376   """
377   return (err.__class__.__name__, err.args)
378
379
380 def MaybeRaise(result):
381   """Is this looks like an encoded Ganeti exception, raise it.
382
383   This function tries to parse the passed argument and if it looks
384   like an encoding done by EncodeException, it will re-raise it.
385
386   """
387   tlt = (tuple, list)
388   if (isinstance(result, tlt) and len(result) == 2 and
389       isinstance(result[1], tlt)):
390     # custom ganeti errors
391     err_class = GetErrorClass(result[0])
392     if err_class is not None:
393       raise err_class, tuple(result[1])