Class: VagrantPlugins::ProviderVeertu::Action::PrepareNFSSettings

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Action::Builtin::MixinSyncedFolders, Vagrant::Util::Retryable
Defined in:
lib/vagrant-veertu/action/prepare_nfs_settings.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ PrepareNFSSettings

Returns a new instance of PrepareNFSSettings.



10
11
12
13
# File 'lib/vagrant-veertu/action/prepare_nfs_settings.rb', line 10

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

Instance Method Details

#add_ips_to_env!(env) ⇒ Object

Extracts the proper host and guest IPs for NFS mounts and stores them in the environment for the SyncedFolder action to use them in mounting.

The ! indicates that this method modifies its argument.

Raises:

  • (Vagrant::Errors::NFSNoHostonlyNetwork)


38
39
40
41
42
43
44
45
46
# File 'lib/vagrant-veertu/action/prepare_nfs_settings.rb', line 38

def add_ips_to_env!(env)
  adapter, host_ip = find_host_only_adapter
  machine_ip       = read_static_machine_ips || read_dynamic_machine_ip(adapter)

  raise Vagrant::Errors::NFSNoHostonlyNetwork if !host_ip || !machine_ip

  env[:nfs_host_ip]    = host_ip
  env[:nfs_machine_ip] = machine_ip
end

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vagrant-veertu/action/prepare_nfs_settings.rb', line 15

def call(env)
  @machine = env[:machine]

  @app.call(env)

  opts = {
    cached: !!env[:synced_folders_cached],
    config: env[:synced_folders_config],
    disable_usable_check: !!env[:test],
  }
  folders = synced_folders(env[:machine], **opts)

  if folders.key?(:nfs)
    @logger.info("Using NFS, preparing NFS settings by reading host IP and machine IP")
    add_ips_to_env!(env)
  end
end

#find_host_only_adapterInteger, String

Finds first host only network adapter and returns its adapter number and IP address

Returns:

  • (Integer, String)

    adapter number, ip address of found host-only adapter



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/vagrant-veertu/action/prepare_nfs_settings.rb', line 52

def find_host_only_adapter
  @machine.provider.driver.read_network_interfaces.each do |adapter, opts|
    if opts[:type] == :hostonly
      @machine.provider.driver.read_host_only_interfaces.each do |interface|
        if interface[:name] == opts[:hostonly]
          return adapter, interface[:ip]
        end
      end
    end
  end

  nil
end

#read_dynamic_machine_ip(adapter) ⇒ String

Returns the IP address of the guest by looking at veertu guest property for the appropriate guest adapter.

For DHCP interfaces, the guest property will not be present until the guest completes

Parameters:

  • adapter (Integer)

    number to read IP for

Returns:

  • (String)

    ip address of adapter



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vagrant-veertu/action/prepare_nfs_settings.rb', line 93

def read_dynamic_machine_ip(adapter)
  return nil unless adapter

  # veertu guest properties are 0-indexed, while showvminfo network
  # interfaces are 1-indexed. go figure.
  guestproperty_adapter = adapter - 1

  # we need to wait for the guest's IP to show up as a guest property.
  # retry thresholds are relatively high since we might need to wait
  # for DHCP, but even static IPs can take a second or two to appear.
  retryable(retry_options.merge(on: Vagrant::Errors::VeertuGuestPropertyNotFound)) do
    @machine.provider.driver.read_guest_ip(guestproperty_adapter)
  end
rescue Vagrant::Errors::VeertuGuestPropertyNotFound
  # this error is more specific with a better error message directing
  # the user towards the fact that it's probably a reportable bug
  raise Vagrant::Errors::NFSNoGuestIP
end

#read_static_machine_ipsArray

Returns the IP address(es) of the guest by looking for static IPs given to host only adapters in the Vagrantfile

Returns:

  • (Array)

    <String> Configured static IPs



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vagrant-veertu/action/prepare_nfs_settings.rb', line 70

def read_static_machine_ips
  ips = []
  @machine.config.vm.networks.each do |type, options|
    if type == :private_network && options[:type] != :dhcp && options[:ip].is_a?(String)
      ips << options[:ip]
    end
  end

  if ips.empty?
    return nil
  end

  ips
end

#retry_optionsObject

Separating these out so we can stub out the sleep in tests



113
114
115
# File 'lib/vagrant-veertu/action/prepare_nfs_settings.rb', line 113

def retry_options
  {tries: 15, sleep: 1}
end