Revision da4a52a3 lib/cmdlib/common.py

b/lib/cmdlib/common.py
65 65
  return full_name
66 66

  
67 67

  
68
def ExpandInstanceName(cfg, name):
68
def ExpandInstanceUuidAndName(cfg, expected_uuid, name):
69 69
  """Wrapper over L{_ExpandItemName} for instance."""
70
  return _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
70
  (uuid, full_name) = _ExpandItemName(cfg.ExpandInstanceName, name, "Instance")
71
  if expected_uuid is not None and uuid != expected_uuid:
72
    raise errors.OpPrereqError(
73
      "The instances UUID '%s' does not match the expected UUID '%s' for"
74
      " instance '%s'. Maybe the instance changed since you submitted this"
75
      " job." % (uuid, expected_uuid, full_name), errors.ECODE_NOTUNIQUE)
76
  return (uuid, full_name)
71 77

  
72 78

  
73 79
def ExpandNodeUuidAndName(cfg, expected_uuid, name):
......
99 105
  return dict.fromkeys(locking.LEVELS, 1)
100 106

  
101 107

  
102
def CheckNodeGroupInstances(cfg, group_uuid, owned_instances):
108
def CheckNodeGroupInstances(cfg, group_uuid, owned_instance_names):
103 109
  """Checks if the instances in a node group are still correct.
104 110

  
105 111
  @type cfg: L{config.ConfigWriter}
106 112
  @param cfg: The cluster configuration
107 113
  @type group_uuid: string
108 114
  @param group_uuid: Node group UUID
109
  @type owned_instances: set or frozenset
110
  @param owned_instances: List of currently owned instances
115
  @type owned_instance_names: set or frozenset
116
  @param owned_instance_names: List of currently owned instances
111 117

  
112 118
  """
113
  wanted_instances = cfg.GetNodeGroupInstances(group_uuid)
114
  if owned_instances != wanted_instances:
119
  wanted_instances = frozenset(cfg.GetInstanceNames(
120
                                 cfg.GetNodeGroupInstances(group_uuid)))
121
  if owned_instance_names != wanted_instances:
115 122
    raise errors.OpPrereqError("Instances in node group '%s' changed since"
116 123
                               " locks were acquired, wanted '%s', have '%s';"
117 124
                               " retry the operation" %
118 125
                               (group_uuid,
119 126
                                utils.CommaJoin(wanted_instances),
120
                                utils.CommaJoin(owned_instances)),
127
                                utils.CommaJoin(owned_instance_names)),
121 128
                               errors.ECODE_STATE)
122 129

  
123 130
  return wanted_instances
......
144 151
  return (node_uuids, [lu.cfg.GetNodeName(uuid) for uuid in node_uuids])
145 152

  
146 153

  
147
def GetWantedInstances(lu, instances):
154
def GetWantedInstances(lu, short_inst_names):
148 155
  """Returns list of checked and expanded instance names.
149 156

  
150 157
  @type lu: L{LogicalUnit}
151 158
  @param lu: the logical unit on whose behalf we execute
152
  @type instances: list
153
  @param instances: list of instance names or None for all instances
154
  @rtype: list
155
  @return: the list of instances, sorted
159
  @type short_inst_names: list
160
  @param short_inst_names: list of instance names or None for all instances
161
  @rtype: tuple of lists
162
  @return: tuple of (instance UUIDs, instance names)
156 163
  @raise errors.OpPrereqError: if the instances parameter is wrong type
157 164
  @raise errors.OpPrereqError: if any of the passed instances is not found
158 165

  
159 166
  """
160
  if instances:
161
    wanted = [ExpandInstanceName(lu.cfg, name) for name in instances]
167
  if short_inst_names:
168
    inst_uuids = [ExpandInstanceUuidAndName(lu.cfg, None, name)[0]
169
                  for name in short_inst_names]
162 170
  else:
163
    wanted = utils.NiceSort(lu.cfg.GetInstanceList())
164
  return wanted
171
    inst_uuids = lu.cfg.GetInstanceList()
172
  return (inst_uuids, [lu.cfg.GetInstanceName(uuid) for uuid in inst_uuids])
165 173

  
166 174

  
167 175
def RunPostHook(lu, node_name):
......
794 802
  @type cfg: L{config.ConfigWriter}
795 803
  @param cfg: Cluster configuration
796 804
  @type instances: dict; string as key, L{objects.Instance} as value
797
  @param instances: Dictionary, instance name as key, instance object as value
805
  @param instances: Dictionary, instance UUID as key, instance object as value
798 806
  @type owned_groups: iterable of string
799 807
  @param owned_groups: List of owned groups
800 808
  @type owned_node_uuids: iterable of string
......
803 811
  @param cur_group_uuid: Optional group UUID to check against instance's groups
804 812

  
805 813
  """
806
  for (name, inst) in instances.items():
814
  for (uuid, inst) in instances.items():
807 815
    assert owned_node_uuids.issuperset(inst.all_nodes), \
808
      "Instance %s's nodes changed while we kept the lock" % name
816
      "Instance %s's nodes changed while we kept the lock" % inst.name
809 817

  
810
    inst_groups = CheckInstanceNodeGroups(cfg, name, owned_groups)
818
    inst_groups = CheckInstanceNodeGroups(cfg, uuid, owned_groups)
811 819

  
812 820
    assert cur_group_uuid is None or cur_group_uuid in inst_groups, \
813
      "Instance %s has no node in group %s" % (name, cur_group_uuid)
821
      "Instance %s has no node in group %s" % (inst.name, cur_group_uuid)
814 822

  
815 823

  
816
def CheckInstanceNodeGroups(cfg, instance_name, owned_groups,
817
                            primary_only=False):
824
def CheckInstanceNodeGroups(cfg, inst_uuid, owned_groups, primary_only=False):
818 825
  """Checks if the owned node groups are still correct for an instance.
819 826

  
820 827
  @type cfg: L{config.ConfigWriter}
821 828
  @param cfg: The cluster configuration
822
  @type instance_name: string
823
  @param instance_name: Instance name
829
  @type inst_uuid: string
830
  @param inst_uuid: Instance UUID
824 831
  @type owned_groups: set or frozenset
825 832
  @param owned_groups: List of currently owned node groups
826 833
  @type primary_only: boolean
827 834
  @param primary_only: Whether to check node groups for only the primary node
828 835

  
829 836
  """
830
  inst_groups = cfg.GetInstanceNodeGroups(instance_name, primary_only)
837
  inst_groups = cfg.GetInstanceNodeGroups(inst_uuid, primary_only)
831 838

  
832 839
  if not owned_groups.issuperset(inst_groups):
833 840
    raise errors.OpPrereqError("Instance %s's node groups changed since"
834 841
                               " locks were acquired, current groups are"
835 842
                               " are '%s', owning groups '%s'; retry the"
836 843
                               " operation" %
837
                               (instance_name,
844
                               (cfg.GetInstanceName(inst_uuid),
838 845
                                utils.CommaJoin(inst_groups),
839 846
                                utils.CommaJoin(owned_groups)),
840 847
                               errors.ECODE_STATE)

Also available in: Unified diff