Revision 7d81bb8b lib/cmdlib/instance_operation.py

b/lib/cmdlib/instance_operation.py
42 42
  ShutdownInstanceDisks
43 43
from ganeti.cmdlib.instance_utils import BuildInstanceHookEnvByObject, \
44 44
  CheckInstanceBridgesExist, CheckNodeFreeMemory, CheckNodeHasOS
45
from ganeti.cmdlib.instance import GetItemFromContainer
45 46

  
46 47

  
47 48
class LUInstanceStartup(LogicalUnit):
......
498 499

  
499 500
    return GetInstanceConsole(self.cfg.GetClusterInfo(), self.instance,
500 501
                              self.cfg.GetNodeInfo(self.instance.primary_node))
502

  
503

  
504
class LUInstanceSnapshot(LogicalUnit):
505
  """Take a snapshot of the instance.
506

  
507
  """
508
  HPATH = "instance-snapshot"
509
  HTYPE = constants.HTYPE_INSTANCE
510
  REQ_BGL = False
511

  
512
  def ExpandNames(self):
513
    self._ExpandAndLockInstance()
514

  
515
  def BuildHooksEnv(self):
516
    """Build hooks env.
517

  
518
    This runs on master, primary and secondary nodes of the instance.
519

  
520
    """
521
    return BuildInstanceHookEnvByObject(self, self.instance)
522

  
523
  def BuildHooksNodes(self):
524
    """Build hooks nodes.
525

  
526
    """
527
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
528
    return (nl, nl)
529

  
530
  def CheckPrereq(self):
531
    """Check prerequisites.
532

  
533
    This checks that the instance is in the cluster and is not running.
534

  
535
    """
536
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
537
    assert instance is not None, \
538
      "Cannot retrieve locked instance %s" % self.op.instance_name
539
    CheckNodeOnline(self, instance.primary_node, "Instance primary node"
540
                    " offline, cannot snapshot")
541

  
542
    self.snapshots = []
543
    for ident, params in self.op.disks:
544
      idx, disk = GetItemFromContainer(ident, 'disk', instance.disks)
545
      snapshot_name = params.get("snapshot_name", None)
546
      if not snapshot_name:
547
        raise errors.OpPrereqError("No snapshot_name passed for disk %s", ident)
548
      self.snapshots.append((idx, disk, snapshot_name))
549

  
550
    self.instance = instance
551

  
552
  def Exec(self, feedback_fn):
553
    """Take a snapshot of the instance the instance.
554

  
555
    """
556
    inst = self.instance
557
    node_uuid = inst.primary_node
558
    for idx, disk, snapshot_name in self.snapshots:
559
      self.cfg.SetDiskID(disk, node_uuid)
560
      feedback_fn("Taking a snapshot of instance...")
561
      result = self.rpc.call_blockdev_snapshot(node_uuid,
562
                                               (disk, inst),
563
                                               snapshot_name)
564
      result.Raise("Could not take a snapshot for instance %s disk/%d %s"
565
                   " on node %s" % (inst, idx, disk.uuid, inst.primary_node))

Also available in: Unified diff