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
1=== modified file 'cloudinit/distros/__init__.py'
2--- cloudinit/distros/__init__.py 2012-11-13 06:19:40 +0000
3+++ cloudinit/distros/__init__.py 2012-11-13 23:26:23 +0000
4@@ -146,17 +146,37 @@
5
6 def update_hostname(self, hostname, fqdn, prev_hostname_fn):
7 applying_hostname = hostname
8+
9+ # Determine what the actual written hostname should be
10 hostname = self._select_hostname(hostname, fqdn)
11- prev_hostname = self._read_hostname(prev_hostname_fn)
12+
13+ # If the previous hostname file exists lets see if we
14+ # can get a hostname from it
15+ if prev_hostname_fn and os.path.exists(prev_hostname_fn):
16+ prev_hostname = self._read_hostname(prev_hostname_fn)
17+ else:
18+ prev_hostname = None
19+
20+ # Lets get where we should write the system hostname
21+ # and what the system hostname is
22 (sys_fn, sys_hostname) = self._read_system_hostname()
23 update_files = []
24+
25+ # If there is no previous hostname or it differs
26+ # from what we want, lets update it or create the
27+ # file in the first place
28 if not prev_hostname or prev_hostname != hostname:
29 update_files.append(prev_hostname_fn)
30
31+ # If the system hostname is different than the previous
32+ # one or the desired one lets update it as well
33 if (not sys_hostname) or (sys_hostname == prev_hostname
34 and sys_hostname != hostname):
35 update_files.append(sys_fn)
36
37+ # Remove duplicates (incase the previous config filename)
38+ # is the same as the system config filename, don't bother
39+ # doing it twice
40 update_files = set([f for f in update_files if f])
41 LOG.debug("Attempting to update hostname to %s in %s files",
42 hostname, len(update_files))
43@@ -173,6 +193,8 @@
44 LOG.debug("%s differs from %s, assuming user maintained hostname.",
45 prev_hostname_fn, sys_fn)
46
47+ # If the system hostname file name was provided set the
48+ # non-fqdn as the transient hostname.
49 if sys_fn in update_files:
50 self._apply_hostname(applying_hostname)
51
52
53=== modified file 'cloudinit/distros/debian.py'
54--- cloudinit/distros/debian.py 2012-11-13 06:06:11 +0000
55+++ cloudinit/distros/debian.py 2012-11-13 23:26:23 +0000
56@@ -88,37 +88,37 @@
57 return hostname
58
59 def _write_hostname(self, your_hostname, out_fn):
60- conf = self._read_hostname_conf(out_fn)
61+ conf = None
62+ try:
63+ # Try to update the previous one
64+ # so lets see if we can read it first.
65+ conf = self._read_hostname_conf(out_fn)
66+ except IOError:
67+ pass
68 if not conf:
69 conf = HostnameConf('')
70- conf.parse()
71 conf.set_hostname(your_hostname)
72 util.write_file(out_fn, str(conf), 0644)
73
74 def _read_system_hostname(self):
75- conf = self._read_hostname_conf(self.hostname_conf_fn)
76- if conf:
77- sys_hostname = conf.hostname
78- else:
79- sys_hostname = None
80+ sys_hostname = self._read_hostname(self.hostname_conf_fn)
81 return (self.hostname_conf_fn, sys_hostname)
82
83 def _read_hostname_conf(self, filename):
84- try:
85- conf = HostnameConf(util.load_file(filename))
86- conf.parse()
87- return conf
88- except IOError:
89- util.logexc(LOG, "Error reading hostname from %s", filename)
90- return None
91+ conf = HostnameConf(util.load_file(filename))
92+ conf.parse()
93+ return conf
94
95 def _read_hostname(self, filename, default=None):
96- conf = self._read_hostname_conf(filename)
97- if not conf:
98- return default
99- if not conf.hostname:
100- return default
101- return conf.hostname
102+ hostname = None
103+ try:
104+ conf = self._read_hostname_conf(filename)
105+ hostname = conf.hostname
106+ except IOError:
107+ pass
108+ if not hostname:
109+ return default
110+ return hostname
111
112 def _get_localhost_ip(self):
113 # Note: http://www.leonardoborda.com/blog/127-0-1-1-ubuntu-debian/