The following 2 patches enhance the ipactl command output (also used in
the ipa init script).

The first patch fixes ticket #765, the second nis just for coherency
with other scripts like this.

Simo.

-- 
Simo Sorce * Red Hat, Inc * New York
>From 3c510115dc4a47d0f1447fe06d1333411e5bee94 Mon Sep 17 00:00:00 2001
From: Simo Sorce <sso...@redhat.com>
Date: Mon, 17 Jan 2011 09:17:08 -0500
Subject: [PATCH 1/3] Add a way to print output from commands

Instead pof always capturing the output, make it possible to let
it go to the standard output pipes.
Use this in ipactl to let init scripts show their output.

Fixes: https://fedorahosted.org/freeipa/ticket/765
---
 install/tools/ipactl         |   28 ++++++++++++++--------------
 ipapython/ipautil.py         |   27 +++++++++++++++++++--------
 ipaserver/install/service.py |   27 +++++++++++++++------------
 3 files changed, 48 insertions(+), 34 deletions(-)

diff --git a/install/tools/ipactl b/install/tools/ipactl
index 059b86049272dd97332e698fbb3d378e6fe8a11c..ee6783ed328235e09458665eadd9c53f9110a904 100755
--- a/install/tools/ipactl
+++ b/install/tools/ipactl
@@ -88,7 +88,7 @@ def ipa_start(serverid):
 
     try:
         print "Starting Directory Service"
-        service.start('dirsrv', instance_name=serverid)
+        service.start('dirsrv', instance_name=serverid, capture_output=False)
     except:
         emit_err("Failed to start Directory Service")
         return
@@ -99,7 +99,7 @@ def ipa_start(serverid):
     except:
         emit_err("Failed to read data from Directory Service")
         emit_err("Shutting down")
-        service.stop('dirsrv', instance_name=serverid)
+        service.stop('dirsrv', instance_name=serverid, capture_output=False)
 
     if len(svc_list) == 0:
         return
@@ -108,18 +108,18 @@ def ipa_start(serverid):
         svc_name = service.SERVICE_LIST[svc][0]
         try:
             print "Starting %s Service" % svc
-            service.start(svc_name)
+            service.start(svc_name, capture_output=False)
         except:
             emit_err("Failed to start %s Service" % svc)
             emit_err("Shutting down")
             for (order, svc) in sorted(svc_list):
                 svc_name = service.SERVICE_LIST[svc][0]
                 try:
-                    service.stop(svc_name)
+                    service.stop(svc_name, capture_output=False)
                 except:
                     pass
             try:
-                service.stop('dirsrv', instance_name=serverid)
+                service.stop('dirsrv', instance_name=serverid, capture_output=False)
             except:
                 pass
             return
@@ -134,12 +134,12 @@ def ipa_stop(serverid):
         # and see if we can get anything. If not throw our hands up and just
         # exit
         try:
-            service.start('dirsrv', instance_name=serverid)
+            service.start('dirsrv', instance_name=serverid, capture_output=False)
             svc_list = get_config()
         except:
             emit_err("Failed to read data from Directory Service")
             emit_err("Shutting down")
-            service.stop('dirsrv', instance_name=serverid)
+            service.stop('dirsrv', instance_name=serverid, capture_output=False)
 
     if len(svc_list) == 0:
         return
@@ -148,13 +148,13 @@ def ipa_stop(serverid):
         svc_name = service.SERVICE_LIST[svc][0]
         try:
             print "Stopping %s Service" % svc
-            service.stop(svc_name)
+            service.stop(svc_name, capture_output=False)
         except:
             emit_err("Failed to stop %s Service" % svc)
 
     try:
         print "Stopping Directory Service"
-        service.stop('dirsrv', instance_name=serverid)
+        service.stop('dirsrv', instance_name=serverid, capture_output=False)
     except:
         emit_err("Failed to stop Directory Service")
         return
@@ -163,7 +163,7 @@ def ipa_stop(serverid):
 def ipa_restart(serverid):
     try:
         print "Restarting Directory Service"
-        service.restart('dirsrv', instance_name=serverid)
+        service.restart('dirsrv', instance_name=serverid, capture_output=False)
     except:
         emit_err("Failed to restart Directory Service")
         return
@@ -174,7 +174,7 @@ def ipa_restart(serverid):
     except:
         emit_err("Failed to read data from Directory Service")
         emit_err("Shutting down")
-        service.stop('dirsrv', instance_name=serverid)
+        service.stop('dirsrv', instance_name=serverid, capture_output=False)
 
     if len(svc_list) == 0:
         return
@@ -183,18 +183,18 @@ def ipa_restart(serverid):
         svc_name = service.SERVICE_LIST[svc][0]
         try:
             print "Restarting %s Service" % svc
-            service.restart(svc_name)
+            service.restart(svc_name, capture_output=False)
         except:
             emit_err("Failed to restart %s Service" % svc)
             emit_err("Shutting down")
             for (order, svc) in sorted(svc_list):
                 svc_name = service.SERVICE_LIST[svc][0]
                 try:
-                    service.stop(svc_name)
+                    service.stop(svc_name, capture_output=False)
                 except:
                     pass
             try:
-                service.stop('dirsrv', instance_name=serverid)
+                service.stop('dirsrv', instance_name=serverid, capture_output=False)
             except:
                 pass
             return
diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
index 77c838e80baf03eb9f6101580f8c17537060c48d..da6ccde13a4f2ce4592609c02fb32d27dfc4d1eb 100644
--- a/ipapython/ipautil.py
+++ b/ipapython/ipautil.py
@@ -90,7 +90,8 @@ def write_tmp_file(txt):
 
     return fd
 
-def run(args, stdin=None, raiseonerr=True, nolog=(), env=None):
+def run(args, stdin=None, raiseonerr=True,
+        nolog=(), env=None, capture_output=True):
     """
     Execute a command and return stdin, stdout and the process return code.
 
@@ -114,14 +115,23 @@ def run(args, stdin=None, raiseonerr=True, nolog=(), env=None):
 
     If an value isn't found in the list it is silently ignored.
     """
+    p_in = None
+    p_out = None
+    p_err = None
+
     if env is None:
         env={"PATH": "/bin:/sbin:/usr/kerberos/bin:/usr/kerberos/sbin:/usr/bin:/usr/sbin"}
     if stdin:
-        p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, env=env)
-        stdout,stderr = p.communicate(stdin)
-    else:
-        p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, env=env)
-        stdout,stderr = p.communicate()
+        p_in = subprocess.PIPE
+    if capture_output:
+        p_out = subprocess.PIPE
+        p_err = subprocess.PIPE
+    elif len(nolog):
+        raise RuntimeError("Can't use nolog if output is not captured")
+
+    p = subprocess.Popen(args, stdin=p_in, stdout=p_out, stderr=p_err,
+                         close_fds=True, env=env)
+    stdout,stderr = p.communicate(stdin)
 
     # The command and its output may include passwords that we don't want
     # to log. Run through the nolog items.
@@ -135,8 +145,9 @@ def run(args, stdin=None, raiseonerr=True, nolog=(), env=None):
         stdout = stdout.replace(quoted, 'XXXXXXXX')
         stderr = stderr.replace(quoted, 'XXXXXXXX')
     logging.info('args=%s' % args)
-    logging.info('stdout=%s' % stdout)
-    logging.info('stderr=%s' % stderr)
+    if capture_output:
+        logging.info('stdout=%s' % stdout)
+        logging.info('stderr=%s' % stderr)
 
     if p.returncode != 0 and raiseonerr:
         raise CalledProcessError(p.returncode, args)
diff --git a/ipaserver/install/service.py b/ipaserver/install/service.py
index 27c55618e70cbdc2c76d012739733eba705924f0..78e7bb8da4ee5d16e25f98ce828960508fe0cfc2 100644
--- a/ipaserver/install/service.py
+++ b/ipaserver/install/service.py
@@ -41,14 +41,17 @@ SERVICE_LIST = {
     'CA':('pki-cad', 50)
 }
 
-def stop(service_name, instance_name=""):
-    ipautil.run(["/sbin/service", service_name, "stop", instance_name])
+def stop(service_name, instance_name="", capture_output=True):
+    ipautil.run(["/sbin/service", service_name, "stop", instance_name],
+                capture_output=capture_output)
 
-def start(service_name, instance_name=""):
-    ipautil.run(["/sbin/service", service_name, "start", instance_name])
+def start(service_name, instance_name="", capture_output=True):
+    ipautil.run(["/sbin/service", service_name, "start", instance_name],
+                capture_output=capture_output)
 
-def restart(service_name, instance_name=""):
-    ipautil.run(["/sbin/service", service_name, "restart", instance_name])
+def restart(service_name, instance_name="", capture_output=True):
+    ipautil.run(["/sbin/service", service_name, "restart", instance_name],
+                capture_output=capture_output)
 
 def is_running(service_name, instance_name=""):
     ret = True
@@ -212,14 +215,14 @@ class Service:
     def set_output(self, fd):
         self.output_fd = fd
 
-    def stop(self, instance_name=""):
-        stop(self.service_name, instance_name)
+    def stop(self, instance_name="", capture_output=True):
+        stop(self.service_name, instance_name, capture_output=capture_output)
 
-    def start(self, instance_name=""):
-        start(self.service_name, instance_name)
+    def start(self, instance_name="", capture_output=True):
+        start(self.service_name, instance_name, capture_output=capture_output)
 
-    def restart(self, instance_name=""):
-        restart(self.service_name, instance_name)
+    def restart(self, instance_name="", capture_output=True):
+        restart(self.service_name, instance_name, capture_output=capture_output)
 
     def is_running(self):
         return is_running(self.service_name)
-- 
1.7.3.4

>From 7014b415f4d25cf433310d05c4136a329b76b89f Mon Sep 17 00:00:00 2001
From: Simo Sorce <sso...@redhat.com>
Date: Mon, 17 Jan 2011 09:20:35 -0500
Subject: [PATCH 2/3] Let ipactl output errors to stderr

Init scripts normally do not log to syslog, instead they write errors to the
stderr pipe. Do the same.
---
 install/tools/ipactl |    9 ++-------
 1 files changed, 2 insertions(+), 7 deletions(-)

diff --git a/install/tools/ipactl b/install/tools/ipactl
index ee6783ed328235e09458665eadd9c53f9110a904..e296cc121e67c8f213ece70597c49d34c45896f1 100755
--- a/install/tools/ipactl
+++ b/install/tools/ipactl
@@ -27,7 +27,7 @@ try:
     import logging
     import ldap
     import socket
-    import syslog
+    import sys
 except ImportError:
     print >> sys.stderr, """\
 There was a problem importing one of the required Python modules. The
@@ -51,8 +51,7 @@ def parse_options():
     return safe_options, options, args
 
 def emit_err(err):
-    syslog.syslog(syslog.LOG_ERR, err)
-    print err
+    print sys.stderr.write(err)
 
 def get_config():
     base = "cn=%s,cn=masters,cn=ipa,cn=etc,%s" % (socket.gethostname(),
@@ -240,8 +239,6 @@ def main():
     api.bootstrap(context='cli', debug=options.debug)
     api.finalize()
 
-    syslog.openlog('ipa', syslog.LOG_NDELAY, syslog.LOG_DAEMON)
-
     serverid = dsinstance.realm_to_serverid(api.env.realm)
 
     if args[0].lower() == "start":
@@ -253,8 +250,6 @@ def main():
     elif args[0].lower() == "status":
         ipa_status(serverid)
 
-    syslog.closelog()
-
 try:
     if __name__ == "__main__":
         sys.exit(main())
-- 
1.7.3.4

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to