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

@ -3,48 +3,55 @@ require "tempfile"
require "vagrant/util/template_renderer" require "vagrant/util/template_renderer"
module VagrantPlugins module VagrantPlugins
module GuestDarwin module GuestDarwin
module Cap module Cap
class ConfigureNetworks class ConfigureNetworks
include Vagrant::Util include Vagrant::Util
def self.configure_networks(machine, networks) def self.configure_networks(machine, networks)
# Slightly different than other plugins, using the template to build commands # Slightly different than other plugins, using the template to build commands
# 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, File.basename("#{machine.id}.interfaces"))
tmpints = File.join(Dir.tmpdir, "#{machine.id}.interfaces") machine.communicate.download("/tmp/vagrant.interfaces",tmpints)
machine.communicate.download("/tmp/vagrant.interfaces",tmpints)
ints = IO.read(tmpints)
ints.split(/\n\n/m).each do |i|
if i.match(/Hardware/) and not i.match(/Ethernet/).nil?
# Ethernet, should be 2 lines,
# (3) Thunderbolt Ethernet
# (Hardware Port: Thunderbolt Ethernet, Device: en1)
devicearry = i.match(/Hardware Port: (.+), Device: en(.+)\)/)
devmap[devicearry[2]] = devicearry[1]
end
end
networks.each do |network|
devlist = []
ints = IO.read(tmpints)
ints.split(/\n\n/m).each do |i|
if i.match(/Hardware/) and not i.match(/Ethernet/).nil?
devmap = {}
# Ethernet, should be 2 lines,
# (3) Thunderbolt Ethernet
# (Hardware Port: Thunderbolt Ethernet, Device: en1)
if network[:type].to_sym == :static # multiline, should match "Thunderbolt Ethernet", "en1"
# network seems 1 indexed - skip NAT interface (en0) also en1 because it seems to not *really* exist on virtualbox? devicearry = i.match(/\([0-9]+\) (.+)\n.*Device: (.+)\)/m)
intnum = network[:interface] + 1 devmap[:interface] = devicearry[2]
puts "Network - #{intnum}" devmap[:service] = devicearry[1]
command = "networksetup -setmanual \"#{devmap[intnum.to_s]}\" #{network[:ip]} #{network[:netmask]} #{network[:gateway]}" devlist << devmap
end
end
puts devlist
elsif network[:type].to_sym == :dhcp networks.each do |network|
command = "networksetup -setdhcp \"#{devmap[options[:interface]]}\"" intnum = network[:interface]
puts network[:interface]
puts network[:type]
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?
command = "networksetup -setmanual \"#{devlist[intnum+1][:service]}\" #{network[:ip]} #{network[:netmask]}"
end elsif network[:type].to_sym == :dhcp
command = "networksetup -setdhcp \"#{devlist[intnum+1][:service]}\""
machine.communicate.sudo("#{command}") end
end
end machine.communicate.sudo(command)
end end
end end
end end
end
end
end end

View File

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

@ -1,35 +1,40 @@
require "vagrant" require "vagrant"
module VagrantPlugins module VagrantPlugins
module GuestDarwin module GuestDarwin
class Plugin < Vagrant.plugin("2") class Plugin < Vagrant.plugin("2")
name "Darwin guest" name "Darwin guest"
description "Darwin guest support." description "Darwin guest support."
guest("darwin") do guest("darwin") do
require File.expand_path("../guest", __FILE__) require File.expand_path("../guest", __FILE__)
Guest Guest
end end
guest_capability("darwin", "change_host_name") do guest_capability("darwin", "change_host_name") do
require_relative "cap/change_host_name" require_relative "cap/change_host_name"
Cap::ChangeHostName Cap::ChangeHostName
end end
guest_capability("darwin", "configure_networks") do guest_capability("darwin", "configure_networks") do
require_relative "cap/configure_networks" require_relative "cap/configure_networks"
Cap::ConfigureNetworks Cap::ConfigureNetworks
end end
guest_capability("darwin", "halt") do guest_capability("darwin", "halt") do
require_relative "cap/halt" require_relative "cap/halt"
Cap::Halt Cap::Halt
end end
guest_capability("darwin", "mount_nfs_folder") do guest_capability("darwin", "mount_nfs_folder") do
require_relative "cap/mount_nfs_folder" require_relative "cap/mount_nfs_folder"
Cap::MountNFSFolder Cap::MountNFSFolder
end end
end
end guest_capability("darwin", "shell_expand_guest_path") do
require_relative "cap/shell_expand_guest_path"
Cap::ShellExpandGuestPath
end
end
end
end end