summaryrefslogtreecommitdiff
path: root/cloudinit
diff options
context:
space:
mode:
authorMatthew Thode <mthode@mthode.org>2016-08-21 18:15:56 -0500
committerMatthew Thode <mthode@mthode.org>2016-08-22 10:33:44 -0500
commit1f8b37e0d80534d2055ee2e888f5a7e36c4b98b4 (patch)
tree6c8f3e088ca1eb16b459c4494f5128b94b7d06cf /cloudinit
parent41271bd8609550d02868b7681b49b8791678beaa (diff)
downloadvyos-cloud-init-1f8b37e0d80534d2055ee2e888f5a7e36c4b98b4.tar.gz
vyos-cloud-init-1f8b37e0d80534d2055ee2e888f5a7e36c4b98b4.zip
Fix Gentoo net config generation
This gets Gentoo work on simple configs with static IPs or DHCP on physical interfaces. This gets Gentoo bootable again.
Diffstat (limited to 'cloudinit')
-rw-r--r--cloudinit/distros/gentoo.py95
-rw-r--r--cloudinit/util.py5
2 files changed, 90 insertions, 10 deletions
diff --git a/cloudinit/distros/gentoo.py b/cloudinit/distros/gentoo.py
index 6267dd6e..1865dc69 100644
--- a/cloudinit/distros/gentoo.py
+++ b/cloudinit/distros/gentoo.py
@@ -1,8 +1,10 @@
# vi: ts=4 expandtab
#
# Copyright (C) 2014 Rackspace, US Inc.
+# Copyright (C) 2016 Matthew Thode.
#
# Author: Nate House <nathan.house@rackspace.com>
+# Author: Matthew Thode <prometheanfire@gentoo.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
@@ -21,6 +23,7 @@ from cloudinit import helpers
from cloudinit import log as logging
from cloudinit import util
+from cloudinit.distros import net_util
from cloudinit.distros.parsers.hostname import HostnameConf
from cloudinit.settings import PER_INSTANCE
@@ -29,9 +32,11 @@ LOG = logging.getLogger(__name__)
class Distro(distros.Distro):
- locale_conf_fn = "/etc/locale.gen"
- network_conf_fn = "/etc/conf.d/net"
- init_cmd = [''] # init scripts
+ locale_conf_fn = '/etc/locale.gen'
+ network_conf_fn = '/etc/conf.d/net'
+ resolve_conf_fn = '/etc/resolv.conf'
+ hostname_conf_fn = '/etc/conf.d/hostname'
+ init_cmd = ['service'] # init scripts
def __init__(self, name, cfg, paths):
distros.Distro.__init__(self, name, cfg, paths)
@@ -50,7 +55,7 @@ class Distro(distros.Distro):
# "" provides trailing newline during join
lines = [
util.make_header(),
- 'LANG="%s"' % (locale),
+ 'LANG="%s"' % locale,
"",
]
util.write_file(out_fn, "\n".join(lines))
@@ -60,8 +65,66 @@ class Distro(distros.Distro):
self.package_command('', pkgs=pkglist)
def _write_network(self, settings):
- util.write_file(self.network_conf_fn, settings)
- return ['all']
+ entries = net_util.translate_network(settings)
+ LOG.debug("Translated ubuntu style network settings %s into %s",
+ settings, entries)
+ dev_names = entries.keys()
+ nameservers = []
+
+ for (dev, info) in entries.items():
+ if 'dns-nameservers' in info:
+ nameservers.extend(info['dns-nameservers'])
+ if dev == 'lo':
+ continue
+ net_fn = self.network_conf_fn + '.' + dev
+ dns_nameservers = info.get('dns-nameservers')
+ if isinstance(dns_nameservers, (list, tuple)):
+ dns_nameservers = str(tuple(dns_nameservers)).replace(',', '')
+ # eth0, {'auto': True, 'ipv6': {}, 'bootproto': 'dhcp'}
+ # lo, {'dns-nameservers': ['10.0.1.3'], 'ipv6': {}, 'auto': True}
+ results = ''
+ if info.get('bootproto') == 'dhcp':
+ results += 'config_{name}="dhcp"'.format(name=dev)
+ else:
+ results += (
+ 'config_{name}="{ip_address} netmask {netmask}"\n'
+ 'mac_{name}="{hwaddr}"\n'
+ ).format(name=dev, ip_address=info.get('address'),
+ netmask=info.get('netmask'),
+ hwaddr=info.get('hwaddress'))
+ results += 'routes_{name}="default via {gateway}"\n'.format(
+ name=dev,
+ gateway=info.get('gateway')
+ )
+ if info.get('dns-nameservers'):
+ results += 'dns_servers_{name}="{dnsservers}"\n'.format(
+ name=dev,
+ dnsservers=dns_nameservers)
+ util.write_file(net_fn, results)
+ self._create_network_symlink(dev)
+ if info.get('auto'):
+ cmd = ['rc-update', 'add', 'net.{name}'.format(name=dev),
+ 'default']
+ try:
+ (_out, err) = util.subp(cmd)
+ if len(err):
+ LOG.warn("Running %s resulted in stderr output: %s",
+ cmd, err)
+ except util.ProcessExecutionError:
+ util.logexc(LOG, "Running interface command %s failed",
+ cmd)
+
+ if nameservers:
+ util.write_file(self.resolve_conf_fn,
+ convert_resolv_conf(nameservers))
+
+ return dev_names
+
+ @staticmethod
+ def _create_network_symlink(interface_name):
+ file_path = '/etc/init.d/net.{name}'.format(name=interface_name)
+ if not util.is_link(file_path):
+ util.sym_link('/etc/init.d/net.lo', file_path)
def _bring_up_interface(self, device_name):
cmd = ['/etc/init.d/net.%s' % device_name, 'restart']
@@ -108,13 +171,16 @@ class Distro(distros.Distro):
if not conf:
conf = HostnameConf('')
conf.set_hostname(your_hostname)
- util.write_file(out_fn, conf, 0o644)
+ gentoo_hostname_config = 'hostname="%s"' % conf
+ gentoo_hostname_config = gentoo_hostname_config.replace('\n', '')
+ util.write_file(out_fn, gentoo_hostname_config, 0o644)
def _read_system_hostname(self):
sys_hostname = self._read_hostname(self.hostname_conf_fn)
- return (self.hostname_conf_fn, sys_hostname)
+ return self.hostname_conf_fn, sys_hostname
- def _read_hostname_conf(self, filename):
+ @staticmethod
+ def _read_hostname_conf(filename):
conf = HostnameConf(util.load_file(filename))
conf.parse()
return conf
@@ -137,7 +203,7 @@ class Distro(distros.Distro):
if pkgs is None:
pkgs = []
- cmd = ['emerge']
+ cmd = list('emerge')
# Redirect output
cmd.append("--quiet")
@@ -158,3 +224,12 @@ class Distro(distros.Distro):
def update_package_sources(self):
self._runner.run("update-sources", self.package_command,
["-u", "world"], freq=PER_INSTANCE)
+
+
+def convert_resolv_conf(settings):
+ """Returns a settings string formatted for resolv.conf."""
+ result = ''
+ if isinstance(settings, list):
+ for ns in settings:
+ result += 'nameserver %s\n' % ns
+ return result
diff --git a/cloudinit/util.py b/cloudinit/util.py
index db80ca96..9c89de61 100644
--- a/cloudinit/util.py
+++ b/cloudinit/util.py
@@ -1639,6 +1639,11 @@ def get_builtin_cfg():
return obj_copy.deepcopy(CFG_BUILTIN)
+def is_link(path):
+ LOG.debug("Testing if a link exists for %s", path)
+ return os.path.islink(path)
+
+
def sym_link(source, link, force=False):
LOG.debug("Creating symbolic link from %r => %r", link, source)
if force and os.path.exists(link):