2012-08-15 04:12:41 +00:00
|
|
|
require "set"
|
|
|
|
|
|
|
|
require "log4r"
|
|
|
|
|
|
|
|
require "vagrant/util/network_ip"
|
2013-01-11 23:50:09 +00:00
|
|
|
require "vagrant/util/scoped_hash_override"
|
2012-08-15 04:12:41 +00:00
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module ProviderVirtualBox
|
|
|
|
module Action
|
2013-01-11 22:16:00 +00:00
|
|
|
# This middleware class sets up all networking for the VirtualBox
|
|
|
|
# instance. This includes host only networks, bridged networking,
|
|
|
|
# forwarded ports, etc.
|
|
|
|
#
|
|
|
|
# This handles all the `config.vm.network` configurations.
|
2012-08-15 04:12:41 +00:00
|
|
|
class Network
|
|
|
|
include Vagrant::Util::NetworkIP
|
2013-01-11 23:50:09 +00:00
|
|
|
include Vagrant::Util::ScopedHashOverride
|
2012-08-15 04:12:41 +00:00
|
|
|
|
|
|
|
def initialize(app, env)
|
2013-01-11 22:16:00 +00:00
|
|
|
@logger = Log4r::Logger.new("vagrant::plugins::virtualbox::network")
|
|
|
|
@app = app
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2013-01-11 22:16:00 +00:00
|
|
|
# TODO: Validate network configuration prior to anything below
|
2012-08-15 04:12:41 +00:00
|
|
|
@env = env
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# Get the list of network adapters from the configuration
|
|
|
|
network_adapters_config = env[:machine].provider_config.network_adapters.dup
|
|
|
|
|
|
|
|
# Assign the adapter slot for each high-level network
|
2014-07-17 08:52:57 +00:00
|
|
|
available_slots = Set.new(1..36)
|
2013-01-11 22:16:00 +00:00
|
|
|
network_adapters_config.each do |slot, _data|
|
|
|
|
available_slots.delete(slot)
|
|
|
|
end
|
|
|
|
|
|
|
|
@logger.debug("Available slots for high-level adapters: #{available_slots.inspect}")
|
2013-02-01 18:51:53 +00:00
|
|
|
@logger.info("Determining network adapters required for high-level configuration...")
|
2013-01-11 22:16:00 +00:00
|
|
|
available_slots = available_slots.to_a.sort
|
2013-03-02 00:48:10 +00:00
|
|
|
env[:machine].config.vm.networks.each do |type, options|
|
2013-01-11 22:16:00 +00:00
|
|
|
# We only handle private and public networks
|
|
|
|
next if type != :private_network && type != :public_network
|
|
|
|
|
2013-01-11 23:50:09 +00:00
|
|
|
options = scoped_hash_override(options, :virtualbox)
|
2013-01-11 22:16:00 +00:00
|
|
|
|
|
|
|
# Figure out the slot that this adapter will go into
|
2013-01-11 23:50:09 +00:00
|
|
|
slot = options[:adapter]
|
2013-01-11 22:16:00 +00:00
|
|
|
if !slot
|
|
|
|
if available_slots.empty?
|
2013-01-11 23:56:56 +00:00
|
|
|
raise Vagrant::Errors::VirtualBoxNoRoomForHighLevelNetwork
|
2013-01-11 22:16:00 +00:00
|
|
|
end
|
2012-08-15 04:12:41 +00:00
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
slot = available_slots.shift
|
|
|
|
end
|
2012-08-15 04:12:41 +00:00
|
|
|
|
2013-11-25 19:02:54 +00:00
|
|
|
# Internal network is a special type
|
|
|
|
if type == :private_network && options[:intnet]
|
|
|
|
type = :internal_network
|
|
|
|
end
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# Configure it
|
|
|
|
data = nil
|
|
|
|
if type == :private_network
|
|
|
|
# private_network = hostonly
|
2013-11-25 19:02:54 +00:00
|
|
|
data = [:hostonly, options]
|
2013-01-11 22:16:00 +00:00
|
|
|
elsif type == :public_network
|
|
|
|
# public_network = bridged
|
2013-11-25 19:02:54 +00:00
|
|
|
data = [:bridged, options]
|
|
|
|
elsif type == :internal_network
|
|
|
|
data = [:intnet, options]
|
2013-01-11 22:16:00 +00:00
|
|
|
end
|
2012-08-15 04:12:41 +00:00
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# Store it!
|
|
|
|
@logger.info(" -- Slot #{slot}: #{data[0]}")
|
|
|
|
network_adapters_config[slot] = data
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
@logger.info("Determining adapters and compiling network configuration...")
|
|
|
|
adapters = []
|
2013-01-11 23:06:38 +00:00
|
|
|
networks = []
|
2013-01-11 22:16:00 +00:00
|
|
|
network_adapters_config.each do |slot, data|
|
2013-03-02 00:48:10 +00:00
|
|
|
type = data[0]
|
|
|
|
options = data[1]
|
2012-08-15 04:12:41 +00:00
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
@logger.info("Network slot #{slot}. Type: #{type}.")
|
2012-08-15 04:12:41 +00:00
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# Get the normalized configuration for this type
|
2013-03-02 00:48:10 +00:00
|
|
|
config = send("#{type}_config", options)
|
2013-01-11 22:16:00 +00:00
|
|
|
config[:adapter] = slot
|
|
|
|
@logger.debug("Normalized configuration: #{config.inspect}")
|
2012-08-15 04:12:41 +00:00
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# Get the VirtualBox adapter configuration
|
|
|
|
adapter = send("#{type}_adapter", config)
|
|
|
|
adapters << adapter
|
|
|
|
@logger.debug("Adapter configuration: #{adapter.inspect}")
|
2013-01-11 23:06:38 +00:00
|
|
|
|
|
|
|
# Get the network configuration
|
|
|
|
network = send("#{type}_network_config", config)
|
|
|
|
network[:auto_config] = config[:auto_config]
|
|
|
|
networks << network
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
if !adapters.empty?
|
|
|
|
# Enable the adapters
|
|
|
|
@logger.info("Enabling adapters...")
|
2014-01-18 02:15:52 +00:00
|
|
|
env[:ui].output(I18n.t("vagrant.actions.vm.network.preparing"))
|
|
|
|
adapters.each do |adapter|
|
|
|
|
env[:ui].detail(I18n.t(
|
|
|
|
"vagrant.virtualbox.network_adapter",
|
|
|
|
adapter: adapter[:adapter].to_s,
|
|
|
|
type: adapter[:type].to_s,
|
|
|
|
extra: "",
|
|
|
|
))
|
|
|
|
end
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
env[:machine].provider.driver.enable_adapters(adapters)
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# Continue the middleware chain.
|
|
|
|
@app.call(env)
|
2012-08-15 04:12:41 +00:00
|
|
|
|
2013-01-11 23:06:38 +00:00
|
|
|
# If we have networks to configure, then we configure it now, since
|
|
|
|
# that requires the machine to be up and running.
|
|
|
|
if !adapters.empty? && !networks.empty?
|
|
|
|
assign_interface_numbers(networks, adapters)
|
|
|
|
|
|
|
|
# Only configure the networks the user requested us to configure
|
|
|
|
networks_to_configure = networks.select { |n| n[:auto_config] }
|
2013-06-09 20:37:15 +00:00
|
|
|
if !networks_to_configure.empty?
|
|
|
|
env[:ui].info I18n.t("vagrant.actions.vm.network.configuring")
|
|
|
|
env[:machine].guest.capability(:configure_networks, networks_to_configure)
|
|
|
|
end
|
2013-01-11 23:06:38 +00:00
|
|
|
end
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
2013-03-02 00:48:10 +00:00
|
|
|
def bridged_config(options)
|
2013-01-11 23:21:09 +00:00
|
|
|
return {
|
2014-05-22 16:35:12 +00:00
|
|
|
auto_config: true,
|
|
|
|
bridge: nil,
|
|
|
|
mac: nil,
|
|
|
|
nic_type: nil,
|
|
|
|
use_dhcp_assigned_default_route: false
|
2013-03-02 00:48:10 +00:00
|
|
|
}.merge(options || {})
|
2013-01-11 23:21:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def bridged_adapter(config)
|
|
|
|
# Find the bridged interfaces that are available
|
|
|
|
bridgedifs = @env[:machine].provider.driver.read_bridged_interfaces
|
|
|
|
bridgedifs.delete_if { |interface| interface[:status] == "Down" }
|
|
|
|
|
|
|
|
# The name of the chosen bridge interface will be assigned to this
|
|
|
|
# variable.
|
|
|
|
chosen_bridge = nil
|
|
|
|
|
|
|
|
if config[:bridge]
|
|
|
|
@logger.debug("Bridge was directly specified in config, searching for: #{config[:bridge]}")
|
|
|
|
|
|
|
|
# Search for a matching bridged interface
|
2015-05-30 19:02:10 +00:00
|
|
|
Array(config[:bridge]).each do |bridge|
|
2015-05-07 16:07:08 +00:00
|
|
|
bridge = bridge.downcase if bridge.respond_to?(:downcase)
|
|
|
|
bridgedifs.each do |interface|
|
|
|
|
if bridge === interface[:name].downcase
|
|
|
|
@logger.debug("Specific bridge found as configured in the Vagrantfile. Using it.")
|
|
|
|
chosen_bridge = interface[:name]
|
|
|
|
break
|
|
|
|
end
|
2013-01-11 23:21:09 +00:00
|
|
|
end
|
2015-05-07 16:07:08 +00:00
|
|
|
break if chosen_bridge
|
2013-01-11 23:21:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# If one wasn't found, then we notify the user here.
|
|
|
|
if !chosen_bridge
|
|
|
|
@env[:ui].info I18n.t("vagrant.actions.vm.bridged_networking.specific_not_found",
|
2014-05-22 16:35:12 +00:00
|
|
|
bridge: config[:bridge])
|
2013-01-11 23:21:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# If we still don't have a bridge chosen (this means that one wasn't
|
|
|
|
# specified in the Vagrantfile, or the bridge specified in the Vagrantfile
|
|
|
|
# wasn't found), then we fall back to the normal means of searchign for a
|
|
|
|
# bridged network.
|
|
|
|
if !chosen_bridge
|
|
|
|
if bridgedifs.length == 1
|
|
|
|
# One bridgable interface? Just use it.
|
|
|
|
chosen_bridge = bridgedifs[0][:name]
|
|
|
|
@logger.debug("Only one bridged interface available. Using it by default.")
|
|
|
|
else
|
|
|
|
# More than one bridgable interface requires a user decision, so
|
|
|
|
# show options to choose from.
|
2014-10-23 17:24:07 +00:00
|
|
|
@env[:ui].info I18n.t(
|
|
|
|
"vagrant.actions.vm.bridged_networking.available",
|
|
|
|
prefix: false)
|
2013-01-11 23:21:09 +00:00
|
|
|
bridgedifs.each_index do |index|
|
|
|
|
interface = bridgedifs[index]
|
2014-05-22 16:35:12 +00:00
|
|
|
@env[:ui].info("#{index + 1}) #{interface[:name]}", prefix: false)
|
2013-01-11 23:21:09 +00:00
|
|
|
end
|
2014-10-23 17:24:07 +00:00
|
|
|
@env[:ui].info(I18n.t(
|
|
|
|
"vagrant.actions.vm.bridged_networking.choice_help")+"\n")
|
2013-01-11 23:21:09 +00:00
|
|
|
|
|
|
|
# The range of valid choices
|
|
|
|
valid = Range.new(1, bridgedifs.length)
|
|
|
|
|
|
|
|
# The choice that the user has chosen as the bridging interface
|
|
|
|
choice = nil
|
|
|
|
while !valid.include?(choice)
|
2014-10-23 17:24:07 +00:00
|
|
|
choice = @env[:ui].ask(
|
2014-10-31 19:16:02 +00:00
|
|
|
"Which interface should the network bridge to? ")
|
2013-01-11 23:21:09 +00:00
|
|
|
choice = choice.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
chosen_bridge = bridgedifs[choice - 1][:name]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@logger.info("Bridging adapter #{config[:adapter]} to #{chosen_bridge}")
|
|
|
|
|
|
|
|
# Given the choice we can now define the adapter we're using
|
|
|
|
return {
|
2014-05-22 16:35:12 +00:00
|
|
|
adapter: config[:adapter],
|
|
|
|
type: :bridged,
|
|
|
|
bridge: chosen_bridge,
|
|
|
|
mac_address: config[:mac],
|
|
|
|
nic_type: config[:nic_type]
|
2013-01-11 23:21:09 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def bridged_network_config(config)
|
2013-05-19 06:41:33 +00:00
|
|
|
if config[:ip]
|
|
|
|
options = {
|
2014-05-22 16:35:12 +00:00
|
|
|
auto_config: true,
|
|
|
|
mac: nil,
|
|
|
|
netmask: "255.255.255.0",
|
|
|
|
type: :static
|
2013-05-19 06:41:33 +00:00
|
|
|
}.merge(config)
|
|
|
|
options[:type] = options[:type].to_sym
|
|
|
|
return options
|
|
|
|
end
|
|
|
|
|
2013-01-11 23:21:09 +00:00
|
|
|
return {
|
2014-05-22 16:35:12 +00:00
|
|
|
type: :dhcp,
|
|
|
|
use_dhcp_assigned_default_route: config[:use_dhcp_assigned_default_route]
|
2013-01-11 23:21:09 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-03-02 00:48:10 +00:00
|
|
|
def hostonly_config(options)
|
2012-08-15 04:12:41 +00:00
|
|
|
options = {
|
2014-05-22 16:35:12 +00:00
|
|
|
auto_config: true,
|
|
|
|
mac: nil,
|
|
|
|
nic_type: nil,
|
|
|
|
netmask: "255.255.255.0",
|
|
|
|
type: :static
|
2013-03-02 00:48:10 +00:00
|
|
|
}.merge(options)
|
2013-01-11 22:16:00 +00:00
|
|
|
|
2013-04-08 17:39:26 +00:00
|
|
|
# Make sure the type is a symbol
|
|
|
|
options[:type] = options[:type].to_sym
|
|
|
|
|
2013-03-20 19:18:32 +00:00
|
|
|
# Default IP is in the 20-bit private network block for DHCP based networks
|
2013-07-13 00:22:53 +00:00
|
|
|
options[:ip] = "172.28.128.1" if options[:type] == :dhcp && !options[:ip]
|
2013-03-20 19:18:32 +00:00
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# Calculate our network address for the given IP/netmask
|
2013-03-02 00:48:10 +00:00
|
|
|
netaddr = network_address(options[:ip], options[:netmask])
|
2013-01-11 22:16:00 +00:00
|
|
|
|
|
|
|
# Verify that a host-only network subnet would not collide
|
|
|
|
# with a bridged networking interface.
|
|
|
|
#
|
|
|
|
# If the subnets overlap in any way then the host only network
|
|
|
|
# will not work because the routing tables will force the
|
|
|
|
# traffic onto the real interface rather than the VirtualBox
|
|
|
|
# interface.
|
|
|
|
@env[:machine].provider.driver.read_bridged_interfaces.each do |interface|
|
|
|
|
that_netaddr = network_address(interface[:ip], interface[:netmask])
|
|
|
|
raise Vagrant::Errors::NetworkCollision if \
|
|
|
|
netaddr == that_netaddr && interface[:status] != "Down"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Split the IP address into its components
|
2012-08-15 04:12:41 +00:00
|
|
|
ip_parts = netaddr.split(".").map { |i| i.to_i }
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# Calculate the adapter IP, which we assume is the IP ".1" at
|
|
|
|
# the end usually.
|
2012-08-15 04:12:41 +00:00
|
|
|
adapter_ip = ip_parts.dup
|
|
|
|
adapter_ip[3] += 1
|
|
|
|
options[:adapter_ip] ||= adapter_ip.join(".")
|
|
|
|
|
2013-03-20 19:18:32 +00:00
|
|
|
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
|
2014-11-29 20:24:39 +00:00
|
|
|
dhcp_options[:dhcp_ip] = options[:dhcp_ip] || dhcp_ip.join(".")
|
2013-03-20 19:18:32 +00:00
|
|
|
|
|
|
|
# Calculate the lower and upper bound for the DHCP server
|
|
|
|
dhcp_lower = ip_parts.dup
|
|
|
|
dhcp_lower[3] += 3
|
2014-11-29 20:24:39 +00:00
|
|
|
dhcp_options[:dhcp_lower] = options[:dhcp_lower] || dhcp_lower.join(".")
|
2013-03-20 19:18:32 +00:00
|
|
|
|
|
|
|
dhcp_upper = ip_parts.dup
|
|
|
|
dhcp_upper[3] = 254
|
2014-11-29 20:24:39 +00:00
|
|
|
dhcp_options[:dhcp_upper] = options[:dhcp_upper] || dhcp_upper.join(".")
|
2013-03-20 19:18:32 +00:00
|
|
|
end
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
return {
|
2014-05-22 16:35:12 +00:00
|
|
|
adapter_ip: options[:adapter_ip],
|
|
|
|
auto_config: options[:auto_config],
|
|
|
|
ip: options[:ip],
|
|
|
|
mac: options[:mac],
|
|
|
|
netmask: options[:netmask],
|
|
|
|
nic_type: options[:nic_type],
|
|
|
|
type: options[:type]
|
2013-03-20 19:18:32 +00:00
|
|
|
}.merge(dhcp_options)
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def hostonly_adapter(config)
|
2013-01-11 22:16:00 +00:00
|
|
|
@logger.info("Searching for matching hostonly network: #{config[:ip]}")
|
|
|
|
interface = hostonly_find_matching_network(config)
|
2012-08-15 04:12:41 +00:00
|
|
|
|
|
|
|
if !interface
|
2013-01-11 22:16:00 +00:00
|
|
|
@logger.info("Network not found. Creating if we can.")
|
2012-08-15 04:12:41 +00:00
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# It is an error if a specific host only network name was specified
|
|
|
|
# but the network wasn't found.
|
2012-08-15 04:12:41 +00:00
|
|
|
if config[:name]
|
2014-05-22 16:35:12 +00:00
|
|
|
raise Vagrant::Errors::NetworkNotFound, name: config[:name]
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# Create a new network
|
|
|
|
interface = hostonly_create_network(config)
|
|
|
|
@logger.info("Created network: #{interface[:name]}")
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
2013-03-20 19:18:32 +00:00
|
|
|
if config[:type] == :dhcp
|
2014-11-29 20:24:39 +00:00
|
|
|
create_dhcp_server_if_necessary(interface, config)
|
2013-03-20 19:18:32 +00:00
|
|
|
end
|
|
|
|
|
2012-08-15 04:12:41 +00:00
|
|
|
return {
|
2014-05-22 16:35:12 +00:00
|
|
|
adapter: config[:adapter],
|
|
|
|
hostonly: interface[:name],
|
|
|
|
mac_address: config[:mac],
|
|
|
|
nic_type: config[:nic_type],
|
|
|
|
type: :hostonly
|
2012-08-15 04:12:41 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-01-11 23:06:38 +00:00
|
|
|
def hostonly_network_config(config)
|
|
|
|
return {
|
2014-05-22 16:35:12 +00:00
|
|
|
type: config[:type],
|
|
|
|
adapter_ip: config[:adapter_ip],
|
|
|
|
ip: config[:ip],
|
|
|
|
netmask: config[:netmask]
|
2013-01-11 23:06:38 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-11-25 19:02:54 +00:00
|
|
|
def intnet_config(options)
|
|
|
|
return {
|
2014-05-22 16:35:12 +00:00
|
|
|
type: "static",
|
|
|
|
ip: nil,
|
|
|
|
netmask: "255.255.255.0",
|
|
|
|
adapter: nil,
|
|
|
|
mac: nil,
|
|
|
|
intnet: nil,
|
|
|
|
auto_config: true
|
2013-11-25 19:02:54 +00:00
|
|
|
}.merge(options || {})
|
|
|
|
end
|
|
|
|
|
|
|
|
def intnet_adapter(config)
|
2014-01-03 16:29:33 +00:00
|
|
|
intnet_name = config[:intnet]
|
|
|
|
intnet_name = "intnet" if intnet_name == true
|
|
|
|
|
2013-11-25 19:02:54 +00:00
|
|
|
return {
|
2014-05-22 16:35:12 +00:00
|
|
|
adapter: config[:adapter],
|
|
|
|
type: :intnet,
|
|
|
|
mac_address: config[:mac],
|
|
|
|
nic_type: config[:nic_type],
|
|
|
|
intnet: intnet_name,
|
2013-11-25 19:02:54 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def intnet_network_config(config)
|
|
|
|
return {
|
2014-05-22 16:35:12 +00:00
|
|
|
type: config[:type],
|
|
|
|
ip: config[:ip],
|
|
|
|
netmask: config[:netmask]
|
2013-11-25 19:02:54 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
def nat_config(options)
|
2013-01-11 23:06:38 +00:00
|
|
|
return {
|
2014-05-22 16:35:12 +00:00
|
|
|
auto_config: false
|
2013-01-11 23:06:38 +00:00
|
|
|
}
|
2013-01-11 22:16:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def nat_adapter(config)
|
2012-08-15 04:12:41 +00:00
|
|
|
return {
|
2014-05-22 16:35:12 +00:00
|
|
|
adapter: config[:adapter],
|
|
|
|
type: :nat,
|
2012-08-15 04:12:41 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-01-11 23:06:38 +00:00
|
|
|
def nat_network_config(config)
|
|
|
|
return {}
|
|
|
|
end
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------
|
|
|
|
# Misc. helpers
|
|
|
|
#-----------------------------------------------------------------
|
|
|
|
# Assigns the actual interface number of a network based on the
|
|
|
|
# enabled NICs on the virtual machine.
|
|
|
|
#
|
|
|
|
# This interface number is used by the guest to configure the
|
|
|
|
# NIC on the guest VM.
|
|
|
|
#
|
|
|
|
# The networks are modified in place by adding an ":interface"
|
|
|
|
# field to each.
|
|
|
|
def assign_interface_numbers(networks, adapters)
|
|
|
|
current = 0
|
|
|
|
adapter_to_interface = {}
|
|
|
|
|
|
|
|
# Make a first pass to assign interface numbers by adapter location
|
|
|
|
vm_adapters = @env[:machine].provider.driver.read_network_interfaces
|
|
|
|
vm_adapters.sort.each do |number, adapter|
|
|
|
|
if adapter[:type] != :none
|
|
|
|
# Not used, so assign the interface number and increment
|
|
|
|
adapter_to_interface[number] = current
|
|
|
|
current += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Make a pass through the adapters to assign the :interface
|
|
|
|
# key to each network configuration.
|
|
|
|
adapters.each_index do |i|
|
|
|
|
adapter = adapters[i]
|
|
|
|
network = networks[i]
|
|
|
|
|
|
|
|
# Figure out the interface number by simple lookup
|
|
|
|
network[:interface] = adapter_to_interface[adapter[:adapter]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
#-----------------------------------------------------------------
|
|
|
|
# Hostonly Helper Functions
|
|
|
|
#-----------------------------------------------------------------
|
|
|
|
# This creates a host only network for the given configuration.
|
|
|
|
def hostonly_create_network(config)
|
|
|
|
@env[:machine].provider.driver.create_host_only_network(
|
2014-05-22 16:35:12 +00:00
|
|
|
adapter_ip: config[:adapter_ip],
|
|
|
|
netmask: config[:netmask]
|
2013-01-11 22:16:00 +00:00
|
|
|
)
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
2013-01-11 22:16:00 +00:00
|
|
|
# This finds a matching host only network for the given configuration.
|
|
|
|
def hostonly_find_matching_network(config)
|
2012-08-15 04:12:41 +00:00
|
|
|
this_netaddr = network_address(config[:ip], config[:netmask])
|
|
|
|
|
|
|
|
@env[:machine].provider.driver.read_host_only_interfaces.each do |interface|
|
2013-01-11 22:16:00 +00:00
|
|
|
return interface if config[:name] && config[:name] == interface[:name]
|
|
|
|
return interface if this_netaddr == \
|
|
|
|
network_address(interface[:ip], interface[:netmask])
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
2014-11-29 18:11:34 +00:00
|
|
|
|
2014-11-29 20:24:39 +00:00
|
|
|
#-----------------------------------------------------------------
|
|
|
|
# DHCP Server Helper Functions
|
|
|
|
#-----------------------------------------------------------------
|
|
|
|
|
|
|
|
DEFAULT_DHCP_SERVER_FROM_VBOX_INSTALL = {
|
|
|
|
network_name: 'HostInterfaceNetworking-vboxnet0',
|
|
|
|
network: 'vboxnet0',
|
|
|
|
ip: '192.168.56.100',
|
|
|
|
netmask: '255.255.255.0',
|
|
|
|
lower: '192.168.56.101',
|
|
|
|
upper: '192.168.56.254'
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
#
|
|
|
|
# When a host-only network of type: :dhcp is configured,
|
|
|
|
# this handles the potential creation of a vbox dhcpserver to manage
|
|
|
|
# it.
|
|
|
|
#
|
|
|
|
# @param [Hash<String>] interface hash as returned from read_host_only_interfaces
|
|
|
|
# @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")
|
|
|
|
return
|
|
|
|
elsif existing_dhcp_server == DEFAULT_DHCP_SERVER_FROM_VBOX_INSTALL
|
|
|
|
@env[:ui].info I18n.t("vagrant.actions.vm.network.cleanup_vbox_default_dhcp")
|
|
|
|
@env[:machine].provider.driver.remove_dhcp_server(existing_dhcp_server[:network_name])
|
|
|
|
else
|
|
|
|
# We have an invalid DHCP server that we're not able to
|
|
|
|
# automatically clean up, so we need to give up and tell the user
|
|
|
|
# to sort out their own vbox dhcpservers and hostonlyifs
|
|
|
|
raise Vagrant::Errors::NetworkDHCPAlreadyAttached
|
|
|
|
end
|
2014-11-29 18:11:34 +00:00
|
|
|
end
|
2014-11-29 20:24:39 +00:00
|
|
|
|
|
|
|
@logger.debug("Creating a DHCP server...")
|
|
|
|
@env[:machine].provider.driver.create_dhcp_server(interface[:name], config)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Detect when an existing DHCP server matches precisely the
|
|
|
|
# requested config for a hostonly interface.
|
|
|
|
#
|
|
|
|
# @param [Hash<String>] dhcp_server as found by read_dhcp_servers
|
|
|
|
# @param [Hash<String>] config as returned from hostonly_config
|
|
|
|
# @return [Boolean]
|
|
|
|
def dhcp_server_matches_config?(dhcp_server, config)
|
|
|
|
dhcp_server[:ip] == config[:dhcp_ip] &&
|
|
|
|
dhcp_server[:lower] == config[:dhcp_lower] &&
|
|
|
|
dhcp_server[:upper] == config[:dhcp_upper]
|
2014-11-29 18:11:34 +00:00
|
|
|
end
|
|
|
|
|
2014-11-29 20:24:39 +00:00
|
|
|
# Returns the existing dhcp server, if any, that is attached to the
|
|
|
|
# specified interface.
|
|
|
|
#
|
|
|
|
# @return [Hash<String>] dhcp_server or nil if not found
|
|
|
|
def find_matching_dhcp_server(interface)
|
|
|
|
@env[:machine].provider.driver.read_dhcp_servers.detect do |dhcp_server|
|
|
|
|
interface[:name] && interface[:name] == dhcp_server[:network]
|
|
|
|
end
|
2014-11-29 18:11:34 +00:00
|
|
|
end
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|