From 5232f2c7b1a5425837b0012209aaad6f3bce48e5 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Mon, 9 Jun 2014 00:48:52 -0300 Subject: [PATCH] Begin work on supporting private networks --- lib/vagrant-lxc/action.rb | 2 + lib/vagrant-lxc/action/private_networks.rb | 60 ++++++++++++++++++++++ lib/vagrant-lxc/action/warn_networks.rb | 6 +-- 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 lib/vagrant-lxc/action/private_networks.rb diff --git a/lib/vagrant-lxc/action.rb b/lib/vagrant-lxc/action.rb index 19dae69..d18e23f 100644 --- a/lib/vagrant-lxc/action.rb +++ b/lib/vagrant-lxc/action.rb @@ -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 diff --git a/lib/vagrant-lxc/action/private_networks.rb b/lib/vagrant-lxc/action/private_networks.rb new file mode 100644 index 0000000..cbbe576 --- /dev/null +++ b/lib/vagrant-lxc/action/private_networks.rb @@ -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 diff --git a/lib/vagrant-lxc/action/warn_networks.rb b/lib/vagrant-lxc/action/warn_networks.rb index fed6bd9..4982cbb 100644 --- a/lib/vagrant-lxc/action/warn_networks.rb +++ b/lib/vagrant-lxc/action/warn_networks.rb @@ -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