Revision 923b1523 lib/config.py

b/lib/config.py
46 46
from ganeti import rpc
47 47
from ganeti import objects
48 48

  
49
def _my_uuidgen():
50
  """Poor-man's uuidgen using the uuidgen binary.
51

  
52
  """
53
  result = utils.RunCmd(["uuidgen", "-r"])
54
  if result.failed:
55
    return None
56
  return result.stdout.rstrip('\n')
57

  
58

  
59
try:
60
  import uuid
61
  _uuidgen = uuid.uuid4
62
except ImportError:
63
  _uuidgen = _my_uuidgen
64

  
49 65

  
50 66
class ConfigWriter:
51 67
  """The interface to the cluster configuration.
......
61 77
      self._cfg_file = constants.CLUSTER_CONF_FILE
62 78
    else:
63 79
      self._cfg_file = cfg_file
80
    self._temporary_ids = set()
64 81

  
65 82
  # this method needs to be static, so that we can call it on the class
66 83
  @staticmethod
......
93 110
      raise errors.ConfigurationError, ("Can't generate unique MAC")
94 111
    return mac
95 112

  
113
  def _ComputeAllLVs(self):
114
    """Compute the list of all LVs.
115

  
116
    """
117
    self._OpenConfig()
118
    self._ReleaseLock()
119
    lvnames = set()
120
    for instance in self._config_data.instances.values():
121
      node_data = instance.MapLVsByNode()
122
      for lv_list in node_data.values():
123
        lvnames.update(lv_list)
124
    return lvnames
125

  
126
  def GenerateUniqueID(self, exceptions=None):
127
    """Generate an unique disk name.
128

  
129
    This checks the current node, instances and disk names for
130
    duplicates.
131

  
132
    Args:
133
      - exceptions: a list with some other names which should be checked
134
                    for uniqueness (used for example when you want to get
135
                    more than one id at one time without adding each one in
136
                    turn to the config file
137

  
138
    Returns: the unique id as a string
139

  
140
    """
141
    existing = set()
142
    existing.update(self._temporary_ids)
143
    existing.update(self._ComputeAllLVs())
144
    existing.update(self._config_data.instances.keys())
145
    existing.update(self._config_data.nodes.keys())
146
    if exceptions is not None:
147
      existing.update(exceptions)
148
    retries = 64
149
    while retries > 0:
150
      unique_id = _uuidgen()
151
      if unique_id not in existing and unique_id is not None:
152
        break
153
    else:
154
      raise errors.ConfigurationError, ("Not able generate an unique ID"
155
                                        " (last tried ID: %s" % unique_id)
156
    self._temporary_ids.add(unique_id)
157
    return unique_id
158

  
96 159
  def _AllMACs(self):
97 160
    """Return all MACs present in the config.
98 161

  
......
133 196
          seen_macs.append(nic.mac)
134 197
    return result
135 198

  
136

  
137 199
  def SetDiskID(self, disk, node_name):
138 200
    """Convert the unique ID to the ID needed on the target nodes.
139 201

  
......
235 297
    if not isinstance(instance, objects.Instance):
236 298
      raise errors.ProgrammerError("Invalid type passed to AddInstance")
237 299

  
300
    all_lvs = instance.MapLVsByNode()
301
    logger.Info("Instance '%s' DISK_LAYOUT: %s" % (instance.name, all_lvs))
302

  
238 303
    self._OpenConfig()
239 304
    self._config_data.instances[instance.name] = instance
240 305
    self._WriteConfig()

Also available in: Unified diff