From 9a8dcd1a18b461ae9164a1d0718f7d51e17b7fd6 Mon Sep 17 00:00:00 2001 From: Alexander Bokovoy Date: Tue, 30 Aug 2011 17:15:46 +0300 Subject: Convert ipa-client/ bits --- ipa-client/ipa-install/ipa-client-install | 201 +++++++++++++----------------- ipa-client/ipaclient/ntpconf.py | 5 +- 2 files changed, 88 insertions(+), 118 deletions(-) diff --git a/ipa-client/ipa-install/ipa-client-install b/ipa-client/ipa-install/ipa-client-install index 495b7f211..ffbbdda5a 100755 --- a/ipa-client/ipa-install/ipa-client-install +++ b/ipa-client/ipa-install/ipa-client-install @@ -132,6 +132,9 @@ def logging_setup(options): console.setFormatter(formatter) logging.getLogger('').addHandler(console) +def log_service_error(name, action, error): + logging.error("%s failed to %s: %s" % (name, action, str(error))) + def nickname_exists(nickname): (sout, serr, returncode) = run(["/usr/bin/certutil", "-L", "-d", "/etc/pki/nssdb", "-n", nickname], raiseonerr=False) @@ -183,20 +186,22 @@ def uninstall(options, env): # Always start certmonger. We can't untrack something if it isn't # running + messagebus = ipaservices.knownservices.messagebus try: - ipaservices.service_start('messagebus') + messagebus.start() except Exception, e: - logging.error("messagebus failed to start: %s" % str(e)) + log_service_error(messagebus.service_name, 'start', e) + cmonger = ipaservices.knownservices.certmonger try: - ipaservices.service_start(ipaservices.SERVICE_CERTMONGER) + cmonger.start() except Exception, e: - logging.error("certmonger failed to start: %s" % str(e)) + log_service_error(cmonger.service_name, 'start', e) try: certmonger.stop_tracking('/etc/pki/nssdb', nickname=client_nss_nickname) except (CalledProcessError, RuntimeError), e: - logging.error("%s failed to stop tracking certificate: %s" % (ipaservices.SERVICE_CERTMONGER, str(e))) + logging.error("%s failed to stop tracking certificate: %s" % (cmonger.service_name, str(e))) if nickname_exists(client_nss_nickname): try: @@ -205,18 +210,18 @@ def uninstall(options, env): print "Failed to remove %s from /etc/pki/nssdb: %s" % (client_nss_nickname, str(e)) try: - ipaservices.service_stop(ipaservices.SERVICE_CERTMONGER) + cmonger.stop() except Exception, e: - logging.error("%s failed to stop: %s" % (ipaservices.SERVICE_CERTMONGER, str(e))) + log_service_error(cmonger.service_name, 'stop', e) # Remove any special principal names we added to the IPA CA helper certmonger.remove_principal_from_cas() try: - ipaservices.service_off(ipaservices.SERVICE_CERTMONGER) + cmonger.disable() except Exception, e: - print "Failed to disable automatic startup of the %s service" % (ipaservices.SERVICE_CERTMONGER) - logging.error("Failed to disable automatic startup of the %s service: %s" % (ipaservices.SERVICE_CERTMONGER, str(e))) + print "Failed to disable automatic startup of the %s service" % (cmonger.service_name) + logging.error("Failed to disable automatic startup of the %s service: %s" % (cmonger.service_name, str(e))) if not options.on_master: print "Unenrolling client from IPA server" @@ -260,33 +265,35 @@ def uninstall(options, env): except CalledProcessError, e: print >>sys.stderr, "Failed to set this machine hostname to %s (%s)." % (old_hostname, str(e)) - if ipaservices.service_is_installed('nscd'): + nscd = ipaservices.knownservices.nscd + if nscd.is_installed(): try: - ipaservices.service_restart('nscd') + nscd.restart() except: - print "Failed to restart start the NSCD daemon" + print "Failed to restart the %s daemon" % (nscd.service_name) try: - ipaservices.service_on('nscd') + nscd.enable() except: - print "Failed to configure automatic startup of the NSCD daemon" + print "Failed to configure automatic startup of the %s daemon" % (nscd.service_name) else: # this is optional service, just log - logging.info("NSCD daemon is not installed, skip configuration") + logging.info("%s daemon is not installed, skip configuration" % (nscd.service_name)) - if ipaservices.service_is_installed('nslcd'): + nslcd = ipaservices.knownservices.nslcd + if nslcd.is_installed(): try: - ipaservices.service_stop('nslcd') + nslcd.stop() except: - print "Failed to stop the NSLCD daemon" + print "Failed to stop the %s daemon" % (nslcd.service_name) try: - ipaservices.service_off('nslcd') + nslcd.disable() except: - print "Failed to disable automatic startup of the NSLCD daemon" + print "Failed to disable automatic startup of the %s daemon" % (nslcd.service_name) else: # this is optional service, just log - logging.info("NSLCD daemon is not installed, skip configuration") + logging.info("%s daemon is not installed, skip configuration" % (nslcd.service_name)) if not options.unattended: print "The original nsswitch.conf configuration has been restored." @@ -406,19 +413,20 @@ def configure_nslcd_conf(fstore, cli_basedn, cli_realm, cli_domain, cli_server, print "Creation of %s: %s" % ('/etc/nslcd.conf', str(e)) return (1, None, None) - if ipaservices.service_is_installed('nslcd'): + nslcd = ipaservices.knownservices.nslcd + if nslcd.is_installed(): try: - ipaservices.service_restart('nslcd') + nslcd.restart() except Exception, e: - logging.error("nslcd failed to restart: %s" % str(e)) + log_service_error(nslcd.service_name, 'restart', e) try: - ipaservices.service_on('nslcd') + nslcd.enable() except Exception, e: - print "Failed to configure automatic startup of the NSLCD daemon" - logging.error("Failed to enable automatic startup of the NSLCD daemon: %s" % str(e)) + print "Failed to configure automatic startup of the %s daemon" % (nslcd.service_name) + logging.error("Failed to enable automatic startup of the %s daemon: %s" % (nslcd.service_name, str(e))) else: - logging.debug("NSLCD daemon is not installed, skip configuration") + logging.debug("%s daemon is not installed, skip configuration" % (nslcd.service_name)) return (0, None, None) return (0, 'NSLCD', '/etc/nslcd.conf') @@ -506,42 +514,44 @@ def configure_certmonger(fstore, subject_base, cli_realm, hostname, options): started = True principal = 'host/%s@%s' % (hostname, cli_realm) + messagebus = ipaservices.knownservices.messagebus try: - ipautil.service_start('messagebus') + messagebus.start() except Exception, e: - logging.error("messagebus failed to start: %s" % str(e)) + log_service_error(messagebus.service_name, 'start', e) # Ensure that certmonger has been started at least once to generate the # cas files in /var/lib/certmonger/cas. + cmonger = ipaservices.knownservices.certmonger try: - ipaservices.service_restart('certmonger') + cmonger.restart() except Exception, e: - logging.error("certmonger failed to restart: %s" % str(e)) + log_service_error(cmonger.service_name, 'restart', e) if options.hostname: # It needs to be stopped if we touch them try: - ipaservices.service_stop('certmonger') + cmonger.stop() except Exception, e: - logging.error("certmonger failed to stop: %s" % str(e)) + log_service_error(cmonger.service_name, 'stop', e) # If the hostname is explicitly set then we need to tell certmonger # which principal name to use when requesting certs. certmonger.add_principal_to_cas(principal) try: - ipaservices.service_restart('certmonger') + cmonger.restart() except Exception, e: - print "Failed to start the certmonger daemon" + print "Failed to start the %s daemon" % (cmonger.service_name) print "Automatic certificate management will not be available" - logging.error("certmonger failed to restart: %s" % str(e)) + log_service_error(cmonger.service_name, 'restart', e) started = False try: - ipaservices.service_on('certmonger') + cmonger.enable() except Exception, e: - print "Failed to configure automatic startup of the certmonger daemon" + print "Failed to configure automatic startup of the %s daemon" % (cmonger.service_name) print "Automatic certificate management will not be available" - logging.error("Failed to disable automatic startup of the certmonger daemon: %s" % str(e)) + logging.error("Failed to disable automatic startup of the %s daemon: %s" % (cmonger.service_name, str(e))) # Request our host cert if started: @@ -550,57 +560,7 @@ def configure_certmonger(fstore, subject_base, cli_realm, hostname, options): try: run(["ipa-getcert", "request", "-d", "/etc/pki/nssdb", "-n", client_nss_nickname, "-N", subject, "-K", principal]) except: - print "certmonger request for host certificate failed" - -def backup_and_replace_hostname(fstore, hostname): - # TODO: this code is for Red Hat-based systems - # it need to be rewritten for cross-paltform support - # so that different configuration backends would be possible - # (GNU/Debian stores this information in a different place) - network_filename = "/etc/sysconfig/network" - # Backup original /etc/sysconfig/network - fstore.backup_file(network_filename) - hostname_pattern = re.compile(''' -(^ - \s* - (?P