Begin work on supporting private networks

This commit is contained in:
Fabio Rehm 2014-06-09 00:48:52 -03:00
parent d22d6588f8
commit 5232f2c7b1
3 changed files with 65 additions and 3 deletions

View file

@ -11,6 +11,7 @@ require 'vagrant-lxc/action/forward_ports'
require 'vagrant-lxc/action/handle_box_metadata'
require 'vagrant-lxc/action/prepare_nfs_settings'
require 'vagrant-lxc/action/prepare_nfs_valid_ids'
require 'vagrant-lxc/action/private_networks'
require 'vagrant-lxc/action/remove_temporary_files'
require 'vagrant-lxc/action/setup_package_files'
require 'vagrant-lxc/action/warn_networks'
@ -71,6 +72,7 @@ module Vagrant
b.use ForwardPorts
b.use Boot
b.use Builtin::WaitForCommunicator
b.use PrivateNetworks
end
end

View file

@ -0,0 +1,60 @@
module Vagrant
module LXC
module Action
class PrivateNetworks
def initialize(app, env)
@app = app
end
def call(env)
@app.call(env)
if private_network_configured?(env[:machine].config)
configure_private_networks(env)
end
end
def private_network_configured?(config)
config.vm.networks.find do |type, _|
type.to_sym == :private_network
end
end
def configure_private_networks(env)
env[:machine].config.vm.networks.find do |type, config|
next if type.to_sym != :private_network
container_name = env[:machine].provider.driver.container_name
ip = config[:ip]
configure_single_network('br1', container_name, ip)
end
end
def configure_single_network(bridge, container_name, ip)
cmd = [
'sudo',
Vagrant::LXC.source_root.join('scripts/private-network').to_s,
bridge,
container_name,
"#{ip}/24"
]
puts cmd.join(' ')
system cmd.join(' ')
cmd = [
'sudo',
'ip',
'addr',
'add',
# TODO: This should not be hard coded and has to run once per bridge
"192.168.1.254/24",
'dev',
bridge
]
puts cmd.join(' ')
system cmd.join(' ')
end
end
end
end
end

View file

@ -7,16 +7,16 @@ module Vagrant
end
def call(env)
if public_or_private_network_configured?(env[:machine].config)
if public_network_configured?(env[:machine].config)
env[:ui].warn(I18n.t("vagrant_lxc.messages.warn_networks"))
end
@app.call(env)
end
def public_or_private_network_configured?(config)
def public_network_configured?(config)
config.vm.networks.find do |type, _|
[:private_network, :public_network].include?(type.to_sym)
type.to_sym == :public_network
end
end
end