403b09
From fa252eac5db87020c7f4ecd3646a25bb7cafd27c Mon Sep 17 00:00:00 2001
403b09
From: Petr Spacek <pspacek@redhat.com>
403b09
Date: Thu, 28 Jul 2016 16:13:55 +0200
403b09
Subject: [PATCH] install: Call hostnamectl set-hostname only if --hostname
403b09
 option is used
403b09
403b09
This commit also splits hostname backup and configuration into two separate
403b09
functions. This allows us to backup hostname without setting it at the
403b09
same time.
403b09
403b09
https://fedorahosted.org/freeipa/ticket/6071
403b09
403b09
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
403b09
---
403b09
 client/ipa-client-install           |  3 ++-
403b09
 doc/guide/guide.org                 | 10 +++++-----
403b09
 ipaplatform/base/tasks.py           |  7 ++-----
403b09
 ipaplatform/redhat/tasks.py         | 13 ++-----------
403b09
 ipaserver/install/server/install.py | 10 +++++-----
403b09
 5 files changed, 16 insertions(+), 27 deletions(-)
403b09
403b09
diff --git a/client/ipa-client-install b/client/ipa-client-install
403b09
index 45185d44feb43a8b8d30e412a26dd63121be4ad1..db2037b8372644f997de498dcf3b99edcf3abb56 100755
403b09
--- a/client/ipa-client-install
403b09
+++ b/client/ipa-client-install
403b09
@@ -2525,7 +2525,8 @@ def install(options, env, fstore, statestore):
403b09
     if options.hostname and not options.on_master:
403b09
         # skip this step when run by ipa-server-install as it always configures
403b09
         # hostname
403b09
-        tasks.backup_and_replace_hostname(fstore, statestore, options.hostname)
403b09
+        tasks.backup_hostname(fstore, statestore)
403b09
+        tasks.set_hostname(options.hostname)
403b09
 
403b09
     ntp_srv_servers = []
403b09
     if not options.on_master and options.conf_ntp:
403b09
diff --git a/doc/guide/guide.org b/doc/guide/guide.org
403b09
index 6d181559f0af90e7be7089aa94ab4900fa4e90b5..2e852a964991781ef5dd7b93ac481891897e1ed0 100644
403b09
--- a/doc/guide/guide.org
403b09
+++ b/doc/guide/guide.org
403b09
@@ -1039,14 +1039,14 @@ def restore_context_default(filepath):
403b09
 # version in platform services
403b09
 restore_context = restore_context_default
403b09
 
403b09
-# Default implementation of backup and replace hostname that does nothing
403b09
-def backup_and_replace_hostname_default(fstore, statestore, hostname):
403b09
+# Default implementation of backup hostname that does nothing
403b09
+def backup_hostname_default(fstore, statestore):
403b09
     return
403b09
 
403b09
-# Backup and replace system's hostname
403b09
-# Since many platforms have their own way how to store system's hostname, this method must be
403b09
+# Backup system's hostname
403b09
+# Since many platforms have their own way of handling system's hostname, this method must be
403b09
 # implemented in platform services
403b09
-backup_and_replace_hostname = backup_and_replace_hostname_default
403b09
+backup_hostname = backup_hostname_default
403b09
 
403b09
 from ipapython.platform.SUPPORTED_PLATFORM import *
403b09
 #+END_SRC
403b09
diff --git a/ipaplatform/base/tasks.py b/ipaplatform/base/tasks.py
403b09
index c6860ce47ad3d043f1561a690c401537f5e2fdb7..1e687b6181fcff20303b50ac18bfde66280f8bfd 100644
403b09
--- a/ipaplatform/base/tasks.py
403b09
+++ b/ipaplatform/base/tasks.py
403b09
@@ -45,14 +45,11 @@ class BaseTaskNamespace(object):
403b09
 
403b09
         raise NotImplementedError()
403b09
 
403b09
-    def backup_and_replace_hostname(self, fstore, statestore, hostname):
403b09
+    def backup_hostname(self, fstore, statestore):
403b09
         """
403b09
         Backs up the current hostname in the statestore (so that it can be
403b09
         restored by the restore_hostname platform task).
403b09
 
403b09
-        Makes sure that new hostname (passed via hostname argument) is set
403b09
-        as a new pemanent hostname for this host.
403b09
-
403b09
         No return value expected.
403b09
         """
403b09
 
403b09
@@ -109,7 +106,7 @@ class BaseTaskNamespace(object):
403b09
     def restore_hostname(self, fstore, statestore):
403b09
         """
403b09
         Restores the original hostname as backed up in the
403b09
-        backup_and_replace_hostname platform task.
403b09
+        backup_hostname platform task.
403b09
         """
403b09
 
403b09
         raise NotImplementedError()
403b09
diff --git a/ipaplatform/redhat/tasks.py b/ipaplatform/redhat/tasks.py
403b09
index 8ac88511e94d640f077c7a0e202bc545ec8bcbbe..dbe005abb0ecbcb398368789fee52895c6d6e980 100644
403b09
--- a/ipaplatform/redhat/tasks.py
403b09
+++ b/ipaplatform/redhat/tasks.py
403b09
@@ -332,22 +332,13 @@ class RedHatTaskNamespace(BaseTaskNamespace):
403b09
 
403b09
         return result
403b09
 
403b09
-    def backup_and_replace_hostname(self, fstore, statestore, hostname):
403b09
-        old_hostname = socket.gethostname()
403b09
-        try:
403b09
-            self.set_hostname(hostname)
403b09
-        except ipautil.CalledProcessError as e:
403b09
-            root_logger.debug(traceback.format_exc())
403b09
-            root_logger.error(
403b09
-                "Failed to set this machine hostname to %s (%s).",
403b09
-                old_hostname, e
403b09
-            )
403b09
-
403b09
+    def backup_hostname(self, fstore, statestore):
403b09
         filepath = paths.ETC_HOSTNAME
403b09
         if os.path.exists(filepath):
403b09
             fstore.backup_file(filepath)
403b09
 
403b09
         # store old hostname
403b09
+        old_hostname = socket.gethostname()
403b09
         statestore.backup_state('network', 'hostname', old_hostname)
403b09
 
403b09
     def restore_hostname(self, fstore, statestore):
403b09
diff --git a/ipaserver/install/server/install.py b/ipaserver/install/server/install.py
403b09
index 1e925595b93ff95a98b3e6c3d0c357b1766dc1dc..94698898934844350488d5fc52d6e1e567624502 100644
403b09
--- a/ipaserver/install/server/install.py
403b09
+++ b/ipaserver/install/server/install.py
403b09
@@ -651,6 +651,7 @@ def install_check(installer):
403b09
     options.dm_password = dm_password
403b09
     options.master_password = master_password
403b09
     options.admin_password = admin_password
403b09
+    options._host_name_overridden = bool(options.host_name)
403b09
     options.host_name = host_name
403b09
     options.ip_addresses = ip_addresses
403b09
 
403b09
@@ -702,11 +703,10 @@ def install(installer):
403b09
         print("Please wait until the prompt is returned.")
403b09
         print("")
403b09
 
403b09
-    # configure /etc/sysconfig/network to contain the custom hostname
403b09
-    tasks.backup_and_replace_hostname(fstore, sstore, host_name)
403b09
-
403b09
-    # set hostname (we need both transient and static)
403b09
-    tasks.set_hostname(host_name)
403b09
+    # set hostname (transient and static) if user instructed us to do so
403b09
+    if options._host_name_overridden:
403b09
+        tasks.backup_hostname(fstore, sstore)
403b09
+        tasks.set_hostname(host_name)
403b09
 
403b09
     if installer._update_hosts_file:
403b09
         update_hosts_file(ip_addresses, host_name, fstore)
403b09
-- 
403b09
2.7.4
403b09