Get all disk-related nodes for an instance.
Also use 'GetInstanceSecondaryNodes' to get the
list of secondary nodes.

Signed-off-by: Ilias Tsitsimpis <[email protected]>
---
 lib/config.py | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/lib/config.py b/lib/config.py
index b86df08..6bc726b 100644
--- a/lib/config.py
+++ b/lib/config.py
@@ -350,6 +350,61 @@ class ConfigWriter(object):
     nodegroup = self._UnlockedGetNodeGroup(node.group)
     return self._UnlockedGetGroupDiskParams(nodegroup)
 
+  # pylint: disable=R0201
+  def _UnlockedGetInstanceNodes(self, instance, disks=None):
+    """Get all disk-related nodes for an instance.
+
+    This function is for internal use, when the config lock is already held.
+
+    """
+    all_nodes = []
+    inst_disks = instance.disks
+    if disks is not None:
+      inst_disks.extend(disks)
+    for disk in inst_disks:
+      all_nodes.extend(disk.all_nodes)
+    all_nodes = set(all_nodes)
+    # ensure that primary node is always the first
+    all_nodes.discard(instance.primary_node)
+    return (instance.primary_node, ) + tuple(all_nodes)
+
+  @_ConfigSync(shared=1)
+  def GetInstanceNodes(self, instance, disks=None):
+    """Get all disk-related nodes for an instance.
+
+    For non-DRBD, this will be empty, for DRBD it will contain both
+    the primary and the secondaries.
+    If additional disks are given, include their nodes to the result.
+    This is done because these disks may not be attached to the instance yet.
+
+    @type instance: L{objects.Instance}
+    @param instance: The instance we want to get nodes for
+    @type disks: list of L{objects.Disk}
+    @param disks: If given, include these disks to the result
+    @return: A list of names for all the nodes of the instance
+
+    """
+    return self._UnlockedGetInstanceNodes(instance, disks=disks)
+
+  def _UnlockedGetInstanceSecondaryNodes(self, instance):
+    """Get the list of secondary nodes.
+
+    This function is for internal use, when the config lock is already held.
+
+    """
+    all_nodes = set(self._UnlockedGetInstanceNodes(instance))
+    all_nodes.discard(instance.primary_node)
+    return tuple(all_nodes)
+
+  @_ConfigSync(shared=1)
+  def GetInstanceSecondaryNodes(self, instance):
+    """Get the list of secondary nodes.
+
+    This is a simple wrapper over _UnlockedGetInstanceNodes.
+
+    """
+    return self._UnlockedGetInstanceSecondaryNodes(instance)
+
   @_ConfigSync(shared=1)
   def GetGroupDiskParams(self, group):
     """Get the disk params populated with inherit chain.
-- 
1.9.1

Reply via email to