Use sys.exit to quit scripts

Instead of print and return, use sys.exit() to quit scripts with an
error message and a non zero return code.

https://fedorahosted.org/freeipa/ticket/425
This commit is contained in:
Jakub Hrozek
2010-11-08 23:13:48 +01:00
committed by Adam Young
parent 3e540272c6
commit 57e1edd052
6 changed files with 45 additions and 73 deletions

View File

@@ -74,11 +74,9 @@ def main():
loglevel = logging.DEBUG
if len(args) != 1:
print "You must specify one action, either enable or disable"
sys.exit(1)
sys.exit("You must specify one action, either enable or disable")
elif args[0] != "enable" and args[0] != "disable" and args[0] != "status":
print "Unrecognized action [" + args[0] + "]"
sys.exit(1)
sys.exit("Unrecognized action [" + args[0] + "]")
logging.basicConfig(level=loglevel,
format='%(levelname)s %(message)s')
@@ -102,9 +100,7 @@ def main():
bind_dn='cn=directory manager', bind_pw=dirman_password
)
except errors.LDAPError, lde:
print "An error occurred while connecting to the server."
print lde
return 1
sys.exit("An error occurred while connecting to the server.\n%s\n" % str(lde))
if args[0] == "status":
try:
@@ -142,9 +138,9 @@ def main():
# We can't disable schema compat if the NIS plugin is enabled
try:
conn.get_entry(netgroup_compat_dn, normalize=False)
print "The NIS plugin is configured, cannot disable compatibility."
print "Run 'ipa-nis-manage disable' first."
return 2
print >>sys.stderr, "The NIS plugin is configured, cannot disable compatibility."
print >>sys.stderr, "Run 'ipa-nis-manage disable' first."
sys.exit(2)
except errors.NotFound:
pass
# Make a quick hack for now, directly delete the entries by name,

View File

@@ -80,8 +80,7 @@ def main():
safe_options, options = parse_options()
if os.getegid() != 0:
print "Must be root to setup server"
return 1
sys.exit("Must be root to setup server")
standard_logging_setup("/var/log/ipaserver-install.log", options.debug, filemode='a')
print "\nThe log file for this installation can be found in /var/log/ipaserver-install.log"
@@ -103,8 +102,7 @@ def main():
# Check bind packages are installed
if not bindinstance.check_inst(options.unattended):
print "Aborting installation"
return 1
sys.exit("Aborting installation")
# Initialize the ipalib api
cfg = dict(
@@ -124,8 +122,7 @@ def main():
ip_address = resolve_host(api.env.host)
if not ip_address or not verify_ip_address(ip_address):
if options.unattended:
print "Unable to resolve IP address for host name"
return 1
sys.exit("Unable to resolve IP address for host name")
else:
ip_address = read_ip_address(api.env.host, fstore)
logging.debug("will use ip_address: %s\n", ip_address)

View File

@@ -88,11 +88,9 @@ def main():
loglevel = logging.DEBUG
if len(args) != 1:
print "You must specify one action, either enable or disable"
sys.exit(1)
sys.exit("You must specify one action, either enable or disable")
elif args[0] != "enable" and args[0] != "disable":
print "Unrecognized action [" + args[0] + "]"
sys.exit(1)
sys.exit("Unrecognized action [" + args[0] + "]")
logging.basicConfig(level=loglevel,
format='%(levelname)s %(message)s')
@@ -118,15 +116,12 @@ def main():
except errors.ACIError:
sys.exit("Incorrect password")
except errors.LDAPError, lde:
print "An error occurred while connecting to the server."
print lde
return 1
sys.exit("An error occurred while connecting to the server: %s" % str(lde))
if args[0] == "enable":
compat = get_entry(compat_dn, conn)
if compat is None:
print "The compat plugin needs to be enabled: ipa-compat-manage enable"
return 1
sys.exit("The compat plugin needs to be enabled: ipa-compat-manage enable")
entry = None
try:
entry = get_entry(nis_config_dn, conn)

View File

@@ -156,9 +156,8 @@ def main():
os.chown(dirname + "/secmod.db", 0, pent.pw_gid )
except Exception, e:
print "an unexpected error occurred: %s" % str(e)
traceback.print_exc()
return 1
traceback.print_exc(file=sys.stderr)
sys.exit("an unexpected error occurred: %s" % str(e))
return 0

View File

@@ -455,8 +455,7 @@ def main():
safe_options, options = parse_options()
if os.getegid() != 0:
print "Must be root to set up server"
return 1
sys.exit("Must be root to set up server")
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
@@ -539,14 +538,12 @@ def main():
# check bind packages are installed
if options.setup_dns:
if not bindinstance.check_inst(options.unattended):
print "Aborting installation"
return 1
sys.exit("Aborting installation")
# check the pkinit plugin is installed
if options.setup_pkinit:
if not krbinstance.check_pkinit_plugin():
print "Aborting installation"
return 1
sys.exit("Aborting installation")
# check the hostname is correctly configured, it must be as the kldap
# utilities just use the hostname as returned by gethostbyname to set
@@ -562,8 +559,7 @@ def main():
try:
verify_fqdn(host_default,options.no_host_dns)
except RuntimeError, e:
logging.error(str(e) + "\n")
return 1
sys.exit(str(e) + "\n")
host_name = host_default
else:
@@ -586,27 +582,25 @@ def main():
if options.ip_address:
ip = options.ip_address
if ip is None and options.unattended:
print "Unable to resolve IP address for host name"
return 1
sys.exit("Unable to resolve IP address for host name")
if not verify_ip_address(ip):
ip = ""
if options.unattended:
return 1
sys.exit(1)
if options.ip_address and options.ip_address != ip:
if options.setup_dns:
ip = options.ip_address
else:
print "Error: the hostname resolves to an IP address that is different"
print "from the one provided on the command line. Please fix your DNS"
print "or /etc/hosts file and restart the installation."
print >>sys.stderr, "Error: the hostname resolves to an IP address that is different"
print >>sys.stderr, "from the one provided on the command line. Please fix your DNS"
print >>sys.stderr, "or /etc/hosts file and restart the installation."
return 1
if options.unattended:
if not ip:
print "Unable to resolve IP address"
return 1
sys.exit("Unable to resolve IP address")
if not ip:
ip = read_ip_address(host_name, fstore)
@@ -622,7 +616,7 @@ def main():
if not options.ds_user:
ds_user = read_ds_user()
if ds_user == "":
return 1
sys.exit(1)
logging.debug("read ds_user: %s\n" % ds_user)
else:
ds_user = options.ds_user
@@ -830,9 +824,7 @@ def main():
try:
run(["/usr/sbin/ipa-client-install", "--on-master", "--unattended", "--domain", domain_name, "--server", host_name, "--realm", realm_name])
except Exception, e:
print "Configuration of client side components failed!"
print "ipa-client-install returned: " + str(e)
return 1
sys.exit("Configuration of client side components failed!\nipa-client-install returned: " + str(e))
print "=============================================================================="
print "Setup complete"

View File

@@ -515,8 +515,7 @@ def main():
return uninstall(options, env)
if fstore.has_files() and not options.force:
print "IPA client is already configured on this system."
return 1
sys.exit("IPA client is already configured on this system.")
cli_domain = None
cli_server = None
@@ -525,8 +524,7 @@ def main():
subject_base = None
if options.unattended and (options.password is None and options.principal is None and options.prompt_password is False) and not options.on_master:
print "One of password and principal are required."
return 1
sys.exit("One of password and principal are required.")
# Create the discovery instance
ds = ipaclient.ipadiscovery.IPADiscovery()
@@ -536,8 +534,8 @@ def main():
else:
ret = ds.search()
if ret == -10:
print "Can't get the fully qualified name of this host"
print "Please check that the client is properly configured"
print >>sys.stderr, "Can't get the fully qualified name of this host"
print >>sys.stderr, "Please check that the client is properly configured"
return ret
if ret == -1 or not ds.getDomainName():
logging.debug("Domain not found")
@@ -577,9 +575,9 @@ def main():
logging.debug("will use server: %s\n", cli_server)
if ret != 0:
print "Failed to verify that "+cli_server+" is an IPA Server."
print "This may mean that the remote server is not up or is not reachable"
print "due to network or firewall settings."
print >>sys.stderr, "Failed to verify that "+cli_server+" is an IPA Server."
print >>sys.stderr, "This may mean that the remote server is not up or is not reachable"
print >>sys.stderr, "due to network or firewall settings."
return ret
if dnsok:
@@ -596,7 +594,7 @@ def main():
if options.realm_name and options.realm_name != ds.getRealmName():
if not options.unattended:
print "ERROR: The provided realm name: ["+options.realm_name+"] does not match with the discovered one: ["+ds.getRealmName()+"]\n"
print >>sys.stderr, "ERROR: The provided realm name: ["+options.realm_name+"] does not match with the discovered one: ["+ds.getRealmName()+"]\n"
return -3
cli_realm = ds.getRealmName()
@@ -634,8 +632,7 @@ def main():
(krb_fd, krb_name) = tempfile.mkstemp()
os.close(krb_fd)
if configure_krb5_conf(fstore, cli_basedn, cli_realm, cli_domain, cli_server, dnsok, options, krb_name):
print "Test kerberos configuration failed"
return 1
sys.exit("Test kerberos configuration failed")
env['KRB5_CONFIG'] = krb_name
join_args = ["/usr/sbin/ipa-join", "-s", cli_server]
if options.debug:
@@ -653,23 +650,20 @@ def main():
sys.stdout.flush()
else:
if sys.stdin.isatty():
print "Password must be provided in non-interactive mode"
return 1
sys.exit("Password must be provided in non-interactive mode")
else:
stdin = sys.stdin.readline()
(stderr, stdout, returncode) = run(["kinit", principal], raiseonerr=False, stdin=stdin, env=env)
print ""
if returncode != 0:
print stdout
return 1
sys.exit(stdout)
elif options.password:
join_args.append("-w")
join_args.append(options.password)
elif options.prompt_password:
if options.unattended:
print "Password must be provided in non-interactive mode"
return 1
sys.exit("Password must be provided in non-interactive mode")
password = getpass.getpass("Password: ")
join_args.append("-w")
join_args.append(password)
@@ -678,7 +672,7 @@ def main():
(stdout, stderr, returncode) = run(join_args, raiseonerr=False, env=env)
if returncode != 0:
print "Joining realm failed: %s" % stderr,
print >>sys.stderr, "Joining realm failed: %s" % stderr,
if not options.force:
return 1
print " Use ipa-getkeytab to obtain a host principal for this server."
@@ -763,8 +757,7 @@ def main():
try:
hardcode_ldap_server(cli_server)
except Exception, e:
print "Adding hardcoded server name to /etc/ldap.conf failed: " + str(e)
return 1
sys.exit("Adding hardcoded server name to /etc/ldap.conf failed: " + str(e))
#Modify pam to add pam_krb5
run(["/usr/sbin/authconfig", "--enablekrb5", "--update", "--nostart"])
@@ -789,16 +782,16 @@ def main():
try:
service('nscd', nscd_action)
except:
print "Failed to %s the NSCD daemon" % nscd_action
print >>sys.stderr, "Failed to %s the NSCD daemon" % nscd_action
if not options.sssd:
print "Caching of users/groups will not be available"
print >>sys.stderr, "Caching of users/groups will not be available"
try:
chkconfig('nscd', nscd_status)
except:
print "Failed to configure automatic startup of the NSCD daemon"
print >>sys.stderr, "Failed to configure automatic startup of the NSCD daemon"
if not options.sssd:
print "Caching of users/groups will not be available after reboot"
print >>sys.stderr, "Caching of users/groups will not be available after reboot"
print "Client configuration complete."