The attached patch related to https://fedorahosted.org/freeipa/ticket/4931

It is certainly not a final solution, more of an initial "hack" of sorts just to gather some suggestions, since I am not even sure if this is the right thing to do.

The reporter from bugzilla suggests to enable debugging of ALL commands called through ipautil.run(), but I think that fixing all cca 157 found usages of run() is too much work with a quite small benefit.

Anyway I would welcome some opinions about this: should the external commands really inherit the debug settings of ipa-* utilities, and if so, is the method showed in this patch the right way to do it?

--
Martin^3 Babinsky
From 3d2329e8caa8392a3efe536033ce49692fe675cd Mon Sep 17 00:00:00 2001
From: Martin Babinsky <[email protected]>
Date: Mon, 23 Mar 2015 17:04:51 +0100
Subject: [PATCH] enable debbuging of spawned ntpd command during client
 install

More of a proof-of-concept patch since the request in
https://fedorahosted.org/freeipa/ticket/4931 calls for this functionality in
ALL spawned commands.
---
 ipa-client/ipa-install/ipa-client-install |  5 +++--
 ipa-client/ipaclient/ntpconf.py           |  5 +++--
 ipapython/ipautil.py                      | 11 ++++++++++-
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/ipa-client/ipa-install/ipa-client-install b/ipa-client/ipa-install/ipa-client-install
index 711fa06e8dea50e263f94d0d83b2b1c1be4e88d9..84bdbe8f35ee4b9268a77fd77813c77aa2d90093 100755
--- a/ipa-client/ipa-install/ipa-client-install
+++ b/ipa-client/ipa-install/ipa-client-install
@@ -2333,11 +2333,12 @@ def install(options, env, fstore, statestore):
         synced_ntp = False
         if ntp_servers:
             for s in ntp_servers:
-                synced_ntp = ipaclient.ntpconf.synconce_ntp(s)
+                synced_ntp = ipaclient.ntpconf.synconce_ntp(s, options.debug)
                 if synced_ntp:
                     break
         if not synced_ntp:
-            synced_ntp = ipaclient.ntpconf.synconce_ntp(cli_server[0])
+            synced_ntp = ipaclient.ntpconf.synconce_ntp(cli_server[0],
+                                                        options.debug)
         if not synced_ntp:
             root_logger.warning("Unable to sync time with IPA NTP " +
                 "server, assuming the time is in sync. Please check " +
diff --git a/ipa-client/ipaclient/ntpconf.py b/ipa-client/ipaclient/ntpconf.py
index 7d5c82a89b51f68362f12869a9234f5b69aa5ba9..bbfb1bd6671a230ded86ce80dd067bb3c5a8d571 100644
--- a/ipa-client/ipaclient/ntpconf.py
+++ b/ipa-client/ipaclient/ntpconf.py
@@ -136,7 +136,7 @@ def config_ntp(server_fqdn, fstore = None, sysstore = None):
     services.knownservices.ntpd.restart()
 
 
-def synconce_ntp(server_fqdn):
+def synconce_ntp(server_fqdn, debug=False):
     """
     Syncs time with specified server using ntpd.
     Primarily designed to be used before Kerberos setup
@@ -155,7 +155,8 @@ def synconce_ntp(server_fqdn):
         timeout = 15
         root_logger.info('Attempting to sync time using ntpd.  '
                          'Will timeout after %d seconds' % timeout)
-        ipautil.run([ntpd, '-qgc', tmp_ntp_conf.name], timeout=timeout)
+        ipautil.run([ntpd, '-qgc', tmp_ntp_conf.name], timeout=timeout,
+                    debug=debug)
         return True
     except ipautil.CalledProcessError:
         return False
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
index 6a06a8e956552597dfd48128b60a1dd6a4cc92f6..cc529abdb605a165d43a4fc2a7decc39b9a1b037 100644
--- a/ipapython/ipautil.py
+++ b/ipapython/ipautil.py
@@ -249,7 +249,7 @@ def shell_quote(string):
 
 def run(args, stdin=None, raiseonerr=True,
         nolog=(), env=None, capture_output=True, skip_output=False, cwd=None,
-        runas=None, timeout=None):
+        runas=None, timeout=None, debug=False, debug_opt='-d'):
     """
     Execute a command and return stdin, stdout and the process return code.
 
@@ -279,6 +279,13 @@ def run(args, stdin=None, raiseonerr=True,
         process will have both real and effective UID and GID set.
     :param timeout: Timeout if the command hasn't returned within the specified
         number of seconds.
+    :param debug: If True, the command is spawned with debug option and both
+        stdout and stderr will be logged.
+    :param debug_opt: The option which enables debug mode for the spawned
+        command if `debug` is True. Please note that there are numerous ways to
+        switch on debug/verbose output of spawned commands/utilities ('-d',
+        '-v', '--debug', etc.) so always make sure that the command you plan to
+        run in debug mode will use the correct one. '-d' is used as default.
     """
     p_in = None
     p_out = None
@@ -308,6 +315,8 @@ def run(args, stdin=None, raiseonerr=True,
         # If a timeout was provided, use the timeout command
         # to execute the requested command.
         args[0:0] = [paths.BIN_TIMEOUT, str(timeout)]
+    if debug:
+        args.append(debug_opt)
 
     arg_string = nolog_replace(' '.join(shell_quote(a) for a in args), nolog)
     root_logger.debug('Starting external process')
-- 
2.1.0

-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to