From 837713c2d13139c8741607ee089dcc9fd25470f2 Mon Sep 17 00:00:00 2001 From: Seth Vargo Date: Sun, 5 Jun 2016 15:03:12 -0400 Subject: [PATCH] guests/linux: Mount NFS in one command --- plugins/guests/linux/cap/mount_nfs.rb | 28 ++++--- .../guests/linux/cap/mount_nfs_test.rb | 78 +++++++++++++++++++ 2 files changed, 94 insertions(+), 12 deletions(-) create mode 100644 test/unit/plugins/guests/linux/cap/mount_nfs_test.rb diff --git a/plugins/guests/linux/cap/mount_nfs.rb b/plugins/guests/linux/cap/mount_nfs.rb index 26dfb625df7..b823bf36c87 100644 --- a/plugins/guests/linux/cap/mount_nfs.rb +++ b/plugins/guests/linux/cap/mount_nfs.rb @@ -7,13 +7,17 @@ class MountNFS extend Vagrant::Util::Retryable def self.mount_nfs_folder(machine, ip, folders) + comm = machine.communicate + + commands = [] + folders.each do |name, opts| # Expand the guest path so we can handle things like "~/vagrant" expanded_guest_path = machine.guest.capability( :shell_expand_guest_path, opts[:guestpath]) # Do the actual creating and mounting - machine.communicate.sudo("mkdir -p #{expanded_guest_path}") + commands << "mkdir -p '#{expanded_guest_path}'" # Mount hostpath = opts[:hostpath].dup @@ -26,18 +30,18 @@ def self.mount_nfs_folder(machine, ip, folders) mount_opts = opts[:mount_options].dup end - mount_command = "mount -o '#{mount_opts.join(",")}' #{ip}:'#{hostpath}' #{expanded_guest_path}" - retryable(on: Vagrant::Errors::LinuxNFSMountFailed, tries: 8, sleep: 3) do - machine.communicate.sudo(mount_command, - error_class: Vagrant::Errors::LinuxNFSMountFailed) - end + commands << "mount -o #{mount_opts.join(",")} '#{ip}:#{hostpath}' '#{expanded_guest_path}'" + + # Emit a mount event + commands << <<-EOH.gsub(/^ {14}/, '') + if command -v /sbin/init &>/dev/null && /sbin/init --version | grep upstart &>/dev/null; then + /sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{expanded_guest_path}' + fi + EOH + end - # Emit an upstart event if we can - machine.communicate.sudo <<-SCRIPT -if command -v /sbin/init &>/dev/null && /sbin/init --version | grep upstart &>/dev/null; then - /sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{expanded_guest_path}' -fi -SCRIPT + retryable(on: Vagrant::Errors::LinuxNFSMountFailed, tries: 8, sleep: 3) do + comm.sudo(commands.join("\n"), error_class: Vagrant::Errors::LinuxNFSMountFailed) end end end diff --git a/test/unit/plugins/guests/linux/cap/mount_nfs_test.rb b/test/unit/plugins/guests/linux/cap/mount_nfs_test.rb new file mode 100644 index 00000000000..6a53c33edf6 --- /dev/null +++ b/test/unit/plugins/guests/linux/cap/mount_nfs_test.rb @@ -0,0 +1,78 @@ +require_relative "../../../../base" + +describe "VagrantPlugins::GuestLinux::Cap::MountNFS" do + let(:caps) do + VagrantPlugins::GuestLinux::Plugin + .components + .guest_capabilities[:linux] + end + + let(:machine) { double("machine") } + let(:comm) { VagrantTests::DummyCommunicator::Communicator.new(machine) } + + before do + allow(machine).to receive(:communicate).and_return(comm) + end + + after do + comm.verify_expectations! + end + + describe ".mount_nfs_folder" do + let(:cap) { caps.get(:mount_nfs_folder) } + + let(:ip) { "1.2.3.4" } + + let(:hostpath) { "/host" } + let(:guestpath) { "/guest" } + + before do + allow(machine).to receive(:guest).and_return( + double("capability", capability: guestpath) + ) + end + + it "mounts the folder" do + folders = { + "/vagrant-nfs" => { + type: :nfs, + guestpath: "/guest", + hostpath: "/host", + } + } + cap.mount_nfs_folder(machine, ip, folders) + + expect(comm.received_commands[0]).to match(/mkdir -p '#{guestpath}'/) + expect(comm.received_commands[0]).to match(/'1.2.3.4:#{hostpath}' '#{guestpath}'/) + end + + it "mounts with options" do + folders = { + "/vagrant-nfs" => { + type: :nfs, + guestpath: "/guest", + hostpath: "/host", + nfs_version: 2, + nfs_udp: true, + } + } + cap.mount_nfs_folder(machine, ip, folders) + + expect(comm.received_commands[0]).to match(/mount -o vers=2,udp/) + end + + it "emits an event" do + folders = { + "/vagrant-nfs" => { + type: :nfs, + guestpath: "/guest", + hostpath: "/host", + } + } + cap.mount_nfs_folder(machine, ip, folders) + + expect(comm.received_commands[0]).to include( + "/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT='#{guestpath}'") + end + end +end