From 9f468d2626447d6f32ba4cfebf1837a6d343211d Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Thu, 29 Sep 2016 15:33:29 -0700 Subject: [PATCH 1/2] guests/linux: Place ethernet devices at start of device list --- plugins/guests/linux/cap/network_interfaces.rb | 18 ++++++++++++++++++ .../linux/cap/network_interfaces_test.rb | 13 +++++++++++++ 2 files changed, 31 insertions(+) diff --git a/plugins/guests/linux/cap/network_interfaces.rb b/plugins/guests/linux/cap/network_interfaces.rb index 8a4c64dd3..ea7dc32da 100644 --- a/plugins/guests/linux/cap/network_interfaces.rb +++ b/plugins/guests/linux/cap/network_interfaces.rb @@ -2,6 +2,11 @@ module VagrantPlugins module GuestLinux module Cap class NetworkInterfaces + # Valid ethernet device prefix values. + # eth - classic prefix + # en - predictable interface names prefix + ETHERNET_PREFIX = ["eth", "en"] + @@logger = Log4r::Logger.new("vagrant::guest::linux::network_interfaces") # Get network interfaces as a list. The result will be something like: @@ -15,10 +20,16 @@ module VagrantPlugins s << data if type == :stdout end ifaces = s.split("\n") + eth_prefix = nil @@logger.debug("Unsorted list: #{ifaces.inspect}") # Break out integers from strings and sort the arrays to provide # a natural sort for the interface names ifaces = ifaces.map do |iface| + unless eth_prefix + eth_prefix = ETHERNET_PREFIX.detect do |prefix| + iface.start_with?(prefix) + end + end iface.scan(/(.+?)(\d+)/).flatten.map do |iface_part| if iface_part.to_i.to_s == iface_part iface_part.to_i @@ -28,6 +39,13 @@ module VagrantPlugins end end.sort.map(&:join) @@logger.debug("Sorted list: #{ifaces.inspect}") + # Extract ethernet devices and place at start of list + if eth_prefix + eth_start = ifaces.index{|iface| iface.start_with?(eth_prefix) } + eth_end = ifaces.rindex{|iface| iface.start_with?(eth_prefix) } + ifaces.unshift(*ifaces.slice!(eth_start, eth_end - 1)) + @@logger.debug("Ethernet preferred sorted list: #{ifaces.inspect}") + end ifaces end end diff --git a/test/unit/plugins/guests/linux/cap/network_interfaces_test.rb b/test/unit/plugins/guests/linux/cap/network_interfaces_test.rb index 8dd4498a7..139cebfdc 100644 --- a/test/unit/plugins/guests/linux/cap/network_interfaces_test.rb +++ b/test/unit/plugins/guests/linux/cap/network_interfaces_test.rb @@ -44,5 +44,18 @@ describe "VagrantPlugins::GuestLinux::Cap::NetworkInterfaces" do result = cap.network_interfaces(machine) expect(result).to eq(["enp0s3", "enp0s5", "enp0s8", "enp0s10", "enp1s3"]) end + + it "sorts ethernet devices discovered with classic naming first in list" do + expect(comm).to receive(:sudo).and_yield(:stdout, "eth1\neth2\ndocker0\nbridge0\neth0") + result = cap.network_interfaces(machine) + expect(result).to eq(["eth0", "eth1", "eth2", "bridge0", "docker0"]) + end + + it "sorts ethernet devices discovered with predictable network interfaces naming first in list" do + expect(comm).to receive(:sudo).and_yield(:stdout, "enp0s8\ndocker0\nenp0s3\nbridge0\nenp0s5") + result = cap.network_interfaces(machine) + expect(result).to eq(["enp0s3", "enp0s5", "enp0s8", "bridge0", "docker0"]) + end + end end From 0300df09fb3aa4f05cca754de10a828502bf0321 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 30 Sep 2016 12:11:54 -0700 Subject: [PATCH 2/2] guests/linux: Update constant name, freeze constant values --- plugins/guests/linux/cap/network_interfaces.rb | 6 +++--- .../plugins/guests/linux/cap/network_interfaces_test.rb | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/guests/linux/cap/network_interfaces.rb b/plugins/guests/linux/cap/network_interfaces.rb index ea7dc32da..6d3ce657c 100644 --- a/plugins/guests/linux/cap/network_interfaces.rb +++ b/plugins/guests/linux/cap/network_interfaces.rb @@ -5,7 +5,7 @@ module VagrantPlugins # Valid ethernet device prefix values. # eth - classic prefix # en - predictable interface names prefix - ETHERNET_PREFIX = ["eth", "en"] + POSSIBLE_ETHERNET_PREFIXES = ["eth".freeze, "en".freeze].freeze @@logger = Log4r::Logger.new("vagrant::guest::linux::network_interfaces") @@ -25,8 +25,8 @@ module VagrantPlugins # Break out integers from strings and sort the arrays to provide # a natural sort for the interface names ifaces = ifaces.map do |iface| - unless eth_prefix - eth_prefix = ETHERNET_PREFIX.detect do |prefix| + if eth_prefix.nil? + eth_prefix = POSSIBLE_ETHERNET_PREFIXES.detect do |prefix| iface.start_with?(prefix) end end diff --git a/test/unit/plugins/guests/linux/cap/network_interfaces_test.rb b/test/unit/plugins/guests/linux/cap/network_interfaces_test.rb index 139cebfdc..810606f2a 100644 --- a/test/unit/plugins/guests/linux/cap/network_interfaces_test.rb +++ b/test/unit/plugins/guests/linux/cap/network_interfaces_test.rb @@ -56,6 +56,5 @@ describe "VagrantPlugins::GuestLinux::Cap::NetworkInterfaces" do result = cap.network_interfaces(machine) expect(result).to eq(["enp0s3", "enp0s5", "enp0s8", "bridge0", "docker0"]) end - end end