From b387f0e15d9c0bca7319b7ec7896b82b6da16249 Mon Sep 17 00:00:00 2001 From: Matija K Date: Sun, 24 May 2015 12:39:28 +0200 Subject: [PATCH] Capability to mount smb shares in darwin guests --- lib/vagrant/errors.rb | 4 +++ .../darwin/cap/choose_addressable_ip_addr.rb | 20 +++++++++++++ .../darwin/cap/mount_smb_shared_folder.rb | 29 +++++++++++++++++++ plugins/guests/darwin/plugin.rb | 10 +++++++ 4 files changed, 63 insertions(+) create mode 100644 plugins/guests/darwin/cap/choose_addressable_ip_addr.rb create mode 100644 plugins/guests/darwin/cap/mount_smb_shared_folder.rb diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index de002ffed..3072d50ff 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -316,6 +316,10 @@ module Vagrant error_key(:corrupt_machine_index) end + class DarwinMountFailed < VagrantError + error_key(:darwin_mount_failed) + end + class DarwinNFSMountFailed < VagrantError error_key(:darwin_nfs_mount_failed) end diff --git a/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb b/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb new file mode 100644 index 000000000..f5b578ebd --- /dev/null +++ b/plugins/guests/darwin/cap/choose_addressable_ip_addr.rb @@ -0,0 +1,20 @@ +module VagrantPlugins + module GuestDarwin + module Cap + module ChooseAddressableIPAddr + def self.choose_addressable_ip_addr(machine, possible) + machine.communicate.tap do |comm| + possible.each do |ip| + command = "ping -c1 -t1 #{ip}" + if comm.test(command) + return ip + end + end + end + + nil + end + end + end + end +end diff --git a/plugins/guests/darwin/cap/mount_smb_shared_folder.rb b/plugins/guests/darwin/cap/mount_smb_shared_folder.rb new file mode 100644 index 000000000..2d47f97ce --- /dev/null +++ b/plugins/guests/darwin/cap/mount_smb_shared_folder.rb @@ -0,0 +1,29 @@ +require "vagrant/util/retryable" +require "shellwords" + +module VagrantPlugins + module GuestDarwin + module Cap + class MountSMBSharedFolder + extend Vagrant::Util::Retryable + def self.mount_smb_shared_folder(machine, name, guestpath, options) + expanded_guest_path = machine.guest.capability(:shell_expand_guest_path, guestpath) + machine.communicate.execute("mkdir -p #{expanded_guest_path}") + + smb_password = Shellwords.shellescape(options[:smb_password]) + mount_options = options[:mount_options]; + + mount_command = "mount -t smbfs " + + (mount_options ? "-o '#{mount_options.join(",")}' " : "") + + "'//#{options[:smb_username]}:#{smb_password}@#{options[:smb_host]}/#{name}' " + + "#{expanded_guest_path}" + retryable(on: Vagrant::Errors::DarwinMountFailed, tries: 10, sleep: 5) do + machine.communicate.sudo( + mount_command, + error_class: Vagrant::Errors::DarwinMountFailed) + end + end + end + end + end +end diff --git a/plugins/guests/darwin/plugin.rb b/plugins/guests/darwin/plugin.rb index 305154fde..8e6dc279b 100644 --- a/plugins/guests/darwin/plugin.rb +++ b/plugins/guests/darwin/plugin.rb @@ -16,6 +16,11 @@ module VagrantPlugins Cap::ChangeHostName end + guest_capability("darwin", "choose_addressable_ip_addr") do + require_relative "cap/choose_addressable_ip_addr" + Cap::ChooseAddressableIPAddr + end + guest_capability("darwin", "configure_networks") do require_relative "cap/configure_networks" Cap::ConfigureNetworks @@ -36,6 +41,11 @@ module VagrantPlugins Cap::MountNFSFolder end + guest_capability("darwin", "mount_smb_shared_folder") do + require_relative "cap/mount_smb_shared_folder" + Cap::MountSMBSharedFolder + end + guest_capability("darwin", "mount_vmware_shared_folder") do require_relative "cap/mount_vmware_shared_folder" Cap::MountVmwareSharedFolder