On 29.07.2011 12:21, Alexander Bokovoy wrote:
> On 29.07.2011 12:01, Martin Kosek wrote:
>> On Fri, 2011-07-29 at 11:42 +0300, Alexander Bokovoy wrote:
>>> https://fedorahosted.org/freeipa/ticket/1368
>>>
>>> also replaces a tab by spaces in one else statement (cosmetic).
>>
>> This works fine. But I have few suggestion for improvement:
>>
>> 1) Shouldn't we also run `hostname NEW_HOSTNAME` so that the new
>> hostname is properly set on the system?
> Makes sense.
> 
>> 2) I would enhance our man pages/help and state that we are changing the
>> system hostname. Current --hostname option is confusing:
>>
>>        --hostname
>>               The hostname of this server (FQDN).  By  default  of  nodename 
>>  from
>>               uname(2) is used.
> Oh, this is not informative at all. I'll get this updated.
Updated patch attached.

-- 
/ Alexander Bokovoy
From 02062eeb0f2c8b1711196d6067c607dfeffc73d5 Mon Sep 17 00:00:00 2001
From: Alexander Bokovoy <aboko...@redhat.com>
Date: Tue, 19 Jul 2011 15:33:53 +0300
Subject: [PATCH] Modify /etc/sysconfig/network on a client when IPA manages
 hostname

https://fedorahosted.org/freeipa/ticket/1368
---
 ipa-client/ipa-install/ipa-client-install |   41 ++++++++++++++++++++++++++---
 1 files changed, 37 insertions(+), 4 deletions(-)

diff --git a/ipa-client/ipa-install/ipa-client-install 
b/ipa-client/ipa-install/ipa-client-install
index 
2e1a28ca087dee9eea04ccc55557a9e6e4f8ce89..70323f98bd98d6f182ddf844fc01279a27f6f7a2
 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,7 @@ 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",
@@ -520,6 +521,34 @@ 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('^HOSTNAME=')
+    temp_filename = None
+    with tempfile.NamedTemporaryFile(delete=False) as new_config:
+        temp_filename = new_config.name
+        with open(network_filename, 'r') as f:
+            for line in f:
+                if hostname_pattern.match(line):
+                    new_config.write("HOSTNAME=%s\n" % (hostname))
+                else:
+                    new_config.write(line)
+        new_config.flush()
+    # At this point new_config is closed but not removed due to 'delete=False' 
above
+    # Now, install the temporary file as configuration and ensure old version 
is available as .orig
+    # While .orig file is not used during uninstall, it is left there for 
administrator.
+    install_file(temp_filename, network_filename)
+    try:
+        ipautil.run(['/bin/hostname', hostname])
+    except CalledProcessError, e:
+        print >>sys.stderr, "Failed to set this machine hostname to %s (%s)." 
% (hostname, str(e))
+
 def configure_sssd_conf(fstore, cli_realm, cli_domain, cli_server, options):
     sssdconfig = SSSDConfig.SSSDConfig()
     sssdconfig.new_config()
@@ -566,6 +595,10 @@ def configure_sssd_conf(fstore, cli_realm, cli_domain, 
cli_server, options):
     sssdconfig.save_domain(domain)
     sssdconfig.write("/etc/sssd/sssd.conf")
 
+    # configure /etc/sysconfig/network to contain the hostname we set.
+    if options.hostname:
+        backup_and_replace_hostname(fstore, options.hostname)
+
     return 0
 
 def resolve_ipaddress(server):
@@ -940,8 +973,8 @@ def main():
             if not options.sssd:
                 print >>sys.stderr, "Failed to configure automatic startup of 
the NSCD daemon"
                 print >>sys.stderr, "Caching of users/groups will not be 
available after reboot"
-           else:
-               print >>sys.stderr, "Failed to disable NSCD daemon. Please 
disable it manually."
+            else:
+                print >>sys.stderr, "Failed to disable NSCD daemon. Please 
disable it manually."
 
     else:
         # this is optional service, just log
-- 
1.7.6

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to