From a22d00234f94f825d5ff2b6af6e94ce11f98c753 Mon Sep 17 00:00:00 2001 From: Alexander Bokovoy Date: Jul 29 2011 14:26:34 +0000 Subject: Modify /etc/sysconfig/network on a client when IPA manages hostname https://fedorahosted.org/freeipa/ticket/1368 --- diff --git a/ipa-client/ipa-install/ipa-client-install b/ipa-client/ipa-install/ipa-client-install index 2e1a28c..c5f66be 100755 --- a/ipa-client/ipa-install/ipa-client-install +++ b/ipa-client/ipa-install/ipa-client-install @@ -28,10 +28,11 @@ try: import logging import tempfile import getpass + import re from ipaclient import ipadiscovery import ipaclient.ipachangeconf import ipaclient.ntpconf - from ipapython.ipautil import run, user_input, CalledProcessError, file_exists + from ipapython.ipautil import run, user_input, CalledProcessError, file_exists, install_file from ipapython import ipautil from ipapython import dnsclient from ipapython import sysrestore @@ -87,7 +88,9 @@ def parse_options(): parser.add_option("", "--uninstall", dest="uninstall", action="store_true", default=False, help="uninstall an existing installation") parser.add_option("", "--hostname", dest="hostname", - help="The hostname of this server (FQDN). By default of nodename from uname(2) is used.") + help="The hostname of this server (FQDN). If specified, the hostname will be set and " + "the system configuration will be updated to persist over reboot. " + "By default a nodename result from uname(2) is used.") parser.add_option("", "--enable-dns-updates", dest="dns_updates", action="store_true", default=False, help="Configures the machine to attempt dns updates when the ip address changes.") parser.add_option("--no-krb5-offline-passwords", dest="krb5_offline_passwords", action="store_false", @@ -236,6 +239,12 @@ def uninstall(options, env): print "Restoring client configuration files" fstore.restore_all_files() + old_hostname = statestore.restore_state('network','hostname') + if old_hostname is not None and old_hostname != hostname: + try: + ipautil.run(['/bin/hostname', old_hostname]) + except CalledProcessError, e: + print >>sys.stderr, "Failed to set this machine hostname to %s (%s)." % (old_hostname, str(e)) if ipautil.service_is_installed('nscd'): try: @@ -520,6 +529,47 @@ def configure_certmonger(fstore, subject_base, cli_realm, hostname, options): 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