Module: VagrantPlugins::SyncedFolderNFSGuest::ProviderVirtualBox::Cap

Extended by:
Vagrant::Util::Retryable
Defined in:
lib/vagrant-nfs_guest/providers/virtualbox/cap/nfs_settings.rb

Class Method Summary collapse

Class Method Details

.find_host_only_adapter(machine) ⇒ Integer, 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



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/vagrant-nfs_guest/providers/virtualbox/cap/nfs_settings.rb', line 19

def self.find_host_only_adapter(machine)
  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

.nfs_settings(machine) ⇒ Object



9
10
11
12
13
# File 'lib/vagrant-nfs_guest/providers/virtualbox/cap/nfs_settings.rb', line 9

def self.nfs_settings(machine)
  adapter, host_ip = self.find_host_only_adapter(machine)
  machine_ip       = self.read_static_machine_ips(machine) || self.read_dynamic_machine_ip(machine, adapter)
  return host_ip, machine_ip
end

.read_dynamic_machine_ip(machine, adapter) ⇒ String

Returns the IP address of the guest by looking at vbox 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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vagrant-nfs_guest/providers/virtualbox/cap/nfs_settings.rb', line 60

def self.read_dynamic_machine_ip(machine, adapter)
  return nil unless adapter

  # vbox 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::VirtualBoxGuestPropertyNotFound)) do
    machine.provider.driver.read_guest_ip(guestproperty_adapter)
  end
rescue Vagrant::Errors::VirtualBoxGuestPropertyNotFound
  # 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_ips(machine) ⇒ Array

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/vagrant-nfs_guest/providers/virtualbox/cap/nfs_settings.rb', line 37

def self.read_static_machine_ips(machine)
  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



80
81
82
# File 'lib/vagrant-nfs_guest/providers/virtualbox/cap/nfs_settings.rb', line 80

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