Merge pull request #1466 from searchmetrics/enable_dhcp_for_private_networks

added dhcp configuration option for private networks
This commit is contained in:
Mitchell Hashimoto 2013-03-20 22:07:24 -07:00
commit f182261a96
1 changed files with 43 additions and 3 deletions

View File

@ -209,9 +209,13 @@ module VagrantPlugins
def hostonly_config(options) def hostonly_config(options)
options = { options = {
:auto_config => true, :auto_config => true,
:netmask => "255.255.255.0" :netmask => "255.255.255.0",
:type => :static
}.merge(options) }.merge(options)
# Default IP is in the 20-bit private network block for DHCP based networks
options[:ip] = "172.28.128.1" if options[:type] == :dhcp
# Calculate our network address for the given IP/netmask # Calculate our network address for the given IP/netmask
netaddr = network_address(options[:ip], options[:netmask]) netaddr = network_address(options[:ip], options[:netmask])
@ -237,6 +241,24 @@ module VagrantPlugins
adapter_ip[3] += 1 adapter_ip[3] += 1
options[:adapter_ip] ||= adapter_ip.join(".") options[:adapter_ip] ||= adapter_ip.join(".")
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] ||= 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] ||= dhcp_lower.join(".")
dhcp_upper = ip_parts.dup
dhcp_upper[3] = 254
dhcp_options[:dhcp_upper] ||= dhcp_upper.join(".")
end
return { return {
:adapter_ip => options[:adapter_ip], :adapter_ip => options[:adapter_ip],
:auto_config => options[:auto_config], :auto_config => options[:auto_config],
@ -244,8 +266,8 @@ module VagrantPlugins
:mac => nil, :mac => nil,
:netmask => options[:netmask], :netmask => options[:netmask],
:nic_type => nil, :nic_type => nil,
:type => :static :type => options[:type]
} }.merge(dhcp_options)
end end
def hostonly_adapter(config) def hostonly_adapter(config)
@ -266,6 +288,24 @@ module VagrantPlugins
@logger.info("Created network: #{interface[:name]}") @logger.info("Created network: #{interface[:name]}")
end end
if config[:type] == :dhcp
# 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[:machine].provider.driver.create_dhcp_server(interface[:name], config)
end
end
return { return {
:adapter => config[:adapter], :adapter => config[:adapter],
:hostonly => interface[:name], :hostonly => interface[:name],