Merge pull request #1914 from b2jrock/darwin-guest

OS X (darwin) guest support.
This commit is contained in:
Mitchell Hashimoto 2013-08-09 10:53:29 -07:00
commit d24caac5ae
9 changed files with 233 additions and 0 deletions

View File

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

View File

@ -0,0 +1,14 @@
module VagrantPlugins
module GuestDarwin
module Cap
class ChangeHostName
def self.change_host_name(machine, name)
if !machine.communicate.test("hostname -f | grep '^#{name}$' || hostname -s | grep '^#{name}$'")
machine.communicate.sudo("scutil --set HostName #{name}")
machine.communicate.sudo("hostname #{name}")
end
end
end
end
end
end

View File

@ -0,0 +1,52 @@
require "tempfile"
require "vagrant/util/template_renderer"
module VagrantPlugins
module GuestDarwin
module Cap
class ConfigureNetworks
include Vagrant::Util
def self.configure_networks(machine, networks)
# Slightly different than other plugins, using the template to build commands
# rather than templating the files.
machine.communicate.sudo("networksetup -detectnewhardware")
machine.communicate.sudo("networksetup -listnetworkserviceorder > /tmp/vagrant.interfaces")
tmpints = File.join(Dir.tmpdir, File.basename("#{machine.id}.interfaces"))
machine.communicate.download("/tmp/vagrant.interfaces",tmpints)
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)
# 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
File.delete(tmpints)
networks.each do |network|
service_name = devlist[network[:interface]][:service]
if network[:type].to_sym == :static
command = "networksetup -setmanual \"#{service_name}\" #{network[:ip]} #{network[:netmask]}"
elsif network[:type].to_sym == :dhcp
command = "networksetup -setdhcp \"#{service_name}\""
end
machine.communicate.sudo(command)
end
end
end
end
end
end

View File

@ -0,0 +1,16 @@
module VagrantPlugins
module GuestDarwin
module Cap
class Halt
def self.halt(machine)
begin
machine.communicate.sudo("shutdown -h now")
rescue IOError
# Do nothing because SSH connection closed and it probably
# means the VM just shut down really fast.
end
end
end
end
end
end

View File

@ -0,0 +1,25 @@
require "vagrant/util/retryable"
module VagrantPlugins
module GuestDarwin
module Cap
class MountNFSFolder
extend Vagrant::Util::Retryable
def self.mount_nfs_folder(machine, ip, folders)
folders.each do |name, opts|
# Expand the guest path so we can handle things like "~/vagrant"
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,36 @@
module VagrantPlugins
module GuestDarwin
module Cap
class MountVmwareSharedFolder
# we seem to be unable to ask 'mount -t vmhgfs' to mount the roots
# of specific shares, so instead we symlink from what is already
# mounted by the guest tools
# (ie. the behaviour of the VMware_fusion provider prior to 0.8.x)
def self.mount_vmware_shared_folder(machine, name, guestpath, options)
machine.communicate.tap do |comm|
# clear prior symlink
if comm.test("test -L \"#{guestpath}\"", :sudo => true)
comm.sudo("rm \"#{guestpath}\"")
end
# clear prior directory if exists
if comm.test("test -d \"#{guestpath}\"", :sudo => true)
comm.sudo("rm -Rf \"#{guestpath}\"")
end
# create intermediate directories if needed
intermediate_dir = File.dirname(guestpath)
if !comm.test("test -d \"#{intermediate_dir}\"", :sudo => true)
comm.sudo("mkdir -p \"#{intermediate_dir}\"")
end
# finally make the symlink
comm.sudo("ln -s \"/Volumes/VMware Shared Folders/#{name}\" \"#{guestpath}\"")
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

@ -0,0 +1,15 @@
require 'vagrant/util/template_renderer'
module VagrantPlugins
module GuestDarwin
# A general Vagrant system implementation for OS X (ie. "Darwin").
#
# Contributed by: - Brian Johnson <b2jrock@gmail.com>
# - Tim Sutton <tim@synthist.net>
class Guest < Vagrant.plugin("2", :guest)
def detect?(machine)
machine.communicate.test("uname -s | grep 'Darwin'")
end
end
end
end

View File

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