Merge lp:~craigtracey/cloud-init/resolv-conf-config into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Craig Tracey
Status: Merged
Merged at revision: 769
Proposed branch: lp:~craigtracey/cloud-init/resolv-conf-config
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 180 lines (+166/-0)
3 files modified
cloudinit/config/cc_resolv_conf.py (+107/-0)
doc/examples/cloud-config-resolv-conf.txt (+20/-0)
templates/resolv.conf.tmpl (+39/-0)
To merge this branch: bzr merge lp:~craigtracey/cloud-init/resolv-conf-config
Reviewer Review Type Date Requested Status
Joshua Harlow (community) Needs Fixing
Review via email: mp+143633@code.launchpad.net

Description of the change

Adding a resolv.conf configuration module (LP: #1100434)

Managing resolv.conf can be quite handy when running in an environment where you would like to control DNS resolution, despite being provided DNS server information by DHCP. This module will allow one to define the structure of their resolv.conf and write it PER_ONCE.

Right now this makes the most sense on RedHat, and therefore, has defined 'distros' as such.

To post a comment you must log in.
762. By Craig Tracey

Fixing missing argument to get_cfg_option_bool

Forgot to pass cfg to this function, and thus this would have never
worked.

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

Can you add a doc/ example for this (basically the same as your comment).

When moving to http://cloud-init.readthedocs.org/en/latest/topics/examples.html

It gives us the ability to show said example 'automatically' (pretty formatted and all).

review: Needs Fixing
763. By ctracey <email address hidden>

Adding a doc example for resolv_conf handler.

As per harlowja's suggestion addding an example to the doc directory for
cc_resolv_conf.py

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'cloudinit/config/cc_resolv_conf.py'
2--- cloudinit/config/cc_resolv_conf.py 1970-01-01 00:00:00 +0000
3+++ cloudinit/config/cc_resolv_conf.py 2013-01-25 22:31:23 +0000
4@@ -0,0 +1,107 @@
5+# vi: ts=4 expandtab
6+#
7+# Copyright (C) 2013 Craig Tracey
8+#
9+# Author: Craig Tracey <craigtracey@gmail.com>
10+#
11+# This program is free software: you can redistribute it and/or modify
12+# it under the terms of the GNU General Public License version 3, as
13+# published by the Free Software Foundation.
14+#
15+# This program is distributed in the hope that it will be useful,
16+# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+# GNU General Public License for more details.
19+#
20+# You should have received a copy of the GNU General Public License
21+# along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
23+# Note:
24+# This module is intended to manage resolv.conf in environments where
25+# early configuration of resolv.conf is necessary for further
26+# bootstrapping and/or where configuration management such as puppet or
27+# chef own dns configuration. As Debian/Ubuntu will, by default, utilize
28+# resovlconf, and similarly RedHat will use sysconfig, this module is
29+# likely to be of little use unless those are configured correctly.
30+#
31+# For RedHat with sysconfig, be sure to set PEERDNS=no for all DHCP
32+# enabled NICs. And, in Ubuntu/Debian it is recommended that DNS
33+# be configured via the standard /etc/network/interfaces configuration
34+# file.
35+#
36+#
37+# Usage Example:
38+#
39+# #cloud-config
40+# manage_resolv_conf: true
41+#
42+# resolv_conf:
43+# nameservers: ['8.8.4.4', '8.8.8.8']
44+# searchdomains:
45+# - foo.example.com
46+# - bar.example.com
47+# domain: example.com
48+# options:
49+# rotate: true
50+# timeout: 1
51+#
52+
53+
54+from cloudinit.settings import PER_ONCE
55+from cloudinit import templater
56+from cloudinit import util
57+
58+frequency = PER_ONCE
59+
60+distros = ['fedora', 'rhel']
61+
62+
63+def generate_resolv_conf(cloud, log, params):
64+ template_fn = cloud.get_template_filename('resolv.conf')
65+ if not template_fn:
66+ log.warn("No template found, not rendering /etc/resolv.conf")
67+ return
68+
69+ flags = []
70+ false_flags = []
71+ if 'options' in params:
72+ for key, val in params['options'].iteritems():
73+ if type(val) == bool:
74+ if val:
75+ flags.append(key)
76+ else:
77+ false_flags.append(key)
78+
79+ for flag in flags + false_flags:
80+ del params['options'][flag]
81+
82+ params['flags'] = flags
83+ log.debug("Writing resolv.conf from template %s" % template_fn)
84+ templater.render_to_file(template_fn, '/etc/resolv.conf', params)
85+
86+
87+def handle(name, cfg, _cloud, log, _args):
88+ """
89+ Handler for resolv.conf
90+
91+ @param name: The module name "resolv-conf" from cloud.cfg
92+ @param cfg: A nested dict containing the entire cloud config contents.
93+ @param cloud: The L{CloudInit} object in use.
94+ @param log: Pre-initialized Python logger object to use for logging.
95+ @param args: Any module arguments from cloud.cfg
96+ """
97+ if "manage_resolv_conf" not in cfg:
98+ log.debug(("Skipping module named %s,"
99+ " no 'manage_resolv_conf' key in configuration"), name)
100+ return
101+
102+ if not util.get_cfg_option_bool(cfg, "manage_resolv_conf", False):
103+ log.debug(("Skipping module named %s,"
104+ " 'manage_resolv_conf' present but set to False"), name)
105+ return
106+
107+ if not "resolv_conf" in cfg:
108+ log.warn("manage_resolv_conf True but no parameters provided!")
109+
110+ generate_resolv_conf(_cloud, log, cfg["resolv_conf"])
111+ return
112
113=== added file 'doc/examples/cloud-config-resolv-conf.txt'
114--- doc/examples/cloud-config-resolv-conf.txt 1970-01-01 00:00:00 +0000
115+++ doc/examples/cloud-config-resolv-conf.txt 2013-01-25 22:31:23 +0000
116@@ -0,0 +1,20 @@
117+#cloud-config
118+#
119+# This is an example file to automatically configure resolv.conf when the
120+# instance boots for the first time.
121+#
122+# Ensure that your yaml is valid and pass this as user-data when starting
123+# the instance. Also be sure that your cloud.cfg file includes this
124+# configuration module in the appropirate section.
125+#
126+manage-resolv-conf: true
127+
128+resolv_conf:
129+ nameservers: ['8.8.4.4', '8.8.8.8']
130+ searchdomains:
131+ - foo.example.com
132+ - bar.example.com
133+ domain: example.com
134+ options:
135+ rotate: true
136+ timeout: 1
137
138=== added file 'templates/resolv.conf.tmpl'
139--- templates/resolv.conf.tmpl 1970-01-01 00:00:00 +0000
140+++ templates/resolv.conf.tmpl 2013-01-25 22:31:23 +0000
141@@ -0,0 +1,39 @@
142+#
143+# Your system has been configured with 'manage-resolv-conf' set to true.
144+# As a result, cloud-init has written this file with configuration data
145+# that it has been provided. Cloud-init, by default, will write this file
146+# a single time (PER_ONCE).
147+#
148+
149+#if $varExists('nameservers')
150+#for $server in $nameservers
151+nameserver $server
152+#end for
153+#end if
154+#if $varExists('searchdomains')
155+search #slurp
156+#for $search in $searchdomains
157+$search #slurp
158+#end for
159+
160+#end if
161+#if $varExists('domain')
162+domain $domain
163+#end if
164+#if $varExists('sortlist')
165+sortlist #slurp
166+#for $sort in $sortlist
167+$sort #slurp
168+#end for
169+
170+#end if
171+#if $varExists('options') or $varExists('flags')
172+options #slurp
173+#for $flag in $flags
174+$flag #slurp
175+#end for
176+#for $key, $value in $options.items()
177+$key:$value #slurp
178+#end for
179+
180+#end if