Revision 80cb875c lib/cmdlib.py

b/lib/cmdlib.py
709 709
  _CheckNicsBridgesExist(lu, instance.nics, node)
710 710

  
711 711

  
712
def _GetNodePrimaryInstances(cfg, node_name):
713
  """Returns primary instances on a node.
714

  
715
  """
716
  instances = []
717

  
718
  for (_, inst) in cfg.GetAllInstancesInfo().iteritems():
719
    if node_name == inst.primary_node:
720
      instances.append(inst)
721

  
722
  return instances
723

  
724

  
712 725
def _GetNodeSecondaryInstances(cfg, node_name):
713 726
  """Returns secondary instances on a node.
714 727

  
......
3941 3954
    return env, nl, nl
3942 3955

  
3943 3956

  
3957
class LUMigrateNode(LogicalUnit):
3958
  """Migrate all instances from a node.
3959

  
3960
  """
3961
  HPATH = "node-migrate"
3962
  HTYPE = constants.HTYPE_NODE
3963
  _OP_REQP = ["node_name", "live"]
3964
  REQ_BGL = False
3965

  
3966
  def ExpandNames(self):
3967
    self.op.node_name = self.cfg.ExpandNodeName(self.op.node_name)
3968
    if self.op.node_name is None:
3969
      raise errors.OpPrereqError("Node '%s' not known" % self.op.node_name)
3970

  
3971
    self.needed_locks = {
3972
      locking.LEVEL_NODE: [self.op.node_name],
3973
      }
3974

  
3975
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
3976

  
3977
    # Create tasklets for migrating instances for all instances on this node
3978
    names = []
3979
    tasklets = []
3980

  
3981
    for inst in _GetNodePrimaryInstances(self.cfg, self.op.node_name):
3982
      logging.debug("Migrating instance %s", inst.name)
3983
      names.append(inst.name)
3984

  
3985
      tasklets.append(TLMigrateInstance(self, inst.name, self.op.live, False))
3986

  
3987
    self.tasklets = tasklets
3988

  
3989
    # Declare instance locks
3990
    self.needed_locks[locking.LEVEL_INSTANCE] = names
3991

  
3992
  def DeclareLocks(self, level):
3993
    if level == locking.LEVEL_NODE:
3994
      self._LockInstancesNodes()
3995

  
3996
  def BuildHooksEnv(self):
3997
    """Build hooks env.
3998

  
3999
    This runs on the master, the primary and all the secondaries.
4000

  
4001
    """
4002
    env = {
4003
      "NODE_NAME": self.op.node_name,
4004
      }
4005

  
4006
    nl = [self.cfg.GetMasterNode()]
4007

  
4008
    return (env, nl, nl)
4009

  
4010

  
3944 4011
class TLMigrateInstance(Tasklet):
3945 4012
  def __init__(self, lu, instance_name, live, cleanup):
3946 4013
    """Initializes this class.
......
4253 4320
    """Perform the migration.
4254 4321

  
4255 4322
    """
4323
    feedback_fn("Migrating instance %s" % self.instance.name)
4324

  
4256 4325
    self.feedback_fn = feedback_fn
4257 4326

  
4258 4327
    self.source_node = self.instance.primary_node

Also available in: Unified diff