Fixes #11094: Determine prefix for docker public networks

Prior to this commit, the docker action was using the method `prefix` on
an IPv4 and IPv6 address. This works fine for ruby versions 2.5 and
newer, however the ruby shipped with Vagrant is before 2.5, and
therefore the IPv4 and IPv6 classes do not have the prefix method,
resulting in an error. This commit fixes that by using a different
method of determining the prefix.
This commit is contained in:
Brian Cain 2019-10-09 09:56:59 -07:00
parent d22cfcb86c
commit 62b7e35169
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 7 additions and 6 deletions

View File

@ -191,7 +191,8 @@ module VagrantPlugins
base_opts[:opt] = "parent=#{bridge_interface.name}" base_opts[:opt] = "parent=#{bridge_interface.name}"
subnet = IPAddr.new(bridge_interface.addr.ip_address << subnet = IPAddr.new(bridge_interface.addr.ip_address <<
"/" << bridge_interface.netmask.ip_unpack.first) "/" << bridge_interface.netmask.ip_unpack.first)
base_opts[:subnet] = "#{subnet}/#{subnet.prefix}" prefix = subnet.ipv4? ? 24 : 64
base_opts[:subnet] = "#{subnet}/#{prefix}"
subnet_addr = IPAddr.new(base_opts[:subnet]) subnet_addr = IPAddr.new(base_opts[:subnet])
base_opts[:driver] = "macvlan" base_opts[:driver] = "macvlan"
base_opts[:gateway] = subnet_addr.succ.to_s base_opts[:gateway] = subnet_addr.succ.to_s
@ -282,7 +283,6 @@ module VagrantPlugins
begin begin
range = IPAddr.new(range) range = IPAddr.new(range)
if !subnet.include?(range) if !subnet.include?(range)
puts "we in here"
env[:ui].warn(I18n.t( env[:ui].warn(I18n.t(
"docker_provider.network_bridge_iprange_outofbounds", "docker_provider.network_bridge_iprange_outofbounds",
subnet: network_options[:subnet], subnet: network_options[:subnet],
@ -297,7 +297,8 @@ module VagrantPlugins
range = nil range = nil
end end
end end
"#{range}/#{range.prefix}" prefix = range.ipv4? ? 24 : 64
"#{range}/#{prefix}"
end end
# Execute the action # Execute the action

View File

@ -305,7 +305,7 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do
describe "#process_public_network" do describe "#process_public_network" do
let(:options) { {:ip=>"172.30.130.2", :subnet=>"172.30.0.0/16", :driver=>"bridge", :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"} } let(:options) { {:ip=>"172.30.130.2", :subnet=>"172.30.0.0/16", :driver=>"bridge", :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"} }
let(:ipaddr) { double("ipaddr", prefix: 22, succ: "10.1.10.2", ipv6?: false) } let(:ipaddr) { double("ipaddr", prefix: 22, succ: "10.1.10.2", ipv4?: true, ipv6?: false) }
it "raises an error if there are no network interfaces" do it "raises an error if there are no network interfaces" do
expect(subject).to receive(:list_interfaces).and_return([]) expect(subject).to receive(:list_interfaces).and_return([])
@ -331,7 +331,7 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do
describe "#request_public_gateway" do describe "#request_public_gateway" do
let(:options) { {:ip=>"172.30.130.2", :subnet=>"172.30.0.0/16", :driver=>"bridge", :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"} } let(:options) { {:ip=>"172.30.130.2", :subnet=>"172.30.0.0/16", :driver=>"bridge", :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"} }
let(:ipaddr) { double("ipaddr", to_s: "172.30.130.2", prefix: 22, succ: "172.30.130.3", let(:ipaddr) { double("ipaddr", to_s: "172.30.130.2", prefix: 22, succ: "172.30.130.3",
ipv6?: false) } ipv4?: true, ipv6?: false) }
it "requests a gateway" do it "requests a gateway" do
allow(IPAddr).to receive(:new).and_return(ipaddr) allow(IPAddr).to receive(:new).and_return(ipaddr)
@ -347,7 +347,7 @@ describe VagrantPlugins::DockerProvider::Action::PrepareNetworks do
describe "#request_public_iprange" do describe "#request_public_iprange" do
let(:options) { {:ip=>"172.30.130.2", :subnet=>"172.30.0.0/16", :driver=>"bridge", :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"} } let(:options) { {:ip=>"172.30.130.2", :subnet=>"172.30.0.0/16", :driver=>"bridge", :id=>"30e017d5-488f-5a2f-a3ke-k8dce8246b60"} }
let(:ipaddr) { double("ipaddr", to_s: "172.30.100.2", prefix: 22, succ: "172.30.100.3", let(:ipaddr) { double("ipaddr", to_s: "172.30.100.2", prefix: 22, succ: "172.30.100.3",
ipv6?: false) } ipv4?: true, ipv6?: false) }
let(:subnet) { double("ipaddr", to_s: "172.30.130.2", prefix: 22, succ: "172.30.130.3", let(:subnet) { double("ipaddr", to_s: "172.30.130.2", prefix: 22, succ: "172.30.130.3",
ipv6?: false) } ipv6?: false) }