From 57821cac8be4e159fe75057dadb489cdff81623d Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 14 Oct 2008 10:21:12 +0000
Subject: [PATCH] Allow instance info to only query the config file
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch adds a new '-s' parameter to β€˜gnt-instance info’ that makes
it return only 'static' information. This is much faster, especially for
drbd instances.

This is a forward-port of rev 1570 on the ganeti-1.2 branch, resending
due to some conflicts.

Reviewed-by: imsnah
---
 lib/cmdlib.py         | 28 ++++++++++++++++++----------
 lib/opcodes.py        |  2 +-
 man/gnt-instance.sgml | 10 ++++++++++
 scripts/gnt-instance  | 32 +++++++++++++++++++++-----------
 4 files changed, 50 insertions(+), 22 deletions(-)

diff --git a/lib/cmdlib.py b/lib/cmdlib.py
index 435ab645c..a3f5209a9 100644
--- a/lib/cmdlib.py
+++ b/lib/cmdlib.py
@@ -4312,7 +4312,7 @@ class LUQueryInstanceData(NoHooksLU):
   """Query runtime instance data.
 
   """
-  _OP_REQP = ["instances"]
+  _OP_REQP = ["instances", "static"]
   REQ_BGL = False
 
   def ExpandNames(self):
@@ -4359,8 +4359,13 @@ class LUQueryInstanceData(NoHooksLU):
     """Compute block device status.
 
     """
-    self.cfg.SetDiskID(dev, instance.primary_node)
-    dev_pstatus = self.rpc.call_blockdev_find(instance.primary_node, dev)
+    static = self.op.static
+    if not static:
+      self.cfg.SetDiskID(dev, instance.primary_node)
+      dev_pstatus = self.rpc.call_blockdev_find(instance.primary_node, dev)
+    else:
+      dev_pstatus = None
+
     if dev.dev_type in constants.LDS_DRBD:
       # we change the snode then (otherwise we use the one passed in)
       if dev.logical_id[0] == instance.primary_node:
@@ -4368,7 +4373,7 @@ class LUQueryInstanceData(NoHooksLU):
       else:
         snode = dev.logical_id[0]
 
-    if snode:
+    if snode and not static:
       self.cfg.SetDiskID(dev, snode)
       dev_sstatus = self.rpc.call_blockdev_find(snode, dev)
     else:
@@ -4396,13 +4401,16 @@ class LUQueryInstanceData(NoHooksLU):
     """Gather and return data"""
     result = {}
     for instance in self.wanted_instances:
-      remote_info = self.rpc.call_instance_info(instance.primary_node,
-                                                instance.name,
-                                                instance.hypervisor)
-      if remote_info and "state" in remote_info:
-        remote_state = "up"
+      if not self.op.static:
+        remote_info = self.rpc.call_instance_info(instance.primary_node,
+                                                  instance.name,
+                                                  instance.hypervisor)
+        if remote_info and "state" in remote_info:
+          remote_state = "up"
+        else:
+          remote_state = "down"
       else:
-        remote_state = "down"
+        remote_state = None
       if instance.status == "down":
         config_state = "down"
       else:
diff --git a/lib/opcodes.py b/lib/opcodes.py
index cd9b88ee1..3fcffa621 100644
--- a/lib/opcodes.py
+++ b/lib/opcodes.py
@@ -415,7 +415,7 @@ class OpQueryInstances(OpCode):
 class OpQueryInstanceData(OpCode):
   """Compute the run-time status of instances."""
   OP_ID = "OP_INSTANCE_QUERY_DATA"
-  __slots__ = ["instances"]
+  __slots__ = ["instances", "static"]
 
 
 class OpSetInstanceParams(OpCode):
diff --git a/man/gnt-instance.sgml b/man/gnt-instance.sgml
index 5141ba162..5e314aa4d 100644
--- a/man/gnt-instance.sgml
+++ b/man/gnt-instance.sgml
@@ -661,6 +661,10 @@
 
         <cmdsynopsis>
           <command>info</command>
+          <group>
+            <arg>-s</arg>
+            <arg>--static</arg>
+          </group>
           <arg rep="repeat"><replaceable>instance</replaceable></arg>
         </cmdsynopsis>
 
@@ -670,6 +674,12 @@
           detailed data about the instance's disks (especially useful
           for drbd disk template).
         </para>
+
+        <para>
+          If the option <option>-s</option> is used, only information
+          available in the configuration file is returned, without
+          querying nodes, making the operation faster.
+        </para>
       </refsect3>
 
       <refsect3>
diff --git a/scripts/gnt-instance b/scripts/gnt-instance
index ceaf6a5af..d3308a327 100755
--- a/scripts/gnt-instance
+++ b/scripts/gnt-instance
@@ -614,7 +614,7 @@ def ConnectToInstanceConsole(opts, args):
       os._exit(1)
 
 
-def _FormatBlockDevInfo(buf, dev, indent_level):
+def _FormatBlockDevInfo(buf, dev, indent_level, static):
   """Show block device information.
 
   This is only used by ShowInstanceConfig(), but it's too big to be
@@ -674,16 +674,17 @@ def _FormatBlockDevInfo(buf, dev, indent_level):
   elif dev["physical_id"] is not None:
     data += ", physical_id: %s" % (dev["physical_id"],)
   buf.write("%*s%s\n" % (2*indent_level, "", data))
-  buf.write("%*s    primary:   " % (2*indent_level, ""))
-  helper(buf, dev["dev_type"], dev["pstatus"])
+  if not static:
+    buf.write("%*s    primary:   " % (2*indent_level, ""))
+    helper(buf, dev["dev_type"], dev["pstatus"])
 
-  if dev["sstatus"]:
+  if dev["sstatus"] and not static:
     buf.write("%*s    secondary: " % (2*indent_level, ""))
     helper(buf, dev["dev_type"], dev["sstatus"])
 
   if dev["children"]:
     for child in dev["children"]:
-      _FormatBlockDevInfo(buf, child, indent_level+1)
+      _FormatBlockDevInfo(buf, child, indent_level+1, static)
 
 
 def ShowInstanceConfig(opts, args):
@@ -691,7 +692,7 @@ def ShowInstanceConfig(opts, args):
 
   """
   retcode = 0
-  op = opcodes.OpQueryInstanceData(instances=args)
+  op = opcodes.OpQueryInstanceData(instances=args, static=opts.static)
   result = SubmitOpCode(op)
   if not result:
     logger.ToStdout("No instances.")
@@ -702,8 +703,12 @@ def ShowInstanceConfig(opts, args):
   for instance_name in result:
     instance = result[instance_name]
     buf.write("Instance name: %s\n" % instance["name"])
-    buf.write("State: configured to be %s, actual state is %s\n" %
-              (instance["config_state"], instance["run_state"]))
+    buf.write("State: configured to be %s" % instance["config_state"])
+    if not opts.static:
+      buf.write(", actual state is %s" % instance["run_state"])
+    buf.write("\n")
+    ##buf.write("Considered for memory checks in cluster verify: %s\n" %
+    ##          instance["auto_balance"])
     buf.write("  Nodes:\n")
     buf.write("    - primary: %s\n" % instance["pnode"])
     buf.write("    - secondaries: %s\n" % ", ".join(instance["snodes"]))
@@ -757,7 +762,7 @@ def ShowInstanceConfig(opts, args):
     buf.write("  Block devices:\n")
 
     for device in instance["disks"]:
-      _FormatBlockDevInfo(buf, device, 1)
+      _FormatBlockDevInfo(buf, device, 1, opts.static)
 
   logger.ToStdout(buf.getvalue().rstrip('\n'))
   return retcode
@@ -923,8 +928,13 @@ commands = {
                "[-f] <instance>",
                "Stops the instance and starts it on the backup node, using"
                " the remote mirror (only for instances of type drbd)"),
-  'info': (ShowInstanceConfig, ARGS_ANY, [DEBUG_OPT], "[<instance>...]",
-           "Show information on the specified instance"),
+  'info': (ShowInstanceConfig, ARGS_ANY,
+           [DEBUG_OPT,
+            make_option("-s", "--static", dest="static",
+                        action="store_true", default=False,
+                        help="Only show configuration data, not runtime data"),
+            ], "[-s] [<instance>...]",
+           "Show information on the specified instance(s)"),
   'list': (ListInstances, ARGS_NONE,
            [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], "",
            "Lists the instances and their status. The available fields are"
-- 
GitLab