Merge lp:~harlowja/cloud-init/fix-hostname-rh into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Joshua Harlow
Status: Merged
Merged at revision: 704
Proposed branch: lp:~harlowja/cloud-init/fix-hostname-rh
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 201 lines (+90/-19)
6 files modified
cloudinit/config/cc_set_hostname.py (+6/-4)
cloudinit/config/cc_update_hostname.py (+5/-3)
cloudinit/distros/__init__.py (+2/-2)
cloudinit/distros/debian.py (+2/-2)
cloudinit/distros/rhel.py (+17/-8)
tests/unittests/test_handler/test_handler_set_hostname.py (+58/-0)
To merge this branch: bzr merge lp:~harlowja/cloud-init/fix-hostname-rh
Reviewer Review Type Date Requested Status
cloud-init Commiters Pending
Review via email: mp+133607@code.launchpad.net
To post a comment you must log in.
704. By Joshua Harlow

Forgot the test!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cloudinit/config/cc_set_hostname.py'
2--- cloudinit/config/cc_set_hostname.py 2012-06-21 16:12:16 +0000
3+++ cloudinit/config/cc_set_hostname.py 2012-11-09 01:43:18 +0000
4@@ -27,9 +27,11 @@
5 " not setting the hostname in module %s"), name)
6 return
7
8- (hostname, _fqdn) = util.get_hostname_fqdn(cfg, cloud)
9+ (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
10 try:
11- log.debug("Setting hostname to %s", hostname)
12- cloud.distro.set_hostname(hostname)
13+ log.debug("Setting the hostname to %s (%s)", fqdn, hostname)
14+ cloud.distro.set_hostname(hostname, fqdn)
15 except Exception:
16- util.logexc(log, "Failed to set hostname to %s", hostname)
17+ util.logexc(log, "Failed to set the hostname to %s (%s)",
18+ fqdn, hostname)
19+ raise
20
21=== modified file 'cloudinit/config/cc_update_hostname.py'
22--- cloudinit/config/cc_update_hostname.py 2012-08-22 18:12:32 +0000
23+++ cloudinit/config/cc_update_hostname.py 2012-11-09 01:43:18 +0000
24@@ -32,10 +32,12 @@
25 " not updating the hostname in module %s"), name)
26 return
27
28- (hostname, _fqdn) = util.get_hostname_fqdn(cfg, cloud)
29+ (hostname, fqdn) = util.get_hostname_fqdn(cfg, cloud)
30 try:
31 prev_fn = os.path.join(cloud.get_cpath('data'), "previous-hostname")
32- cloud.distro.update_hostname(hostname, prev_fn)
33+ log.debug("Updating hostname to %s (%s)", fqdn, hostname)
34+ cloud.distro.update_hostname(hostname, fqdn, prev_fn)
35 except Exception:
36- util.logexc(log, "Failed to set the hostname to %s", hostname)
37+ util.logexc(log, "Failed to update the hostname to %s (%s)",
38+ fqdn, hostname)
39 raise
40
41=== modified file 'cloudinit/distros/__init__.py'
42--- cloudinit/distros/__init__.py 2012-11-07 15:42:54 +0000
43+++ cloudinit/distros/__init__.py 2012-11-09 01:43:18 +0000
44@@ -58,11 +58,11 @@
45 return self._cfg.get(opt_name, default)
46
47 @abc.abstractmethod
48- def set_hostname(self, hostname):
49+ def set_hostname(self, hostname, fqdn=None):
50 raise NotImplementedError()
51
52 @abc.abstractmethod
53- def update_hostname(self, hostname, prev_hostname_fn):
54+ def update_hostname(self, hostname, fqdn, prev_hostname_fn):
55 raise NotImplementedError()
56
57 @abc.abstractmethod
58
59=== modified file 'cloudinit/distros/debian.py'
60--- cloudinit/distros/debian.py 2012-10-28 02:25:48 +0000
61+++ cloudinit/distros/debian.py 2012-11-09 01:43:18 +0000
62@@ -67,7 +67,7 @@
63 else:
64 return distros.Distro._bring_up_interfaces(self, device_names)
65
66- def set_hostname(self, hostname):
67+ def set_hostname(self, hostname, fqdn=None):
68 self._write_hostname(hostname, "/etc/hostname")
69 LOG.debug("Setting hostname to %s", hostname)
70 util.subp(['hostname', hostname])
71@@ -76,7 +76,7 @@
72 # "" gives trailing newline.
73 util.write_file(out_fn, "%s\n" % str(hostname), 0644)
74
75- def update_hostname(self, hostname, prev_fn):
76+ def update_hostname(self, hostname, fqdn, prev_fn):
77 hostname_prev = self._read_hostname(prev_fn)
78 hostname_in_etc = self._read_hostname("/etc/hostname")
79 update_files = []
80
81=== modified file 'cloudinit/distros/rhel.py'
82--- cloudinit/distros/rhel.py 2012-09-25 20:44:16 +0000
83+++ cloudinit/distros/rhel.py 2012-11-09 01:43:18 +0000
84@@ -146,8 +146,13 @@
85 lines.insert(0, _make_header())
86 util.write_file(fn, "\n".join(lines), 0644)
87
88- def set_hostname(self, hostname):
89- self._write_hostname(hostname, '/etc/sysconfig/network')
90+ def set_hostname(self, hostname, fqdn=None):
91+ # See: http://bit.ly/TwitgL
92+ # Should be fqdn if we can use it
93+ sysconfig_hostname = fqdn
94+ if not sysconfig_hostname:
95+ sysconfig_hostname = hostname
96+ self._write_hostname(sysconfig_hostname, '/etc/sysconfig/network')
97 LOG.debug("Setting hostname to %s", hostname)
98 util.subp(['hostname', hostname])
99
100@@ -165,28 +170,32 @@
101 }
102 self._update_sysconfig_file(out_fn, host_cfg)
103
104- def update_hostname(self, hostname, prev_file):
105+ def update_hostname(self, hostname, fqdn, prev_file):
106+ # See: http://bit.ly/TwitgL
107+ # Should be fqdn if we can use it
108+ sysconfig_hostname = fqdn
109+ if not sysconfig_hostname:
110+ sysconfig_hostname = hostname
111 hostname_prev = self._read_hostname(prev_file)
112 hostname_in_sys = self._read_hostname("/etc/sysconfig/network")
113 update_files = []
114- if not hostname_prev or hostname_prev != hostname:
115+ if not hostname_prev or hostname_prev != sysconfig_hostname:
116 update_files.append(prev_file)
117 if (not hostname_in_sys or
118 (hostname_in_sys == hostname_prev
119- and hostname_in_sys != hostname)):
120+ and hostname_in_sys != sysconfig_hostname)):
121 update_files.append("/etc/sysconfig/network")
122 for fn in update_files:
123 try:
124- self._write_hostname(hostname, fn)
125+ self._write_hostname(sysconfig_hostname, fn)
126 except:
127 util.logexc(LOG, "Failed to write hostname %s to %s",
128- hostname, fn)
129+ sysconfig_hostname, fn)
130 if (hostname_in_sys and hostname_prev and
131 hostname_in_sys != hostname_prev):
132 LOG.debug(("%s differs from /etc/sysconfig/network."
133 " Assuming user maintained hostname."), prev_file)
134 if "/etc/sysconfig/network" in update_files:
135- # Only do this if we are running in non-adjusted root mode
136 LOG.debug("Setting hostname to %s", hostname)
137 util.subp(['hostname', hostname])
138
139
140=== added file 'tests/unittests/test_handler/test_handler_set_hostname.py'
141--- tests/unittests/test_handler/test_handler_set_hostname.py 1970-01-01 00:00:00 +0000
142+++ tests/unittests/test_handler/test_handler_set_hostname.py 2012-11-09 01:43:18 +0000
143@@ -0,0 +1,58 @@
144+from cloudinit.config import cc_set_hostname
145+
146+from cloudinit import cloud
147+from cloudinit import distros
148+from cloudinit import helpers
149+from cloudinit import util
150+
151+from tests.unittests import helpers as t_help
152+
153+import logging
154+
155+from StringIO import StringIO
156+
157+from configobj import ConfigObj
158+
159+LOG = logging.getLogger(__name__)
160+
161+
162+class TestHostname(t_help.FilesystemMockingTestCase):
163+ def setUp(self):
164+ super(TestHostname, self).setUp()
165+ self.tmp = self.makeDir(prefix="unittest_")
166+
167+ def _fetch_distro(self, kind):
168+ cls = distros.fetch(kind)
169+ paths = helpers.Paths({})
170+ return cls(kind, {}, paths)
171+
172+ def test_write_hostname_rhel(self):
173+ cfg = {
174+ 'hostname': 'blah.blah.blah.yahoo.com',
175+ }
176+ distro = self._fetch_distro('rhel')
177+ paths = helpers.Paths({})
178+ ds = None
179+ cc = cloud.Cloud(ds, paths, {}, distro, None)
180+ self.patchUtils(self.tmp)
181+ self.patchOS(self.tmp)
182+ cc_set_hostname.handle('cc_set_hostname',
183+ cfg, cc, LOG, [])
184+ contents = util.load_file("/etc/sysconfig/network")
185+ n_cfg = ConfigObj(StringIO(contents))
186+ self.assertEquals({'HOSTNAME': 'blah.blah.blah.yahoo.com'},
187+ dict(n_cfg))
188+
189+ def test_write_hostname_debian(self):
190+ cfg = {
191+ 'hostname': 'blah.blah.blah.yahoo.com',
192+ }
193+ distro = self._fetch_distro('debian')
194+ paths = helpers.Paths({})
195+ ds = None
196+ cc = cloud.Cloud(ds, paths, {}, distro, None)
197+ self.patchUtils(self.tmp)
198+ cc_set_hostname.handle('cc_set_hostname',
199+ cfg, cc, LOG, [])
200+ contents = util.load_file("/etc/hostname")
201+ self.assertEquals('blah', contents.strip())