From d5b031dc9e2e1a88296ecc408bcf4964497e0c18 Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Tue, 4 Dec 2012 18:10:15 +0100
Subject: [PATCH] Allow ignoring successful commands in "gnt-cluster command"

In some cases it is useful to ignore the output of and avoid mentioning
successful commands. One would be when looking for a certain string in
a file:

  $ gnt-cluster command egrep -q '^testing$' /etc/...

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Helga Velroyen <helgav@google.com>
---
 lib/cli.py                | 6 ++++++
 lib/client/gnt_cluster.py | 7 ++++++-
 man/gnt-cluster.rst       | 3 ++-
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/lib/cli.py b/lib/cli.py
index b7a4d3129..603cdc474 100644
--- a/lib/cli.py
+++ b/lib/cli.py
@@ -82,6 +82,7 @@ __all__ = [
   "EARLY_RELEASE_OPT",
   "ENABLED_HV_OPT",
   "ERROR_CODES_OPT",
+  "FAILURE_ONLY_OPT",
   "FIELDS_OPT",
   "FILESTORE_DIR_OPT",
   "FILESTORE_DRIVER_OPT",
@@ -1363,6 +1364,11 @@ SHOW_MACHINE_OPT = cli_option("-M", "--show-machine-names", default=False,
                               action="store_true",
                               help="Show machine name for every line in output")
 
+FAILURE_ONLY_OPT = cli_option("--failure-only", default=False,
+                              action="store_true",
+                              help=("Hide successful results and show failures"
+                                    " only (determined by the exit code)"))
+
 
 def _PriorityOptionCb(option, _, value, parser):
   """Callback for processing C{--priority} option.
diff --git a/lib/client/gnt_cluster.py b/lib/client/gnt_cluster.py
index ecfa4ce75..ccbc7a9e6 100644
--- a/lib/client/gnt_cluster.py
+++ b/lib/client/gnt_cluster.py
@@ -539,6 +539,11 @@ def RunClusterCommand(opts, args):
 
   for name in nodes:
     result = srun.Run(name, constants.SSH_LOGIN_USER, command)
+
+    if opts.failure_only and result.exit_code == constants.EXIT_SUCCESS:
+      # Do not output anything for successful commands
+      continue
+
     ToStdout("------------------------------------------------")
     if opts.show_machine_names:
       for line in result.output.splitlines():
@@ -1524,7 +1529,7 @@ commands = {
     "[-n node...] <filename>", "Copies a file to all (or only some) nodes"),
   "command": (
     RunClusterCommand, [ArgCommand(min=1)],
-    [NODE_LIST_OPT, NODEGROUP_OPT, SHOW_MACHINE_OPT],
+    [NODE_LIST_OPT, NODEGROUP_OPT, SHOW_MACHINE_OPT, FAILURE_ONLY_OPT],
     "[-n node...] <command>", "Runs a command on all (or only some) nodes"),
   "info": (
     ShowClusterConfig, ARGS_NONE, [ROMAN_OPT],
diff --git a/man/gnt-cluster.rst b/man/gnt-cluster.rst
index e4bcc28c2..b67f90421 100644
--- a/man/gnt-cluster.rst
+++ b/man/gnt-cluster.rst
@@ -59,7 +59,8 @@ group, e.g.::
     # gnt-cluster command -g default date
 
 The ``-M`` option can be used to prepend the node name to all output
-lines.
+lines. The ``--failure-only`` option hides successful commands, making
+it easier to see failures.
 
 The command is executed serially on the selected nodes. If the
 master node is present in the list, the command will be executed
-- 
GitLab