Change mount_nfs_folder cap to be more like the linux version, adding a

bunch of retires rather than a long sleep.

Add DarwinNFSMountFailed error. This might move be more appropriate at
the plugin level.

Integrate some of tvsutton's work in configure_networks to get the
implementation closer to working in both fusion and virtualbox.

Add shell_expand_guest_path capability (also copied from linux)
This commit is contained in:
Brian Johnson 2013-08-05 17:39:44 -07:00
parent 5606aa8b1e
commit 3611ff39f4
5 changed files with 128 additions and 77 deletions

View File

@ -183,6 +183,10 @@ module Vagrant
error_key(:copy_private_key_failed) error_key(:copy_private_key_failed)
end end
class DarwinNFSMountFailed < VagrantError
error_key(:darwin_nfs_mount_failed)
end
class DestroyRequiresForce < VagrantError class DestroyRequiresForce < VagrantError
error_key(:destroy_requires_force) error_key(:destroy_requires_force)
end end

View File

@ -13,35 +13,42 @@ module VagrantPlugins
# rather than templating the files. # rather than templating the files.
machine.communicate.sudo("networksetup -detectnewhardware") machine.communicate.sudo("networksetup -detectnewhardware")
devmap = {}
machine.communicate.sudo("networksetup -listnetworkserviceorder > /tmp/vagrant.interfaces") machine.communicate.sudo("networksetup -listnetworkserviceorder > /tmp/vagrant.interfaces")
tmpints = File.join(Dir.tmpdir, "#{machine.id}.interfaces") tmpints = File.join(Dir.tmpdir, File.basename("#{machine.id}.interfaces"))
machine.communicate.download("/tmp/vagrant.interfaces",tmpints) machine.communicate.download("/tmp/vagrant.interfaces",tmpints)
devlist = []
ints = IO.read(tmpints) ints = IO.read(tmpints)
ints.split(/\n\n/m).each do |i| ints.split(/\n\n/m).each do |i|
if i.match(/Hardware/) and not i.match(/Ethernet/).nil? if i.match(/Hardware/) and not i.match(/Ethernet/).nil?
devmap = {}
# Ethernet, should be 2 lines, # Ethernet, should be 2 lines,
# (3) Thunderbolt Ethernet # (3) Thunderbolt Ethernet
# (Hardware Port: Thunderbolt Ethernet, Device: en1) # (Hardware Port: Thunderbolt Ethernet, Device: en1)
devicearry = i.match(/Hardware Port: (.+), Device: en(.+)\)/)
devmap[devicearry[2]] = devicearry[1] # multiline, should match "Thunderbolt Ethernet", "en1"
devicearry = i.match(/\([0-9]+\) (.+)\n.*Device: (.+)\)/m)
devmap[:interface] = devicearry[2]
devmap[:service] = devicearry[1]
devlist << devmap
end end
end end
puts devlist
networks.each do |network| networks.each do |network|
intnum = network[:interface]
puts network[:interface]
puts network[:type]
if network[:type].to_sym == :static if network[:type].to_sym == :static
# network seems 1 indexed - skip NAT interface (en0) also en1 because it seems to not *really* exist on virtualbox? # network seems 1 indexed - skip NAT interface (en0) also en1 because it seems to not *really* exist on virtualbox?
intnum = network[:interface] + 1 command = "networksetup -setmanual \"#{devlist[intnum+1][:service]}\" #{network[:ip]} #{network[:netmask]}"
puts "Network - #{intnum}"
command = "networksetup -setmanual \"#{devmap[intnum.to_s]}\" #{network[:ip]} #{network[:netmask]} #{network[:gateway]}"
elsif network[:type].to_sym == :dhcp elsif network[:type].to_sym == :dhcp
command = "networksetup -setdhcp \"#{devmap[options[:interface]]}\"" command = "networksetup -setdhcp \"#{devlist[intnum+1][:service]}\""
end end
machine.communicate.sudo("#{command}") machine.communicate.sudo(command)
end end
end end
end end

View File

@ -1,13 +1,22 @@
require "vagrant/util/retryable"
module VagrantPlugins module VagrantPlugins
module GuestDarwin module GuestDarwin
module Cap module Cap
class MountNFSFolder class MountNFSFolder
extend Vagrant::Util::Retryable
def self.mount_nfs_folder(machine, ip, folders) def self.mount_nfs_folder(machine, ip, folders)
puts "30 second nap...."
sleep(30)
folders.each do |name, opts| folders.each do |name, opts|
machine.communicate.sudo("if [ ! -d #{opts[:guestpath]} ]; then mkdir -p #{opts[:guestpath]};fi") # Expand the guest path so we can handle things like "~/vagrant"
machine.communicate.sudo("mount -t nfs '#{ip}:#{opts[:hostpath]}' '#{opts[:guestpath]}'") expanded_guest_path = machine.guest.capability(
:shell_expand_guest_path, opts[:guestpath])
machine.communicate.sudo("if [ ! -d #{expanded_guest_path} ]; then mkdir -p #{expanded_guest_path};fi")
mount_command = "mount -t nfs '#{ip}:#{opts[:hostpath]}' '#{expanded_guest_path}'"
retryable(:on => Vagrant::Errors::DarwinNFSMountFailed, :tries => 10, :sleep => 5) do
machine.communicate.sudo(mount_command, :error_class => Vagrant::Errors::DarwinNFSMountFailed)
end
end end
end end
end end

View File

@ -0,0 +1,26 @@
module VagrantPlugins
module GuestDarwin
module Cap
class ShellExpandGuestPath
def self.shell_expand_guest_path(machine, path)
real_path = nil
machine.communicate.execute("printf #{path}") do |type, data|
if type == :stdout
real_path ||= ""
real_path += data
end
end
if !real_path
# If no real guest path was detected, this is really strange
# and we raise an exception because this is a bug.
raise DarwinShellExpandFailed
end
# Chomp the string so that any trailing newlines are killed
return real_path.chomp
end
end
end
end
end

View File

@ -30,6 +30,11 @@ module VagrantPlugins
require_relative "cap/mount_nfs_folder" require_relative "cap/mount_nfs_folder"
Cap::MountNFSFolder Cap::MountNFSFolder
end end
guest_capability("darwin", "shell_expand_guest_path") do
require_relative "cap/shell_expand_guest_path"
Cap::ShellExpandGuestPath
end
end end
end end
end end