Statistics
| Branch: | Tag: | Revision:

root / lib / errors.py @ 402ff455

History | View | Annotate | Download (10.6 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
#: Wrong arguments (at syntax level)
34
ECODE_INVAL = "wrong_input"
35

    
36
#: Wrong entity state
37
ECODE_STATE = "wrong_state"
38

    
39
#: Entity not found
40
ECODE_NOENT = "unknown_entity"
41

    
42
#: Entity already exists
43
ECODE_EXISTS = "already_exists"
44

    
45
#: Resource not unique (e.g. MAC or IP duplication)
46
ECODE_NOTUNIQUE = "resource_not_unique"
47

    
48
#: Internal cluster error
49
ECODE_FAULT = "internal_error"
50

    
51
#: Environment error (e.g. node disk error)
52
ECODE_ENVIRON = "environment_error"
53

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

    
67

    
68
class GenericError(Exception):
69
  """Base exception for Ganeti.
70

71
  """
72

    
73

    
74
class LockError(GenericError):
75
  """Lock error exception.
76

77
  This signifies problems in the locking subsystem.
78

79
  """
80

    
81

    
82
class PidFileLockError(LockError):
83
  """PID file is already locked by another process.
84

85
  """
86

    
87

    
88
class HypervisorError(GenericError):
89
  """Hypervisor-related exception.
90

91
  This is raised in case we can't communicate with the hypervisor
92
  properly.
93

94
  """
95

    
96

    
97
class ProgrammerError(GenericError):
98
  """Programming-related error.
99

100
  This is raised in cases we determine that the calling conventions
101
  have been violated, meaning we got some desynchronisation between
102
  parts of our code. It signifies a real programming bug.
103

104
  """
105

    
106

    
107
class BlockDeviceError(GenericError):
108
  """Block-device related exception.
109

110
  This is raised in case we can't setup the instance's block devices
111
  properly.
112

113
  """
114

    
115

    
116
class ConfigurationError(GenericError):
117
  """Configuration related exception.
118

119
  Things like having an instance with a primary node that doesn't
120
  exist in the config or such raise this exception.
121

122
  """
123

    
124

    
125
class ConfigVersionMismatch(ConfigurationError):
126
  """Version mismatch in the configuration file.
127

128
  The error has two arguments: the expected and the actual found
129
  version.
130

131
  """
132

    
133

    
134
class AddressPoolError(GenericError):
135
  """Errors related to IP address pools.
136

137
  """
138

    
139

    
140
class ReservationError(GenericError):
141
  """Errors reserving a resource.
142

143
  """
144

    
145

    
146
class RemoteError(GenericError):
147
  """Programming-related error on remote call.
148

149
  This is raised when an unhandled error occurs in a call to a
150
  remote node.  It usually signifies a real programming bug.
151

152
  """
153

    
154

    
155
class SignatureError(GenericError):
156
  """Error authenticating a remote message.
157

158
  This is raised when the hmac signature on a message doesn't verify correctly
159
  to the message itself. It can happen because of network unreliability or
160
  because of spurious traffic.
161

162
  """
163

    
164

    
165
class ParameterError(GenericError):
166
  """A passed parameter to a command is invalid.
167

168
  This is raised when the parameter passed to a request function is
169
  invalid. Correct code should have verified this before passing the
170
  request structure.
171

172
  The argument to this exception should be the parameter name.
173

174
  """
175

    
176

    
177
class ResultValidationError(GenericError):
178
  """The iallocation results fails validation.
179

180
  """
181

    
182

    
183
class OpPrereqError(GenericError):
184
  """Prerequisites for the OpCode are not fulfilled.
185

186
  This exception has two arguments: an error message, and one of the
187
  ECODE_* codes.
188

189
  """
190

    
191

    
192
class OpExecError(GenericError):
193
  """Error during OpCode execution.
194

195
  """
196

    
197

    
198
class OpResultError(GenericError):
199
  """Issue with OpCode result.
200

201
  """
202

    
203

    
204
class OpCodeUnknown(GenericError):
205
  """Unknown opcode submitted.
206

207
  This signifies a mismatch between the definitions on the client and
208
  server side.
209

210
  """
211

    
212

    
213
class JobLost(GenericError):
214
  """Submitted job lost.
215

216
  The job was submitted but it cannot be found in the current job
217
  list.
218

219
  """
220

    
221

    
222
class JobFileCorrupted(GenericError):
223
  """Job file could not be properly decoded/restored.
224

225
  """
226

    
227

    
228
class ResolverError(GenericError):
229
  """Host name cannot be resolved.
230

231
  This is not a normal situation for Ganeti, as we rely on having a
232
  working resolver.
233

234
  The non-resolvable hostname is available as the first element of the
235
  args tuple; the other two elements of the tuple are the first two
236
  args of the socket.gaierror exception (error code and description).
237

238
  """
239

    
240

    
241
class HooksFailure(GenericError):
242
  """A generic hook failure.
243

244
  This signifies usually a setup misconfiguration.
245

246
  """
247

    
248

    
249
class HooksAbort(HooksFailure):
250
  """A required hook has failed.
251

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

257
  """
258

    
259

    
260
class UnitParseError(GenericError):
261
  """Unable to parse size unit.
262

263
  """
264

    
265

    
266
class ParseError(GenericError):
267
  """Generic parse error.
268

269
  Raised when unable to parse user input.
270

271
  """
272

    
273

    
274
class TypeEnforcementError(GenericError):
275
  """Unable to enforce data type.
276

277
  """
278

    
279

    
280
class X509CertError(GenericError):
281
  """Invalid X509 certificate.
282

283
  This error has two arguments: the certificate filename and the error cause.
284

285
  """
286

    
287

    
288
class TagError(GenericError):
289
  """Generic tag error.
290

291
  The argument to this exception will show the exact error.
292

293
  """
294

    
295

    
296
class CommandError(GenericError):
297
  """External command error.
298

299
  """
300

    
301

    
302
class StorageError(GenericError):
303
  """Storage-related exception.
304

305
  """
306

    
307

    
308
class InotifyError(GenericError):
309
  """Error raised when there is a failure setting up an inotify watcher.
310

311
  """
312

    
313

    
314
class QuitGanetiException(Exception):
315
  """Signal Ganeti that it must quit.
316

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

326
  Examples::
327

328
    # Return a result of "True" to the caller, but quit ganeti afterwards
329
    raise QuitGanetiException(True, None)
330
    # Send an error to the caller, and quit ganeti
331
    raise QuitGanetiException(False, "Fatal safety violation, shutting down")
332

333
  """
334

    
335

    
336
class JobQueueError(GenericError):
337
  """Job queue error.
338

339
  """
340

    
341

    
342
class JobQueueDrainError(JobQueueError):
343
  """Job queue is marked for drain error.
344

345
  This is raised when a job submission attempt is made but the queue
346
  is marked for drain.
347

348
  """
349

    
350

    
351
class JobQueueFull(JobQueueError):
352
  """Job queue full error.
353

354
  Raised when job queue size reached its hard limit.
355

356
  """
357

    
358

    
359
class ConfdMagicError(GenericError):
360
  """A magic fourcc error in Ganeti confd.
361

362
  Errors processing the fourcc in ganeti confd datagrams.
363

364
  """
365

    
366

    
367
class ConfdClientError(GenericError):
368
  """A magic fourcc error in Ganeti confd.
369

370
  Errors in the confd client library.
371

372
  """
373

    
374

    
375
class UdpDataSizeError(GenericError):
376
  """UDP payload too big.
377

378
  """
379

    
380

    
381
class NoCtypesError(GenericError):
382
  """python ctypes module is not found in the system.
383

384
  """
385

    
386

    
387
class IPAddressError(GenericError):
388
  """Generic IP address error.
389

390
  """
391

    
392

    
393
class LuxiError(GenericError):
394
  """LUXI error.
395

396
  """
397

    
398

    
399
class QueryFilterParseError(ParseError):
400
  """Error while parsing query filter.
401

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

407
  """
408
  def GetDetails(self):
409
    """Returns a list of strings with details about the error.
410

411
    """
412
    try:
413
      (_, inner) = self.args
414
    except IndexError:
415
      return None
416

    
417
    return [str(inner.line),
418
            (" " * (inner.column - 1)) + "^",
419
            str(inner)]
420

    
421

    
422
class RapiTestResult(GenericError):
423
  """Exception containing results from RAPI test utilities.
424

425
  """
426

    
427

    
428
class FileStoragePathError(GenericError):
429
  """Error from file storage path validation.
430

431
  """
432

    
433

    
434
# errors should be added above
435

    
436

    
437
def GetErrorClass(name):
438
  """Return the class of an exception.
439

440
  Given the class name, return the class itself.
441

442
  @type name: str
443
  @param name: the exception name
444
  @rtype: class
445
  @return: the actual class, or None if not found
446

447
  """
448
  item = globals().get(name, None)
449
  if item is not None:
450
    if not (isinstance(item, type(Exception)) and
451
            issubclass(item, GenericError)):
452
      item = None
453
  return item
454

    
455

    
456
def EncodeException(err):
457
  """Encodes an exception into a format that L{MaybeRaise} will recognise.
458

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

462
  @type err: GenericError child
463
  @param err: usually a child of GenericError (but any exception
464
      will be accepted)
465
  @rtype: tuple
466
  @return: tuple of (exception name, exception arguments)
467

468
  """
469
  return (err.__class__.__name__, err.args)
470

    
471

    
472
def GetEncodedError(result):
473
  """If this looks like an encoded Ganeti exception, return it.
474

475
  This function tries to parse the passed argument and if it looks
476
  like an encoding done by EncodeException, it will return the class
477
  object and arguments.
478

479
  """
480
  tlt = (tuple, list)
481
  if (isinstance(result, tlt) and len(result) == 2 and
482
      isinstance(result[1], tlt)):
483
    # custom ganeti errors
484
    errcls = GetErrorClass(result[0])
485
    if errcls:
486
      return (errcls, tuple(result[1]))
487

    
488
  return None
489

    
490

    
491
def MaybeRaise(result):
492
  """If this looks like an encoded Ganeti exception, raise it.
493

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

497
  """
498
  error = GetEncodedError(result)
499
  if error:
500
    (errcls, args) = error
501
    # pylint: disable=W0142
502
    raise errcls(*args)