>From 2e1b1367b6d3ff5bd5220b2191b05ff52e052ab2 Mon Sep 17 00:00:00 2001
From: Abhishek Koneru <akoneru@redhat.com>
Date: Thu, 25 Apr 2013 09:30:16 -0400
Subject: [PATCH] Show error messages and stack trace when an error occurs.

Print the stacktrace to the log file if there is an error while
executing pkispawn.

Ticket #592
---
 base/server/src/engine/pkihelper.py |  119 +++++++++++++++++------------------
 base/server/src/pkispawn            |   20 +++++-
 2 files changed, 78 insertions(+), 61 deletions(-)

diff --git a/base/server/src/engine/pkihelper.py b/base/server/src/engine/pkihelper.py
index 0bcc43ee71debceb162c6a319b72aa1d9f7e380e..400d488543d3bd2fad0db1837965a26d12650ca4 100644
--- a/base/server/src/engine/pkihelper.py
+++ b/base/server/src/engine/pkihelper.py
@@ -178,7 +178,7 @@ class identity:
         except subprocess.CalledProcessError as exc:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
         return
 
     def __add_uid(self, pki_user, pki_group):
@@ -253,7 +253,7 @@ class identity:
         except subprocess.CalledProcessError as exc:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
         return
 
     def add_uid_and_gid(self, pki_user, pki_group):
@@ -268,7 +268,7 @@ class identity:
             config.pki_log.error(log.PKI_KEYERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return pki_uid
 
     def get_gid(self, critical_failure=True):
@@ -278,7 +278,7 @@ class identity:
             config.pki_log.error(log.PKI_KEYERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return pki_gid
 
     def set_uid(self, name, critical_failure=True):
@@ -294,7 +294,7 @@ class identity:
             config.pki_log.error(log.PKI_KEYERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return pki_uid
 
     def set_gid(self, name, critical_failure=True):
@@ -310,7 +310,7 @@ class identity:
             config.pki_log.error(log.PKI_KEYERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return pki_gid
 
 
@@ -869,7 +869,7 @@ class instance:
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
         return rv
 
     def apache_instances(self):
@@ -896,7 +896,7 @@ class instance:
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
         return rv
 
     def pki_instance_subsystems(self):
@@ -924,7 +924,7 @@ class instance:
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
         return rv
 
     def tomcat_instance_subsystems(self):
@@ -938,7 +938,7 @@ class instance:
         except OSErr as e:
             config.pki_log.error(log.PKI_OSERROR_1, str(e),
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
         return rv
 
     def tomcat_instances(self):
@@ -965,7 +965,7 @@ class instance:
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
         return rv
 
     def verify_subsystem_exists(self):
@@ -979,7 +979,7 @@ class instance:
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
 
     def verify_subsystem_does_not_exist(self):
         try:
@@ -992,7 +992,7 @@ class instance:
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
 
     def get_instance_status(self):
         self.connection = pki.client.PKIConnection(
@@ -1075,7 +1075,7 @@ class directory:
                 config.pki_log.error(log.PKI_OSERROR_1, exc,
                                      extra=config.PKI_INDENTATION_LEVEL_2)
                 if critical_failure == True:
-                    sys.exit(1)
+                    raise
         return
 
     def modify(self, name, uid=None, gid=None,
@@ -1130,7 +1130,7 @@ class directory:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def delete(self, name, recursive_flag=True, critical_failure=True):
@@ -1155,7 +1155,7 @@ class directory:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def exists(self, name):
@@ -1167,7 +1167,7 @@ class directory:
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
 
     def is_empty(self, name):
         try:
@@ -1182,7 +1182,7 @@ class directory:
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
 
     def set_mode(self, name, uid=None, gid=None,
                  dir_perms=config.PKI_DEPLOYMENT_DEFAULT_DIR_PERMISSIONS,
@@ -1317,7 +1317,7 @@ class directory:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
 
     def copy(self, old_name, new_name, uid=None, gid=None,
              dir_perms=config.PKI_DEPLOYMENT_DEFAULT_DIR_PERMISSIONS,
@@ -1371,12 +1371,12 @@ class directory:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         except shutil.Error as exc:
             config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
 
@@ -1428,7 +1428,7 @@ class file:
                 config.pki_log.error(log.PKI_OSERROR_1, exc,
                                      extra=config.PKI_INDENTATION_LEVEL_2)
                 if critical_failure == True:
-                    sys.exit(1)
+                    raise
         return
 
     def modify(self, name, uid=None, gid=None,
@@ -1483,7 +1483,7 @@ class file:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def delete(self, name, critical_failure=True):
@@ -1502,7 +1502,7 @@ class file:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def exists(self, name):
@@ -1514,7 +1514,7 @@ class file:
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
 
     def copy(self, old_name, new_name, uid=None, gid=None,
              perms=config.PKI_DEPLOYMENT_DEFAULT_FILE_PERMISSIONS, acls=None,
@@ -1566,12 +1566,12 @@ class file:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         except shutil.Error as exc:
             config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def apply_slot_substitution(
@@ -1626,12 +1626,12 @@ class file:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         except shutil.Error as exc:
             config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def copy_with_slot_substitution(
@@ -1696,12 +1696,12 @@ class file:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         except shutil.Error as exc:
             config.pki_log.error(log.PKI_SHUTIL_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def generate_noise_file(self, name, bytes, uid=None, gid=None,
@@ -1756,7 +1756,7 @@ class file:
                 config.pki_log.error(log.PKI_OSERROR_1, exc,
                                      extra=config.PKI_INDENTATION_LEVEL_2)
                 if critical_failure == True:
-                    sys.exit(1)
+                    raise
         return
 
 
@@ -1813,7 +1813,7 @@ class symlink:
                 config.pki_log.error(log.PKI_OSERROR_1, exc,
                                      extra=config.PKI_INDENTATION_LEVEL_2)
                 if critical_failure == True:
-                    sys.exit(1)
+                    raise
         return
 
     def modify(self, link, uid=None, gid=None,
@@ -1866,7 +1866,7 @@ class symlink:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def delete(self, link, critical_failure=True):
@@ -1885,7 +1885,7 @@ class symlink:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def exists(self, name):
@@ -1897,8 +1897,7 @@ class symlink:
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
-
+            raise
 
 # PKI Deployment War File Class
 class war:
@@ -1934,17 +1933,17 @@ class war:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         except zipfile.BadZipfile as exc:
             config.pki_log.error(log.PKI_BADZIPFILE_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         except zipfile.LargeZipFile as exc:
             config.pki_log.error(log.PKI_LARGEZIPFILE_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
 
@@ -1989,7 +1988,7 @@ class password:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def create_client_pkcs12_password_conf(self, path, overwrite_flag=False,
@@ -2015,7 +2014,7 @@ class password:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def get_password(self, path, token_name, critical_failure=True):
@@ -2097,12 +2096,12 @@ class certutil:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def verify_certificate_exists(self, path, pki_cert_database,
@@ -2175,7 +2174,7 @@ class certutil:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return True
 
     def generate_self_signed_certificate(self, path, pki_cert_database,
@@ -2309,12 +2308,12 @@ class certutil:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def import_cert(self, nickname, trust, input_file, password_file,
@@ -2366,12 +2365,12 @@ class certutil:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def generate_certificate_request(self, subject, key_size,
@@ -2447,12 +2446,12 @@ class certutil:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
 # pk12util class
@@ -2500,12 +2499,12 @@ class pk12util:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         except OSError as exc:
             config.pki_log.error(log.PKI_OSERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
 
@@ -2590,7 +2589,7 @@ class kra_connector:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(-1)
+                raise
         return
 
     def execute_using_pki(self, caport, cahost, subsystemnick,
@@ -2836,7 +2835,7 @@ class security_domain:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(-1)
+                raise
 
         return None
 
@@ -2891,7 +2890,7 @@ class security_domain:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(-1)
+                raise
         return None
 
 # PKI Deployment 'systemd' Execution Management Class
@@ -2919,7 +2918,7 @@ class systemd:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def stop(self, critical_failure=True):
@@ -2945,7 +2944,7 @@ class systemd:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
     def restart(self, critical_failure=True):
@@ -2971,7 +2970,7 @@ class systemd:
             config.pki_log.error(log.PKI_SUBPROCESS_ERROR_1, exc,
                                  extra=config.PKI_INDENTATION_LEVEL_2)
             if critical_failure == True:
-                sys.exit(1)
+                raise
         return
 
 
@@ -3036,7 +3035,7 @@ class config_client:
             config.pki_log.error(
                 log.PKI_CONFIG_JAVA_CONFIGURATION_EXCEPTION + " " + str(e),
                 extra=config.PKI_INDENTATION_LEVEL_2)
-            sys.exit(1)
+            raise
         return
 
     def process_admin_cert(self, admin_cert):
diff --git a/base/server/src/pkispawn b/base/server/src/pkispawn
index fb0ff66873849f7ff24e8d2013dcfdfd558b238b..d6a3fab00d83595f867f727b3b0ea65f87c6a64a 100755
--- a/base/server/src/pkispawn
+++ b/base/server/src/pkispawn
@@ -38,6 +38,7 @@ try:
     import subprocess
     import time
     import urllib2
+    import traceback
     from time import strftime as date
     from pki.deployment import pkiconfig as config
     from pki.deployment.pkiparser import PKIConfigParser
@@ -368,7 +369,13 @@ def main(argv):
                                pki_scriptlet,
                                fromlist = [pki_scriptlet])
         instance = scriptlet.PkiScriptlet()
-        rv = instance.spawn()
+        try:
+            rv = instance.spawn()
+        except:
+            log_error_details()
+            print "Installation failed."
+            print
+            sys.exit(0)
         if rv != 0:
             sys.exit(1)
     config.pki_log.debug(log.PKI_DICTIONARY_MASTER,
@@ -393,6 +400,17 @@ def print_install_information():
                                 config.pki_master_dict['pki_https_port'],
                                 config.pki_subsystem.lower())
     print log.PKI_SPAWN_INFORMATION_FOOTER
+    
+def log_error_details():
+        type, value, stacktrace = sys.exc_info()
+        config.pki_log.debug("Error Type: " + type.__name__, extra=config.PKI_INDENTATION_LEVEL_2)
+        config.pki_log.debug("Error Message: " + str(value), extra=config.PKI_INDENTATION_LEVEL_2)
+        stacktrace_list = traceback.format_list(traceback.extract_tb(stacktrace))
+        stacktrace = ""
+        for l in stacktrace_list:
+            stacktrace += l
+        config.pki_log.debug(stacktrace, extra=config.PKI_INDENTATION_LEVEL_2)
+        sys.exc_clear()
 
 # PKI Deployment Entry Point
 if __name__ == "__main__":
-- 
1.7.10.2

