Class: Vagrant::Action::Builtin::NFS

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/action/builtin/nfs.rb

Overview

This built-in middleware exports and mounts NFS shared folders.

To use this middleware, two configuration parameters must be given via the environment hash:

- `:nfs_host_ip` - The IP of where to mount the NFS folder from.
- `:nfs_machine_ip` - The IP of the machine where the NFS folder
  will be mounted.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ NFS

Returns a new instance of NFS.



22
23
24
25
# File 'lib/vagrant/action/builtin/nfs.rb', line 22

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant::action::builtin::nfs")
end

Instance Method Details

#call(env) ⇒ Object



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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vagrant/action/builtin/nfs.rb', line 27

def call(env)
  # We forward things along first. We do everything on the tail
  # end of the middleware call.
  @app.call(env)

  # Used by prepare_permission, so we need to save it
  @env = env

  folders = {}
  env[:machine].config.vm.synced_folders.each do |id, opts|
    # If this synced folder doesn't enable NFS, ignore it.
    next if !opts[:nfs]

    # Expand the host path, create it if we have to and
    # store away the folder.
    hostpath = Pathname.new(opts[:hostpath]).
      expand_path(env[:root_path])

    if !hostpath.directory? && opts[:create]
      # Host path doesn't exist, so let's create it.
      @logger.debug("Host path doesn't exist, creating: #{hostpath}")

      begin
        FileUtils.mkpath(hostpath)
      rescue Errno::EACCES
        raise Vagrant::Errors::SharedFolderCreateFailed,
          :path => hostpath.to_s
      end
    end

    # Determine the real path by expanding symlinks and getting
    # proper casing. We have to do this after creating it
    # if it doesn't exist.
    hostpath = hostpath.realpath
    hostpath = Util::Platform.fs_real_path(hostpath)

    # Set the hostpath back on the options and save it
    opts[:hostpath] = hostpath.to_s
    folders[id] = opts
  end

  if !folders.empty?
    raise Errors::NFSNoHostIP if !env[:nfs_host_ip]
    raise Errors::NFSNoGuestIP if !env[:nfs_machine_ip]

    machine_ip = env[:nfs_machine_ip]
    machine_ip = [machine_ip] if !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(opts) }

    # Export the folders
    env[:ui].info I18n.t("vagrant.actions.vm.nfs.exporting")
    env[:host].nfs_export(env[:machine].id, machine_ip, folders)

    # Mount
    env[: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|
      mount_folders[id] = opts.dup if opts[:guestpath]
    end

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