Move install script error handling to a common function

All of our install/admin scripts had a try/except block calling the
main function and handling common exceptions. These were copy-pasted
from each other and modified to various levels of sophistication.
This refactors them out of installers to a single function, which
includes a final pass/fail message for all of the scripts.

Non-install scripts that set up the same log handler levels for
stderr and log file are not changed, as it's not possible to log
to only the logfile without changing the logger configuration.

https://fedorahosted.org/freeipa/ticket/2071
This commit is contained in:
Petr Viktorin
2012-05-31 14:34:09 +02:00
committed by Martin Kosek
parent 9e877585e2
commit 0ca29fac9a
13 changed files with 243 additions and 267 deletions

View File

@@ -21,8 +21,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import traceback
from ipaserver.plugins.ldap2 import ldap2
from ipaserver.install import adtrustinstance
from ipaserver.install.installutils import *
@@ -35,6 +33,8 @@ import krbV
import ldap
from ipapython.ipa_log_manager import *
log_file_name = "/var/log/ipaserver-install.log"
def parse_options():
parser = IPAOptionParser(version=version.VERSION)
parser.add_option("-p", "--ds-password", dest="dm_password",
@@ -86,8 +86,8 @@ def main():
if os.getegid() != 0:
sys.exit("Must be root to setup AD trusts on server")
standard_logging_setup("/var/log/ipaserver-install.log", debug=options.debug, filemode='a')
print "\nThe log file for this installation can be found in /var/log/ipaserver-install.log"
standard_logging_setup(log_file_name, debug=options.debug, filemode='a')
print "\nThe log file for this installation can be found in %s" % log_file_name
root_logger.debug('%s was invoked with options: %s' % (sys.argv[0], safe_options))
root_logger.debug("missing options might be asked for interactively later\n")
@@ -227,26 +227,6 @@ def main():
return 0
try:
sys.exit(main())
except SystemExit, e:
sys.exit(e)
except KeyboardInterrupt:
print "Installation cancelled."
except RuntimeError, e:
print str(e)
except HostnameLocalhost:
print "The hostname resolves to the localhost address (127.0.0.1/::1)"
print "Please change your /etc/hosts file so that the hostname"
print "resolves to the ip address of your network interface."
print "The KDC service does not listen on localhost"
print ""
print "Please fix your /etc/hosts file and restart the setup program"
except Exception, e:
message = "Unexpected error - see ipaserver-install.log for details:\n %s" % str(e)
print message
message = str(e)
for str in traceback.format_tb(sys.exc_info()[2]):
message = message + "\n" + str
root_logger.debug(message)
sys.exit(1)
if __name__ == '__main__':
installutils.run_script(main, log_file_name=log_file_name,
operation_name='ipa-adtrust-install')