2014-10-22 16:26:38 +00:00
|
|
|
require "vagrant/action/builtin/mixin_synced_folders"
|
|
|
|
|
2013-02-08 23:34:04 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module ProviderVirtualBox
|
|
|
|
module Action
|
|
|
|
class PrepareNFSSettings
|
2014-10-22 16:26:38 +00:00
|
|
|
include Vagrant::Action::Builtin::MixinSyncedFolders
|
2013-11-22 18:26:36 +00:00
|
|
|
include Vagrant::Util::Retryable
|
|
|
|
|
2013-12-14 06:03:14 +00:00
|
|
|
def initialize(app, env)
|
2013-02-08 23:34:04 +00:00
|
|
|
@app = app
|
|
|
|
@logger = Log4r::Logger.new("vagrant::action::vm::nfs")
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2013-11-22 18:26:36 +00:00
|
|
|
@machine = env[:machine]
|
2013-12-14 06:03:14 +00:00
|
|
|
|
2013-02-08 23:34:04 +00:00
|
|
|
@app.call(env)
|
|
|
|
|
2014-10-22 16:26:38 +00:00
|
|
|
opts = {
|
|
|
|
cached: !!env[:synced_folders_cached],
|
|
|
|
config: env[:synced_folders_config],
|
|
|
|
}
|
|
|
|
folders = synced_folders(env[:machine], **opts)
|
|
|
|
|
|
|
|
if folders.has_key?(:nfs)
|
2013-11-22 18:26:36 +00:00
|
|
|
@logger.info("Using NFS, preparing NFS settings by reading host IP and machine IP")
|
2013-12-14 06:03:14 +00:00
|
|
|
add_ips_to_env!(env)
|
2013-03-18 20:21:06 +00:00
|
|
|
end
|
2013-11-22 18:26:36 +00:00
|
|
|
end
|
2013-03-18 05:33:02 +00:00
|
|
|
|
2013-11-22 18:26:36 +00:00
|
|
|
# We're using NFS if we have any synced folder with NFS configured. If
|
|
|
|
# we are not using NFS we don't need to do the extra work to
|
|
|
|
# populate these fields in the environment.
|
|
|
|
def using_nfs?
|
|
|
|
@machine.config.vm.synced_folders.any? { |_, opts| opts[:type] == :nfs }
|
|
|
|
end
|
2013-03-18 20:21:06 +00:00
|
|
|
|
2013-11-22 18:26:36 +00:00
|
|
|
# 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.
|
2013-12-14 06:03:14 +00:00
|
|
|
def add_ips_to_env!(env)
|
2013-11-22 18:26:36 +00:00
|
|
|
adapter, host_ip = find_host_only_adapter
|
2013-12-28 23:01:08 +00:00
|
|
|
machine_ip = read_static_machine_ips || read_dynamic_machine_ip(adapter)
|
2013-11-22 18:26:36 +00:00
|
|
|
|
|
|
|
raise Vagrant::Errors::NFSNoHostonlyNetwork if !host_ip || !machine_ip
|
|
|
|
|
|
|
|
env[:nfs_host_ip] = host_ip
|
|
|
|
env[:nfs_machine_ip] = machine_ip
|
2013-02-08 23:34:04 +00:00
|
|
|
end
|
|
|
|
|
2013-11-22 18:26:36 +00:00
|
|
|
# Finds first host only network adapter and returns its adapter number
|
|
|
|
# and IP address
|
2013-02-08 23:34:04 +00:00
|
|
|
#
|
2013-11-22 18:26:36 +00:00
|
|
|
# @return [Integer, String] adapter number, ip address of found host-only adapter
|
|
|
|
def find_host_only_adapter
|
|
|
|
@machine.provider.driver.read_network_interfaces.each do |adapter, opts|
|
2013-02-08 23:34:04 +00:00
|
|
|
if opts[:type] == :hostonly
|
2013-11-22 18:26:36 +00:00
|
|
|
@machine.provider.driver.read_host_only_interfaces.each do |interface|
|
2013-02-08 23:34:04 +00:00
|
|
|
if interface[:name] == opts[:hostonly]
|
2013-11-22 18:26:36 +00:00
|
|
|
return adapter, interface[:ip]
|
2013-02-08 23:34:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2013-12-28 23:01:08 +00:00
|
|
|
# Returns the IP address(es) of the guest by looking for static IPs
|
|
|
|
# given to host only adapters in the Vagrantfile
|
|
|
|
#
|
|
|
|
# @return [Array]<String> Configured static IPs
|
|
|
|
def read_static_machine_ips
|
|
|
|
ips = []
|
|
|
|
@machine.config.vm.networks.each do |type, options|
|
2014-09-04 21:20:03 +00:00
|
|
|
if type == :private_network && options[:type] != :dhcp && options[:ip].is_a?(String)
|
2013-12-28 23:01:08 +00:00
|
|
|
ips << options[:ip]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if ips.empty?
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
ips
|
|
|
|
end
|
|
|
|
|
2013-11-22 18:26:36 +00:00
|
|
|
# Returns the IP address of the guest by looking at vbox guest property
|
|
|
|
# for the appropriate guest adapter.
|
2013-02-08 23:34:04 +00:00
|
|
|
#
|
2013-11-22 18:26:36 +00:00
|
|
|
# For DHCP interfaces, the guest property will not be present until the
|
|
|
|
# guest completes
|
|
|
|
#
|
|
|
|
# @param [Integer] adapter number to read IP for
|
|
|
|
# @return [String] ip address of adapter
|
2013-12-28 23:01:08 +00:00
|
|
|
def read_dynamic_machine_ip(adapter)
|
|
|
|
return nil unless adapter
|
|
|
|
|
2013-11-22 18:26:36 +00:00
|
|
|
# vbox guest properties are 0-indexed, while showvminfo network
|
|
|
|
# interfaces are 1-indexed. go figure.
|
|
|
|
guestproperty_adapter = adapter - 1
|
2013-02-08 23:34:04 +00:00
|
|
|
|
2013-11-22 18:26:36 +00:00
|
|
|
# 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)
|
2013-07-18 04:01:49 +00:00
|
|
|
end
|
2013-11-22 18:26:36 +00:00
|
|
|
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
|
2013-07-18 04:01:49 +00:00
|
|
|
|
2013-11-22 18:26:36 +00:00
|
|
|
# Separating these out so we can stub out the sleep in tests
|
|
|
|
def retry_options
|
|
|
|
{tries: 15, sleep: 1}
|
2013-02-08 23:34:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|