Merge pull request #12211 from soapy1/get-default-dhcp-ip

Get default dhcp ip from a matching host ip
This commit is contained in:
Sophia Castellarin 2021-03-08 13:57:46 -06:00 committed by GitHub
commit 0c653a3d4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 5 deletions

View File

@ -255,9 +255,17 @@ module VagrantPlugins
# Make sure the type is a symbol
options[:type] = options[:type].to_sym
# Default IP is in the 20-bit private network block for DHCP based networks
options[:ip] = "172.28.128.1" if options[:type] == :dhcp && !options[:ip]
if options[:type] == :dhcp && !options[:ip]
# Try to find a matching device to set the config ip to
matching_device = hostonly_find_matching_network(options)
if matching_device
options[:ip] = matching_device[:ip]
else
# Default IP is in the 20-bit private network block for DHCP based networks
options[:ip] = "172.28.128.1"
end
end
begin
ip = IPAddr.new(options[:ip])
@ -469,7 +477,7 @@ module VagrantPlugins
# This finds a matching host only network for the given configuration.
def hostonly_find_matching_network(config)
this_netaddr = network_address(config[:ip], config[:netmask])
this_netaddr = network_address(config[:ip], config[:netmask]) if config[:ip]
@env[:machine].provider.driver.read_host_only_interfaces.each do |interface|
return interface if config[:name] && config[:name] == interface[:name]
@ -515,7 +523,6 @@ module VagrantPlugins
# @param [Hash<String>] config hash as returned from hostonly_config
def create_dhcp_server_if_necessary(interface, config)
existing_dhcp_server = find_matching_dhcp_server(interface)
if existing_dhcp_server
if dhcp_server_matches_config?(existing_dhcp_server, config)
@logger.debug("DHCP server already properly configured")

View File

@ -91,10 +91,44 @@ describe VagrantPlugins::ProviderVirtualBox::Action::Network do
allow(machine).to receive(:guest) { guest }
end
it "tries to setup dhpc server using the ip for the specified network" do
allow(driver).to receive(:create_host_only_network) {{ name: 'vboxnet0' }}
allow(driver).to receive(:create_dhcp_server)
allow(guest).to receive(:capability)
allow(subject).to receive(:hostonly_find_matching_network).and_return({name: "vboxnet1", ip: "192.168.55.1"})
subject.call(env)
expect(driver).to have_received(:create_dhcp_server).with('vboxnet1', {
adapter_ip: "192.168.55.1",
auto_config: true,
ip: "192.168.55.1",
mac: nil,
name: nil,
netmask: "255.255.255.0",
nic_type: nil,
type: :dhcp,
dhcp_ip: "192.168.55.2",
dhcp_lower: "192.168.55.3",
dhcp_upper: "192.168.55.254",
adapter: 2
})
expect(guest).to have_received(:capability).with(:configure_networks, [{
type: :dhcp,
adapter_ip: "192.168.55.1",
ip: "192.168.55.1",
netmask: "255.255.255.0",
auto_config: true,
interface: nil
}])
end
it "creates a host only interface and a dhcp server using default ips, then tells the guest to configure the network after boot" do
allow(driver).to receive(:create_host_only_network) {{ name: 'vboxnet0' }}
allow(driver).to receive(:create_dhcp_server)
allow(guest).to receive(:capability)
allow(subject).to receive(:hostonly_find_matching_network).and_return(nil)
subject.call(env)