gnt-instance console: Improve error reporting
[ganeti-local] / lib / config.py
index ef37882..2cab4ca 100644 (file)
@@ -81,15 +81,15 @@ class TemporaryReservationManager:
     self._ec_reserved = {}
 
   def Reserved(self, resource):
-    for holder_reserved in self._ec_reserved.items():
+    for holder_reserved in self._ec_reserved.values():
       if resource in holder_reserved:
         return True
     return False
 
   def Reserve(self, ec_id, resource):
     if self.Reserved(resource):
-      raise errors.ReservationError("Duplicate reservation for resource: %s." %
-                                    (resource))
+      raise errors.ReservationError("Duplicate reservation for resource '%s'"
+                                    % str(resource))
     if ec_id not in self._ec_reserved:
       self._ec_reserved[ec_id] = set([resource])
     else:
@@ -854,12 +854,12 @@ class ConfigWriter:
 
   @locking.ssynchronized(_config_lock, shared=1)
   def LookupNodeGroup(self, target):
-    """Lookup a node group.
+    """Lookup a node group's UUID.
 
     @type target: string or None
-    @param  target: group name or uuid or None to look for the default
+    @param target: group name or UUID or None to look for the default
     @rtype: string
-    @return: nodegroup uuid
+    @return: nodegroup UUID
     @raises errors.OpPrereqError: when the target group cannot be found
 
     """
@@ -874,7 +874,22 @@ class ConfigWriter:
     for nodegroup in self._config_data.nodegroups.values():
       if nodegroup.name == target:
         return nodegroup.uuid
-    raise errors.OpPrereqError("Nodegroup '%s' not found", target)
+    raise errors.OpPrereqError("Nodegroup '%s' not found" % target)
+
+  @locking.ssynchronized(_config_lock, shared=1)
+  def GetNodeGroup(self, uuid):
+    """Lookup a node group.
+
+    @type uuid: string
+    @param uuid: group UUID
+    @rtype: L{objects.NodeGroup} or None
+    @return: nodegroup object, or None if not found
+
+    """
+    if uuid not in self._config_data.nodegroups:
+      return None
+
+    return self._config_data.nodegroups[uuid]
 
   @locking.ssynchronized(_config_lock, shared=1)
   def GetAllNodeGroupsInfo(self):
@@ -883,6 +898,13 @@ class ConfigWriter:
     """
     return dict(self._config_data.nodegroups)
 
+  @locking.ssynchronized(_config_lock, shared=1)
+  def GetNodeGroupList(self):
+    """Get a list of node groups.
+
+    """
+    return self._config_data.nodegroups.keys()
+
   @locking.ssynchronized(_config_lock)
   def AddInstance(self, instance, ec_id):
     """Add an instance to the config.
@@ -983,10 +1005,14 @@ class ConfigWriter:
       if disk.dev_type == constants.LD_FILE:
         # rename the file paths in logical and physical id
         file_storage_dir = os.path.dirname(os.path.dirname(disk.logical_id[1]))
+        disk_fname = "disk%s" % disk.iv_name.split("/")[1]
         disk.physical_id = disk.logical_id = (disk.logical_id[0],
                                               utils.PathJoin(file_storage_dir,
                                                              inst.name,
-                                                             disk.iv_name))
+                                                             disk_fname))
+
+    # Force update of ssconf files
+    self._config_data.cluster.serial_no += 1
 
     self._config_data.instances[inst.name] = inst
     self._WriteConfig()
@@ -1139,6 +1165,25 @@ class ConfigWriter:
     """
     return self._UnlockedGetNodeInfo(node_name)
 
+  @locking.ssynchronized(_config_lock, shared=1)
+  def GetNodeInstances(self, node_name):
+    """Get the instances of a node, as stored in the config.
+
+    @param node_name: the node name, e.g. I{node1.example.com}
+
+    @rtype: (list, list)
+    @return: a tuple with two lists: the primary and the secondary instances
+
+    """
+    pri = []
+    sec = []
+    for inst in self._config_data.instances.values():
+      if inst.primary_node == node_name:
+        pri.append(inst.name)
+      if node_name in inst.secondary_nodes:
+        sec.append(inst.name)
+    return (pri, sec)
+
   def _UnlockedGetNodeList(self):
     """Return the list of nodes which are in the configuration.
 
@@ -1173,6 +1218,15 @@ class ConfigWriter:
     return self._UnlockedGetOnlineNodeList()
 
   @locking.ssynchronized(_config_lock, shared=1)
+  def GetNonVmCapableNodeList(self):
+    """Return the list of nodes which are not vm capable.
+
+    """
+    all_nodes = [self._UnlockedGetNodeInfo(node)
+                 for node in self._UnlockedGetNodeList()]
+    return [node.name for node in all_nodes if not node.vm_capable]
+
+  @locking.ssynchronized(_config_lock, shared=1)
   def GetAllNodesInfo(self):
     """Get the configuration of all nodes.