Revision 45460c20 lib/cmdlib/instance_operation.py

b/lib/cmdlib/instance_operation.py
51 51
  ShutdownInstanceDisks
52 52
from ganeti.cmdlib.instance_utils import BuildInstanceHookEnvByObject, \
53 53
  CheckInstanceBridgesExist, CheckNodeFreeMemory, CheckNodeHasOS
54
from ganeti.cmdlib.instance import GetItemFromContainer
54 55

  
55 56

  
56 57
class LUInstanceStartup(LogicalUnit):
......
507 508

  
508 509
    return GetInstanceConsole(self.cfg.GetClusterInfo(), self.instance,
509 510
                              self.cfg.GetNodeInfo(self.instance.primary_node))
511

  
512

  
513
class LUInstanceSnapshot(LogicalUnit):
514
  """Take a snapshot of the instance.
515

  
516
  """
517
  HPATH = "instance-snapshot"
518
  HTYPE = constants.HTYPE_INSTANCE
519
  REQ_BGL = False
520

  
521
  def ExpandNames(self):
522
    self._ExpandAndLockInstance()
523

  
524
  def BuildHooksEnv(self):
525
    """Build hooks env.
526

  
527
    This runs on master, primary and secondary nodes of the instance.
528

  
529
    """
530
    return BuildInstanceHookEnvByObject(self, self.instance)
531

  
532
  def BuildHooksNodes(self):
533
    """Build hooks nodes.
534

  
535
    """
536
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
537
    return (nl, nl)
538

  
539
  def CheckPrereq(self):
540
    """Check prerequisites.
541

  
542
    This checks that the instance is in the cluster and is not running.
543

  
544
    """
545
    instance = self.cfg.GetInstanceInfo(self.op.instance_uuid)
546
    assert instance is not None, \
547
      "Cannot retrieve locked instance %s" % self.op.instance_name
548
    CheckNodeOnline(self, instance.primary_node, "Instance primary node"
549
                    " offline, cannot snapshot")
550

  
551
    self.snapshots = []
552
    for ident, params in self.op.disks:
553
      idx, disk = GetItemFromContainer(ident, 'disk', instance.disks)
554
      snapshot_name = params.get("snapshot_name", None)
555
      if not snapshot_name:
556
        raise errors.OpPrereqError("No snapshot_name passed for disk %s", ident)
557
      self.snapshots.append((idx, disk, snapshot_name))
558

  
559
    self.instance = instance
560

  
561
  def Exec(self, feedback_fn):
562
    """Take a snapshot of the instance the instance.
563

  
564
    """
565
    inst = self.instance
566
    node_uuid = inst.primary_node
567
    for idx, disk, snapshot_name in self.snapshots:
568
      feedback_fn("Taking a snapshot of instance...")
569
      result = self.rpc.call_blockdev_snapshot(node_uuid,
570
                                               (disk, inst),
571
                                               snapshot_name,
572
                                               None)
573
      result.Raise("Could not take a snapshot for instance %s disk/%d %s"
574
                   " on node %s" % (inst, idx, disk.uuid, inst.primary_node))

Also available in: Unified diff