Class: VagrantWinNFSd::SyncedFolder

Inherits:
VagrantPlugins::SyncedFolderNFS::SyncedFolder
  • Object
show all
Defined in:
lib/vagrant-winnfsd/synced_folder.rb

Instance Method Summary collapse

Instance Method Details

#enable(machine, folders, nfsopts) ⇒ Object

Raises:

  • (Vagrant::Errors::NFSNoHostIP)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vagrant-winnfsd/synced_folder.rb', line 6

def enable(machine, folders, nfsopts)
  raise Vagrant::Errors::NFSNoHostIP unless nfsopts[:nfs_host_ip]
  raise Vagrant::Errors::NFSNoGuestIP unless nfsopts[:nfs_machine_ip]

  if machine.guest.capability?(:nfs_client_installed)
    installed = machine.guest.capability(:nfs_client_installed)
    unless installed
      can_install = machine.guest.capability?(:nfs_client_install)
      raise Vagrant::Errors::NFSClientNotInstalledInGuest unless can_install
      machine.ui.info I18n.t("vagrant.actions.vm.nfs.installing")
      machine.guest.capability(:nfs_client_install)
    end
  end

  machine_ip = nfsopts[:nfs_machine_ip]
  machine_ip = [machine_ip] unless machine_ip.is_a?(Array)

  # Prepare the folder, this means setting up various options
  # and such on the folder itself.
  folders.each { |id, opts| prepare_folder(machine, opts) }

  # Determine what folders we'll export
  export_folders = folders.dup
  export_folders.keys.each do |id|
    opts = export_folders[id]
    if opts.has_key?(:nfs_export) && !opts[:nfs_export]
      export_folders.delete(id)
    end
  end

  # Export the folders. We do this with a class-wide lock because
  # NFS exporting often requires sudo privilege and we don't want
  # overlapping input requests. [GH-2680]
  @@lock.synchronize do
    begin
      machine.env.lock("nfs-export") do
        machine.ui.info I18n.t("vagrant.actions.vm.nfs.exporting")
        machine.env.host.capability(
          :nfs_export,
          machine.ui, machine.id, machine_ip, export_folders)
      end
    rescue Vagrant::Errors::EnvironmentLockedError
      sleep 1
      retry
    end
  end

  # Mount
  machine.ui.info I18n.t("vagrant.actions.vm.nfs.mounting")

  # Only mount folders that have a guest path specified.
  mount_folders = {}
  folders.each do |id, opts|
    if Vagrant::Util::Platform.windows?
      unless opts[:mount_options]
        mount_opts = ["vers=#{opts[:nfs_version]}"]
        mount_opts << "udp" if opts[:nfs_udp]
        mount_opts << "nolock"

        opts[:mount_options] = mount_opts
      end

      opts[:hostpath] = '/' + opts[:hostpath].gsub(':', '').gsub('\\', '/')
    end
    mount_folders[id] = opts.dup if opts[:guestpath]
  end

  # Mount them!
  machine.guest.capability(
    :mount_nfs_folder, nfsopts[:nfs_host_ip], mount_folders)
end