Can now specify owner/group of shared folders. [closes GH-350]

This commit is contained in:
Mitchell Hashimoto 2011-07-05 23:53:15 -07:00
parent 179845a36e
commit 8733a745ce
9 changed files with 31 additions and 66 deletions

View File

@ -8,6 +8,7 @@
- New `:inline` option for shell provisioner to provide inline - New `:inline` option for shell provisioner to provide inline
scripts as a string. [GH-395] scripts as a string. [GH-395]
- Host only network now properly works on multiple adapters. [GH-365] - Host only network now properly works on multiple adapters. [GH-365]
- Can now specify owner/group for regular shared folders. [GH-350]
## 0.7.6 (July 2, 2011) ## 0.7.6 (July 2, 2011)

View File

@ -18,8 +18,6 @@ Vagrant::Config.run do |config|
config.vm.base_mac = nil config.vm.base_mac = nil
config.vm.forward_port("ssh", 22, 2222, :auto => true) config.vm.forward_port("ssh", 22, 2222, :auto => true)
config.vm.disk_image_format = 'VMDK' config.vm.disk_image_format = 'VMDK'
config.vm.shared_folder_uid = nil
config.vm.shared_folder_gid = nil
config.vm.boot_mode = "vrdp" config.vm.boot_mode = "vrdp"
config.vm.system = :linux config.vm.system = :linux

View File

@ -55,7 +55,13 @@ module Vagrant
@env.ui.info(I18n.t("vagrant.actions.vm.share_folders.mounting_entry", @env.ui.info(I18n.t("vagrant.actions.vm.share_folders.mounting_entry",
:name => name, :name => name,
:guest_path => data[:guestpath])) :guest_path => data[:guestpath]))
@env["vm"].system.mount_shared_folder(ssh, name, data[:guestpath])
# Calculate the owner and group
owner = data[:owner] || @env["config"].ssh.username
group = data[:group] || @env["config"].ssh.username
# Mount the actual folder
@env["vm"].system.mount_shared_folder(ssh, name, data[:guestpath], owner, group)
else else
# If no guest path is specified, then automounting is disabled # If no guest path is specified, then automounting is disabled
@env.ui.info(I18n.t("vagrant.actions.vm.share_folders.nomount_entry", @env.ui.info(I18n.t("vagrant.actions.vm.share_folders.nomount_entry",

View File

@ -20,8 +20,6 @@ module Vagrant
attr_reader :network_options attr_reader :network_options
attr_reader :provisioners attr_reader :provisioners
attr_accessor :disk_image_format attr_accessor :disk_image_format
attr_writer :shared_folder_uid
attr_writer :shared_folder_gid
attr_accessor :system attr_accessor :system
def initialize def initialize
@ -46,7 +44,9 @@ module Vagrant
def share_folder(name, guestpath, hostpath, opts=nil) def share_folder(name, guestpath, hostpath, opts=nil)
@shared_folders[name] = { @shared_folders[name] = {
:guestpath => guestpath, :guestpath => guestpath,
:hostpath => hostpath :hostpath => hostpath,
:owner => nil,
:group => nil
}.merge(opts || {}) }.merge(opts || {})
end end
@ -73,14 +73,6 @@ module Vagrant
raise Errors::VagrantError, :_key => :provisioner_equals_not_supported raise Errors::VagrantError, :_key => :provisioner_equals_not_supported
end end
def shared_folder_uid
@shared_folder_uid || env.config.ssh.username
end
def shared_folder_gid
@shared_folder_gid || env.config.ssh.username
end
def customize(&block) def customize(&block)
push_proc(&block) push_proc(&block)
end end

View File

@ -60,7 +60,7 @@ module Vagrant
# @param [String] name The name of the shared folder. # @param [String] name The name of the shared folder.
# @param [String] guestpath The path on the machine which the user # @param [String] guestpath The path on the machine which the user
# wants the folder mounted. # wants the folder mounted.
def mount_shared_folder(ssh, name, guestpath); end def mount_shared_folder(ssh, name, guestpath, owner, group); end
# Mounts a shared folder via NFS. This assumes that the exports # Mounts a shared folder via NFS. This assumes that the exports
# via the host are already done. # via the host are already done.

View File

@ -38,10 +38,10 @@ module Vagrant
end end
end end
def mount_shared_folder(ssh, name, guestpath) def mount_shared_folder(ssh, name, guestpath, owner, group)
ssh.exec!("sudo mkdir -p #{guestpath}") ssh.exec!("sudo mkdir -p #{guestpath}")
mount_folder(ssh, name, guestpath) mount_folder(ssh, name, guestpath, owner, group)
ssh.exec!("sudo chown #{vm.env.config.ssh.username} #{guestpath}") ssh.exec!("sudo chown `id -u #{owner}`:`id -g #{group}` #{guestpath}")
end end
def mount_nfs(ip, folders) def mount_nfs(ip, folders)
@ -58,11 +58,11 @@ module Vagrant
#------------------------------------------------------------------- #-------------------------------------------------------------------
# "Private" methods which assist above methods # "Private" methods which assist above methods
#------------------------------------------------------------------- #-------------------------------------------------------------------
def mount_folder(ssh, name, guestpath, sleeptime=5) def mount_folder(ssh, name, guestpath, owner, group, sleeptime=5)
# Determine the permission string to attach to the mount command # Determine the permission string to attach to the mount command
perms = [] perms = []
perms << "uid=`id -u #{vm.env.config.vm.shared_folder_uid}`" perms << "uid=`id -u #{owner}`"
perms << "gid=`id -g #{vm.env.config.vm.shared_folder_gid}`" perms << "gid=`id -g #{group}`"
perms = " -o #{perms.join(",")}" if !perms.empty? perms = " -o #{perms.join(",")}" if !perms.empty?
attempts = 0 attempts = 0

View File

@ -114,8 +114,8 @@ class ShareFoldersVMActionTest < Test::Unit::TestCase
context "mounting the shared folders" do context "mounting the shared folders" do
setup do setup do
@folders = stub_shared_folders(<<-sf) @folders = stub_shared_folders(<<-sf)
config.vm.share_folder("foo", "fooguest", "foohost") config.vm.share_folder("foo", "fooguest", "foohost", :owner => "yo", :group => "fo")
config.vm.share_folder("bar", "barguest", "barhost") config.vm.share_folder("bar", "barguest", "barhost", :owner => "foo", :group => "bar")
config.vm.share_folder("foo_no_mount", nil, "foohost2") config.vm.share_folder("foo_no_mount", nil, "foohost2")
sf sf
@ssh = mock("ssh") @ssh = mock("ssh")
@ -127,9 +127,9 @@ class ShareFoldersVMActionTest < Test::Unit::TestCase
mount_seq = sequence("mount_seq") mount_seq = sequence("mount_seq")
@folders.each do |name, data| @folders.each do |name, data|
if data[:guestpath] if data[:guestpath]
@vm.system.expects(:mount_shared_folder).with(@ssh, name, data[:guestpath]).in_sequence(mount_seq) @vm.system.expects(:mount_shared_folder).with(@ssh, name, data[:guestpath], data[:owner], data[:group]).in_sequence(mount_seq)
else else
@vm.system.expects(:mount_shared_folder).with(@ssh, name, anything).never @vm.system.expects(:mount_shared_folder).with(@ssh, name, anything, anything, anything).never
end end
end end

View File

@ -54,28 +54,6 @@ class ConfigVMTest < Test::Unit::TestCase
end end
end end
context "uid/gid" do
should "return the shared folder UID if set" do
@config.shared_folder_uid = "foo"
assert_equal "foo", @config.shared_folder_uid
end
should "return the SSH username if UID not set" do
@config.shared_folder_uid = nil
assert_equal @username, @config.shared_folder_uid
end
should "return the shared folder GID if set" do
@config.shared_folder_gid = "foo"
assert_equal "foo", @config.shared_folder_gid
end
should "return the SSH username if GID not set" do
@config.shared_folder_gid = nil
assert_equal @username, @config.shared_folder_gid
end
end
context "deprecated config" do context "deprecated config" do
should "raise an error for provisioner=" do should "raise an error for provisioner=" do
assert_raises(Vagrant::Errors::VagrantError) { assert_raises(Vagrant::Errors::VagrantError) {

View File

@ -31,15 +31,17 @@ class LinuxSystemTest < Test::Unit::TestCase
setup do setup do
@name = "foo" @name = "foo"
@guestpath = "/bar" @guestpath = "/bar"
@owner = "owner"
@group = "group"
end end
should "create the dir, mount the folder, then set permissions" do should "create the dir, mount the folder, then set permissions" do
mount_seq = sequence("mount_seq") mount_seq = sequence("mount_seq")
@ssh.expects(:exec!).with("sudo mkdir -p #{@guestpath}").in_sequence(mount_seq) @ssh.expects(:exec!).with("sudo mkdir -p #{@guestpath}").in_sequence(mount_seq)
@instance.expects(:mount_folder).with(@ssh, @name, @guestpath).in_sequence(mount_seq) @instance.expects(:mount_folder).with(@ssh, @name, @guestpath, @owner, @group).in_sequence(mount_seq)
@ssh.expects(:exec!).with("sudo chown #{@vm.env.config.ssh.username} #{@guestpath}").in_sequence(mount_seq) @ssh.expects(:exec!).with("sudo chown `id -u #{@owner}`:`id -g #{@group}` #{@guestpath}").in_sequence(mount_seq)
@instance.mount_shared_folder(@ssh, @name, @guestpath) @instance.mount_shared_folder(@ssh, @name, @guestpath, @owner, @group)
end end
end end
@ -50,6 +52,8 @@ class LinuxSystemTest < Test::Unit::TestCase
setup do setup do
@name = "foo" @name = "foo"
@guestpath = "bar" @guestpath = "bar"
@owner = "owner"
@group = "group"
@sleeptime = 0 @sleeptime = 0
@limit = 10 @limit = 10
@ -57,11 +61,11 @@ class LinuxSystemTest < Test::Unit::TestCase
end end
def mount_folder def mount_folder
@instance.mount_folder(@ssh, @name, @guestpath, @sleeptime) @instance.mount_folder(@ssh, @name, @guestpath, @owner, @group, @sleeptime)
end end
should "execute the proper mount command" do should "execute the proper mount command" do
@ssh.expects(:exec!).with("sudo mount -t vboxsf -o uid=`id -u #{@vm.env.config.ssh.username}`,gid=`id -g #{@vm.env.config.ssh.username}` #{@name} #{@guestpath}").returns(@success_return) @ssh.expects(:exec!).with("sudo mount -t vboxsf -o uid=`id -u #{@owner}`,gid=`id -g #{@group}` #{@name} #{@guestpath}").returns(@success_return)
mount_folder mount_folder
end end
@ -96,19 +100,5 @@ class LinuxSystemTest < Test::Unit::TestCase
mount_folder mount_folder
} }
end end
should "add uid AND gid to mount" do
uid = "foo"
gid = "bar"
env = vagrant_env(vagrantfile(<<-vf))
config.vm.shared_folder_uid = "#{uid}"
config.vm.shared_folder_gid = "#{gid}"
vf
@vm.stubs(:env).returns(env)
@ssh.expects(:exec!).with("sudo mount -t vboxsf -o uid=`id -u #{uid}`,gid=`id -g #{gid}` #{@name} #{@guestpath}").returns(@success_return)
mount_folder
end
end end
end end