Merge lp:~craigtracey/cloud-init/update-etc-hosts into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Craig Tracey
Status: Merged
Merged at revision: 775
Proposed branch: lp:~craigtracey/cloud-init/update-etc-hosts
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 170 lines (+55/-30)
8 files modified
cloudinit/config/__init__.py (+3/-1)
cloudinit/config/cc_update_etc_hosts.py (+3/-2)
cloudinit/distros/__init__.py (+15/-0)
cloudinit/distros/debian.py (+1/-0)
cloudinit/distros/rhel.py (+1/-0)
cloudinit/stages.py (+7/-2)
templates/hosts.debian.tmpl (+25/-0)
templates/hosts.ubuntu.tmpl (+0/-25)
To merge this branch: bzr merge lp:~craigtracey/cloud-init/update-etc-hosts
Reviewer Review Type Date Requested Status
Joshua Harlow (community) Needs Fixing
Review via email: mp+143413@code.launchpad.net

Description of the change

Fix broken cc_update_etc_hosts (LP: #1100036)

Right now, all distros but ubuntu will fail to manage /etc/hosts. This
is due to the fact that the templates are named:
- hosts.ubuntu.tmpl
- hosts.redhat.tmpl

The config handler is specifically looking for a template with the
given distro name.

This change addresses this issue and is contingent upon support of
'osfamilies' as implemented in LP: #1100029
(lp:~craigtracey/cloud-init/osfamilies)

To post a comment you must log in.
Revision history for this message
Joshua Harlow (harlowja) wrote :

Does distro attribute 'self.osfamily = 'redhat'' need to be hard-coded?

The nice thing about having this come from yaml (the system_info section) is that it doesn't need to be (so can be configured to be different). Do u think this should happen? Seems useful?

review: Needs Fixing
Revision history for this message
Craig Tracey (craigtracey) wrote :

Hey Joshua,

As I just posed in #cloud-init (you were away), while I agreed with this and filed LP #1105494, after implementing it, I wasn't really sure how this would be used. I agree that distro should be flexible (not necessarily come via introspection) and defined in yaml. I also agree that distro should handle the case where a distro that is not explicitly supported could piggyback on another (ie. centos defines distro as rhel).

However, I could not fathom a case where you would want to alter osfamily to be something different than what the distro defined its osfamily as (ie. distro: rhel, osfamily: debian). Do you have a use case in mind? I am certainly not against implementing this, I just want to be sure I understand how this could be used.

Thanks for all the reviews - much appreciated!
Craig

Revision history for this message
Joshua Harlow (harlowja) wrote :

All merged. Thanks for the fixes :-)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cloudinit/config/__init__.py'
2--- cloudinit/config/__init__.py 2012-06-29 18:20:34 +0000
3+++ cloudinit/config/__init__.py 2013-01-15 21:43:21 +0000
4@@ -52,5 +52,7 @@
5 if freq and freq not in FREQUENCIES:
6 LOG.warn("Module %s has an unknown frequency %s", mod, freq)
7 if not hasattr(mod, 'distros'):
8- setattr(mod, 'distros', None)
9+ setattr(mod, 'distros', [])
10+ if not hasattr(mod, 'osfamilies'):
11+ setattr(mod, 'osfamilies', [])
12 return mod
13
14=== modified file 'cloudinit/config/cc_update_etc_hosts.py'
15--- cloudinit/config/cc_update_etc_hosts.py 2012-10-28 02:25:48 +0000
16+++ cloudinit/config/cc_update_etc_hosts.py 2013-01-15 21:43:21 +0000
17@@ -37,10 +37,11 @@
18
19 # Render from a template file
20 tpl_fn_name = cloud.get_template_filename("hosts.%s" %
21- (cloud.distro.name))
22+ (cloud.distro.osfamily))
23 if not tpl_fn_name:
24 raise RuntimeError(("No hosts template could be"
25- " found for distro %s") % (cloud.distro.name))
26+ " found for distro %s") %
27+ (cloud.distro.osfamily))
28
29 templater.render_to_file(tpl_fn_name, '/etc/hosts',
30 {'hostname': hostname, 'fqdn': fqdn})
31
32=== modified file 'cloudinit/distros/__init__.py'
33--- cloudinit/distros/__init__.py 2013-01-07 16:36:10 +0000
34+++ cloudinit/distros/__init__.py 2013-01-15 21:43:21 +0000
35@@ -35,6 +35,11 @@
36
37 from cloudinit.distros.parsers import hosts
38
39+OSFAMILIES = {
40+ 'debian': ['debian', 'ubuntu'],
41+ 'redhat': ['fedora', 'rhel']
42+}
43+
44 LOG = logging.getLogger(__name__)
45
46
47@@ -143,6 +148,16 @@
48 def _select_hostname(self, hostname, fqdn):
49 raise NotImplementedError()
50
51+ @staticmethod
52+ def expand_osfamily(family_list):
53+ distros = []
54+ for family in family_list:
55+ if not family in OSFAMILIES:
56+ raise ValueError("No distibutions found for osfamily %s"
57+ % (family))
58+ distros.extend(OSFAMILIES[family])
59+ return distros
60+
61 def update_hostname(self, hostname, fqdn, prev_hostname_fn):
62 applying_hostname = hostname
63
64
65=== modified file 'cloudinit/distros/debian.py'
66--- cloudinit/distros/debian.py 2012-11-13 23:24:53 +0000
67+++ cloudinit/distros/debian.py 2013-01-15 21:43:21 +0000
68@@ -48,6 +48,7 @@
69 # calls from repeatly happening (when they
70 # should only happen say once per instance...)
71 self._runner = helpers.Runners(paths)
72+ self.osfamily = 'debian'
73
74 def apply_locale(self, locale, out_fn=None):
75 if not out_fn:
76
77=== modified file 'cloudinit/distros/rhel.py'
78--- cloudinit/distros/rhel.py 2012-11-13 06:14:31 +0000
79+++ cloudinit/distros/rhel.py 2013-01-15 21:43:21 +0000
80@@ -60,6 +60,7 @@
81 # calls from repeatly happening (when they
82 # should only happen say once per instance...)
83 self._runner = helpers.Runners(paths)
84+ self.osfamily = 'redhat'
85
86 def install_packages(self, pkglist):
87 self.package_command('install', pkglist)
88
89=== modified file 'cloudinit/stages.py'
90--- cloudinit/stages.py 2012-12-17 13:41:11 +0000
91+++ cloudinit/stages.py 2013-01-15 21:43:21 +0000
92@@ -529,11 +529,16 @@
93 freq = mod.frequency
94 if not freq in FREQUENCIES:
95 freq = PER_INSTANCE
96- worked_distros = mod.distros
97+
98+ worked_distros = set(mod.distros)
99+ worked_distros.update(
100+ distros.Distro.expand_osfamily(mod.osfamilies))
101+
102 if (worked_distros and d_name not in worked_distros):
103 LOG.warn(("Module %s is verified on %s distros"
104 " but not on %s distro. It may or may not work"
105- " correctly."), name, worked_distros, d_name)
106+ " correctly."), name, list(worked_distros),
107+ d_name)
108 # Use the configs logger and not our own
109 # TODO(harlowja): possibly check the module
110 # for having a LOG attr and just give it back
111
112=== added file 'templates/hosts.debian.tmpl'
113--- templates/hosts.debian.tmpl 1970-01-01 00:00:00 +0000
114+++ templates/hosts.debian.tmpl 2013-01-15 21:43:21 +0000
115@@ -0,0 +1,25 @@
116+## This file (/etc/cloud/templates/hosts.tmpl) is only utilized
117+## if enabled in cloud-config. Specifically, in order to enable it
118+## you need to add the following to config:
119+## manage_etc_hosts: True
120+##
121+## Note, double-hash commented lines will not appear in /etc/hosts
122+#
123+# Your system has configured 'manage_etc_hosts' as True.
124+# As a result, if you wish for changes to this file to persist
125+# then you will need to either
126+# a.) make changes to the master file in /etc/cloud/templates/hosts.tmpl
127+# b.) change or remove the value of 'manage_etc_hosts' in
128+# /etc/cloud/cloud.cfg or cloud-config from user-data
129+#
130+## The value '$hostname' will be replaced with the local-hostname
131+127.0.1.1 $fqdn $hostname
132+127.0.0.1 localhost
133+
134+# The following lines are desirable for IPv6 capable hosts
135+::1 ip6-localhost ip6-loopback
136+fe00::0 ip6-localnet
137+ff00::0 ip6-mcastprefix
138+ff02::1 ip6-allnodes
139+ff02::2 ip6-allrouters
140+ff02::3 ip6-allhosts
141
142=== removed file 'templates/hosts.ubuntu.tmpl'
143--- templates/hosts.ubuntu.tmpl 2012-07-09 20:41:45 +0000
144+++ templates/hosts.ubuntu.tmpl 1970-01-01 00:00:00 +0000
145@@ -1,25 +0,0 @@
146-## This file (/etc/cloud/templates/hosts.tmpl) is only utilized
147-## if enabled in cloud-config. Specifically, in order to enable it
148-## you need to add the following to config:
149-## manage_etc_hosts: True
150-##
151-## Note, double-hash commented lines will not appear in /etc/hosts
152-#
153-# Your system has configured 'manage_etc_hosts' as True.
154-# As a result, if you wish for changes to this file to persist
155-# then you will need to either
156-# a.) make changes to the master file in /etc/cloud/templates/hosts.tmpl
157-# b.) change or remove the value of 'manage_etc_hosts' in
158-# /etc/cloud/cloud.cfg or cloud-config from user-data
159-#
160-## The value '$hostname' will be replaced with the local-hostname
161-127.0.1.1 $fqdn $hostname
162-127.0.0.1 localhost
163-
164-# The following lines are desirable for IPv6 capable hosts
165-::1 ip6-localhost ip6-loopback
166-fe00::0 ip6-localnet
167-ff00::0 ip6-mcastprefix
168-ff02::1 ip6-allnodes
169-ff02::2 ip6-allrouters
170-ff02::3 ip6-allhosts