This patch addresses https://fedorahosted.org/freeipa/ticket/4966

The noise during rollback/uninstall is caused mainly by unsuccessful attempts to remove files that do not exist anymore. These errors are now logged at debug level and do not pop-up to stdout/stderr.

--
Martin^3 Babinsky
From d852d237c86abc1db3780d2be169dd89745a06de Mon Sep 17 00:00:00 2001
From: Martin Babinsky <mbabi...@redhat.com>
Date: Tue, 14 Apr 2015 13:55:33 +0200
Subject: [PATCH] suppress errors arising from deleting non-existent files
 during client uninstall

When rolling back partially configured IPA client a number of OSErrors pop up
due to uninstaller trying to remove files that do not exist anymore. This
patch supresses these errors while keeping them in log as debug messages.

https://fedorahosted.org/freeipa/ticket/4966
---
 ipa-client/ipa-install/ipa-client-install | 40 +++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 12 deletions(-)

diff --git a/ipa-client/ipa-install/ipa-client-install b/ipa-client/ipa-install/ipa-client-install
index 3f9a7419a10ddcb4618e80789a06a05058d1e8a4..06cdb944a023a7d8150f534eda64565d5f020893 100755
--- a/ipa-client/ipa-install/ipa-client-install
+++ b/ipa-client/ipa-install/ipa-client-install
@@ -224,6 +224,29 @@ def logging_setup(options):
         console_format='%(message)s')
 
 
+def log_file_removal_error(e, filename, add_warning=False):
+    """
+    Logs OSErrors raised during file removal according to their nature.
+    OSError 2 (no such file/directory) will be logged only at debug
+    level, other OSError exceptions will be logged as errors.
+
+    :param e: OSError instance
+    :param filename: filename which triggered the error
+    :param add_warning: If set to `True`, prints another message which
+    instructs the user to manually remove the offending file (if it exists)
+    """
+    assert isinstance(e, OSError)
+    logger_func = root_logger.error
+    if e.errno == 2:
+        logger_func = root_logger.debug
+        add_warning = False
+
+    logger_func("Failed to remove file %s: %s", filename, e)
+    if add_warning:
+        logger_func('Please remove %s manually, as it can cause subsequent '
+                    'installation to fail.', filename)
+
+
 def log_service_error(name, action, error):
     root_logger.error("%s failed to %s: %s", name, action, str(error))
 
@@ -532,7 +555,7 @@ def uninstall(options, env):
         try:
             os.remove(filename)
         except OSError, e:
-            root_logger.error("Failed to remove %s: %s", filename, e)
+            log_file_removal_error(e, filename)
 
     for nickname, trust_flags in ipa_certs:
         while sys_db.has_nickname(nickname):
@@ -734,9 +757,8 @@ def uninstall(options, env):
             if file_exists(preferences_fname):
                 try:
                     os.remove(preferences_fname)
-                except Exception, e:
-                    root_logger.warning("'%s' could not be removed: %s." % preferences_fname, str(e))
-                    root_logger.warning("Please remove file '%s' manually." % preferences_fname)
+                except OSError as e:
+                    log_file_removal_error(e, preferences_fname, True)
 
     rv = 0
 
@@ -763,10 +785,7 @@ def uninstall(options, env):
     try:
         os.remove(paths.IPA_DEFAULT_CONF)
     except OSError, e:
-        root_logger.warning('/etc/ipa/default.conf could not be removed: %s',
-                            str(e))
-        root_logger.warning('Please remove /etc/ipa/default.conf manually, '
-                            'as it can cause subsequent installation to fail.')
+        log_file_removal_error(e, paths.IPA_DEFAULT_CONF, True)
 
     # Remove the CA cert from the systemwide certificate store
     tasks.remove_ca_certs_from_systemwide_ca_store()
@@ -775,10 +794,7 @@ def uninstall(options, env):
     try:
         os.remove(CACERT)
     except OSError, e:
-        root_logger.warning('%s could not be removed: %s', CACERT, str(e))
-        root_logger.warning('Please remove %s manually, '
-                            'as it can cause subsequent '
-                            'installation to fail.', CACERT)
+        log_file_removal_error(e, CACERT, True)
 
     root_logger.info("Client uninstall complete.")
 
-- 
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