Bridged networking basics.

This commit is contained in:
Mitchell Hashimoto 2011-12-31 11:27:37 -08:00
parent 8f3a7ce22d
commit 42883cbd20
7 changed files with 118 additions and 6 deletions

View File

@ -26,6 +26,7 @@ module Vagrant
module VM
autoload :Boot, 'vagrant/action/vm/boot'
autoload :BridgedNetwork, 'vagrant/action/vm/bridged_network'
autoload :CheckAccessible, 'vagrant/action/vm/check_accessible'
autoload :CheckBox, 'vagrant/action/vm/check_box'
autoload :CheckGuestAdditions, 'vagrant/action/vm/check_guest_additions'

View File

@ -29,6 +29,7 @@ module Vagrant
use VM::ClearSharedFolders
use VM::ShareFolders
use VM::HostName
use VM::BridgedNetwork
use VM::HostOnlyNetwork
use VM::Customize
use VM::Boot

View File

@ -0,0 +1,98 @@
require 'log4r'
module Vagrant
module Action
module VM
# This action sets up any bridged networking for the virtual
# machine.
class BridgedNetwork
def initialize(app, env)
@logger = Log4r::Logger.new("vagrant::action::vm::bridged_network")
@app = app
end
def call(env)
@env = env
networks = bridged_networks
@logger.debug("Must configure #{networks.length} bridged networks")
if !networks.empty?
# Determine what bridged interface to connect to for each
# network. This returns the same array with the `bridge`
# key available with the interface.
networks = determine_bridged_interface(networks)
# Status output
env[:ui].info I18n.t("vagrant.actions.vm.bridged_networking.preparing")
networks.each do |options|
env[:ui].info I18n.t("vagrant.actions.vm.bridged_networking.bridging",
:adapter => options[:adapter],
:bridge => options[:bridge])
end
# Setup the network interfaces on the VM if we need to
setup_network_interfaces(networks)
end
@app.call(env)
if !networks.empty?
@env[:ui].info I18n.t("vagrant.actions.vm.bridged_networking.enabling")
# Prepare for new networks
# Enable the networks
end
end
def bridged_networks
results = []
@env[:vm].config.vm.networks.each do |type, args|
if type == :bridged
options = args[0] || {}
results << { :adapter => 2 }.merge(options)
end
end
results
end
def determine_bridged_interface(networks)
bridgedifs = @env[:vm].driver.read_bridged_interfaces
results = []
networks.each do |network|
# TODO: Allow choosing the bridged interface. For now, we just use the
# first one blindly.
options = network.dup
options[:bridge] = bridgedifs[0][:name]
@logger.info("Bridging #{options[:adapter]} => #{options[:bridge]}")
results << options
end
results
end
def setup_network_interfaces(networks)
adapters = []
networks.each do |options|
adapters << {
:adapter => options[:adapter] + 1,
:type => :bridged,
:bridge => options[:bridge]
}
end
# Enable the adapters
@logger.info("Enabling bridged networking adapters")
@env[:vm].driver.enable_adapters(adapters)
end
end
end
end
end

View File

@ -7,7 +7,7 @@ module Vagrant
# networking on VMs if configured as such.
class HostOnlyNetwork
def initialize(app, env)
@logger = Log4r::Logger.new("vagrant::action::vm::network")
@logger = Log4r::Logger.new("vagrant::action::vm::hostonly_network")
@app = app
end
@ -31,7 +31,7 @@ module Vagrant
# Enable the network interfaces
if !networks.empty?
@env[:ui].info I18n.t("vagrant.actions.vm.network.enabling")
@env[:ui].info I18n.t("vagrant.actions.vm.host_only_network.enabling")
# Prepare for new networks...
networks.each do |network_options|
@ -79,7 +79,7 @@ module Vagrant
# Enables and assigns the host only network to the proper
# adapter on the VM, and saves the adapter.
def assign_network(networks)
@env[:ui].info I18n.t("vagrant.actions.vm.network.preparing")
@env[:ui].info I18n.t("vagrant.actions.vm.host_only_network.preparing")
host_only_interfaces = @env[:vm].driver.read_host_only_interfaces
adapters = []

View File

@ -122,6 +122,11 @@ module Vagrant
adapters.each do |adapter|
args.concat(["--nic#{adapter[:adapter]}", adapter[:type].to_s])
if adapter[:bridge]
args.concat(["--bridgeadapter#{adapter[:adapter]}",
adapter[:bridge]])
end
if adapter[:hostonly]
args.concat(["--hostonlyadapter#{adapter[:adapter]}",
adapter[:hostonly]])

View File

@ -205,12 +205,12 @@ module Vagrant
class NetworkCollision < VagrantError
status_code(29)
error_key(:collides, "vagrant.actions.vm.network")
error_key(:collides, "vagrant.actions.vm.host_only_network")
end
class NetworkNotFound < VagrantError
status_code(30)
error_key(:not_found, "vagrant.actions.vm.network")
error_key(:not_found, "vagrant.actions.vm.host_only_network")
end
class NFSHostRequired < VagrantError

View File

@ -280,6 +280,13 @@ en:
failed_to_boot: |-
Failed to connect to VM via SSH. Please verify the VM successfully booted
by looking at the VirtualBox GUI.
bridged_networking:
bridging: |-
Bridging adapter #%{adapter} to '%{bridge}'
enabling: |-
Enabling bridged network...
preparing: |-
Preparing bridged networking...
check_box:
not_found: Box %{name} was not found. Fetching box from specified URL...
not_specified: |-
@ -383,7 +390,7 @@ en:
to work properly (and hence port forwarding, SSH, etc.). Specifying this
MAC address is typically up to the box and box maintiner. Please contact
the relevant person to solve this issue.
network:
host_only_network:
collides: |-
The specified host network collides with a non-hostonly network!
This will cause your specified IP to be inaccessible. Please change