guests/linux: Update network interface sorting implementation
Always pull ordered ethernet devices to the head of the list. Ensure aliases are not included.
This commit is contained in:
parent
c6423e8d9d
commit
e2b18fc65d
|
@ -20,16 +20,10 @@ module VagrantPlugins
|
||||||
s << data if type == :stdout
|
s << data if type == :stdout
|
||||||
end
|
end
|
||||||
ifaces = s.split("\n")
|
ifaces = s.split("\n")
|
||||||
eth_prefix = nil
|
|
||||||
@@logger.debug("Unsorted list: #{ifaces.inspect}")
|
@@logger.debug("Unsorted list: #{ifaces.inspect}")
|
||||||
# Break out integers from strings and sort the arrays to provide
|
# Break out integers from strings and sort the arrays to provide
|
||||||
# a natural sort for the interface names
|
# a natural sort for the interface names
|
||||||
ifaces = ifaces.map do |iface|
|
ifaces = ifaces.map do |iface|
|
||||||
if eth_prefix.nil?
|
|
||||||
eth_prefix = POSSIBLE_ETHERNET_PREFIXES.detect do |prefix|
|
|
||||||
iface.start_with?(prefix)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
iface.scan(/(.+?)(\d+)/).flatten.map do |iface_part|
|
iface.scan(/(.+?)(\d+)/).flatten.map do |iface_part|
|
||||||
if iface_part.to_i.to_s == iface_part
|
if iface_part.to_i.to_s == iface_part
|
||||||
iface_part.to_i
|
iface_part.to_i
|
||||||
|
@ -40,12 +34,14 @@ module VagrantPlugins
|
||||||
end.sort.map(&:join)
|
end.sort.map(&:join)
|
||||||
@@logger.debug("Sorted list: #{ifaces.inspect}")
|
@@logger.debug("Sorted list: #{ifaces.inspect}")
|
||||||
# Extract ethernet devices and place at start of list
|
# Extract ethernet devices and place at start of list
|
||||||
if eth_prefix
|
resorted_ifaces = []
|
||||||
eth_start = ifaces.index{|iface| iface.start_with?(eth_prefix) }
|
resorted_ifaces += ifaces.find_all do |iface|
|
||||||
eth_end = ifaces.rindex{|iface| iface.start_with?(eth_prefix) }
|
POSSIBLE_ETHERNET_PREFIXES.any?{|prefix| iface.start_with?(prefix)} &&
|
||||||
ifaces.unshift(*ifaces.slice!(eth_start, eth_end - 1))
|
!iface.include?(':')
|
||||||
@@logger.debug("Ethernet preferred sorted list: #{ifaces.inspect}")
|
|
||||||
end
|
end
|
||||||
|
resorted_ifaces += ifaces - resorted_ifaces
|
||||||
|
ifaces = resorted_ifaces
|
||||||
|
@@logger.debug("Ethernet preferred sorted list: #{ifaces.inspect}")
|
||||||
ifaces
|
ifaces
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -56,5 +56,17 @@ describe "VagrantPlugins::GuestLinux::Cap::NetworkInterfaces" do
|
||||||
result = cap.network_interfaces(machine)
|
result = cap.network_interfaces(machine)
|
||||||
expect(result).to eq(["enp0s3", "enp0s5", "enp0s8", "bridge0", "docker0"])
|
expect(result).to eq(["enp0s3", "enp0s5", "enp0s8", "bridge0", "docker0"])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "sorts ethernet devices discovered with predictable network interfaces naming first in list with less" do
|
||||||
|
expect(comm).to receive(:sudo).and_yield(:stdout, "enp0s3\nenp0s8\ndocker0")
|
||||||
|
result = cap.network_interfaces(machine)
|
||||||
|
expect(result).to eq(["enp0s3", "enp0s8", "docker0"])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not include ethernet devices aliases within prefix device listing" do
|
||||||
|
expect(comm).to receive(:sudo).and_yield(:stdout, "eth1\neth2\ndocker0\nbridge0\neth0\neth0:0")
|
||||||
|
result = cap.network_interfaces(machine)
|
||||||
|
expect(result).to eq(["eth0", "eth1", "eth2", "bridge0", "docker0", "eth0:0"])
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue