← Back to team overview

cloud-init-dev team mailing list archive

[Merge] lp:~harlowja/cloud-init/update-prev-hostname-read into lp:cloud-init

 

Joshua Harlow has proposed merging lp:~harlowja/cloud-init/update-prev-hostname-read into lp:cloud-init.

Requested reviews:
  cloud init development team (cloud-init-dev)
Related bugs:
  Bug #1078452 in cloud-init: "update-hostname module results in "Error reading hostname from /var/lib/cloud/data/previoius-hostname""
  https://bugs.launchpad.net/cloud-init/+bug/1078452

For more details, see:
https://code.launchpad.net/~harlowja/cloud-init/update-prev-hostname-read/+merge/134213

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.

-- 
https://code.launchpad.net/~harlowja/cloud-init/update-prev-hostname-read/+merge/134213
Your team cloud init development team is requested to review the proposed merge of lp:~harlowja/cloud-init/update-prev-hostname-read into lp:cloud-init.
=== modified file 'cloudinit/distros/__init__.py'
--- cloudinit/distros/__init__.py	2012-11-13 06:19:40 +0000
+++ cloudinit/distros/__init__.py	2012-11-13 21:50:27 +0000
@@ -146,17 +146,37 @@
 
     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 @@
             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)