action/network: Refactor the calculation of host-only adapter IP and DHCP settings

This commit is contained in:
Mikhail Zholobov 2016-08-09 21:42:13 +03:00
parent c2cedc98c2
commit a02aaa3195
No known key found for this signature in database
GPG Key ID: 467E2D8B15AE6DB3
1 changed files with 14 additions and 26 deletions

View File

@ -287,44 +287,32 @@ module VagrantPlugins
interface_name: interface[:name]
end
end
# Split the IP address into its components
ip_parts = netaddr.split(".").map { |i| i.to_i }
# Calculate the adapter IP, which we assume is the IP ".1" at
# the end usually.
adapter_ip = ip_parts.dup
adapter_ip[3] += 1
options[:adapter_ip] ||= adapter_ip.join(".")
elsif ip.ipv6?
# Default subnet prefix length
options[:netmask] ||= 64
# Set adapter IP to <prefix>::1
options[:adapter_ip] ||= (ip.mask(options[:netmask].to_i) | 1).to_s
# Append a 6 to the end of the type
options[:type] = "#{options[:type]}6".to_sym
else
raise "BUG: Unknown IP type: #{ip.inspect}"
end
# Calculate the adapter IP which is the network address with
# the final bit + 1. Usually it is "x.x.x.1" for IPv4 and
# "<prefix>::1" for IPv6
options[:adapter_ip] ||= (ip.mask(options[:netmask]) | 1).to_s
dhcp_options = {}
if options[:type] == :dhcp
# Calculate the DHCP server IP, which is the network address
# with the final octet + 2. So "172.28.0.0" turns into "172.28.0.2"
dhcp_ip = ip_parts.dup
dhcp_ip[3] += 2
dhcp_options[:dhcp_ip] = options[:dhcp_ip] || dhcp_ip.join(".")
# Calculate the lower and upper bound for the DHCP server
dhcp_lower = ip_parts.dup
dhcp_lower[3] += 3
dhcp_options[:dhcp_lower] = options[:dhcp_lower] || dhcp_lower.join(".")
dhcp_upper = ip_parts.dup
dhcp_upper[3] = 254
dhcp_options[:dhcp_upper] = options[:dhcp_upper] || dhcp_upper.join(".")
# Calculate the DHCP server IP and lower & upper bound
# Example: for "192.168.22.64/26" network range those are:
# dhcp_ip: "192.168.22.66",
# dhcp_lower: "192.168.22.67"
# dhcp_upper: "192.168.22.126"
ip_range = ip.mask(options[:netmask]).to_range
dhcp_options[:dhcp_ip] = options[:dhcp_ip] || (ip_range.first | 2).to_s
dhcp_options[:dhcp_lower] = options[:dhcp_lower] || (ip_range.first | 3).to_s
dhcp_options[:dhcp_upper] = options[:dhcp_upper] || (ip_range.last(2).first).to_s
end
return {