2013-04-04 06:46:39 +00:00
|
|
|
require "vagrant/util/retryable"
|
|
|
|
|
2013-04-04 06:33:20 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module GuestLinux
|
|
|
|
module Cap
|
2016-06-24 23:08:58 +00:00
|
|
|
class NFS
|
2013-04-04 06:46:39 +00:00
|
|
|
extend Vagrant::Util::Retryable
|
|
|
|
|
2016-06-24 23:08:58 +00:00
|
|
|
def self.nfs_client_installed(machine)
|
|
|
|
machine.communicate.test("test -x /sbin/mount.nfs")
|
|
|
|
end
|
|
|
|
|
2013-04-04 06:46:39 +00:00
|
|
|
def self.mount_nfs_folder(machine, ip, folders)
|
2016-06-05 19:03:12 +00:00
|
|
|
comm = machine.communicate
|
|
|
|
|
2016-10-24 17:06:59 +00:00
|
|
|
# Mount each folder separately so we can retry.
|
2013-04-04 06:33:20 +00:00
|
|
|
folders.each do |name, opts|
|
2016-06-19 17:39:21 +00:00
|
|
|
# Shellescape the paths in case they do not have special characters.
|
|
|
|
guest_path = Shellwords.escape(opts[:guestpath])
|
|
|
|
host_path = Shellwords.escape(opts[:hostpath])
|
2013-04-04 06:33:20 +00:00
|
|
|
|
2016-06-19 17:39:21 +00:00
|
|
|
# Build the list of mount options.
|
|
|
|
mount_opts = []
|
|
|
|
mount_opts << "vers=#{opts[:nfs_version]}" if opts[:nfs_version]
|
2013-11-23 21:43:48 +00:00
|
|
|
mount_opts << "udp" if opts[:nfs_udp]
|
2013-09-01 19:25:21 +00:00
|
|
|
if opts[:mount_options]
|
2016-06-19 17:39:21 +00:00
|
|
|
mount_opts = mount_opts + opts[:mount_options].dup
|
2013-09-01 19:25:21 +00:00
|
|
|
end
|
2016-06-19 17:39:21 +00:00
|
|
|
mount_opts = mount_opts.join(",")
|
|
|
|
|
2016-10-24 17:06:59 +00:00
|
|
|
machine.communicate.sudo("mkdir -p #{guest_path}")
|
|
|
|
|
|
|
|
# Perform the mount operation and emit mount event if applicable
|
|
|
|
command = <<-EOH.gsub(/^ */, '')
|
|
|
|
mount -o #{mount_opts} #{ip}:#{host_path} #{guest_path}
|
|
|
|
result=$?
|
|
|
|
if test $result -eq 0; then
|
2016-11-29 07:10:25 +00:00
|
|
|
if test -x /sbin/initctl && command -v /sbin/init && /sbin/init 2>/dev/null --version | grep upstart; then
|
2016-10-24 17:06:59 +00:00
|
|
|
/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT=#{guest_path}
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
exit $result
|
2016-06-05 19:03:12 +00:00
|
|
|
fi
|
|
|
|
EOH
|
2013-12-22 11:46:24 +00:00
|
|
|
|
2016-06-19 17:39:21 +00:00
|
|
|
# Run the command, raising a specific error.
|
|
|
|
retryable(on: Vagrant::Errors::NFSMountFailed, tries: 3, sleep: 5) do
|
2016-10-24 17:06:59 +00:00
|
|
|
machine.communicate.sudo(command,
|
2016-06-19 17:39:21 +00:00
|
|
|
error_class: Vagrant::Errors::NFSMountFailed,
|
|
|
|
)
|
|
|
|
end
|
2013-04-04 06:33:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|