From 8f1b54395e63caeaa9f4782300af2989fbeaa376 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Wed, 12 Mar 2014 10:26:14 -0300 Subject: [PATCH] core: Introduce a synced folder plugin for vagrant 1.4+ --- lib/vagrant-backports/utils.rb | 4 ++++ lib/vagrant-lxc/plugin.rb | 7 ++++++ lib/vagrant-lxc/synced_folder.rb | 37 ++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 lib/vagrant-lxc/synced_folder.rb diff --git a/lib/vagrant-backports/utils.rb b/lib/vagrant-backports/utils.rb index 096b5fb..dec12f3 100644 --- a/lib/vagrant-backports/utils.rb +++ b/lib/vagrant-backports/utils.rb @@ -5,6 +5,10 @@ module Vagrant Gem::Version.new(Vagrant::VERSION) >= Gem::Version.new('1.3.0') end + def vagrant_1_4_or_later? + Gem::Version.new(Vagrant::VERSION) >= Gem::Version.new('1.4.0') + end + def vagrant_1_5_or_later? Gem::Version.new(Vagrant::VERSION) >= Gem::Version.new('1.5.0') end diff --git a/lib/vagrant-lxc/plugin.rb b/lib/vagrant-lxc/plugin.rb index b2dd7b6..bb29817 100644 --- a/lib/vagrant-lxc/plugin.rb +++ b/lib/vagrant-lxc/plugin.rb @@ -23,6 +23,13 @@ module Vagrant require File.expand_path("../config", __FILE__) Config end + + if Vagrant::Backports.vagrant_1_4_or_later? + synced_folder(:lxc) do + require File.expand_path("../synced_folder", __FILE__) + SyncedFolder + end + end end end end diff --git a/lib/vagrant-lxc/synced_folder.rb b/lib/vagrant-lxc/synced_folder.rb new file mode 100644 index 0000000..49f8e07 --- /dev/null +++ b/lib/vagrant-lxc/synced_folder.rb @@ -0,0 +1,37 @@ +module Vagrant + module LXC + class SyncedFolder < Vagrant.plugin("2", :synced_folder) + def usable?(machine) + # These synced folders only work if the provider is LXC + machine.provider_name == :lxc + end + + def prepare(machine, folders, _opts) + machine.ui.output(I18n.t("vagrant.actions.vm.share_folders.mounting")) + + folders.each do |id, data| + host_path = Pathname.new(File.expand_path(data[:hostpath], machine.env.root_path)) + guest_path = data[:guestpath] + + if !host_path.directory? && data[:create] + # Host path doesn't exist, so let's create it. + @logger.debug("Host path doesn't exist, creating: #{host_path}") + + begin + host_path.mkpath + rescue Errno::EACCES + raise Vagrant::Errors::SharedFolderCreateFailed, + :path => hostpath.to_s + end + end + + machine.provider.driver.share_folder(host_path, guest_path) + # Guest path specified, so mount the folder to specified point + machine.ui.detail(I18n.t("vagrant.actions.vm.share_folders.mounting_entry", + guestpath: data[:guestpath], + hostpath: data[:hostpath])) + end + end + end + end +end