From 0ae8b2f5d1fda34f1efa50de8defd127a7907576 Mon Sep 17 00:00:00 2001 From: Harm Weites Date: Fri, 8 Aug 2014 19:18:42 +0000 Subject: merge: These are the changes from the freebsd-static-networking branch. --- cloudinit/distros/freebsd.py | 83 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 8 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py index d98f9578..1085185b 100644 --- a/cloudinit/distros/freebsd.py +++ b/cloudinit/distros/freebsd.py @@ -26,6 +26,9 @@ from cloudinit import log as logging from cloudinit import ssh_util from cloudinit import util +from cloudinit.distros import net_util +from cloudinit.distros.parsers.resolv_conf import ResolvConf + LOG = logging.getLogger(__name__) @@ -33,6 +36,7 @@ class Distro(distros.Distro): rc_conf_fn = "/etc/rc.conf" login_conf_fn = '/etc/login.conf' login_conf_fn_bak = '/etc/login.conf.orig' + resolv_conf_fn = '/etc/resolv.conf' def __init__(self, name, cfg, paths): distros.Distro.__init__(self, name, cfg, paths) @@ -44,30 +48,34 @@ class Distro(distros.Distro): # Updates a key in /etc/rc.conf. def updatercconf(self, key, value): - LOG.debug("updatercconf: %s => %s", key, value) + LOG.debug("Checking %s for: %s = %s", self.rc_conf_fn, key, value) conf = self.loadrcconf() config_changed = False for item in conf: if item == key and conf[item] != value: conf[item] = value - LOG.debug("[rc.conf]: Value %s for key %s needs to be changed", - value, key) + LOG.debug("Changing key in %s: %s = %s", self.rc_conf_fn, key, + value) config_changed = True if config_changed: - LOG.debug("Writing new %s file", self.rc_conf_fn) + LOG.info("Writing %s", self.rc_conf_fn) buf = StringIO() for keyval in conf.items(): - buf.write("%s=%s\n" % keyval) + buf.write('%s="%s"\n' % keyval) util.write_file(self.rc_conf_fn, buf.getvalue()) - # Load the contents of /etc/rc.conf and store all keys in a dict. + # Load the contents of /etc/rc.conf and store all keys in a dict. Make sure + # quotes are ignored: + # hostname="bla" def loadrcconf(self): conf = {} lines = util.load_file(self.rc_conf_fn).splitlines() for line in lines: tok = line.split('=') - conf[tok[0]] = tok[1].rstrip() + key = tok[0] + val = re.sub(r'^"|"$', '', tok[1].rstrip()) + conf[key] = val return conf def readrcconf(self, key): @@ -218,7 +226,66 @@ class Distro(distros.Distro): ssh_util.setup_user_keys(keys, name, options=None) def _write_network(self, settings): - return + entries = net_util.translate_network(settings) + LOG.debug("Translated network settings") + LOG.debug("\n========== UBUNTU FORMAT START ==========") + LOG.debug("%s\n========== UBUNTU FORMAT END ==========", settings) + LOG.debug("\n========== GENERIC FORMAT START ==========") + LOG.debug("%s\n========== GENERIC FORMAT END ==========", entries) + + nameservers = [] + searchdomains = [] + dev_names = entries.keys() + for (dev, info) in entries.iteritems(): + # Skip the loopback interface. + if dev == 'lo0': + continue + + LOG.info('Configuring interface %s', dev) + + if info.get('bootproto') == 'static': + LOG.debug('Configuring dev %s with %s / %s', dev, info.get('address'), info.get('netmask')) + # Configure an ipv4 address. + ifconfig = info.get('address') + ' netmask ' + info.get('netmask') + + # Configure the gateway. + self.updatercconf('defaultrouter', info.get('gateway')) + + if 'dns-nameservers' in info: + nameservers.extend(info['dns-nameservers']) + if 'dns-search' in info: + searchservers.extend(info['dns-search']) + else: + ifconfig = 'DHCP' + + self.updatercconf('ifconfig_' + dev, ifconfig) + + # Try to read the /etc/resolv.conf or just start from scratch if that + # fails. + try: + resolvconf = ResolvConf(util.load_file(self.resolv_conf_fn)) + resolvconf.parse() + except IOError: + util.logexc(LOG, "Failed to parse %s, use new empty file", self.resolv_conf_fn) + resolvconf = ResolvConf('') + resolvconf.parse() + + # Add some nameservers + for server in nameservers: + try: + resolvconf.add_nameserver(server) + except ValueError: + util.logexc(LOG, "Failed to add nameserver %s", server) + + # And add any searchdomains. + for domain in searchdomains: + try: + resolvconf.add_search_domain(domain) + except ValueError: + util.logexc(LOG, "Failed to add search domain %s", domain) + util.write_file(self.resolv_conf_fn, str(resolvconf), 0644) + + return dev_names def apply_locale(self, locale, out_fn=None): # Adjust the locals value to the new value -- cgit v1.2.3 From 019c90f07061adeda54173ea9afd7752cd11cd90 Mon Sep 17 00:00:00 2001 From: Harm Weites Date: Fri, 8 Aug 2014 19:23:56 +0000 Subject: fix: Skip lines from /etc/rc.conf not matching the pattern key=value. --- cloudinit/distros/freebsd.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'cloudinit') diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py index 1085185b..b4d841f8 100644 --- a/cloudinit/distros/freebsd.py +++ b/cloudinit/distros/freebsd.py @@ -72,6 +72,11 @@ class Distro(distros.Distro): conf = {} lines = util.load_file(self.rc_conf_fn).splitlines() for line in lines: + if not re.match(r'^(.+)=(.+)', line): + LOG.debug("Skipping line from /etc/rc.conf: %s", line) + continue + + # TODO: just use the matches please... tok = line.split('=') key = tok[0] val = re.sub(r'^"|"$', '', tok[1].rstrip()) -- cgit v1.2.3 From 33826c4e1b6cbb10a27633a5dd9627fcb88808b4 Mon Sep 17 00:00:00 2001 From: Harm Weites Date: Sat, 9 Aug 2014 11:34:46 +0000 Subject: fix: Pass -y to growfs to keep it from asking for confirmation. --- cloudinit/config/cc_resizefs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cloudinit') diff --git a/cloudinit/config/cc_resizefs.py b/cloudinit/config/cc_resizefs.py index be406034..e290efe0 100644 --- a/cloudinit/config/cc_resizefs.py +++ b/cloudinit/config/cc_resizefs.py @@ -41,7 +41,7 @@ def _resize_xfs(mount_point, devpth): # pylint: disable=W0613 def _resize_ufs(mount_point, devpth): # pylint: disable=W0613 - return ('growfs', devpth) + return ('growfs', '-y', devpth) # Do not use a dictionary as these commands should be able to be used # for multiple filesystem types if possible, e.g. one command for -- cgit v1.2.3 From a0e3506c6dddf140589228c1ac74f48d2a7cde49 Mon Sep 17 00:00:00 2001 From: Harm Weites Date: Sat, 23 Aug 2014 12:19:09 +0000 Subject: fix: Drop some overly loud debug messages. --- cloudinit/distros/freebsd.py | 6 ------ 1 file changed, 6 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py index b4d841f8..415c6ba6 100644 --- a/cloudinit/distros/freebsd.py +++ b/cloudinit/distros/freebsd.py @@ -232,12 +232,6 @@ class Distro(distros.Distro): def _write_network(self, settings): entries = net_util.translate_network(settings) - LOG.debug("Translated network settings") - LOG.debug("\n========== UBUNTU FORMAT START ==========") - LOG.debug("%s\n========== UBUNTU FORMAT END ==========", settings) - LOG.debug("\n========== GENERIC FORMAT START ==========") - LOG.debug("%s\n========== GENERIC FORMAT END ==========", entries) - nameservers = [] searchdomains = [] dev_names = entries.keys() -- cgit v1.2.3 From 1b9b66be04aafcb39f349ccb48905afc393cfc32 Mon Sep 17 00:00:00 2001 From: Harm Weites Date: Tue, 26 Aug 2014 18:10:03 +0000 Subject: change: Use a compiled regex and use the included match groups instead of matching yet again. --- cloudinit/distros/freebsd.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py index 415c6ba6..9c923480 100644 --- a/cloudinit/distros/freebsd.py +++ b/cloudinit/distros/freebsd.py @@ -69,17 +69,17 @@ class Distro(distros.Distro): # quotes are ignored: # hostname="bla" def loadrcconf(self): + RE_MATCH = re.compile(r'^(\w+)="?(\w+)"?') conf = {} lines = util.load_file(self.rc_conf_fn).splitlines() for line in lines: - if not re.match(r'^(.+)=(.+)', line): + m = RE_MATCH.match(line) + if not m: LOG.debug("Skipping line from /etc/rc.conf: %s", line) continue - # TODO: just use the matches please... - tok = line.split('=') - key = tok[0] - val = re.sub(r'^"|"$', '', tok[1].rstrip()) + key = m.group(1).rstrip() + val = m.group(2).rstrip() conf[key] = val return conf -- cgit v1.2.3 From fbfd3789ede42ad50e0b79f202ab18b65537af52 Mon Sep 17 00:00:00 2001 From: Harm Weites Date: Thu, 28 Aug 2014 17:54:00 +0000 Subject: fix: The original regex was a little harsh, the rest of the bits regarding keys and values from /etc/rc.conf is tweaked as well (harlowja). --- cloudinit/distros/freebsd.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py index 9c923480..42ef2290 100644 --- a/cloudinit/distros/freebsd.py +++ b/cloudinit/distros/freebsd.py @@ -51,12 +51,18 @@ class Distro(distros.Distro): LOG.debug("Checking %s for: %s = %s", self.rc_conf_fn, key, value) conf = self.loadrcconf() config_changed = False - for item in conf: - if item == key and conf[item] != value: - conf[item] = value - LOG.debug("Changing key in %s: %s = %s", self.rc_conf_fn, key, - value) - config_changed = True + if key not in conf: + LOG.debug("Adding key in %s: %s = %s", self.rc_conf_fn, key, + value) + conf[key] = value + config_changed = True + else: + for item in conf.keys(): + if item == key and conf[item] != value: + conf[item] = value + LOG.debug("Changing key in %s: %s = %s", self.rc_conf_fn, + key, value) + config_changed = True if config_changed: LOG.info("Writing %s", self.rc_conf_fn) @@ -69,7 +75,7 @@ class Distro(distros.Distro): # quotes are ignored: # hostname="bla" def loadrcconf(self): - RE_MATCH = re.compile(r'^(\w+)="?(\w+)"?') + RE_MATCH = re.compile(r'^(\w+)\s*=\s*(.*)\s*') conf = {} lines = util.load_file(self.rc_conf_fn).splitlines() for line in lines: @@ -77,9 +83,17 @@ class Distro(distros.Distro): if not m: LOG.debug("Skipping line from /etc/rc.conf: %s", line) continue - key = m.group(1).rstrip() val = m.group(2).rstrip() + # Kill them quotes (not completely correct, aka won't handle + # quoted values, but should be ok ...) + if val[0] in ('"', "'"): + val = val[1:] + if val[-1] in ('"', "'"): + val = val[0:-1] + if len(val) == 0: + LOG.debug("Skipping empty value from /etc/rc.conf: %s", line) + continue conf[key] = val return conf @@ -237,7 +251,7 @@ class Distro(distros.Distro): dev_names = entries.keys() for (dev, info) in entries.iteritems(): # Skip the loopback interface. - if dev == 'lo0': + if dev.startswith('lo'): continue LOG.info('Configuring interface %s', dev) -- cgit v1.2.3