cleanups for rhel network config

Rework the rhel sysconfig writing/updating so that it goes through a
single function which helps ensure correctness. Also write to
/etc/sysconfig/network when we have written out devices to ensure that
networking is on.
This commit is contained in:
Joshua Harlow 2012-09-24 21:02:18 -04:00 committed by Scott Moser
commit adf346404a

View File

@ -26,6 +26,7 @@ from cloudinit import distros
from cloudinit import helpers from cloudinit import helpers
from cloudinit import log as logging from cloudinit import log as logging
from cloudinit import util from cloudinit import util
from cloudinit import version
from cloudinit.settings import PER_INSTANCE from cloudinit.settings import PER_INSTANCE
@ -56,6 +57,18 @@ D_QUOTE_CHARS = {
} }
def _make_sysconfig_bool(val):
if val:
return 'yes'
else:
return 'no'
def _make_header():
ci_ver = version.version_string()
return '# Created by cloud-init v. %s' % (ci_ver)
class Distro(distros.Distro): class Distro(distros.Distro):
def __init__(self, name, cfg, paths): def __init__(self, name, cfg, paths):
@ -76,9 +89,8 @@ class Distro(distros.Distro):
if search_servers: if search_servers:
contents.append("search %s" % (" ".join(search_servers))) contents.append("search %s" % (" ".join(search_servers)))
if contents: if contents:
resolve_rw_fn = self._paths.join(False, "/etc/resolv.conf") contents.insert(0, _make_header())
contents.insert(0, '# Created by cloud-init') util.write_file("/etc/resolv.conf", "\n".join(contents), 0644)
util.write_file(resolve_rw_fn, "\n".join(contents), 0644)
def _write_network(self, settings): def _write_network(self, settings):
# TODO(harlowja) fix this... since this is the ubuntu format # TODO(harlowja) fix this... since this is the ubuntu format
@ -91,80 +103,78 @@ class Distro(distros.Distro):
dev_names = entries.keys() dev_names = entries.keys()
for (dev, info) in entries.iteritems(): for (dev, info) in entries.iteritems():
net_fn = NETWORK_FN_TPL % (dev) net_fn = NETWORK_FN_TPL % (dev)
net_ro_fn = self._paths.join(True, net_fn) net_cfg = {
(prev_exist, net_cfg) = self._read_conf(net_ro_fn) 'DEVICE': dev,
net_cfg['DEVICE'] = dev 'NETMASK': info.get('netmask'),
boot_proto = info.get('bootproto') 'IPADDR': info.get('address'),
if boot_proto: 'BOOTPROTO': info.get('bootproto'),
net_cfg['BOOTPROTO'] = boot_proto 'GATEWAY': info.get('gateway'),
net_mask = info.get('netmask') 'BROADCAST': info.get('broadcast'),
if net_mask: 'MACADDR': info.get('hwaddress'),
net_cfg["NETMASK"] = net_mask 'ONBOOT': _make_sysconfig_bool(info.get('auto')),
addr = info.get('address') }
if addr: self._update_sysconfig_file(net_fn, net_cfg)
net_cfg["IPADDR"] = addr
if info.get('auto'):
net_cfg['ONBOOT'] = 'yes'
else:
net_cfg['ONBOOT'] = 'no'
gtway = info.get('gateway')
if gtway:
net_cfg["GATEWAY"] = gtway
bcast = info.get('broadcast')
if bcast:
net_cfg["BROADCAST"] = bcast
mac_addr = info.get('hwaddress')
if mac_addr:
net_cfg["MACADDR"] = mac_addr
lines = net_cfg.write()
if 'dns-nameservers' in info: if 'dns-nameservers' in info:
nameservers.extend(info['dns-nameservers']) nameservers.extend(info['dns-nameservers'])
if 'dns-search' in info: if 'dns-search' in info:
searchservers.extend(info['dns-search']) searchservers.extend(info['dns-search'])
if not prev_exist:
lines.insert(0, '# Created by cloud-init')
w_contents = "\n".join(lines)
net_rw_fn = self._paths.join(False, net_fn)
util.write_file(net_rw_fn, w_contents, 0644)
if nameservers or searchservers: if nameservers or searchservers:
self._write_resolve(nameservers, searchservers) self._write_resolve(nameservers, searchservers)
if dev_names:
net_cfg = {
'NETWORKING': _make_sysconfig_bool(True),
}
self._update_sysconfig_file("/etc/sysconfig/network", net_cfg)
return dev_names return dev_names
def _update_sysconfig_file(self, fn, adjustments, allow_empty=False):
if not adjustments:
return
(exists, contents) = self._read_conf(fn)
updated_am = 0
for (k, v) in adjustments.items():
if v is None:
continue
v = str(v)
if len(v) == 0 and not allow_empty:
continue
contents[k] = v
updated_am += 1
if updated_am:
lines = contents.write()
if not exists:
lines.insert(0, _make_header())
util.write_file(fn, "\n".join(lines), 0644)
def set_hostname(self, hostname): def set_hostname(self, hostname):
out_fn = self._paths.join(False, '/etc/sysconfig/network') self._write_hostname(hostname, '/etc/sysconfig/network')
self._write_hostname(hostname, out_fn) LOG.debug("Setting hostname to %s", hostname)
if out_fn == '/etc/sysconfig/network': util.subp(['hostname', hostname])
# Only do this if we are running in non-adjusted root mode
LOG.debug("Setting hostname to %s", hostname)
util.subp(['hostname', hostname])
def apply_locale(self, locale, out_fn=None): def apply_locale(self, locale, out_fn=None):
if not out_fn: if not out_fn:
out_fn = self._paths.join(False, '/etc/sysconfig/i18n') out_fn = '/etc/sysconfig/i18n'
ro_fn = self._paths.join(True, '/etc/sysconfig/i18n') locale_cfg = {
(_exists, contents) = self._read_conf(ro_fn) 'LANG': locale,
contents['LANG'] = locale }
w_contents = "\n".join(contents.write()) self._update_sysconfig_file(out_fn, locale_cfg)
util.write_file(out_fn, w_contents, 0644)
def _write_hostname(self, hostname, out_fn): def _write_hostname(self, hostname, out_fn):
(_exists, contents) = self._read_conf(out_fn) host_cfg = {
contents['HOSTNAME'] = hostname 'HOSTNAME': hostname,
w_contents = "\n".join(contents.write()) }
util.write_file(out_fn, w_contents, 0644) self._update_sysconfig_file(out_fn, host_cfg)
def update_hostname(self, hostname, prev_file): def update_hostname(self, hostname, prev_file):
hostname_prev = self._read_hostname(prev_file) hostname_prev = self._read_hostname(prev_file)
read_fn = self._paths.join(True, "/etc/sysconfig/network") hostname_in_sys = self._read_hostname("/etc/sysconfig/network")
hostname_in_sys = self._read_hostname(read_fn)
update_files = [] update_files = []
if not hostname_prev or hostname_prev != hostname: if not hostname_prev or hostname_prev != hostname:
update_files.append(prev_file) update_files.append(prev_file)
if (not hostname_in_sys or if (not hostname_in_sys or
(hostname_in_sys == hostname_prev (hostname_in_sys == hostname_prev
and hostname_in_sys != hostname)): and hostname_in_sys != hostname)):
write_fn = self._paths.join(False, "/etc/sysconfig/network") update_files.append("/etc/sysconfig/network")
update_files.append(write_fn)
for fn in update_files: for fn in update_files:
try: try:
self._write_hostname(hostname, fn) self._write_hostname(hostname, fn)
@ -202,14 +212,12 @@ class Distro(distros.Distro):
raise RuntimeError(("Invalid timezone %s," raise RuntimeError(("Invalid timezone %s,"
" no file found at %s") % (tz, tz_file)) " no file found at %s") % (tz, tz_file))
# Adjust the sysconfig clock zone setting # Adjust the sysconfig clock zone setting
read_fn = self._paths.join(True, "/etc/sysconfig/clock") clock_cfg = {
(_exists, contents) = self._read_conf(read_fn) 'ZONE': tz,
contents['ZONE'] = tz }
tz_contents = "\n".join(contents.write()) self._update_sysconfig_file("/etc/sysconfig/clock", clock_cfg)
write_fn = self._paths.join(False, "/etc/sysconfig/clock")
util.write_file(write_fn, tz_contents)
# This ensures that the correct tz will be used for the system # This ensures that the correct tz will be used for the system
util.copy(tz_file, self._paths.join(False, "/etc/localtime")) util.copy(tz_file, "/etc/localtime")
def package_command(self, command, args=None): def package_command(self, command, args=None):
cmd = ['yum'] cmd = ['yum']