Try to prevent instance memory changes N+1 failures
[ganeti-local] / lib / config.py
index 3234ee1..fe580ef 100644 (file)
@@ -942,6 +942,16 @@ class ConfigWriter:
     if check_uuid:
       self._EnsureUUID(group, ec_id)
 
+    try:
+      existing_uuid = self._UnlockedLookupNodeGroup(group.name)
+    except errors.OpPrereqError:
+      pass
+    else:
+      raise errors.OpPrereqError("Desired group name '%s' already exists as a"
+                                 " node group (UUID: %s)" %
+                                 (group.name, existing_uuid),
+                                 errors.ECODE_EXISTS)
+
     group.serial_no = 1
     group.ctime = group.mtime = time.time()
     group.UpgradeConfig()
@@ -969,8 +979,7 @@ class ConfigWriter:
     self._config_data.cluster.serial_no += 1
     self._WriteConfig()
 
-  @locking.ssynchronized(_config_lock, shared=1)
-  def LookupNodeGroup(self, target):
+  def _UnlockedLookupNodeGroup(self, target):
     """Lookup a node group's UUID.
 
     @type target: string or None
@@ -994,6 +1003,20 @@ class ConfigWriter:
     raise errors.OpPrereqError("Node group '%s' not found" % target,
                                errors.ECODE_NOENT)
 
+  @locking.ssynchronized(_config_lock, shared=1)
+  def LookupNodeGroup(self, target):
+    """Lookup a node group's UUID.
+
+    This function is just a wrapper over L{_UnlockedLookupNodeGroup}.
+
+    @type target: string or None
+    @param target: group name or UUID or None to look for the default
+    @rtype: string
+    @return: nodegroup UUID
+
+    """
+    return self._UnlockedLookupNodeGroup(target)
+
   def _UnlockedGetNodeGroup(self, uuid):
     """Lookup a node group.
 
@@ -1377,6 +1400,17 @@ class ConfigWriter:
                     for node in self._UnlockedGetNodeList()])
     return my_dict
 
+  @locking.ssynchronized(_config_lock, shared=1)
+  def GetNodeGroupsFromNodes(self, nodes):
+    """Returns groups for a list of nodes.
+
+    @type nodes: list of string
+    @param nodes: List of node names
+    @rtype: frozenset
+
+    """
+    return frozenset(self._UnlockedGetNodeInfo(name).group for name in nodes)
+
   def _UnlockedGetMasterCandidateStats(self, exceptions=None):
     """Get the number of current and maximum desired and possible candidates.