3
# Copyright (C) 2013 Craig Tracey
5
# Author: Craig Tracey <craigtracey@gmail.com>
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License version 3, as
9
# published by the Free Software Foundation.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
# This module is intended to manage resolv.conf in environments where
21
# early configuration of resolv.conf is necessary for further
22
# bootstrapping and/or where configuration management such as puppet or
23
# chef own dns configuration. As Debian/Ubuntu will, by default, utilize
24
# resovlconf, and similarly RedHat will use sysconfig, this module is
25
# likely to be of little use unless those are configured correctly.
27
# For RedHat with sysconfig, be sure to set PEERDNS=no for all DHCP
28
# enabled NICs. And, in Ubuntu/Debian it is recommended that DNS
29
# be configured via the standard /etc/network/interfaces configuration
36
# manage_resolv_conf: true
39
# nameservers: ['8.8.4.4', '8.8.8.8']
50
from cloudinit.settings import PER_INSTANCE
51
from cloudinit import templater
52
from cloudinit import util
54
frequency = PER_INSTANCE
56
distros = ['fedora', 'rhel']
59
def generate_resolv_conf(cloud, log, params):
60
template_fn = cloud.get_template_filename('resolv.conf')
62
log.warn("No template found, not rendering /etc/resolv.conf")
67
if 'options' in params:
68
for key, val in params['options'].iteritems():
73
false_flags.append(key)
75
for flag in flags + false_flags:
76
del params['options'][flag]
78
params['flags'] = flags
79
log.debug("Writing resolv.conf from template %s" % template_fn)
80
templater.render_to_file(template_fn, '/etc/resolv.conf', params)
83
def handle(name, cfg, _cloud, log, _args):
85
Handler for resolv.conf
87
@param name: The module name "resolv-conf" from cloud.cfg
88
@param cfg: A nested dict containing the entire cloud config contents.
89
@param cloud: The L{CloudInit} object in use.
90
@param log: Pre-initialized Python logger object to use for logging.
91
@param args: Any module arguments from cloud.cfg
93
if "manage_resolv_conf" not in cfg:
94
log.debug(("Skipping module named %s,"
95
" no 'manage_resolv_conf' key in configuration"), name)
98
if not util.get_cfg_option_bool(cfg, "manage_resolv_conf", False):
99
log.debug(("Skipping module named %s,"
100
" 'manage_resolv_conf' present but set to False"), name)
103
if not "resolv_conf" in cfg:
104
log.warn("manage_resolv_conf True but no parameters provided!")
106
generate_resolv_conf(_cloud, log, cfg["resolv_conf"])