vagrant/plugins/guests/linux/cap/nfs.rb

58 lines
1.9 KiB
Ruby
Raw Normal View History

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
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
def self.mount_nfs_folder(machine, ip, folders)
2016-06-05 19:03:12 +00:00
comm = machine.communicate
2013-04-04 06:33:20 +00:00
folders.each do |name, opts|
# Mount each folder separately so we can retry.
commands = ["set -e"]
2013-04-04 06:33:20 +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
# Build the list of mount options.
mount_opts = []
mount_opts << "vers=#{opts[:nfs_version]}" if opts[:nfs_version]
mount_opts << "udp" if opts[:nfs_udp]
if opts[:mount_options]
mount_opts = mount_opts + opts[:mount_options].dup
end
mount_opts = mount_opts.join(",")
# Make the directory on the guest.
commands << "mkdir -p #{guest_path}"
# Perform the mount operation.
commands << "mount -o #{mount_opts} #{ip}:#{host_path} #{guest_path}"
2016-06-05 19:03:12 +00:00
# Emit a mount event
commands << <<-EOH.gsub(/^ {14}/, '')
2016-09-20 20:57:54 +00:00
if test -x /sbin/initctl && command -v /sbin/init && /sbin/init --version | grep upstart; then
/sbin/initctl emit --no-wait vagrant-mounted MOUNTPOINT=#{guest_path}
2016-06-05 19:03:12 +00:00
fi
EOH
# Run the command, raising a specific error.
retryable(on: Vagrant::Errors::NFSMountFailed, tries: 3, sleep: 5) do
machine.communicate.sudo(commands.join("\n"),
error_class: Vagrant::Errors::NFSMountFailed,
)
end
2013-04-04 06:33:20 +00:00
end
end
end
end
end
end