From 56b9f2db82d8bde071358476faca4db77180c20c Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Tue, 5 Feb 2013 16:22:00 +0100
Subject: [PATCH] Implement option to skip logging of QA commands
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This patch adds an optional kwarg that allows skipping the "Command:
…" line in QA output. This is helpful if we only need to run some
maintenance actions, that are QA-related and not Ganeti-related.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>
---
 qa/qa_utils.py | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/qa/qa_utils.py b/qa/qa_utils.py
index 9a67a0a77..6f37aecbc 100644
--- a/qa/qa_utils.py
+++ b/qa/qa_utils.py
@@ -160,7 +160,7 @@ def _AssertRetCode(rcode, fail, cmdstr, nodename):
                          (cmdstr, nodename, rcode))
 
 
-def AssertCommand(cmd, fail=False, node=None):
+def AssertCommand(cmd, fail=False, node=None, log_cmd=True):
   """Checks that a remote command succeeds.
 
   @param cmd: either a string (the command to execute) or a list (to
@@ -170,6 +170,8 @@ def AssertCommand(cmd, fail=False, node=None):
   @param node: if passed, it should be the node on which the command
       should be executed, instead of the master node (can be either a
       dict or a string)
+  @param log_cmd: if False, the command won't be logged (simply passed to
+      StartSSH)
   @return: the return code of the command
   @raise qa_error.Error: if the command fails when it shouldn't or vice versa
 
@@ -184,7 +186,7 @@ def AssertCommand(cmd, fail=False, node=None):
   else:
     cmdstr = utils.ShellQuoteArgs(cmd)
 
-  rcode = StartSSH(nodename, cmdstr).wait()
+  rcode = StartSSH(nodename, cmdstr, log_cmd=log_cmd).wait()
   _AssertRetCode(rcode, fail, cmdstr, nodename)
 
   return rcode
@@ -234,24 +236,25 @@ def GetSSHCommand(node, cmd, strict=True, opts=None, tty=None):
   return args
 
 
-def StartLocalCommand(cmd, _nolog_opts=False, **kwargs):
+def StartLocalCommand(cmd, _nolog_opts=False, log_cmd=True, **kwargs):
   """Starts a local command.
 
   """
-  if _nolog_opts:
-    pcmd = [i for i in cmd if not i.startswith("-")]
-  else:
-    pcmd = cmd
-  print "Command: %s" % utils.ShellQuoteArgs(pcmd)
+  if log_cmd:
+    if _nolog_opts:
+      pcmd = [i for i in cmd if not i.startswith("-")]
+    else:
+      pcmd = cmd
+    print "Command: %s" % utils.ShellQuoteArgs(pcmd)
   return subprocess.Popen(cmd, shell=False, **kwargs)
 
 
-def StartSSH(node, cmd, strict=True):
+def StartSSH(node, cmd, strict=True, log_cmd=True):
   """Starts SSH.
 
   """
   return StartLocalCommand(GetSSHCommand(node, cmd, strict=strict),
-                           _nolog_opts=True)
+                           _nolog_opts=True, log_cmd=log_cmd)
 
 
 def StartMultiplexer(node):
-- 
GitLab