summaryrefslogtreecommitdiff
path: root/cloudinit/distros/__init__.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2012-11-13 18:41:31 -0500
committerScott Moser <smoser@ubuntu.com>2012-11-13 18:41:31 -0500
commit8ae9c57ca05a0f99131a6693c109cbb52c0f0d4d (patch)
tree6df7295f5eab5740761b598ebfe545a06b88a8f0 /cloudinit/distros/__init__.py
parente016e7c7837c70f5fce0c3b3d9bd944a8d43f9f0 (diff)
parente91fbc058cdd709a561863202231076788323782 (diff)
downloadvyos-cloud-init-8ae9c57ca05a0f99131a6693c109cbb52c0f0d4d.tar.gz
vyos-cloud-init-8ae9c57ca05a0f99131a6693c109cbb52c0f0d4d.zip
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. LP: #1078452
Diffstat (limited to 'cloudinit/distros/__init__.py')
-rw-r--r--cloudinit/distros/__init__.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py
index 10e07e82..ea0bac23 100644
--- a/cloudinit/distros/__init__.py
+++ b/cloudinit/distros/__init__.py
@@ -146,17 +146,37 @@ class Distro(object):
def update_hostname(self, hostname, fqdn, prev_hostname_fn):
applying_hostname = hostname
+
+ # Determine what the actual written hostname should be
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()
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:
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
and sys_hostname != hostname):
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])
LOG.debug("Attempting to update hostname to %s in %s files",
hostname, len(update_files))
@@ -173,6 +193,8 @@ class Distro(object):
LOG.debug("%s differs from %s, assuming user maintained hostname.",
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:
self._apply_hostname(applying_hostname)