install: Call hostnamectl set-hostname only if --hostname option is used

This commit also splits hostname backup and configuration into two separate
functions. This allows us to backup hostname without setting it at the
same time.

https://fedorahosted.org/freeipa/ticket/6071

Reviewed-By: Jan Cholasta <jcholast@redhat.com>
This commit is contained in:
Petr Spacek 2016-07-28 16:13:55 +02:00 committed by Jan Cholasta
parent a83523e37e
commit 80e544e7a9
5 changed files with 16 additions and 27 deletions

View File

@ -2525,7 +2525,8 @@ def install(options, env, fstore, statestore):
if options.hostname and not options.on_master:
# skip this step when run by ipa-server-install as it always configures
# hostname
tasks.backup_and_replace_hostname(fstore, statestore, options.hostname)
tasks.backup_hostname(fstore, statestore)
tasks.set_hostname(options.hostname)
ntp_srv_servers = []
if not options.on_master and options.conf_ntp:

View File

@ -1039,14 +1039,14 @@ def restore_context_default(filepath):
# version in platform services
restore_context = restore_context_default
# Default implementation of backup and replace hostname that does nothing
def backup_and_replace_hostname_default(fstore, statestore, hostname):
# Default implementation of backup hostname that does nothing
def backup_hostname_default(fstore, statestore):
return
# Backup and replace system's hostname
# Since many platforms have their own way how to store system's hostname, this method must be
# Backup system's hostname
# Since many platforms have their own way of handling system's hostname, this method must be
# implemented in platform services
backup_and_replace_hostname = backup_and_replace_hostname_default
backup_hostname = backup_hostname_default
from ipapython.platform.SUPPORTED_PLATFORM import *
#+END_SRC

View File

@ -45,14 +45,11 @@ class BaseTaskNamespace(object):
raise NotImplementedError()
def backup_and_replace_hostname(self, fstore, statestore, hostname):
def backup_hostname(self, fstore, statestore):
"""
Backs up the current hostname in the statestore (so that it can be
restored by the restore_hostname platform task).
Makes sure that new hostname (passed via hostname argument) is set
as a new pemanent hostname for this host.
No return value expected.
"""
@ -109,7 +106,7 @@ class BaseTaskNamespace(object):
def restore_hostname(self, fstore, statestore):
"""
Restores the original hostname as backed up in the
backup_and_replace_hostname platform task.
backup_hostname platform task.
"""
raise NotImplementedError()

View File

@ -332,22 +332,13 @@ class RedHatTaskNamespace(BaseTaskNamespace):
return result
def backup_and_replace_hostname(self, fstore, statestore, hostname):
old_hostname = socket.gethostname()
try:
self.set_hostname(hostname)
except ipautil.CalledProcessError as e:
root_logger.debug(traceback.format_exc())
root_logger.error(
"Failed to set this machine hostname to %s (%s).",
old_hostname, e
)
def backup_hostname(self, fstore, statestore):
filepath = paths.ETC_HOSTNAME
if os.path.exists(filepath):
fstore.backup_file(filepath)
# store old hostname
old_hostname = socket.gethostname()
statestore.backup_state('network', 'hostname', old_hostname)
def restore_hostname(self, fstore, statestore):

View File

@ -651,6 +651,7 @@ def install_check(installer):
options.dm_password = dm_password
options.master_password = master_password
options.admin_password = admin_password
options._host_name_overridden = bool(options.host_name)
options.host_name = host_name
options.ip_addresses = ip_addresses
@ -702,11 +703,10 @@ def install(installer):
print("Please wait until the prompt is returned.")
print("")
# configure /etc/sysconfig/network to contain the custom hostname
tasks.backup_and_replace_hostname(fstore, sstore, host_name)
# set hostname (we need both transient and static)
tasks.set_hostname(host_name)
# set hostname (transient and static) if user instructed us to do so
if options._host_name_overridden:
tasks.backup_hostname(fstore, sstore)
tasks.set_hostname(host_name)
if installer._update_hosts_file:
update_hosts_file(ip_addresses, host_name, fstore)