From bb58463474e334b8c8d1769101bd3afc48ebfef4 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Mon, 21 Mar 2016 20:42:26 -0500 Subject: Added net.find_fallback_network_device() to find an appropriate device to dhcp on in the event that no network configuration was provided to cloud-init - Devices in /sys/class/net aside from loopback devices are scanned - Each device is tested to determine if it has a carrier using /sys/class/net/DEV/carrier, devices which do are preferred as they are most likely connected to the outside world - Devices which do not have a carrier but which might still be connected due to being in a dormant or down state are used as fallbacks in case no devices are found which have a carrier - A network state dictionary is generated to be passed to render_network_state to write ENI - A systemd link file is generated that will rename the chosen device to eth0 --- cloudinit/net/__init__.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) (limited to 'cloudinit') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 3cf99604..800ffe61 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -20,6 +20,8 @@ import errno import glob import os import re +import string +import textwrap from cloudinit import log as logging from cloudinit import util @@ -280,6 +282,87 @@ def parse_net_config(path): return ns +def find_fallback_network_device(): + """Determine which attached net dev is most likely to have a connection and + generate network state to run dhcp on that interface""" + ns = {'interfaces': {}, 'dns': {'search': [], 'nameservers': []}, + 'routes': []} + default_link_file = textwrap.dedent(""" + #cloud-init + [Match] + MACAddress={mac} + + [Link] + Name={name} + """) + + # get list of interfaces that could have connections + invalid_interfaces = set(['lo']) + potential_interfaces = set(os.listdir(SYS_CLASS_NET)) + potential_interfaces = potential_interfaces.difference(invalid_interfaces) + + # sort into interfaces with carrier, interfaces which could have carrier, + # and ignore interfaces that are definitely disconnected + connected = [] + possibly_connected = [] + for interface in potential_interfaces: + sysfs_carrier = os.path.join(SYS_CLASS_NET, interface, 'carrier') + carrier = int(util.load_file(sysfs_carrier).strip()) + if carrier: + connected.append(interface) + continue + # check if nic is dormant or down, as this may make a nick appear to + # not have a carrier even though it could acquire one when brought + # online by dhclient + sysfs_dormant = os.path.join(SYS_CLASS_NET, interface, 'dormant') + dormant = int(util.load_file(sysfs_dormant).strip()) + if dormant: + possibly_connected.append(interface) + continue + sysfs_operstate = os.path.join(SYS_CLASS_NET, interface, 'operstate') + operstate = util.load_file(sysfs_operstate).strip() + if operstate in ['dormant', 'down', 'lowerlayerdown', 'unknown']: + possibly_connected.append(interface) + continue + + # don't bother with interfaces that might not be connected if there are + # some that definitely are + if connected: + potential_interfaces = connected + else: + potential_interfaces = possibly_connected + + # if there are no interfaces, give up + if not potential_interfaces: + return + + # if eth0 exists use it above anything else, otherwise get the interface + # that looks 'first' + if 'eth0' in potential_interfaces: + name = 'eth0' + else: + name = potential_interfaces.sort( + key=lambda x: int(x.strip(string.ascii_letters)))[0] + + sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') + mac = util.load_file(sysfs_mac).strip() + + # generate net config for interface, rename interface to eth0 for backwards + # compatibility, and attempt both dhcp4 and dhcp6 + ns['interfaces']['eth0'] = { + 'mac_address': mac, 'name': 'eth0', 'type': 'physical', + 'mode': 'manual', 'inet': 'inet', + 'subnets': [{'type': 'dhcp4'}, {'type': 'dhcp6'}] + } + + # insert params into link file + link_file = default_link_file.format(name=name, mac=mac) + + syslink_name = "/etc/systemd/network/50-cloud-init-{}.link".format(name) + + return (ns, link_file, syslink_name) + + def render_persistent_net(network_state): ''' Given state, emit udev rules to map mac to ifname -- cgit v1.2.3 From 9a146e83189ef3128a04c9e0c1d21c6181f554f1 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Mon, 21 Mar 2016 21:10:20 -0500 Subject: Added _write_network_fallback function to distros.debian and abstract to distros base, and apply_fallback_network to distros to call _write_network_fallback. Note that since _write_network_fallback is only implemented for debian and ubuntu a check is needed to ensure that it does not break behaviour for other distros. Added function to disable .cfg files to util, since it may be useful elsewhere --- cloudinit/distros/__init__.py | 12 ++++++++++++ cloudinit/distros/debian.py | 10 ++++++++++ cloudinit/util.py | 9 +++++++++ 3 files changed, 31 insertions(+) (limited to 'cloudinit') diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 74b484a7..e32ddd57 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -78,6 +78,10 @@ class Distro(object): def _write_network_config(self, settings): raise NotImplementedError() + @abc.abstractmethod + def _write_network_fallback(self): + raise NotImplementedError() + def _find_tz_file(self, tz): tz_file = os.path.join(self.tz_zone_dir, str(tz)) if not os.path.isfile(tz_file): @@ -143,6 +147,14 @@ class Distro(object): return self._bring_up_interfaces(dev_names) return False + def apply_fallback_network(self, bring_up=True): + # Write it out + dev_names = self._write_network_fallback() + # Now try to bring them up + if bring_up: + return self._bring_up_interfaces(dev_names) + return False + @abc.abstractmethod def apply_locale(self, locale, out_fn=None): raise NotImplementedError() diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py index 909d6deb..18d5d124 100644 --- a/cloudinit/distros/debian.py +++ b/cloudinit/distros/debian.py @@ -82,6 +82,16 @@ class Distro(distros.Distro): net.render_network_state(network_state=ns, target="/") return [] + def _write_network_fallback(self): + # old fallback configuration is obsolete, disable it + util.disable_cfg_file('/etc/network/interfaces.d/eth0.cfg') + (ns, link_file, syslink_name) = net.find_fallback_network_device() + if link_file is not None: + util.write_file(syslink_name, link_file) + if ns is not None: + net.render_network_stat(network_state=ns, target="/") + return [] + def _bring_up_interfaces(self, device_names): use_all = False for d in device_names: diff --git a/cloudinit/util.py b/cloudinit/util.py index 20916e53..fa3a6163 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -849,6 +849,15 @@ def read_seeded(base="", ext="", timeout=5, retries=10, file_retries=0): return (md, ud) +def disable_conf_file(conf): + # disable .cfg file by renaming it if it exists + if not os.path.exists(conf): + return None + target_path = os.path.join(conf, '.disabled') + rename(conf, target_path) + return target_path + + def read_conf_d(confd): # Get reverse sorted list (later trumps newer) confs = sorted(os.listdir(confd), reverse=True) -- cgit v1.2.3 From 4c3468985d93929df4e9486b2e68938806fbfa1b Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Mon, 21 Mar 2016 23:41:47 -0500 Subject: Fix typo in disable_conf_file and mistake in call --- cloudinit/distros/debian.py | 2 +- cloudinit/util.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py index 18d5d124..38d22d85 100644 --- a/cloudinit/distros/debian.py +++ b/cloudinit/distros/debian.py @@ -84,7 +84,7 @@ class Distro(distros.Distro): def _write_network_fallback(self): # old fallback configuration is obsolete, disable it - util.disable_cfg_file('/etc/network/interfaces.d/eth0.cfg') + util.disable_conf_file('/etc/network/interfaces.d/eth0.cfg') (ns, link_file, syslink_name) = net.find_fallback_network_device() if link_file is not None: util.write_file(syslink_name, link_file) diff --git a/cloudinit/util.py b/cloudinit/util.py index fa3a6163..58ab3c75 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -853,7 +853,7 @@ def disable_conf_file(conf): # disable .cfg file by renaming it if it exists if not os.path.exists(conf): return None - target_path = os.path.join(conf, '.disabled') + target_path = conf + '.disabled' rename(conf, target_path) return target_path -- cgit v1.2.3 From 2aacb06be37e7e8aa84d11ae8c566a26f9df27e4 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 00:33:35 -0500 Subject: Wrap read calls to /sys/class/net/DEV/{carrier, dormant, operstate} in try/except blocks because there are sometimes read errors on the files and this should not cause a stacktrace --- cloudinit/net/__init__.py | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 800ffe61..e5b45926 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -306,24 +306,34 @@ def find_fallback_network_device(): connected = [] possibly_connected = [] for interface in potential_interfaces: - sysfs_carrier = os.path.join(SYS_CLASS_NET, interface, 'carrier') - carrier = int(util.load_file(sysfs_carrier).strip()) - if carrier: - connected.append(interface) - continue + try: + sysfs_carrier = os.path.join(SYS_CLASS_NET, interface, 'carrier') + carrier = int(util.load_file(sysfs_carrier).strip()) + if carrier: + connected.append(interface) + continue + except OSError: + pass # check if nic is dormant or down, as this may make a nick appear to # not have a carrier even though it could acquire one when brought # online by dhclient - sysfs_dormant = os.path.join(SYS_CLASS_NET, interface, 'dormant') - dormant = int(util.load_file(sysfs_dormant).strip()) - if dormant: - possibly_connected.append(interface) - continue - sysfs_operstate = os.path.join(SYS_CLASS_NET, interface, 'operstate') - operstate = util.load_file(sysfs_operstate).strip() - if operstate in ['dormant', 'down', 'lowerlayerdown', 'unknown']: - possibly_connected.append(interface) - continue + try: + sysfs_dormant = os.path.join(SYS_CLASS_NET, interface, 'dormant') + dormant = int(util.load_file(sysfs_dormant).strip()) + if dormant: + possibly_connected.append(interface) + continue + except OSError: + pass + try: + sysfs_operstate = os.path.join(SYS_CLASS_NET, interface, + 'operstate') + operstate = util.load_file(sysfs_operstate).strip() + if operstate in ['dormant', 'down', 'lowerlayerdown', 'unknown']: + possibly_connected.append(interface) + continue + except OSError: + pass # don't bother with interfaces that might not be connected if there are # some that definitely are -- cgit v1.2.3 From 217d92372ca5a4e994f1e9bc9580363dbba59032 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 00:43:31 -0500 Subject: Fix typo --- cloudinit/net/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index e5b45926..4641e54d 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -351,8 +351,9 @@ def find_fallback_network_device(): if 'eth0' in potential_interfaces: name = 'eth0' else: - name = potential_interfaces.sort( - key=lambda x: int(x.strip(string.ascii_letters)))[0] + potential_interfaces.sort( + key=lambda x: int(x.strip(string.ascii_letters))) + name = potential_interfaces[0] sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') mac = util.load_file(sysfs_mac).strip() -- cgit v1.2.3 From aab9331089abcf3f5074f3cf7659502ca0752114 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 00:48:17 -0500 Subject: Typo fix --- cloudinit/distros/debian.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cloudinit') diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py index 38d22d85..8e57f70e 100644 --- a/cloudinit/distros/debian.py +++ b/cloudinit/distros/debian.py @@ -89,7 +89,7 @@ class Distro(distros.Distro): if link_file is not None: util.write_file(syslink_name, link_file) if ns is not None: - net.render_network_stat(network_state=ns, target="/") + net.render_network_state(network_state=ns, target="/") return [] def _bring_up_interfaces(self, device_names): -- cgit v1.2.3 From 7e399773a95e21e4c825dab61847d6abcd2aa511 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 01:17:12 -0500 Subject: For find_fallback_network_device, kwarg rename_to_default specifies whether or not to attempt renaming the network interface to the default interface. Default interface is controleld by net.DEFAULT_PRIMARY_INTERFACE and is currently set to eth0 for legacy reasons. By default cloud-init will not attempt to rename the device as this does not work in some situtations depending on the backing driver of the device. --- cloudinit/net/__init__.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 4641e54d..e2e50441 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -48,6 +48,8 @@ NET_CONFIG_BRIDGE_OPTIONS = [ "bridge_hello", "bridge_maxage", "bridge_maxwait", "bridge_stp", ] +DEFAULT_PRIMARY_INTERFACE = 'eth0' + def sys_dev_path(devname, path=""): return SYS_CLASS_NET + devname + "/" + path @@ -282,9 +284,10 @@ def parse_net_config(path): return ns -def find_fallback_network_device(): +def find_fallback_network_device(rename_to_default=False): """Determine which attached net dev is most likely to have a connection and generate network state to run dhcp on that interface""" + # by default use eth0 as primary interface ns = {'interfaces': {}, 'dns': {'search': [], 'nameservers': []}, 'routes': []} default_link_file = textwrap.dedent(""" @@ -348,8 +351,8 @@ def find_fallback_network_device(): # if eth0 exists use it above anything else, otherwise get the interface # that looks 'first' - if 'eth0' in potential_interfaces: - name = 'eth0' + if DEFAULT_PRIMARY_INTERFACE in potential_interfaces: + name = DEFAULT_PRIMARY_INTERFACE else: potential_interfaces.sort( key=lambda x: int(x.strip(string.ascii_letters))) @@ -358,18 +361,23 @@ def find_fallback_network_device(): sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') mac = util.load_file(sysfs_mac).strip() + target_name = name + if rename_to_default: + target_name = DEFAULT_PRIMARY_INTERFACE + # generate net config for interface, rename interface to eth0 for backwards # compatibility, and attempt both dhcp4 and dhcp6 - ns['interfaces']['eth0'] = { - 'mac_address': mac, 'name': 'eth0', 'type': 'physical', + ns['interfaces'][target_name] = { + 'mac_address': mac, 'name': target_name, 'type': 'physical', 'mode': 'manual', 'inet': 'inet', 'subnets': [{'type': 'dhcp4'}, {'type': 'dhcp6'}] } # insert params into link file - link_file = default_link_file.format(name=name, mac=mac) + link_file = default_link_file.format(name=target_name, mac=mac) - syslink_name = "/etc/systemd/network/50-cloud-init-{}.link".format(name) + syslink_name = "/etc/systemd/network/50-cloud-init-{}.link".format( + target_name) return (ns, link_file, syslink_name) -- cgit v1.2.3 From 88bfbe22a2f1128f358501bc10f5a2cbd1f7facf Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 01:24:01 -0500 Subject: Basic code added to kick off network configuration and cause fallback network configuration to be run if there is no network configuration provided by the datasource. NOTE: the code added here will not behave correctly if a net datasource has network configuration. this code is temporary and should be reverted once support for network configuration for net datasources after retrieving config is in place. based on: http://paste.ubuntu.com/15443576/ With this in place cloud-init properly chooses a fallback interface, configures it and brings it online --- bin/cloud-init | 2 ++ cloudinit/sources/__init__.py | 4 ++++ cloudinit/stages.py | 11 +++++++++++ 3 files changed, 17 insertions(+) (limited to 'cloudinit') diff --git a/bin/cloud-init b/bin/cloud-init index f101a713..2dfa8ec7 100755 --- a/bin/cloud-init +++ b/bin/cloud-init @@ -251,6 +251,7 @@ def main_init(name, args): # Stage 5 try: init.fetch() + init.apply_networking() except sources.DataSourceNotFoundException: # In the case of 'cloud-init init' without '--local' it is a bit # more likely that the user would consider it failure if nothing was @@ -261,6 +262,7 @@ def main_init(name, args): else: util.logexc(LOG, ("No instance datasource found!" " Likely bad things to come!")) + init.apply_networking() if not args.force: if args.local: return (None, []) diff --git a/cloudinit/sources/__init__.py b/cloudinit/sources/__init__.py index d3cfa560..08058762 100644 --- a/cloudinit/sources/__init__.py +++ b/cloudinit/sources/__init__.py @@ -217,6 +217,10 @@ class DataSource(object): def get_package_mirror_info(self): return self.distro.get_package_mirror_info(data_source=self) + @property + def network_config(self): + return self.metadata.network_config + def normalize_pubkey_data(pubkey_data): keys = [] diff --git a/cloudinit/stages.py b/cloudinit/stages.py index dbcf3d55..d508897b 100644 --- a/cloudinit/stages.py +++ b/cloudinit/stages.py @@ -587,6 +587,17 @@ class Init(object): # Run the handlers self._do_handlers(user_data_msg, c_handlers_list, frequency) + def apply_networking(self): + """Attempt to apply network configuration, either using network + configuration from datasource or fallback configuration if that is + not available""" + if self.datasource and self.datasource.network_config: + ds_net_conf = self.datasource.network_config + res = self.distro.apply_network_config(ds_net_conf, bring_up=True) + else: + res = self.distro.apply_fallback_network(bring_up=True) + return res + class Modules(object): def __init__(self, init, cfg_files=None, reporter=None): -- cgit v1.2.3 From 3a7e3d198172f26b7b97707520d06fe5303fadbc Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 01:33:20 -0500 Subject: Got rid of blank lines in net.find_fallback_network_device --- cloudinit/net/__init__.py | 7 ------- 1 file changed, 7 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index e2e50441..2596b4f5 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -303,7 +303,6 @@ def find_fallback_network_device(rename_to_default=False): invalid_interfaces = set(['lo']) potential_interfaces = set(os.listdir(SYS_CLASS_NET)) potential_interfaces = potential_interfaces.difference(invalid_interfaces) - # sort into interfaces with carrier, interfaces which could have carrier, # and ignore interfaces that are definitely disconnected connected = [] @@ -344,11 +343,9 @@ def find_fallback_network_device(rename_to_default=False): potential_interfaces = connected else: potential_interfaces = possibly_connected - # if there are no interfaces, give up if not potential_interfaces: return - # if eth0 exists use it above anything else, otherwise get the interface # that looks 'first' if DEFAULT_PRIMARY_INTERFACE in potential_interfaces: @@ -360,11 +357,9 @@ def find_fallback_network_device(rename_to_default=False): sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') mac = util.load_file(sysfs_mac).strip() - target_name = name if rename_to_default: target_name = DEFAULT_PRIMARY_INTERFACE - # generate net config for interface, rename interface to eth0 for backwards # compatibility, and attempt both dhcp4 and dhcp6 ns['interfaces'][target_name] = { @@ -372,10 +367,8 @@ def find_fallback_network_device(rename_to_default=False): 'mode': 'manual', 'inet': 'inet', 'subnets': [{'type': 'dhcp4'}, {'type': 'dhcp6'}] } - # insert params into link file link_file = default_link_file.format(name=target_name, mac=mac) - syslink_name = "/etc/systemd/network/50-cloud-init-{}.link".format( target_name) -- cgit v1.2.3 From e66bcc8b2ea7648c15476cdd43d3753ee6c27ff1 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 01:48:39 -0500 Subject: - Rename find_fallback_network_device to generate_fallback_config - Removed systemd .link file generation, as it is not needed right now - Changed return of generate_fallback_config to be just ns dict - In distros.debian don't attempt to write .link file --- cloudinit/distros/debian.py | 4 +--- cloudinit/net/__init__.py | 23 ++++------------------- 2 files changed, 5 insertions(+), 22 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py index 8e57f70e..0fa47274 100644 --- a/cloudinit/distros/debian.py +++ b/cloudinit/distros/debian.py @@ -85,9 +85,7 @@ class Distro(distros.Distro): def _write_network_fallback(self): # old fallback configuration is obsolete, disable it util.disable_conf_file('/etc/network/interfaces.d/eth0.cfg') - (ns, link_file, syslink_name) = net.find_fallback_network_device() - if link_file is not None: - util.write_file(syslink_name, link_file) + ns = net.generate_fallback_config() if ns is not None: net.render_network_state(network_state=ns, target="/") return [] diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 2596b4f5..48b82a2c 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -21,7 +21,6 @@ import glob import os import re import string -import textwrap from cloudinit import log as logging from cloudinit import util @@ -284,20 +283,12 @@ def parse_net_config(path): return ns -def find_fallback_network_device(rename_to_default=False): +def generate_fallback_config(): """Determine which attached net dev is most likely to have a connection and generate network state to run dhcp on that interface""" # by default use eth0 as primary interface ns = {'interfaces': {}, 'dns': {'search': [], 'nameservers': []}, 'routes': []} - default_link_file = textwrap.dedent(""" - #cloud-init - [Match] - MACAddress={mac} - - [Link] - Name={name} - """) # get list of interfaces that could have connections invalid_interfaces = set(['lo']) @@ -358,21 +349,15 @@ def find_fallback_network_device(rename_to_default=False): sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') mac = util.load_file(sysfs_mac).strip() target_name = name - if rename_to_default: - target_name = DEFAULT_PRIMARY_INTERFACE - # generate net config for interface, rename interface to eth0 for backwards - # compatibility, and attempt both dhcp4 and dhcp6 + + # generate net config for interface ns['interfaces'][target_name] = { 'mac_address': mac, 'name': target_name, 'type': 'physical', 'mode': 'manual', 'inet': 'inet', 'subnets': [{'type': 'dhcp4'}, {'type': 'dhcp6'}] } - # insert params into link file - link_file = default_link_file.format(name=target_name, mac=mac) - syslink_name = "/etc/systemd/network/50-cloud-init-{}.link".format( - target_name) - return (ns, link_file, syslink_name) + return ns def render_persistent_net(network_state): -- cgit v1.2.3 From 7eccb0f0f3693662b3f288e7a74cb5bd6d7814ea Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 02:02:22 -0500 Subject: In generate_fallback_config return full netconfig dict with 'config' and 'version' keys --- cloudinit/distros/debian.py | 5 +++-- cloudinit/net/__init__.py | 11 +++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py index 0fa47274..de8c4c6c 100644 --- a/cloudinit/distros/debian.py +++ b/cloudinit/distros/debian.py @@ -85,8 +85,9 @@ class Distro(distros.Distro): def _write_network_fallback(self): # old fallback configuration is obsolete, disable it util.disable_conf_file('/etc/network/interfaces.d/eth0.cfg') - ns = net.generate_fallback_config() - if ns is not None: + nconf = net.generate_fallback_config() + if nconf is not None: + ns = nconf['config'] net.render_network_state(network_state=ns, target="/") return [] diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 48b82a2c..389c2afb 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -287,8 +287,11 @@ def generate_fallback_config(): """Determine which attached net dev is most likely to have a connection and generate network state to run dhcp on that interface""" # by default use eth0 as primary interface - ns = {'interfaces': {}, 'dns': {'search': [], 'nameservers': []}, - 'routes': []} + nconf = {'config': {'interfaces': {}, + 'dns': {'search': [], 'nameservers': []}, 'routes': [] + }, + 'version': 1 + } # get list of interfaces that could have connections invalid_interfaces = set(['lo']) @@ -351,13 +354,13 @@ def generate_fallback_config(): target_name = name # generate net config for interface - ns['interfaces'][target_name] = { + nconf['config']['interfaces'][target_name] = { 'mac_address': mac, 'name': target_name, 'type': 'physical', 'mode': 'manual', 'inet': 'inet', 'subnets': [{'type': 'dhcp4'}, {'type': 'dhcp6'}] } - return ns + return nconf def render_persistent_net(network_state): -- cgit v1.2.3 From 3a3f960d5cfee60766e7de9e1fced537cac72106 Mon Sep 17 00:00:00 2001 From: Wesley Wiedenmeier Date: Tue, 22 Mar 2016 02:20:06 -0500 Subject: In generate_fallback_config() fix function to sort potential interfaces to work on interfaces with characters between their numbers --- cloudinit/net/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'cloudinit') diff --git a/cloudinit/net/__init__.py b/cloudinit/net/__init__.py index 389c2afb..36f07a02 100644 --- a/cloudinit/net/__init__.py +++ b/cloudinit/net/__init__.py @@ -346,7 +346,7 @@ def generate_fallback_config(): name = DEFAULT_PRIMARY_INTERFACE else: potential_interfaces.sort( - key=lambda x: int(x.strip(string.ascii_letters))) + key=lambda x: int(''.join(i for i in x if i in string.digits))) name = potential_interfaces[0] sysfs_mac = os.path.join(SYS_CLASS_NET, name, 'address') -- cgit v1.2.3 From 78c99ef3faecde46b3e460dffa1af69654e8bbff Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Tue, 22 Mar 2016 03:33:05 -0400 Subject: drop changes other than generate_fallback_config --- cloudinit/distros/__init__.py | 12 ------------ cloudinit/distros/debian.py | 9 --------- cloudinit/stages.py | 11 ----------- cloudinit/util.py | 9 --------- 4 files changed, 41 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index e32ddd57..74b484a7 100644 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -78,10 +78,6 @@ class Distro(object): def _write_network_config(self, settings): raise NotImplementedError() - @abc.abstractmethod - def _write_network_fallback(self): - raise NotImplementedError() - def _find_tz_file(self, tz): tz_file = os.path.join(self.tz_zone_dir, str(tz)) if not os.path.isfile(tz_file): @@ -147,14 +143,6 @@ class Distro(object): return self._bring_up_interfaces(dev_names) return False - def apply_fallback_network(self, bring_up=True): - # Write it out - dev_names = self._write_network_fallback() - # Now try to bring them up - if bring_up: - return self._bring_up_interfaces(dev_names) - return False - @abc.abstractmethod def apply_locale(self, locale, out_fn=None): raise NotImplementedError() diff --git a/cloudinit/distros/debian.py b/cloudinit/distros/debian.py index de8c4c6c..909d6deb 100644 --- a/cloudinit/distros/debian.py +++ b/cloudinit/distros/debian.py @@ -82,15 +82,6 @@ class Distro(distros.Distro): net.render_network_state(network_state=ns, target="/") return [] - def _write_network_fallback(self): - # old fallback configuration is obsolete, disable it - util.disable_conf_file('/etc/network/interfaces.d/eth0.cfg') - nconf = net.generate_fallback_config() - if nconf is not None: - ns = nconf['config'] - net.render_network_state(network_state=ns, target="/") - return [] - def _bring_up_interfaces(self, device_names): use_all = False for d in device_names: diff --git a/cloudinit/stages.py b/cloudinit/stages.py index 64da3b5b..c230ec0d 100644 --- a/cloudinit/stages.py +++ b/cloudinit/stages.py @@ -567,17 +567,6 @@ class Init(object): # Run the handlers self._do_handlers(user_data_msg, c_handlers_list, frequency) - def apply_networking(self): - """Attempt to apply network configuration, either using network - configuration from datasource or fallback configuration if that is - not available""" - if self.datasource and self.datasource.network_config: - ds_net_conf = self.datasource.network_config - res = self.distro.apply_network_config(ds_net_conf, bring_up=True) - else: - res = self.distro.apply_fallback_network(bring_up=True) - return res - class Modules(object): def __init__(self, init, cfg_files=None, reporter=None): diff --git a/cloudinit/util.py b/cloudinit/util.py index 58ab3c75..20916e53 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -849,15 +849,6 @@ def read_seeded(base="", ext="", timeout=5, retries=10, file_retries=0): return (md, ud) -def disable_conf_file(conf): - # disable .cfg file by renaming it if it exists - if not os.path.exists(conf): - return None - target_path = conf + '.disabled' - rename(conf, target_path) - return target_path - - def read_conf_d(confd): # Get reverse sorted list (later trumps newer) confs = sorted(os.listdir(confd), reverse=True) -- cgit v1.2.3