Check for conflicting DHCP servers

This commit is contained in:
Mitchell Hashimoto 2012-01-08 20:28:47 -08:00
parent d79f5d8d6f
commit 8aa4e58ea8
5 changed files with 77 additions and 7 deletions

View File

@ -227,13 +227,22 @@ module Vagrant
end
if config[:type] == :dhcp
# TODO: Check that there isn't another DHCP server on this network
# that is different.
# Check that if there is a DHCP server attached on our interface,
# then it is identical. Otherwise, we can't set it.
if interface[:dhcp]
valid = interface[:dhcp][:ip] == config[:dhcp_ip] &&
interface[:dhcp][:lower] == config[:dhcp_lower] &&
interface[:dhcp][:upper] == config[:dhcp_upper]
raise Errors::NetworkDHCPAlreadyAttached if !valid
@logger.debug("DHCP server already properly configured")
else
# Configure the DHCP server for the network.
@logger.debug("Creating a DHCP server...")
@env[:vm].driver.create_dhcp_server(interface[:name], config)
end
end
return {
:adapter => config[:adapter],

View File

@ -53,7 +53,8 @@ module Vagrant
return {
:name => name,
:ip => options[:adapter_ip],
:netmask => options[:netmask]
:netmask => options[:netmask],
:dhcp => nil
}
end
@ -235,6 +236,26 @@ module Vagrant
end
def read_host_only_interfaces
dhcp = {}
execute("list", "dhcpservers").split("\n\n").each do |block|
info = {}
block.split("\n").each do |line|
if line =~ /^NetworkName:\s+HostInterfaceNetworking-(.+?)$/
info[:network] = $1.to_s
elsif line =~ /^IP:\s+(.+?)$/
info[:ip] = $1.to_s
elsif line =~ /^lowerIPAddress:\s+(.+?)$/
info[:lower] = $1.to_s
elsif line =~ /^upperIPAddress:\s+(.+?)$/
info[:upper] = $1.to_s
end
end
# Set the DHCP info
dhcp[info[:network]] = info
end
execute("list", "hostonlyifs").split("\n\n").collect do |block|
info = {}
@ -248,6 +269,9 @@ module Vagrant
end
end
# Set the DHCP info if it exists
info[:dhcp] = dhcp[info[:name]] if dhcp[info[:name]]
info
end
end

View File

@ -53,7 +53,8 @@ module Vagrant
return {
:name => name,
:ip => options[:adapter_ip],
:netmask => options[:netmask]
:netmask => options[:netmask],
:dhcp => nil
}
end
@ -235,6 +236,26 @@ module Vagrant
end
def read_host_only_interfaces
dhcp = {}
execute("list", "dhcpservers").split("\n\n").each do |block|
info = {}
block.split("\n").each do |line|
if line =~ /^NetworkName:\s+HostInterfaceNetworking-(.+?)$/
info[:network] = $1.to_s
elsif line =~ /^IP:\s+(.+?)$/
info[:ip] = $1.to_s
elsif line =~ /^lowerIPAddress:\s+(.+?)$/
info[:lower] = $1.to_s
elsif line =~ /^upperIPAddress:\s+(.+?)$/
info[:upper] = $1.to_s
end
end
# Set the DHCP info
dhcp[info[:network]] = info
end
execute("list", "hostonlyifs").split("\n\n").collect do |block|
info = {}
@ -248,6 +269,9 @@ module Vagrant
end
end
# Set the DHCP info if it exists
info[:dhcp] = dhcp[info[:name]] if dhcp[info[:name]]
info
end
end

View File

@ -223,6 +223,11 @@ module Vagrant
error_key(:no_adapters, "vagrant.actions.vm.network")
end
class NetworkDHCPAlreadyAttached < VagrantError
status_code(68)
error_key(:dhcp_already_attached, "vagrant.actions.vm.network")
end
class NetworkNotFound < VagrantError
status_code(30)
error_key(:not_found, "vagrant.actions.vm.host_only_network")

View File

@ -422,6 +422,14 @@ en:
overlap.
configuring: |-
Configuring and enabling network interfaces...
dhcp_already_attached: |-
A host only network interface you're attempting to configure via DHCP
already has a conflicting host only adapter with DHCP enabled. The
DHCP on this adapter is incompatible with the DHCP settings. Two
host only network interfaces are not allowed to overlap, and each
host only network interface can have only one DHCP server. Please
reconfigure your host only network or remove the virtual machine
using the other host only network.
no_adapters: |-
No available adapters on the virtual machine were found to accomodate
for all configured networks. VirtualBox virtual machines have 8