commit c42be2c044383376c2ca608f307e0d5d29e6f872
Author: Petr Pudlak <pudlak@google.com>
Date:   Mon Nov 11 16:37:27 2013 +0100

    Use configured SSH ports when connecting to a console
    
    This is accomplished by passing the corresponding node group to
    hv_*.py. Tests for hv_*.py that call GetInstanceConsole updated.
    
    Signed-off-by: Petr Pudlak <pudlak@google.com>
    Reviewed-by: Hrvoje Ribicic <riba@google.com>

diff --git a/lib/backend.py b/lib/backend.py
index 1265470..75804a9 100644
--- a/lib/backend.py
+++ b/lib/backend.py
@@ -1489,8 +1489,8 @@ def GetInstanceConsoleInfo(instance_param_dict,
   allowing any number of instance queries to be done.
 
   @type instance_param_dict: dict of string to tuple of dictionaries, where the
-    dictionaries represent: L{objects.Instance}, L{objects.Node}, HvParams,
-    BeParams
+    dictionaries represent: L{objects.Instance}, L{objects.Node},
+    L{objects.NodeGroup}, HvParams, BeParams
   @param instance_param_dict: mapping of instance name to parameters necessary
     for console information retrieval
 
@@ -1512,15 +1512,17 @@ def GetInstanceConsoleInfo(instance_param_dict,
   for inst_name in instance_param_dict:
     instance = instance_param_dict[inst_name]["instance"]
     pnode = instance_param_dict[inst_name]["node"]
+    group = instance_param_dict[inst_name]["group"]
     hvparams = instance_param_dict[inst_name]["hvParams"]
     beparams = instance_param_dict[inst_name]["beParams"]
 
     instance = objects.Instance.FromDict(instance)
     pnode = objects.Node.FromDict(pnode)
+    group = objects.NodeGroup.FromDict(group)
 
     h = get_hv_fn(instance.hypervisor)
-    output[inst_name] = h.GetInstanceConsole(instance, pnode, hvparams,
-                                             beparams).ToDict()
+    output[inst_name] = h.GetInstanceConsole(instance, pnode, group,
+                                             hvparams, beparams).ToDict()
 
   return output
 
diff --git a/lib/cmdlib/instance_operation.py b/lib/cmdlib/instance_operation.py
index e5ba0eb..a99ef97 100644
--- a/lib/cmdlib/instance_operation.py
+++ b/lib/cmdlib/instance_operation.py
@@ -430,12 +430,13 @@ class LUInstanceReboot(LogicalUnit):
     self.cfg.MarkInstanceUp(self.instance.uuid)
 
 
-def GetInstanceConsole(cluster, instance, primary_node):
+def GetInstanceConsole(cluster, instance, primary_node, node_group):
   """Returns console information for an instance.
 
   @type cluster: L{objects.Cluster}
   @type instance: L{objects.Instance}
   @type primary_node: L{objects.Node}
+  @type node_group: L{objects.NodeGroup}
   @rtype: dict
 
   """
@@ -444,7 +445,8 @@ def GetInstanceConsole(cluster, instance, primary_node):
   # instance and then saving the defaults in the instance itself.
   hvparams = cluster.FillHV(instance)
   beparams = cluster.FillBE(instance)
-  console = hyper.GetInstanceConsole(instance, primary_node, hvparams, beparams)
+  console = hyper.GetInstanceConsole(instance, primary_node, node_group,
+                                     hvparams, beparams)
 
   assert console.instance == instance.name
   assert console.Validate()
@@ -503,5 +505,7 @@ class LUInstanceConsole(NoHooksLU):
     logging.debug("Connecting to console of %s on %s", self.instance.name,
                   self.cfg.GetNodeName(node_uuid))
 
-    return GetInstanceConsole(self.cfg.GetClusterInfo(), self.instance,
-                              self.cfg.GetNodeInfo(self.instance.primary_node))
+    node = self.cfg.GetNodeInfo(self.instance.primary_node)
+    group = self.cfg.GetNodeGroup(node.group)
+    return GetInstanceConsole(self.cfg.GetClusterInfo(),
+                              self.instance, node, group)
diff --git a/lib/cmdlib/instance_query.py b/lib/cmdlib/instance_query.py
index 619d395..45a9efa 100644
--- a/lib/cmdlib/instance_query.py
+++ b/lib/cmdlib/instance_query.py
@@ -166,9 +166,10 @@ class InstanceQuery(QueryBase):
       for inst in instance_list:
         if inst.uuid in live_data:
           # Instance is running
+          node = lu.cfg.GetNodeInfo(inst.primary_node)
+          group = lu.cfg.GetNodeGroup(node.group)
           consinfo[inst.uuid] = \
-            GetInstanceConsole(cluster, inst,
-                               lu.cfg.GetNodeInfo(inst.primary_node))
+            GetInstanceConsole(cluster, inst, node, group)
         else:
           consinfo[inst.uuid] = None
     else:
diff --git a/lib/hypervisor/hv_base.py b/lib/hypervisor/hv_base.py
index f43305b..1e620b3 100644
--- a/lib/hypervisor/hv_base.py
+++ b/lib/hypervisor/hv_base.py
@@ -268,7 +268,8 @@ class BaseHypervisor(object):
     raise NotImplementedError
 
   @classmethod
-  def GetInstanceConsole(cls, instance, primary_node, hvparams, beparams):
+  def GetInstanceConsole(cls, instance, primary_node, node_group,
+                         hvparams, beparams):
     """Return information for connecting to the console of an instance.
 
     """
diff --git a/lib/hypervisor/hv_chroot.py b/lib/hypervisor/hv_chroot.py
index 55c2583..993df87 100644
--- a/lib/hypervisor/hv_chroot.py
+++ b/lib/hypervisor/hv_chroot.py
@@ -260,7 +260,7 @@ class ChrootManager(hv_base.BaseHypervisor):
 
   @classmethod
   def GetInstanceConsole(cls, instance, primary_node, # pylint: disable=W0221
-                         hvparams, beparams, root_dir=None):
+                         node_group, hvparams, beparams, root_dir=None):
     """Return information for connecting to the console of an instance.
 
     """
@@ -269,9 +269,11 @@ class ChrootManager(hv_base.BaseHypervisor):
       if not os.path.ismount(root_dir):
         raise HypervisorError("Instance %s is not running" % instance.name)
 
+    ndparams = node_group.FillND(primary_node)
     return objects.InstanceConsole(instance=instance.name,
                                    kind=constants.CONS_SSH,
                                    host=primary_node.name,
+                                   port=ndparams.get(constants.ND_SSH_PORT),
                                    user=constants.SSH_CONSOLE_USER,
                                    command=["chroot", root_dir])
 
diff --git a/lib/hypervisor/hv_fake.py b/lib/hypervisor/hv_fake.py
index 81bbf6c..b2501f5 100644
--- a/lib/hypervisor/hv_fake.py
+++ b/lib/hypervisor/hv_fake.py
@@ -228,7 +228,8 @@ class FakeHypervisor(hv_base.BaseHypervisor):
     return result
 
   @classmethod
-  def GetInstanceConsole(cls, instance, primary_node, hvparams, beparams):
+  def GetInstanceConsole(cls, instance, primary_node, node_group,
+                         hvparams, beparams):
     """Return information for connecting to the console of an instance.
 
     """
diff --git a/lib/hypervisor/hv_kvm.py b/lib/hypervisor/hv_kvm.py
index b379cd2..ed854fe 100644
--- a/lib/hypervisor/hv_kvm.py
+++ b/lib/hypervisor/hv_kvm.py
@@ -2452,7 +2452,8 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     return result
 
   @classmethod
-  def GetInstanceConsole(cls, instance, primary_node, hvparams, beparams):
+  def GetInstanceConsole(cls, instance, primary_node, node_group,
+                         hvparams, beparams):
     """Return a command for connecting to the console of an instance.
 
     """
@@ -2462,9 +2463,11 @@ class KVMHypervisor(hv_base.BaseHypervisor):
              utils.ShellQuote(cls._InstanceMonitor(instance.name)),
              "STDIO,%s" % cls._SocatUnixConsoleParams(),
              "UNIX-CONNECT:%s" % cls._InstanceSerial(instance.name)]
+      ndparams = node_group.FillND(primary_node)
       return objects.InstanceConsole(instance=instance.name,
                                      kind=constants.CONS_SSH,
                                      host=primary_node.name,
+                                     port=ndparams.get(constants.ND_SSH_PORT),
                                      user=constants.SSH_CONSOLE_USER,
                                      command=cmd)
 
diff --git a/lib/hypervisor/hv_lxc.py b/lib/hypervisor/hv_lxc.py
index f5f875a..1471284 100644
--- a/lib/hypervisor/hv_lxc.py
+++ b/lib/hypervisor/hv_lxc.py
@@ -407,13 +407,16 @@ class LXCHypervisor(hv_base.BaseHypervisor):
     return self.GetLinuxNodeInfo()
 
   @classmethod
-  def GetInstanceConsole(cls, instance, primary_node, hvparams, beparams):
+  def GetInstanceConsole(cls, instance, primary_node, node_group,
+                         hvparams, beparams):
     """Return a command for connecting to the console of an instance.
 
     """
+    ndparams = node_group.FillND(primary_node)
     return objects.InstanceConsole(instance=instance.name,
                                    kind=constants.CONS_SSH,
                                    host=primary_node.name,
+                                   port=ndparams.get(constants.ND_SSH_PORT),
                                    user=constants.SSH_CONSOLE_USER,
                                    command=["lxc-console", "-n", instance.name])
 
diff --git a/lib/hypervisor/hv_xen.py b/lib/hypervisor/hv_xen.py
index 71920bf..3c4dae9 100644
--- a/lib/hypervisor/hv_xen.py
+++ b/lib/hypervisor/hv_xen.py
@@ -772,14 +772,17 @@ class XenHypervisor(hv_base.BaseHypervisor):
     instance_list = self._GetInstanceList(True, hvparams)
     return _GetNodeInfo(result.stdout, instance_list)
 
-  def GetInstanceConsole(self, instance, primary_node, hvparams, beparams):
+  def GetInstanceConsole(self, instance, primary_node, node_group,
+                         hvparams, beparams):
     """Return a command for connecting to the console of an instance.
 
     """
     xen_cmd = self._GetCommand(hvparams)
+    ndparams = node_group.FillND(primary_node)
     return objects.InstanceConsole(instance=instance.name,
                                    kind=constants.CONS_SSH,
                                    host=primary_node.name,
+                                   port=ndparams.get(constants.ND_SSH_PORT),
                                    user=constants.SSH_CONSOLE_USER,
                                    command=[pathutils.XEN_CONSOLE_WRAPPER,
                                             xen_cmd, instance.name])
diff --git a/test/py/ganeti.backend_unittest.py b/test/py/ganeti.backend_unittest.py
index 9590c1a..226c8f3 100755
--- a/test/py/ganeti.backend_unittest.py
+++ b/test/py/ganeti.backend_unittest.py
@@ -619,6 +619,7 @@ class TestInstanceConsoleInfo(unittest.TestCase):
     dictMaker = lambda hyName: {
       "instance":{"hypervisor":hyName},
       "node":{},
+      "group":{},
       "hvParams":{},
       "beParams":{},
     }
diff --git a/test/py/ganeti.hypervisor.hv_chroot_unittest.py b/test/py/ganeti.hypervisor.hv_chroot_unittest.py
index 548d8d1..9a5b9e7 100755
--- a/test/py/ganeti.hypervisor.hv_chroot_unittest.py
+++ b/test/py/ganeti.hypervisor.hv_chroot_unittest.py
@@ -44,8 +44,10 @@ class TestConsole(unittest.TestCase):
   def test(self):
     instance = objects.Instance(name="fake.example.com",
                                 primary_node="node837-uuid")
-    node = objects.Node(name="node837", uuid="node837-uuid")
-    cons = hv_chroot.ChrootManager.GetInstanceConsole(instance, node, {}, {},
+    node = objects.Node(name="node837", uuid="node837-uuid", ndparams={})
+    group = objects.NodeGroup(name="group164", ndparams={})
+    cons = hv_chroot.ChrootManager.GetInstanceConsole(instance, node, group,
+                                                      {}, {},
                                                       root_dir=self.tmpdir)
     self.assertTrue(cons.Validate())
     self.assertEqual(cons.kind, constants.CONS_SSH)
diff --git a/test/py/ganeti.hypervisor.hv_fake_unittest.py b/test/py/ganeti.hypervisor.hv_fake_unittest.py
index 700e0e9..ef0292e 100755
--- a/test/py/ganeti.hypervisor.hv_fake_unittest.py
+++ b/test/py/ganeti.hypervisor.hv_fake_unittest.py
@@ -35,8 +35,10 @@ import testutils
 class TestConsole(unittest.TestCase):
   def test(self):
     instance = objects.Instance(name="fake.example.com")
-    node = objects.Node(name="fakenode.example.com")
-    cons = hv_fake.FakeHypervisor.GetInstanceConsole(instance, node, {}, {})
+    node = objects.Node(name="fakenode.example.com", ndparams={})
+    group = objects.NodeGroup(name="default", ndparams={})
+    cons = hv_fake.FakeHypervisor.GetInstanceConsole(instance, node, group,
+                                                     {}, {})
     self.assertTrue(cons.Validate())
     self.assertEqual(cons.kind, constants.CONS_MESSAGE)
 
diff --git a/test/py/ganeti.hypervisor.hv_kvm_unittest.py b/test/py/ganeti.hypervisor.hv_kvm_unittest.py
index 698c080..723f698 100755
--- a/test/py/ganeti.hypervisor.hv_kvm_unittest.py
+++ b/test/py/ganeti.hypervisor.hv_kvm_unittest.py
@@ -200,21 +200,24 @@ class TestQmp(testutils.GanetiTestCase):
 
 
 class TestConsole(unittest.TestCase):
-  def _Test(self, instance, node, hvparams):
-    cons = hv_kvm.KVMHypervisor.GetInstanceConsole(instance, node, hvparams, {})
+  def _Test(self, instance, node, group, hvparams):
+    cons = hv_kvm.KVMHypervisor.GetInstanceConsole(instance, node, group,
+                                                   hvparams, {})
     self.assertTrue(cons.Validate())
     return cons
 
   def testSerial(self):
     instance = objects.Instance(name="kvm.example.com",
                                 primary_node="node6017-uuid")
-    node = objects.Node(name="node6017", uuid="node6017-uuid")
+    node = objects.Node(name="node6017", uuid="node6017-uuid",
+                        ndparams={})
+    group = objects.NodeGroup(name="group6134", ndparams={})
     hvparams = {
       constants.HV_SERIAL_CONSOLE: True,
       constants.HV_VNC_BIND_ADDRESS: None,
       constants.HV_KVM_SPICE_BIND: None,
       }
-    cons = self._Test(instance, node, hvparams)
+    cons = self._Test(instance, node, group, hvparams)
     self.assertEqual(cons.kind, constants.CONS_SSH)
     self.assertEqual(cons.host, node.name)
     self.assertEqual(cons.command[0], pathutils.KVM_CONSOLE_WRAPPER)
@@ -224,13 +227,15 @@ class TestConsole(unittest.TestCase):
     instance = objects.Instance(name="kvm.example.com",
                                 primary_node="node7235-uuid",
                                 network_port=constants.VNC_BASE_PORT + 10)
-    node = objects.Node(name="node7235", uuid="node7235-uuid")
+    node = objects.Node(name="node7235", uuid="node7235-uuid",
+                        ndparams={})
+    group = objects.NodeGroup(name="group3632", ndparams={})
     hvparams = {
       constants.HV_SERIAL_CONSOLE: False,
       constants.HV_VNC_BIND_ADDRESS: "192.0.2.1",
       constants.HV_KVM_SPICE_BIND: None,
       }
-    cons = self._Test(instance, node, hvparams)
+    cons = self._Test(instance, node, group, hvparams)
     self.assertEqual(cons.kind, constants.CONS_VNC)
     self.assertEqual(cons.host, "192.0.2.1")
     self.assertEqual(cons.port, constants.VNC_BASE_PORT + 10)
@@ -240,13 +245,15 @@ class TestConsole(unittest.TestCase):
     instance = objects.Instance(name="kvm.example.com",
                                 primary_node="node7235",
                                 network_port=11000)
-    node = objects.Node(name="node7235", uuid="node7235-uuid")
+    node = objects.Node(name="node7235", uuid="node7235-uuid",
+                        ndparams={})
+    group = objects.NodeGroup(name="group0132", ndparams={})
     hvparams = {
       constants.HV_SERIAL_CONSOLE: False,
       constants.HV_VNC_BIND_ADDRESS: None,
       constants.HV_KVM_SPICE_BIND: "192.0.2.1",
       }
-    cons = self._Test(instance, node, hvparams)
+    cons = self._Test(instance, node, group, hvparams)
     self.assertEqual(cons.kind, constants.CONS_SPICE)
     self.assertEqual(cons.host, "192.0.2.1")
     self.assertEqual(cons.port, 11000)
@@ -255,13 +262,15 @@ class TestConsole(unittest.TestCase):
     instance = objects.Instance(name="kvm.example.com",
                                 primary_node="node24325",
                                 network_port=0)
-    node = objects.Node(name="node24325", uuid="node24325-uuid")
+    node = objects.Node(name="node24325", uuid="node24325-uuid",
+                        ndparams={})
+    group = objects.NodeGroup(name="group9184", ndparams={})
     hvparams = {
       constants.HV_SERIAL_CONSOLE: False,
       constants.HV_VNC_BIND_ADDRESS: None,
       constants.HV_KVM_SPICE_BIND: None,
       }
-    cons = self._Test(instance, node, hvparams)
+    cons = self._Test(instance, node, group, hvparams)
     self.assertEqual(cons.kind, constants.CONS_MESSAGE)
 
 
diff --git a/test/py/ganeti.hypervisor.hv_lxc_unittest.py b/test/py/ganeti.hypervisor.hv_lxc_unittest.py
index 62ba58e..bd41476 100755
--- a/test/py/ganeti.hypervisor.hv_lxc_unittest.py
+++ b/test/py/ganeti.hypervisor.hv_lxc_unittest.py
@@ -36,8 +36,11 @@ class TestConsole(unittest.TestCase):
   def test(self):
     instance = objects.Instance(name="lxc.example.com",
                                 primary_node="node199-uuid")
-    node = objects.Node(name="node199", uuid="node199-uuid")
-    cons = hv_lxc.LXCHypervisor.GetInstanceConsole(instance, node, {}, {})
+    node = objects.Node(name="node199", uuid="node199-uuid",
+                        ndparams={})
+    group = objects.NodeGroup(name="group991", ndparams={})
+    cons = hv_lxc.LXCHypervisor.GetInstanceConsole(instance, node, group,
+                                                   {}, {})
     self.assertTrue(cons.Validate())
     self.assertEqual(cons.kind, constants.CONS_SSH)
     self.assertEqual(cons.host, node.name)
diff --git a/test/py/ganeti.hypervisor.hv_xen_unittest.py b/test/py/ganeti.hypervisor.hv_xen_unittest.py
index 81749c0..3ae25cf 100755
--- a/test/py/ganeti.hypervisor.hv_xen_unittest.py
+++ b/test/py/ganeti.hypervisor.hv_xen_unittest.py
@@ -53,8 +53,10 @@ class TestConsole(unittest.TestCase):
     for cls in [hv_xen.XenPvmHypervisor(), hv_xen.XenHvmHypervisor()]:
       instance = objects.Instance(name="xen.example.com",
                                   primary_node="node24828-uuid")
-      node = objects.Node(name="node24828", uuid="node24828-uuid")
-      cons = cls.GetInstanceConsole(instance, node, hvparams, {})
+      node = objects.Node(name="node24828", uuid="node24828-uuid",
+                          ndparams={})
+      group = objects.NodeGroup(name="group52341", ndparams={})
+      cons = cls.GetInstanceConsole(instance, node, group, hvparams, {})
       self.assertTrue(cons.Validate())
       self.assertEqual(cons.kind, constants.CONS_SSH)
       self.assertEqual(cons.host, node.name)
