Always set hostname

This prevents cases when hostname on system is set inconsistently
(transient and static hostname differs) and may cause IPA errors.

This commit ensures that all hostnames are set properly.

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

Reviewed-By: David Kupka <dkupka@redhat.com>
This commit is contained in:
Martin Basti 2016-04-19 18:36:32 +02:00
parent 14ee02dcbd
commit c5686295f1
6 changed files with 36 additions and 64 deletions

View File

@ -713,12 +713,12 @@ def uninstall(options, env):
root_logger.warning(
"Failed to disable automatic startup of the SSSD daemon: %s", e)
tasks.restore_hostname(fstore, statestore)
if fstore.has_files():
root_logger.info("Restoring client configuration files")
tasks.restore_network_configuration(fstore, statestore)
fstore.restore_all_files()
ipautil.restore_hostname(statestore)
unconfigure_nisdomain()
nscd = services.knownservices.nscd

View File

@ -25,7 +25,7 @@ This base platform module exports default filesystem paths.
class BasePathNamespace(object):
BASH = "/bin/bash"
BIN_FALSE = "/bin/false"
BIN_HOSTNAME = "/bin/hostname"
BIN_HOSTNAMECTL = "/bin/hostnamectl"
LS = "/bin/ls"
SH = "/bin/sh"
SYSTEMCTL = "/bin/systemctl"

View File

@ -48,7 +48,7 @@ class BaseTaskNamespace(object):
def backup_and_replace_hostname(self, fstore, statestore, hostname):
"""
Backs up the current hostname in the statestore (so that it can be
restored by the restore_network_configuration platform task).
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.
@ -106,7 +106,7 @@ class BaseTaskNamespace(object):
return
def restore_network_configuration(self, fstore, statestore):
def restore_hostname(self, fstore, statestore):
"""
Restores the original hostname as backed up in the
backup_and_replace_hostname platform task.
@ -237,6 +237,14 @@ class BaseTaskNamespace(object):
"""
return parse_version(version)
def set_hostname(self, hostname):
"""
Set hostname for the system
No return value expected, raise CalledProcessError when error occurred
"""
return
def configure_httpd_service_ipa_conf(self):
"""Configure httpd service to work with IPA"""
raise NotImplementedError()

View File

@ -26,10 +26,10 @@ system tasks.
from __future__ import print_function
import os
import stat
import socket
import sys
import base64
import traceback
from cffi import FFI
from ctypes.util import find_library
from functools import total_ordering
@ -330,38 +330,31 @@ class RedHatTaskNamespace(BaseTaskNamespace):
def backup_and_replace_hostname(self, fstore, statestore, hostname):
old_hostname = socket.gethostname()
try:
ipautil.run([paths.BIN_HOSTNAME, hostname])
self.set_hostname(hostname)
except ipautil.CalledProcessError as e:
print(("Failed to set this machine hostname to "
"%s (%s)." % (hostname, str(e))), file=sys.stderr)
filepath = paths.ETC_HOSTNAME
if os.path.exists(filepath):
# read old hostname
with open(filepath, 'r') as f:
for line in f.readlines():
line = line.strip()
if not line or line.startswith('#'):
# skip comment or empty line
continue
old_hostname = line
break
fstore.backup_file(filepath)
with open(filepath, 'w') as f:
f.write("%s\n" % hostname)
os.chmod(filepath,
stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
os.chown(filepath, 0, 0)
self.restore_context(filepath)
# store old hostname
statestore.backup_state('network', 'hostname', old_hostname)
def restore_network_configuration(self, fstore, statestore):
def restore_hostname(self, fstore, statestore):
old_filepath = paths.SYSCONFIG_NETWORK
old_hostname = statestore.get_state('network', 'hostname')
hostname_was_configured = False
if old_hostname is not None:
try:
self.set_hostname(old_hostname)
except ipautil.CalledProcessError as e:
root_logger.debug(traceback.format_exc())
root_logger.error(
"Failed to restore this machine hostname to %s (%s).",
old_hostname, e
)
if fstore.has_file(old_filepath):
# This is Fedora >=18 instance that was upgraded from previous
@ -371,20 +364,11 @@ class RedHatTaskNamespace(BaseTaskNamespace):
fstore.restore_file(old_filepath, old_filepath_restore)
print("Deprecated configuration file '%s' was restored to '%s'" \
% (old_filepath, old_filepath_restore))
hostname_was_configured = True
filepath = paths.ETC_HOSTNAME
if fstore.has_file(filepath):
fstore.restore_file(filepath)
hostname_was_configured = True
if not hostname_was_configured and old_hostname:
# hostname was not configured before but was set by IPA. Delete
# /etc/hostname to restore previous configuration
try:
os.remove(filepath)
except OSError:
pass
def set_selinux_booleans(self, required_settings, backup_func=None):
def get_setsebool_args(changes):
@ -490,4 +474,7 @@ class RedHatTaskNamespace(BaseTaskNamespace):
paths.SYSTEMD_SYSTEM_HTTPD_IPA_CONF, e
)
def set_hostname(self, hostname):
ipautil.run([paths.BIN_HOSTNAMECTL, 'set-hostname', hostname])
tasks = RedHatTaskNamespace()

View File

@ -1475,18 +1475,6 @@ def dn_attribute_property(private_name):
return property(getter, setter)
def restore_hostname(statestore):
"""
Restore hostname of a machine, if it was set before
"""
old_hostname = statestore.restore_state('network','hostname')
system_hostname = socket.gethostname()
if old_hostname is not None and old_hostname != system_hostname:
try:
run([paths.BIN_HOSTNAME, old_hostname])
except CalledProcessError as e:
print("Failed to set this machine hostname back to %s: %s" % (old_hostname, str(e)), file=sys.stderr)
def posixify(string):
"""
Convert a string to a more strict alpha-numeric representation.

View File

@ -573,16 +573,6 @@ def install_check(installer):
host_name = host_name.lower()
root_logger.debug("will use host_name: %s\n" % host_name)
system_hostname = get_fqdn()
if host_name != system_hostname:
print(file=sys.stderr)
print(("Warning: hostname %s does not match system "
"hostname %s." % (host_name, system_hostname)), file=sys.stderr)
print(("System hostname will be updated during the "
"installation process"), file=sys.stderr)
print("to prevent service failures.", file=sys.stderr)
print(file=sys.stderr)
if not options.domain_name:
domain_name = read_domain_name(host_name[host_name.find(".")+1:],
not installer.interactive)
@ -827,10 +817,11 @@ def install(installer):
print("Please wait until the prompt is returned.")
print("")
system_hostname = get_fqdn()
if host_name != system_hostname:
# configure /etc/sysconfig/network to contain the custom hostname
tasks.backup_and_replace_hostname(fstore, sstore, host_name)
# 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)
if installer._update_hosts_file:
update_hosts_file(ip_addresses, host_name, fstore)
@ -1212,7 +1203,7 @@ def uninstall(installer):
custodiainstance.CustodiaInstance().uninstall()
memcacheinstance.MemcacheInstance().uninstall()
otpdinstance.OtpdInstance().uninstall()
tasks.restore_network_configuration(fstore, sstore)
tasks.restore_hostname(fstore, sstore)
fstore.restore_all_files()
try:
os.remove(paths.ROOT_IPA_CACHE)
@ -1234,8 +1225,6 @@ def uninstall(installer):
services.knownservices.ipa.disable()
ipautil.restore_hostname(sstore)
# remove upgrade state file
sysupgrade.remove_upgrade_file()