From 8733a745ce9cce57117a497be116f50bbb1a63b7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 5 Jul 2011 23:53:15 -0700 Subject: [PATCH] Can now specify owner/group of shared folders. [closes GH-350] --- CHANGELOG.md | 1 + config/default.rb | 2 -- lib/vagrant/action/vm/share_folders.rb | 8 +++++- lib/vagrant/config/vm.rb | 14 +++------- lib/vagrant/systems/base.rb | 2 +- lib/vagrant/systems/linux.rb | 12 ++++----- test/vagrant/action/vm/share_folders_test.rb | 8 +++--- test/vagrant/config/vm_test.rb | 22 --------------- test/vagrant/systems/linux_test.rb | 28 +++++++------------- 9 files changed, 31 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59d727b87..ac70a3d23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - New `:inline` option for shell provisioner to provide inline scripts as a string. [GH-395] - 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) diff --git a/config/default.rb b/config/default.rb index 6ed5e63e7..b977c99ba 100644 --- a/config/default.rb +++ b/config/default.rb @@ -18,8 +18,6 @@ Vagrant::Config.run do |config| config.vm.base_mac = nil config.vm.forward_port("ssh", 22, 2222, :auto => true) 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.system = :linux diff --git a/lib/vagrant/action/vm/share_folders.rb b/lib/vagrant/action/vm/share_folders.rb index 197334607..8296250dc 100644 --- a/lib/vagrant/action/vm/share_folders.rb +++ b/lib/vagrant/action/vm/share_folders.rb @@ -55,7 +55,13 @@ module Vagrant @env.ui.info(I18n.t("vagrant.actions.vm.share_folders.mounting_entry", :name => name, :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 # If no guest path is specified, then automounting is disabled @env.ui.info(I18n.t("vagrant.actions.vm.share_folders.nomount_entry", diff --git a/lib/vagrant/config/vm.rb b/lib/vagrant/config/vm.rb index c4e4d7d6f..278f126ca 100644 --- a/lib/vagrant/config/vm.rb +++ b/lib/vagrant/config/vm.rb @@ -20,8 +20,6 @@ module Vagrant attr_reader :network_options attr_reader :provisioners attr_accessor :disk_image_format - attr_writer :shared_folder_uid - attr_writer :shared_folder_gid attr_accessor :system def initialize @@ -46,7 +44,9 @@ module Vagrant def share_folder(name, guestpath, hostpath, opts=nil) @shared_folders[name] = { :guestpath => guestpath, - :hostpath => hostpath + :hostpath => hostpath, + :owner => nil, + :group => nil }.merge(opts || {}) end @@ -73,14 +73,6 @@ module Vagrant raise Errors::VagrantError, :_key => :provisioner_equals_not_supported 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) push_proc(&block) end diff --git a/lib/vagrant/systems/base.rb b/lib/vagrant/systems/base.rb index 34b38500d..1e0729df1 100644 --- a/lib/vagrant/systems/base.rb +++ b/lib/vagrant/systems/base.rb @@ -60,7 +60,7 @@ module Vagrant # @param [String] name The name of the shared folder. # @param [String] guestpath The path on the machine which the user # 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 # via the host are already done. diff --git a/lib/vagrant/systems/linux.rb b/lib/vagrant/systems/linux.rb index 592cc5bfa..85400d304 100644 --- a/lib/vagrant/systems/linux.rb +++ b/lib/vagrant/systems/linux.rb @@ -38,10 +38,10 @@ module Vagrant end end - def mount_shared_folder(ssh, name, guestpath) + def mount_shared_folder(ssh, name, guestpath, owner, group) ssh.exec!("sudo mkdir -p #{guestpath}") - mount_folder(ssh, name, guestpath) - ssh.exec!("sudo chown #{vm.env.config.ssh.username} #{guestpath}") + mount_folder(ssh, name, guestpath, owner, group) + ssh.exec!("sudo chown `id -u #{owner}`:`id -g #{group}` #{guestpath}") end def mount_nfs(ip, folders) @@ -58,11 +58,11 @@ module Vagrant #------------------------------------------------------------------- # "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 perms = [] - perms << "uid=`id -u #{vm.env.config.vm.shared_folder_uid}`" - perms << "gid=`id -g #{vm.env.config.vm.shared_folder_gid}`" + perms << "uid=`id -u #{owner}`" + perms << "gid=`id -g #{group}`" perms = " -o #{perms.join(",")}" if !perms.empty? attempts = 0 diff --git a/test/vagrant/action/vm/share_folders_test.rb b/test/vagrant/action/vm/share_folders_test.rb index 9b9de8878..58ac1ccc3 100644 --- a/test/vagrant/action/vm/share_folders_test.rb +++ b/test/vagrant/action/vm/share_folders_test.rb @@ -114,8 +114,8 @@ class ShareFoldersVMActionTest < Test::Unit::TestCase context "mounting the shared folders" do setup do @folders = stub_shared_folders(<<-sf) - config.vm.share_folder("foo", "fooguest", "foohost") - config.vm.share_folder("bar", "barguest", "barhost") + config.vm.share_folder("foo", "fooguest", "foohost", :owner => "yo", :group => "fo") + config.vm.share_folder("bar", "barguest", "barhost", :owner => "foo", :group => "bar") config.vm.share_folder("foo_no_mount", nil, "foohost2") sf @ssh = mock("ssh") @@ -127,9 +127,9 @@ class ShareFoldersVMActionTest < Test::Unit::TestCase mount_seq = sequence("mount_seq") @folders.each do |name, data| 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 - @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 diff --git a/test/vagrant/config/vm_test.rb b/test/vagrant/config/vm_test.rb index 4106ab5b6..3370213f6 100644 --- a/test/vagrant/config/vm_test.rb +++ b/test/vagrant/config/vm_test.rb @@ -54,28 +54,6 @@ class ConfigVMTest < Test::Unit::TestCase 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 should "raise an error for provisioner=" do assert_raises(Vagrant::Errors::VagrantError) { diff --git a/test/vagrant/systems/linux_test.rb b/test/vagrant/systems/linux_test.rb index 16b44d390..6df77f823 100644 --- a/test/vagrant/systems/linux_test.rb +++ b/test/vagrant/systems/linux_test.rb @@ -31,15 +31,17 @@ class LinuxSystemTest < Test::Unit::TestCase setup do @name = "foo" @guestpath = "/bar" + @owner = "owner" + @group = "group" end should "create the dir, mount the folder, then set permissions" do mount_seq = 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) - @ssh.expects(:exec!).with("sudo chown #{@vm.env.config.ssh.username} #{@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 `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 @@ -50,6 +52,8 @@ class LinuxSystemTest < Test::Unit::TestCase setup do @name = "foo" @guestpath = "bar" + @owner = "owner" + @group = "group" @sleeptime = 0 @limit = 10 @@ -57,11 +61,11 @@ class LinuxSystemTest < Test::Unit::TestCase end def mount_folder - @instance.mount_folder(@ssh, @name, @guestpath, @sleeptime) + @instance.mount_folder(@ssh, @name, @guestpath, @owner, @group, @sleeptime) end 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 end @@ -96,19 +100,5 @@ class LinuxSystemTest < Test::Unit::TestCase mount_folder } 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