Only attempt to read the previous hostname file if it exists.
- Instead of always reading the previous hostname file even if it did not exist lets only read it if it is a valid variable and is actually a existent file instead of just attempting to read it always. - Also update the logging that is done when a previous file does not exist.
This commit is contained in:
commit
28275d9e49
|
@ -146,17 +146,37 @@ class Distro(object):
|
||||||
|
|
||||||
def update_hostname(self, hostname, fqdn, prev_hostname_fn):
|
def update_hostname(self, hostname, fqdn, prev_hostname_fn):
|
||||||
applying_hostname = hostname
|
applying_hostname = hostname
|
||||||
|
|
||||||
|
# Determine what the actual written hostname should be
|
||||||
hostname = self._select_hostname(hostname, fqdn)
|
hostname = self._select_hostname(hostname, fqdn)
|
||||||
prev_hostname = self._read_hostname(prev_hostname_fn)
|
|
||||||
|
# If the previous hostname file exists lets see if we
|
||||||
|
# can get a hostname from it
|
||||||
|
if prev_hostname_fn and os.path.exists(prev_hostname_fn):
|
||||||
|
prev_hostname = self._read_hostname(prev_hostname_fn)
|
||||||
|
else:
|
||||||
|
prev_hostname = None
|
||||||
|
|
||||||
|
# Lets get where we should write the system hostname
|
||||||
|
# and what the system hostname is
|
||||||
(sys_fn, sys_hostname) = self._read_system_hostname()
|
(sys_fn, sys_hostname) = self._read_system_hostname()
|
||||||
update_files = []
|
update_files = []
|
||||||
|
|
||||||
|
# If there is no previous hostname or it differs
|
||||||
|
# from what we want, lets update it or create the
|
||||||
|
# file in the first place
|
||||||
if not prev_hostname or prev_hostname != hostname:
|
if not prev_hostname or prev_hostname != hostname:
|
||||||
update_files.append(prev_hostname_fn)
|
update_files.append(prev_hostname_fn)
|
||||||
|
|
||||||
|
# If the system hostname is different than the previous
|
||||||
|
# one or the desired one lets update it as well
|
||||||
if (not sys_hostname) or (sys_hostname == prev_hostname
|
if (not sys_hostname) or (sys_hostname == prev_hostname
|
||||||
and sys_hostname != hostname):
|
and sys_hostname != hostname):
|
||||||
update_files.append(sys_fn)
|
update_files.append(sys_fn)
|
||||||
|
|
||||||
|
# Remove duplicates (incase the previous config filename)
|
||||||
|
# is the same as the system config filename, don't bother
|
||||||
|
# doing it twice
|
||||||
update_files = set([f for f in update_files if f])
|
update_files = set([f for f in update_files if f])
|
||||||
LOG.debug("Attempting to update hostname to %s in %s files",
|
LOG.debug("Attempting to update hostname to %s in %s files",
|
||||||
hostname, len(update_files))
|
hostname, len(update_files))
|
||||||
|
@ -173,6 +193,8 @@ class Distro(object):
|
||||||
LOG.debug("%s differs from %s, assuming user maintained hostname.",
|
LOG.debug("%s differs from %s, assuming user maintained hostname.",
|
||||||
prev_hostname_fn, sys_fn)
|
prev_hostname_fn, sys_fn)
|
||||||
|
|
||||||
|
# If the system hostname file name was provided set the
|
||||||
|
# non-fqdn as the transient hostname.
|
||||||
if sys_fn in update_files:
|
if sys_fn in update_files:
|
||||||
self._apply_hostname(applying_hostname)
|
self._apply_hostname(applying_hostname)
|
||||||
|
|
||||||
|
|
|
@ -88,37 +88,37 @@ class Distro(distros.Distro):
|
||||||
return hostname
|
return hostname
|
||||||
|
|
||||||
def _write_hostname(self, your_hostname, out_fn):
|
def _write_hostname(self, your_hostname, out_fn):
|
||||||
conf = self._read_hostname_conf(out_fn)
|
conf = None
|
||||||
|
try:
|
||||||
|
# Try to update the previous one
|
||||||
|
# so lets see if we can read it first.
|
||||||
|
conf = self._read_hostname_conf(out_fn)
|
||||||
|
except IOError:
|
||||||
|
pass
|
||||||
if not conf:
|
if not conf:
|
||||||
conf = HostnameConf('')
|
conf = HostnameConf('')
|
||||||
conf.parse()
|
|
||||||
conf.set_hostname(your_hostname)
|
conf.set_hostname(your_hostname)
|
||||||
util.write_file(out_fn, str(conf), 0644)
|
util.write_file(out_fn, str(conf), 0644)
|
||||||
|
|
||||||
def _read_system_hostname(self):
|
def _read_system_hostname(self):
|
||||||
conf = self._read_hostname_conf(self.hostname_conf_fn)
|
sys_hostname = self._read_hostname(self.hostname_conf_fn)
|
||||||
if conf:
|
|
||||||
sys_hostname = conf.hostname
|
|
||||||
else:
|
|
||||||
sys_hostname = None
|
|
||||||
return (self.hostname_conf_fn, sys_hostname)
|
return (self.hostname_conf_fn, sys_hostname)
|
||||||
|
|
||||||
def _read_hostname_conf(self, filename):
|
def _read_hostname_conf(self, filename):
|
||||||
try:
|
conf = HostnameConf(util.load_file(filename))
|
||||||
conf = HostnameConf(util.load_file(filename))
|
conf.parse()
|
||||||
conf.parse()
|
return conf
|
||||||
return conf
|
|
||||||
except IOError:
|
|
||||||
util.logexc(LOG, "Error reading hostname from %s", filename)
|
|
||||||
return None
|
|
||||||
|
|
||||||
def _read_hostname(self, filename, default=None):
|
def _read_hostname(self, filename, default=None):
|
||||||
conf = self._read_hostname_conf(filename)
|
hostname = None
|
||||||
if not conf:
|
try:
|
||||||
|
conf = self._read_hostname_conf(filename)
|
||||||
|
hostname = conf.hostname
|
||||||
|
except IOError:
|
||||||
|
pass
|
||||||
|
if not hostname:
|
||||||
return default
|
return default
|
||||||
if not conf.hostname:
|
return hostname
|
||||||
return default
|
|
||||||
return conf.hostname
|
|
||||||
|
|
||||||
def _get_localhost_ip(self):
|
def _get_localhost_ip(self):
|
||||||
# Note: http://www.leonardoborda.com/blog/127-0-1-1-ubuntu-debian/
|
# Note: http://www.leonardoborda.com/blog/127-0-1-1-ubuntu-debian/
|
||||||
|
|
Loading…
Reference in New Issue