Separate logic into other functions for readability

This commit is contained in:
Seth Vargo 2015-12-08 10:52:54 -05:00
parent bcf61d001b
commit 9ba8cfcd50
1 changed files with 25 additions and 10 deletions

View File

@ -3,6 +3,7 @@ require "socket"
require "log4r"
require "vagrant/util/presence"
require "vagrant/util/scoped_hash_override"
module VagrantPlugins
@ -12,6 +13,7 @@ module VagrantPlugins
# a VM with an IPv6 host-only network will someties lose the
# route to that machine.
class NetworkFixIPv6
include Vagrant::Util::Presence
include Vagrant::Util::ScopedHashOverride
def initialize(app, env)
@ -41,17 +43,15 @@ module VagrantPlugins
# If we have no IPv6, forget it
return if !has_v6
ifaces = env[:machine].provider.driver.read_network_interfaces
ifaces.select! { |id, iface| iface[:type] == :hostonly }
iface_names = ifaces.values.map { |iface| iface[:hostonly] }
networks = env[:machine].provider.driver.read_host_only_interfaces
networks.select! { |network| iface_names.include?(network[:name]) }
networks.each do |network|
next if network[:ipv6] == ""
next if network[:status] != 'Up'
ip = IPAddr.new(network[:ipv6]) |
('1' * (128 - network[:ipv6_prefix].to_i)).to_i(2)
networks(env).each do |network|
next if !present?(network[:ipv6])
next if network[:status] != "Up"
ip = IPAddr.new(network[:ipv6])
ip |= ("1" * (128 - network[:ipv6_prefix].to_i)).to_i(2)
@logger.info("testing IPv6: #{ip}")
begin
UDPSocket.new(Socket::AF_INET6).connect(ip.to_s, 80)
rescue Errno::EHOSTUNREACH
@ -60,6 +60,21 @@ module VagrantPlugins
end
end
end
# The list of interface names for host-only adapters.
# @return [Array<String>]
def host_only_interface_names(env)
env[:machine].provider.driver.read_network_interfaces
.map { |_, i| i[:hostonly] if i[:type] == :hostonly }.compact
end
# The list of networks that are tied to a host-only adapter.
# @return [Array]
def networks(env)
iface_names = self.host_only_interface_names(env)
env[:machine].provider.driver.read_host_only_interfaces
.select { |network| iface_names.include?(network[:name]) }
end
end
end
end