Revision cae20ea0 lib/config.py

b/lib/config.py
281 281
    """
282 282
    return self._UnlockedGetInstanceDisks(instance)
283 283

  
284
  def _UnlockedAddDisk(self, disk):
285
    """Add a disk to the config.
286

  
287
    This function is for internal use, when the config lock is already held.
288
    The `_WriteConfig' function has to be called afterwards (so we can
289
    add many disks at once).
290

  
291
    @type disk: L{objects.Disk}
292
    @param disk: the disk object
293

  
294
    """
295
    if not isinstance(disk, objects.Disk):
296
      raise errors.ProgrammerError("Invalid type passed to _UnlockedAddDisk")
297

  
298
    logging.info("Adding disk %s to configuration", disk.uuid)
299

  
300
    self._CheckUniqueUUID(disk, include_temporary=False)
301
    disk.serial_no = 1
302
    disk.ctime = disk.mtime = time.time()
303
    self._config_data.disks[disk.uuid] = disk
304
    self._config_data.cluster.serial_no += 1
305

  
306
  # pylint: disable=R0201
307
  def _UpdateIvNames(self, base_index, disks):
308
    """Update the C{iv_name} attribute of disks.
309

  
310
    @type disks: list of L{objects.Disk}
311

  
312
    """
313
    for (idx, disk) in enumerate(disks):
314
      disk.iv_name = "disk/%s" % (base_index + idx)
315

  
316
  def _UnlockedAttachInstDisk(self, instance, disk, idx=None):
317
    """Attach a disk to an instance.
318

  
319
    This function is for internal use, when the config lock is already held.
320
    The `_WriteConfig' function has to be called afterwards (so we can
321
    add a disk to config and attach it to an instance at once).
322

  
323
    @type instance: L{objects.Instance}
324
    @param instance: the instance object
325
    @type disk: L{objects.Disk}
326
    @param disk: the disk object
327
    @type idx: int
328
    @param idx: the index of the newly attached disk
329

  
330
    """
331
    if not isinstance(instance, objects.Instance) or \
332
        not isinstance(disk, objects.Disk):
333
      raise errors.ProgrammerError(
334
        "Invalid type passed to _UnlockedAttachInstDisk")
335

  
336
    if idx is None:
337
      idx = len(instance.disks)
338
    else:
339
      if idx < 0:
340
        raise IndexError("Not accepting negative indices other than -1")
341
      elif idx > len(instance.disks):
342
        raise IndexError("Got disk index %s, but there are only %s" %
343
                         (idx, len(instance.disks)))
344

  
345
    # Disk must not be attached anywhere else
346
    if disk.instance:
347
      raise errors.ReservationError("Disk %s already attached to instance %s"
348
                                    % (disk.uuid, instance.name))
349

  
350
    disk.instance = instance.uuid
351
    disk.serial_no += 1
352
    disk.mtime = time.time()
353

  
354
    instance.disks.insert(idx, disk.uuid)
355
    inst_disks = self._UnlockedGetInstanceDisks(instance)
356
    self._UpdateIvNames(idx, inst_disks[idx:])
357
    instance.serial_no += 1
358
    instance.mtime = time.time()
359

  
360
  @locking.ssynchronized(_config_lock)
361
  def AddInstDisk(self, instance, disk, idx=None):
362
    """Add a disk to the config and attach it to instance.
363

  
364
    @type instance: L{objects.Instance}
365
    @param instance: the instance object
366
    @type disk: L{objects.Disk}
367
    @param disk: the disk object
368
    @type idx: int
369
    @param idx: the index of the newly attached disk
370

  
371
    """
372
    self._UnlockedAddDisk(disk)
373
    self._UnlockedAttachInstDisk(instance, disk, idx)
374
    self._WriteConfig()
375

  
284 376
  def _UnlockedGetDiskInfo(self, disk_uuid):
285 377
    """Returns information about an instance.
286 378

  
......
2907 2999
      test = target in self._config_data.nodegroups.values()
2908 3000
    elif isinstance(target, objects.Network):
2909 3001
      test = target in self._config_data.networks.values()
3002
    elif isinstance(target, objects.Disk):
3003
      test = target in self._config_data.disks.values()
2910 3004
    else:
2911 3005
      raise errors.ProgrammerError("Invalid object type (%s) passed to"
2912 3006
                                   " ConfigWriter.Update" % type(target))

Also available in: Unified diff