Merge lp:~harlowja/cloud-init/update-prev-hostname-read into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Joshua Harlow
Status: Merged
Merged at revision: 733
Proposed branch: lp:~harlowja/cloud-init/update-prev-hostname-read
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 113 lines (+43/-21)
2 files modified
cloudinit/distros/__init__.py (+23/-1)
cloudinit/distros/debian.py (+20/-20)
To merge this branch: bzr merge lp:~harlowja/cloud-init/update-prev-hostname-read
Reviewer Review Type Date Requested Status
Scott Moser Needs Fixing
Review via email: mp+134213@code.launchpad.net

Description of the change

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.

To post a comment you must log in.
Revision history for this message
Scott Moser (smoser) wrote :

$ sudo rm /var/lib/cloud/data/previous-*

$ sudo cloud-init single --name=update-hostname
Cloud-init v. 0.7.1 running 'single' at Tue, 13 Nov 2012 22:59:46 +0000. Up 8497.87 seconds.
2012-11-13 22:59:46,560 - util.py[WARNING]: Error reading hostname from /var/lib/cloud/data/previous-hostname

The second issue comes from the update path.

review: Needs Fixing
732. By Joshua Harlow

Update how errors are handled when writing and reading hostnames.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'cloudinit/distros/__init__.py'
--- cloudinit/distros/__init__.py 2012-11-13 06:19:40 +0000
+++ cloudinit/distros/__init__.py 2012-11-13 23:26:23 +0000
@@ -146,17 +146,37 @@
146146
147 def update_hostname(self, hostname, fqdn, prev_hostname_fn):147 def update_hostname(self, hostname, fqdn, prev_hostname_fn):
148 applying_hostname = hostname148 applying_hostname = hostname
149
150 # Determine what the actual written hostname should be
149 hostname = self._select_hostname(hostname, fqdn)151 hostname = self._select_hostname(hostname, fqdn)
150 prev_hostname = self._read_hostname(prev_hostname_fn)152
153 # If the previous hostname file exists lets see if we
154 # can get a hostname from it
155 if prev_hostname_fn and os.path.exists(prev_hostname_fn):
156 prev_hostname = self._read_hostname(prev_hostname_fn)
157 else:
158 prev_hostname = None
159
160 # Lets get where we should write the system hostname
161 # and what the system hostname is
151 (sys_fn, sys_hostname) = self._read_system_hostname()162 (sys_fn, sys_hostname) = self._read_system_hostname()
152 update_files = []163 update_files = []
164
165 # If there is no previous hostname or it differs
166 # from what we want, lets update it or create the
167 # file in the first place
153 if not prev_hostname or prev_hostname != hostname:168 if not prev_hostname or prev_hostname != hostname:
154 update_files.append(prev_hostname_fn)169 update_files.append(prev_hostname_fn)
155170
171 # If the system hostname is different than the previous
172 # one or the desired one lets update it as well
156 if (not sys_hostname) or (sys_hostname == prev_hostname173 if (not sys_hostname) or (sys_hostname == prev_hostname
157 and sys_hostname != hostname):174 and sys_hostname != hostname):
158 update_files.append(sys_fn)175 update_files.append(sys_fn)
159176
177 # Remove duplicates (incase the previous config filename)
178 # is the same as the system config filename, don't bother
179 # doing it twice
160 update_files = set([f for f in update_files if f])180 update_files = set([f for f in update_files if f])
161 LOG.debug("Attempting to update hostname to %s in %s files",181 LOG.debug("Attempting to update hostname to %s in %s files",
162 hostname, len(update_files))182 hostname, len(update_files))
@@ -173,6 +193,8 @@
173 LOG.debug("%s differs from %s, assuming user maintained hostname.",193 LOG.debug("%s differs from %s, assuming user maintained hostname.",
174 prev_hostname_fn, sys_fn)194 prev_hostname_fn, sys_fn)
175195
196 # If the system hostname file name was provided set the
197 # non-fqdn as the transient hostname.
176 if sys_fn in update_files:198 if sys_fn in update_files:
177 self._apply_hostname(applying_hostname)199 self._apply_hostname(applying_hostname)
178200
179201
=== modified file 'cloudinit/distros/debian.py'
--- cloudinit/distros/debian.py 2012-11-13 06:06:11 +0000
+++ cloudinit/distros/debian.py 2012-11-13 23:26:23 +0000
@@ -88,37 +88,37 @@
88 return hostname88 return hostname
8989
90 def _write_hostname(self, your_hostname, out_fn):90 def _write_hostname(self, your_hostname, out_fn):
91 conf = self._read_hostname_conf(out_fn)91 conf = None
92 try:
93 # Try to update the previous one
94 # so lets see if we can read it first.
95 conf = self._read_hostname_conf(out_fn)
96 except IOError:
97 pass
92 if not conf:98 if not conf:
93 conf = HostnameConf('')99 conf = HostnameConf('')
94 conf.parse()
95 conf.set_hostname(your_hostname)100 conf.set_hostname(your_hostname)
96 util.write_file(out_fn, str(conf), 0644)101 util.write_file(out_fn, str(conf), 0644)
97102
98 def _read_system_hostname(self):103 def _read_system_hostname(self):
99 conf = self._read_hostname_conf(self.hostname_conf_fn)104 sys_hostname = self._read_hostname(self.hostname_conf_fn)
100 if conf:
101 sys_hostname = conf.hostname
102 else:
103 sys_hostname = None
104 return (self.hostname_conf_fn, sys_hostname)105 return (self.hostname_conf_fn, sys_hostname)
105106
106 def _read_hostname_conf(self, filename):107 def _read_hostname_conf(self, filename):
107 try:108 conf = HostnameConf(util.load_file(filename))
108 conf = HostnameConf(util.load_file(filename))109 conf.parse()
109 conf.parse()110 return conf
110 return conf
111 except IOError:
112 util.logexc(LOG, "Error reading hostname from %s", filename)
113 return None
114111
115 def _read_hostname(self, filename, default=None):112 def _read_hostname(self, filename, default=None):
116 conf = self._read_hostname_conf(filename)113 hostname = None
117 if not conf:114 try:
118 return default115 conf = self._read_hostname_conf(filename)
119 if not conf.hostname:116 hostname = conf.hostname
120 return default117 except IOError:
121 return conf.hostname118 pass
119 if not hostname:
120 return default
121 return hostname
122122
123 def _get_localhost_ip(self):123 def _get_localhost_ip(self):
124 # Note: http://www.leonardoborda.com/blog/127-0-1-1-ubuntu-debian/124 # Note: http://www.leonardoborda.com/blog/127-0-1-1-ubuntu-debian/