Statistics
| Branch: | Tag: | Revision:

root / lib / errors.py @ e2dd6ece

History | View | Annotate | Download (10.7 kB)

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

    
30
#: Not enough resources (iallocator failure, disk space, memory, etc.)
31
ECODE_NORES = "insufficient_resources"
32

    
33
#: Temporarily out of resources; operation can be tried again
34
ECODE_TEMP_NORES = "temp_insufficient_resources"
35

    
36
#: Wrong arguments (at syntax level)
37
ECODE_INVAL = "wrong_input"
38

    
39
#: Wrong entity state
40
ECODE_STATE = "wrong_state"
41

    
42
#: Entity not found
43
ECODE_NOENT = "unknown_entity"
44

    
45
#: Entity already exists
46
ECODE_EXISTS = "already_exists"
47

    
48
#: Resource not unique (e.g. MAC or IP duplication)
49
ECODE_NOTUNIQUE = "resource_not_unique"
50

    
51
#: Internal cluster error
52
ECODE_FAULT = "internal_error"
53

    
54
#: Environment error (e.g. node disk error)
55
ECODE_ENVIRON = "environment_error"
56

    
57
#: List of all failure types
58
ECODE_ALL = frozenset([
59
  ECODE_RESOLVER,
60
  ECODE_NORES,
61
  ECODE_TEMP_NORES,
62
  ECODE_INVAL,
63
  ECODE_STATE,
64
  ECODE_NOENT,
65
  ECODE_EXISTS,
66
  ECODE_NOTUNIQUE,
67
  ECODE_FAULT,
68
  ECODE_ENVIRON,
69
  ])
70

    
71

    
72
class GenericError(Exception):
73
  """Base exception for Ganeti.
74

75
  """
76

    
77

    
78
class LockError(GenericError):
79
  """Lock error exception.
80

81
  This signifies problems in the locking subsystem.
82

83
  """
84

    
85

    
86
class PidFileLockError(LockError):
87
  """PID file is already locked by another process.
88

89
  """
90

    
91

    
92
class HypervisorError(GenericError):
93
  """Hypervisor-related exception.
94

95
  This is raised in case we can't communicate with the hypervisor
96
  properly.
97

98
  """
99

    
100

    
101
class ProgrammerError(GenericError):
102
  """Programming-related error.
103

104
  This is raised in cases we determine that the calling conventions
105
  have been violated, meaning we got some desynchronisation between
106
  parts of our code. It signifies a real programming bug.
107

108
  """
109

    
110

    
111
class BlockDeviceError(GenericError):
112
  """Block-device related exception.
113

114
  This is raised in case we can't setup the instance's block devices
115
  properly.
116

117
  """
118

    
119

    
120
class ConfigurationError(GenericError):
121
  """Configuration related exception.
122

123
  Things like having an instance with a primary node that doesn't
124
  exist in the config or such raise this exception.
125

126
  """
127

    
128

    
129
class ConfigVersionMismatch(ConfigurationError):
130
  """Version mismatch in the configuration file.
131

132
  The error has two arguments: the expected and the actual found
133
  version.
134

135
  """
136

    
137

    
138
class AddressPoolError(GenericError):
139
  """Errors related to IP address pools.
140

141
  """
142

    
143

    
144
class ReservationError(GenericError):
145
  """Errors reserving a resource.
146

147
  """
148

    
149

    
150
class RemoteError(GenericError):
151
  """Programming-related error on remote call.
152

153
  This is raised when an unhandled error occurs in a call to a
154
  remote node.  It usually signifies a real programming bug.
155

156
  """
157

    
158

    
159
class SignatureError(GenericError):
160
  """Error authenticating a remote message.
161

162
  This is raised when the hmac signature on a message doesn't verify correctly
163
  to the message itself. It can happen because of network unreliability or
164
  because of spurious traffic.
165

166
  """
167

    
168

    
169
class ParameterError(GenericError):
170
  """A passed parameter to a command is invalid.
171

172
  This is raised when the parameter passed to a request function is
173
  invalid. Correct code should have verified this before passing the
174
  request structure.
175

176
  The argument to this exception should be the parameter name.
177

178
  """
179

    
180

    
181
class ResultValidationError(GenericError):
182
  """The iallocation results fails validation.
183

184
  """
185

    
186

    
187
class OpPrereqError(GenericError):
188
  """Prerequisites for the OpCode are not fulfilled.
189

190
  This exception has two arguments: an error message, and one of the
191
  ECODE_* codes.
192

193
  """
194

    
195

    
196
class OpExecError(GenericError):
197
  """Error during OpCode execution.
198

199
  """
200

    
201

    
202
class OpResultError(GenericError):
203
  """Issue with OpCode result.
204

205
  """
206

    
207

    
208
class OpCodeUnknown(GenericError):
209
  """Unknown opcode submitted.
210

211
  This signifies a mismatch between the definitions on the client and
212
  server side.
213

214
  """
215

    
216

    
217
class JobLost(GenericError):
218
  """Submitted job lost.
219

220
  The job was submitted but it cannot be found in the current job
221
  list.
222

223
  """
224

    
225

    
226
class JobFileCorrupted(GenericError):
227
  """Job file could not be properly decoded/restored.
228

229
  """
230

    
231

    
232
class ResolverError(GenericError):
233
  """Host name cannot be resolved.
234

235
  This is not a normal situation for Ganeti, as we rely on having a
236
  working resolver.
237

238
  The non-resolvable hostname is available as the first element of the
239
  args tuple; the other two elements of the tuple are the first two
240
  args of the socket.gaierror exception (error code and description).
241

242
  """
243

    
244

    
245
class HooksFailure(GenericError):
246
  """A generic hook failure.
247

248
  This signifies usually a setup misconfiguration.
249

250
  """
251

    
252

    
253
class HooksAbort(HooksFailure):
254
  """A required hook has failed.
255

256
  This caused an abort of the operation in the initial phase. This
257
  exception always has an attribute args which is a list of tuples of:
258
    - node: the source node on which this hooks has failed
259
    - script: the name of the script which aborted the run
260

261
  """
262

    
263

    
264
class UnitParseError(GenericError):
265
  """Unable to parse size unit.
266

267
  """
268

    
269

    
270
class ParseError(GenericError):
271
  """Generic parse error.
272

273
  Raised when unable to parse user input.
274

275
  """
276

    
277

    
278
class TypeEnforcementError(GenericError):
279
  """Unable to enforce data type.
280

281
  """
282

    
283

    
284
class X509CertError(GenericError):
285
  """Invalid X509 certificate.
286

287
  This error has two arguments: the certificate filename and the error cause.
288

289
  """
290

    
291

    
292
class TagError(GenericError):
293
  """Generic tag error.
294

295
  The argument to this exception will show the exact error.
296

297
  """
298

    
299

    
300
class CommandError(GenericError):
301
  """External command error.
302

303
  """
304

    
305

    
306
class StorageError(GenericError):
307
  """Storage-related exception.
308

309
  """
310

    
311

    
312
class InotifyError(GenericError):
313
  """Error raised when there is a failure setting up an inotify watcher.
314

315
  """
316

    
317

    
318
class QuitGanetiException(Exception):
319
  """Signal Ganeti that it must quit.
320

321
  This is not necessarily an error (and thus not a subclass of
322
  GenericError), but it's an exceptional circumstance and it is thus
323
  treated. This exception should be instantiated with two values. The
324
  first one will specify the return code to the caller, and the second
325
  one will be the returned result (either as an error or as a normal
326
  result). Usually only the leave cluster rpc call should return
327
  status True (as there it's expected we quit), every other call will
328
  return status False (as a critical error was encountered).
329

330
  Examples::
331

332
    # Return a result of "True" to the caller, but quit ganeti afterwards
333
    raise QuitGanetiException(True, None)
334
    # Send an error to the caller, and quit ganeti
335
    raise QuitGanetiException(False, "Fatal safety violation, shutting down")
336

337
  """
338

    
339

    
340
class JobQueueError(GenericError):
341
  """Job queue error.
342

343
  """
344

    
345

    
346
class JobQueueDrainError(JobQueueError):
347
  """Job queue is marked for drain error.
348

349
  This is raised when a job submission attempt is made but the queue
350
  is marked for drain.
351

352
  """
353

    
354

    
355
class JobQueueFull(JobQueueError):
356
  """Job queue full error.
357

358
  Raised when job queue size reached its hard limit.
359

360
  """
361

    
362

    
363
class ConfdMagicError(GenericError):
364
  """A magic fourcc error in Ganeti confd.
365

366
  Errors processing the fourcc in ganeti confd datagrams.
367

368
  """
369

    
370

    
371
class ConfdClientError(GenericError):
372
  """A magic fourcc error in Ganeti confd.
373

374
  Errors in the confd client library.
375

376
  """
377

    
378

    
379
class UdpDataSizeError(GenericError):
380
  """UDP payload too big.
381

382
  """
383

    
384

    
385
class NoCtypesError(GenericError):
386
  """python ctypes module is not found in the system.
387

388
  """
389

    
390

    
391
class IPAddressError(GenericError):
392
  """Generic IP address error.
393

394
  """
395

    
396

    
397
class LuxiError(GenericError):
398
  """LUXI error.
399

400
  """
401

    
402

    
403
class QueryFilterParseError(ParseError):
404
  """Error while parsing query filter.
405

406
  This exception must be instantiated with two values. The first one is a
407
  string with an error description, the second one is an instance of a subclass
408
  of C{pyparsing.ParseBaseException} (used to display the exact error
409
  location).
410

411
  """
412
  def GetDetails(self):
413
    """Returns a list of strings with details about the error.
414

415
    """
416
    try:
417
      (_, inner) = self.args
418
    except IndexError:
419
      return None
420

    
421
    return [str(inner.line),
422
            (" " * (inner.column - 1)) + "^",
423
            str(inner)]
424

    
425

    
426
class RapiTestResult(GenericError):
427
  """Exception containing results from RAPI test utilities.
428

429
  """
430

    
431

    
432
class FileStoragePathError(GenericError):
433
  """Error from file storage path validation.
434

435
  """
436

    
437

    
438
# errors should be added above
439

    
440

    
441
def GetErrorClass(name):
442
  """Return the class of an exception.
443

444
  Given the class name, return the class itself.
445

446
  @type name: str
447
  @param name: the exception name
448
  @rtype: class
449
  @return: the actual class, or None if not found
450

451
  """
452
  item = globals().get(name, None)
453
  if item is not None:
454
    if not (isinstance(item, type(Exception)) and
455
            issubclass(item, GenericError)):
456
      item = None
457
  return item
458

    
459

    
460
def EncodeException(err):
461
  """Encodes an exception into a format that L{MaybeRaise} will recognise.
462

463
  The passed L{err} argument will be formatted as a tuple (exception
464
  name, arguments) that the MaybeRaise function will recognise.
465

466
  @type err: GenericError child
467
  @param err: usually a child of GenericError (but any exception
468
      will be accepted)
469
  @rtype: tuple
470
  @return: tuple of (exception name, exception arguments)
471

472
  """
473
  return (err.__class__.__name__, err.args)
474

    
475

    
476
def GetEncodedError(result):
477
  """If this looks like an encoded Ganeti exception, return it.
478

479
  This function tries to parse the passed argument and if it looks
480
  like an encoding done by EncodeException, it will return the class
481
  object and arguments.
482

483
  """
484
  tlt = (tuple, list)
485
  if (isinstance(result, tlt) and len(result) == 2 and
486
      isinstance(result[1], tlt)):
487
    # custom ganeti errors
488
    errcls = GetErrorClass(result[0])
489
    if errcls:
490
      return (errcls, tuple(result[1]))
491

    
492
  return None
493

    
494

    
495
def MaybeRaise(result):
496
  """If this looks like an encoded Ganeti exception, raise it.
497

498
  This function tries to parse the passed argument and if it looks
499
  like an encoding done by EncodeException, it will re-raise it.
500

501
  """
502
  error = GetEncodedError(result)
503
  if error:
504
    (errcls, args) = error
505
    # pylint: disable=W0142
506
    raise errcls(*args)