From d5abb523de7712943cb4f27daecba34f2f3b608b Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Wed, 12 Mar 2014 10:30:37 -0300 Subject: [PATCH] core: Implement support for built in synced folders and add required logic for NFS --- lib/vagrant-lxc/action.rb | 13 +++- .../action/prepare_nfs_settings.rb | 64 +++++++++++++++++++ .../action/prepare_nfs_valid_ids.rb | 19 ++++++ lib/vagrant-lxc/driver.rb | 10 ++- 4 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 lib/vagrant-lxc/action/prepare_nfs_settings.rb create mode 100644 lib/vagrant-lxc/action/prepare_nfs_valid_ids.rb diff --git a/lib/vagrant-lxc/action.rb b/lib/vagrant-lxc/action.rb index d66170e..7a0508c 100644 --- a/lib/vagrant-lxc/action.rb +++ b/lib/vagrant-lxc/action.rb @@ -15,9 +15,10 @@ require 'vagrant-lxc/action/forward_ports' require 'vagrant-lxc/action/handle_box_metadata' require 'vagrant-lxc/action/is_running' require 'vagrant-lxc/action/message' +require 'vagrant-lxc/action/prepare_nfs_settings' +require 'vagrant-lxc/action/prepare_nfs_valid_ids' require 'vagrant-lxc/action/remove_temporary_files' require 'vagrant-lxc/action/setup_package_files' -require 'vagrant-lxc/action/share_folders' require 'vagrant-lxc/action/warn_networks' unless Vagrant::Backports.vagrant_1_3_or_later? @@ -59,7 +60,15 @@ module Vagrant b.use Builtin::Provision b.use Builtin::EnvSet, :port_collision_repair => true b.use Builtin::HandleForwardedPortCollisions - b.use ShareFolders + if Vagrant::Backports.vagrant_1_4_or_later? + b.use PrepareNFSValidIds + b.use Builtin::SyncedFolderCleanup + b.use Builtin::SyncedFolders + b.use PrepareNFSSettings + else + require 'vagrant-lxc/action/share_folders' + b.use ShareFolders + end b.use Builtin::SetHostname b.use WarnNetworks b.use ForwardPorts diff --git a/lib/vagrant-lxc/action/prepare_nfs_settings.rb b/lib/vagrant-lxc/action/prepare_nfs_settings.rb new file mode 100644 index 0000000..82c1a4d --- /dev/null +++ b/lib/vagrant-lxc/action/prepare_nfs_settings.rb @@ -0,0 +1,64 @@ +module Vagrant + module LXC + module Action + class PrepareNFSSettings + include Vagrant::Util::Retryable + + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::action::vm::nfs") + end + + def call(env) + @machine = env[:machine] + + @app.call(env) + + # if using_nfs? # TODO: && !privileged_container? + # raise Errors::NfsWithoutPrivilegedError + # end + + if using_nfs? + @logger.info("Using NFS, preparing NFS settings by reading host IP and machine IP") + add_ips_to_env!(env) + end + end + + # We're using NFS if we have any synced folder with NFS configured. If + # we are not using NFS we don't need to do the extra work to + # populate these fields in the environment. + def using_nfs? + @machine.config.vm.synced_folders.any? { |_, opts| opts[:type] == :nfs } + end + + # TODO: + # def privileged_container? + # @machine.provider.driver.privileged?(@machine.id) + # end + + # Extracts the proper host and guest IPs for NFS mounts and stores them + # in the environment for the SyncedFolder action to use them in + # mounting. + # + # The ! indicates that this method modifies its argument. + def add_ips_to_env!(env) + provider = @machine.provider + + host_ip = read_host_ip + machine_ip = provider.ssh_info[:host] + + raise Vagrant::Errors::NFSNoHostonlyNetwork if !host_ip || !machine_ip + + env[:nfs_host_ip] = host_ip + env[:nfs_machine_ip] = machine_ip + end + + def read_host_ip + @machine.communicate.execute 'echo $SSH_CLIENT' do |buffer, output| + return output.chomp.split(' ')[0] if buffer == :stdout + end + end + end + end + end +end diff --git a/lib/vagrant-lxc/action/prepare_nfs_valid_ids.rb b/lib/vagrant-lxc/action/prepare_nfs_valid_ids.rb new file mode 100644 index 0000000..fcae3b4 --- /dev/null +++ b/lib/vagrant-lxc/action/prepare_nfs_valid_ids.rb @@ -0,0 +1,19 @@ +module Vagrant + module LXC + module Action + class PrepareNFSValidIds + def initialize(app, env) + @app = app + @logger = Log4r::Logger.new("vagrant::action::vm::nfs") + end + + def call(env) + machine = env[:machine] + env[:nfs_valid_ids] = machine.provider.driver.all_containers + + @app.call(env) + end + end + end + end +end diff --git a/lib/vagrant-lxc/driver.rb b/lib/vagrant-lxc/driver.rb index 6f612aa..74b0888 100644 --- a/lib/vagrant-lxc/driver.rb +++ b/lib/vagrant-lxc/driver.rb @@ -31,6 +31,10 @@ module Vagrant raise ContainerNotFound if @container_name && ! @cli.list.include?(@container_name) end + def all_containers + @cli.list + end + def base_path Pathname.new("#{CONTAINERS_PATH}/#{@container_name}") end @@ -68,10 +72,14 @@ module Vagrant end end - @customizations << ['mount.entry', "#{folder[:hostpath]} #{guestpath} none bind 0 0"] + share_folder(folder[:hostpath], guestpath) end end + def share_folder(host_path, guest_path) + @customizations << ['mount.entry', "#{host_path} #{guest_path} none bind 0 0"] + end + def start(customizations) @logger.info('Starting container...')