From fc66ec1660e897020f075ed1d3f22fb2ba2a43e8 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Sat, 28 Dec 2013 17:01:08 -0600 Subject: [PATCH] providers/virtualbox: allow and prefer static guest IPs for NFS Since vbox guest properties are proving to be less reliable than we had hoped, bring back the static config parsing mechanism for finding a guest IP to hand to NFS. If we find a static IP (or set of IPs) we'll use that instead of trying to probe guest properties. This retains NFS support for DHCP interfaces while regaining the reliability that we previously had when static IPs were required. --- .../virtualbox/action/prepare_nfs_settings.rb | 26 ++++++++++++++++--- .../action/prepare_nfs_settings_test.rb | 25 ++++++++++++------ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/plugins/providers/virtualbox/action/prepare_nfs_settings.rb b/plugins/providers/virtualbox/action/prepare_nfs_settings.rb index f6e9ba5cf..857da8f4e 100644 --- a/plugins/providers/virtualbox/action/prepare_nfs_settings.rb +++ b/plugins/providers/virtualbox/action/prepare_nfs_settings.rb @@ -34,8 +34,7 @@ module VagrantPlugins # The ! indicates that this method modifies its argument. def add_ips_to_env!(env) adapter, host_ip = find_host_only_adapter - machine_ip = nil - machine_ip = read_machine_ip(adapter) if adapter + machine_ip = read_static_machine_ips || read_dynamic_machine_ip(adapter) raise Vagrant::Errors::NFSNoHostonlyNetwork if !host_ip || !machine_ip @@ -61,6 +60,25 @@ module VagrantPlugins nil end + # Returns the IP address(es) of the guest by looking for static IPs + # given to host only adapters in the Vagrantfile + # + # @return [Array] Configured static IPs + def read_static_machine_ips + ips = [] + @machine.config.vm.networks.each do |type, options| + if type == :private_network && options[:ip].is_a?(String) + ips << options[:ip] + end + end + + if ips.empty? + return nil + end + + ips + end + # Returns the IP address of the guest by looking at vbox guest property # for the appropriate guest adapter. # @@ -69,7 +87,9 @@ module VagrantPlugins # # @param [Integer] adapter number to read IP for # @return [String] ip address of adapter - def read_machine_ip(adapter) + def read_dynamic_machine_ip(adapter) + return nil unless adapter + # vbox guest properties are 0-indexed, while showvminfo network # interfaces are 1-indexed. go figure. guestproperty_adapter = adapter - 1 diff --git a/test/unit/plugins/providers/virtualbox/action/prepare_nfs_settings_test.rb b/test/unit/plugins/providers/virtualbox/action/prepare_nfs_settings_test.rb index 03223fa1a..c3f3e6016 100644 --- a/test/unit/plugins/providers/virtualbox/action/prepare_nfs_settings_test.rb +++ b/test/unit/plugins/providers/virtualbox/action/prepare_nfs_settings_test.rb @@ -51,6 +51,10 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do {name: "vmnet2", ip: "1.2.3.4"}, ]) driver.stub(:read_guest_ip).with(1).and_return("2.3.4.5") + + # override sleep to 0 so test does not take seconds + retry_options = subject.retry_options + subject.stub(:retry_options).and_return(retry_options.merge(sleep: 0)) end it "sets nfs_host_ip and nfs_machine_ip properly" do @@ -74,10 +78,6 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do ] driver.stub(:read_guest_ip) { raise_then_return.shift.call } - # override sleep to 0 so test does not take seconds - retry_options = subject.retry_options - subject.stub(:retry_options).and_return(retry_options.merge(sleep: 0)) - subject.call(env) env[:nfs_host_ip].should == "1.2.3.4" @@ -89,12 +89,21 @@ describe VagrantPlugins::ProviderVirtualBox::Action::PrepareNFSSettings do raise Vagrant::Errors::VirtualBoxGuestPropertyNotFound, :guest_property => 'stub' } - # override sleep to 0 so test does not take seconds - retry_options = subject.retry_options - subject.stub(:retry_options).and_return(retry_options.merge(sleep: 0)) - expect { subject.call(env) }. to raise_error(Vagrant::Errors::NFSNoGuestIP) end + + it "allows statically configured guest IPs to work for NFS, even when guest property would fail" do + env[:machine].config.vm.network :private_network, ip: "11.12.13.14" + + driver.stub(:read_guest_ip) { + raise Vagrant::Errors::VirtualBoxGuestPropertyNotFound, :guest_property => "stub" + } + + subject.call(env) + + env[:nfs_host_ip].should == "1.2.3.4" + env[:nfs_machine_ip].should == ["11.12.13.14"] + end end end