2012-08-15 04:12:41 +00:00
|
|
|
require "pathname"
|
|
|
|
|
|
|
|
require "log4r"
|
|
|
|
|
2013-03-17 22:31:57 +00:00
|
|
|
require "vagrant/util/scoped_hash_override"
|
|
|
|
|
2012-08-15 04:12:41 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module ProviderVirtualBox
|
|
|
|
module Action
|
|
|
|
class ShareFolders
|
2013-03-17 22:31:57 +00:00
|
|
|
include Vagrant::Util::ScopedHashOverride
|
|
|
|
|
2012-08-15 04:12:41 +00:00
|
|
|
def initialize(app, env)
|
|
|
|
@logger = Log4r::Logger.new("vagrant::action::vm::share_folders")
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
@env = env
|
|
|
|
|
|
|
|
prepare_folders
|
|
|
|
create_metadata
|
|
|
|
|
|
|
|
@app.call(env)
|
|
|
|
|
|
|
|
mount_shared_folders
|
|
|
|
end
|
|
|
|
|
|
|
|
# This method returns an actual list of VirtualBox shared
|
|
|
|
# folders to create and their proper path.
|
|
|
|
def shared_folders
|
2013-01-23 17:42:19 +00:00
|
|
|
{}.tap do |result|
|
|
|
|
@env[:machine].config.vm.synced_folders.each do |id, data|
|
2013-03-17 22:31:57 +00:00
|
|
|
data = scoped_hash_override(data, :virtualbox)
|
|
|
|
|
2013-02-08 23:34:04 +00:00
|
|
|
# Ignore NFS shared folders
|
|
|
|
next if data[:nfs]
|
2012-08-15 04:12:41 +00:00
|
|
|
|
2013-03-17 22:31:57 +00:00
|
|
|
# Ignore disabled shared folders
|
|
|
|
next if data[:disabled]
|
|
|
|
|
2013-01-23 17:42:19 +00:00
|
|
|
# This to prevent overwriting the actual shared folders data
|
|
|
|
result[id] = data.dup
|
|
|
|
end
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Prepares the shared folders by verifying they exist and creating them
|
|
|
|
# if they don't.
|
|
|
|
def prepare_folders
|
2013-01-23 17:42:19 +00:00
|
|
|
shared_folders.each do |id, options|
|
2012-08-15 04:12:41 +00:00
|
|
|
hostpath = Pathname.new(options[:hostpath]).expand_path(@env[:root_path])
|
|
|
|
|
|
|
|
if !hostpath.directory? && options[:create]
|
|
|
|
# Host path doesn't exist, so let's create it.
|
|
|
|
@logger.debug("Host path doesn't exist, creating: #{hostpath}")
|
|
|
|
|
|
|
|
begin
|
|
|
|
hostpath.mkpath
|
|
|
|
rescue Errno::EACCES
|
|
|
|
raise Vagrant::Errors::SharedFolderCreateFailed,
|
|
|
|
:path => hostpath.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_metadata
|
|
|
|
@env[:ui].info I18n.t("vagrant.actions.vm.share_folders.creating")
|
|
|
|
|
|
|
|
folders = []
|
2013-01-23 17:42:19 +00:00
|
|
|
shared_folders.each do |id, data|
|
2012-08-15 04:12:41 +00:00
|
|
|
folders << {
|
2013-01-23 17:42:19 +00:00
|
|
|
:name => id,
|
2012-08-15 04:12:41 +00:00
|
|
|
:hostpath => File.expand_path(data[:hostpath], @env[:root_path]),
|
|
|
|
:transient => data[:transient]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
@env[:machine].provider.driver.share_folders(folders)
|
|
|
|
end
|
|
|
|
|
|
|
|
def mount_shared_folders
|
|
|
|
@env[:ui].info I18n.t("vagrant.actions.vm.share_folders.mounting")
|
|
|
|
|
|
|
|
# short guestpaths first, so we don't step on ourselves
|
2013-01-23 17:42:19 +00:00
|
|
|
folders = shared_folders.sort_by do |id, data|
|
2012-08-15 04:12:41 +00:00
|
|
|
if data[:guestpath]
|
|
|
|
data[:guestpath].length
|
|
|
|
else
|
|
|
|
# A long enough path to just do this at the end.
|
|
|
|
10000
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Go through each folder and mount
|
2013-01-23 17:42:19 +00:00
|
|
|
folders.each do |id, data|
|
2012-08-15 04:12:41 +00:00
|
|
|
if data[:guestpath]
|
|
|
|
# Guest path specified, so mount the folder to specified point
|
|
|
|
@env[:ui].info(I18n.t("vagrant.actions.vm.share_folders.mounting_entry",
|
|
|
|
:guest_path => data[:guestpath]))
|
|
|
|
|
|
|
|
# Dup the data so we can pass it to the guest API
|
|
|
|
data = data.dup
|
|
|
|
|
|
|
|
# Calculate the owner and group
|
|
|
|
data[:owner] ||= @env[:machine].config.ssh.username
|
|
|
|
data[:group] ||= @env[:machine].config.ssh.username
|
|
|
|
|
|
|
|
# Mount the actual folder
|
2013-04-04 06:01:43 +00:00
|
|
|
@env[:machine].guest.capability(
|
|
|
|
:mount_virtualbox_shared_folder, id, data[:guestpath], data)
|
2012-08-15 04:12:41 +00:00
|
|
|
else
|
|
|
|
# If no guest path is specified, then automounting is disabled
|
|
|
|
@env[:ui].info(I18n.t("vagrant.actions.vm.share_folders.nomount_entry",
|
2013-01-23 17:45:56 +00:00
|
|
|
:host_path => data[:hostpath]))
|
2012-08-15 04:12:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|