From bfc93ae98b79ebaf802488bc8b7bf22ac1c777c1 Mon Sep 17 00:00:00 2001 From: Michael Kuzmin Date: Sun, 18 May 2014 20:08:26 +0400 Subject: [PATCH 1/2] Reuse folder synchronization code from Vagrant core --- lib/vSphere/action.rb | 7 +-- lib/vSphere/action/sync_folders.rb | 94 ------------------------------ lib/vSphere/errors.rb | 3 - locales/en.yml | 13 ----- 4 files changed, 3 insertions(+), 114 deletions(-) delete mode 100644 lib/vSphere/action/sync_folders.rb diff --git a/lib/vSphere/action.rb b/lib/vSphere/action.rb index 834f5360..80d4455b 100644 --- a/lib/vSphere/action.rb +++ b/lib/vSphere/action.rb @@ -37,8 +37,8 @@ def self.action_provision end b3.use Provision - b3.use SyncFolders - end + b3.use SyncedFolders + end end end end @@ -104,7 +104,7 @@ def self.action_up end b.use CloseVSphere b.use Provision - b.use SyncFolders + b.use SyncedFolders end end @@ -165,7 +165,6 @@ def self.action_get_ssh_info autoload :MessageNotRunning, action_root.join('message_not_running') autoload :PowerOff, action_root.join('power_off') autoload :PowerOn, action_root.join('power_on') - autoload :SyncFolders, action_root.join('sync_folders') end end end diff --git a/lib/vSphere/action/sync_folders.rb b/lib/vSphere/action/sync_folders.rb deleted file mode 100644 index 8ab5fe29..00000000 --- a/lib/vSphere/action/sync_folders.rb +++ /dev/null @@ -1,94 +0,0 @@ -require 'i18n' -require "vagrant/util/subprocess" -require "vagrant/util/scoped_hash_override" -require "vagrant/util/which" - -module VagrantPlugins - module VSphere - module Action - # This middleware uses `rsync` to sync the folders over to the vSphere instance - # Borrowed from the Vagrant AWS gem, see https://github.com/mitchellh/vagrant-aws/blob/master/lib/vagrant-aws/action/sync_folders.rb - class SyncFolders - include Vagrant::Util::ScopedHashOverride - - def initialize(app, env) - @app = app - end - - def call(env) - @app.call(env) - - ssh_info = env[:machine].ssh_info - - env[:machine].config.vm.synced_folders.each do |id, data| - data = scoped_hash_override(data, :vsphere) - - # Ignore disabled shared folders - next if data[:disabled] - - unless Vagrant::Util::Which.which('rsync') - env[:ui].warn(I18n.t('vsphere.errors.rsync_not_found')) - break - end - hostpath = File.expand_path(data[:hostpath], env[:root_path]) - guestpath = data[:guestpath] - - # Make sure there is a trailing slash on the host path to - # avoid creating an additional directory with rsync - hostpath = "#{hostpath}/" if hostpath !~ /\/$/ - - # on windows rsync.exe requires cygdrive-style paths - if Vagrant::Util::Platform.windows? - hostpath = hostpath.gsub(/^(\w):/) { "/cygdrive/#{$1}" } - end - - env[:ui].info(I18n.t("vsphere.rsync_folder", - :hostpath => hostpath, - :guestpath => guestpath)) - - # Create the guest path - env[:machine].communicate.sudo("mkdir -p '#{guestpath}'") - env[:machine].communicate.sudo("chown #{ssh_info[:username]} '#{guestpath}'") - - # Rsync over to the guest path using the SSH info - command = [ - "rsync", "--verbose", "--archive", "-z", - "--exclude", ".vagrant/", - "-e", "ssh -p #{ssh_info[:port]} -o StrictHostKeyChecking=no #{get_private_key_options ssh_info}", - hostpath, - "#{ssh_info[:username]}@#{ssh_info[:host]}:#{guestpath}"] - - - # we need to fix permissions when using rsync.exe on windows, see - # http://stackoverflow.com/questions/5798807/rsync-permission-denied-created-directories-have-no-permissions - if Vagrant::Util::Platform.windows? - command.insert(1, "--chmod", "ugo=rwX") - end - - r = Vagrant::Util::Subprocess.execute(*command) - if r.exit_code != 0 - raise Errors::RsyncError, - :guestpath => guestpath, - :hostpath => hostpath, - :stderr => r.stderr - end - end - end - - private - - def get_private_key_options(ssh_info) - if ssh_info[:private_key_path].is_a? String - build_key_option ssh_info[:private_key_path] - elsif ssh_info[:private_key_path].is_a? Array - ssh_info[:private_key_path].map { |path| build_key_option path }.join(' ') - end - end - - def build_key_option(key) - "-i '#{key}'" - end - end - end - end -end diff --git a/lib/vSphere/errors.rb b/lib/vSphere/errors.rb index 3b5adc40..0e7f9279 100644 --- a/lib/vSphere/errors.rb +++ b/lib/vSphere/errors.rb @@ -6,9 +6,6 @@ module Errors class VSphereError < Vagrant::Errors::VagrantError error_namespace('vsphere.errors') end - class RsyncError < VSphereError - error_key(:rsync_error) - end end end end diff --git a/locales/en.yml b/locales/en.yml index 9e3b3861..8608c52e 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -18,8 +18,6 @@ en: The VM is not running waiting_for_ssh: |- Waiting for SSH to become available... - rsync_folder: |- - Rsyncing folder: %{hostpath} => %{guestpath} errors: missing_template: |- @@ -36,19 +34,8 @@ en: Configured configuration spec not found missing_datastore: |- Configured data store not found - rsync_error: |- - There was an error when attemping to rsync a share folder. - Please inspect the error message below for more info. - - Host path: %{hostpath} - Guest path: %{guestpath} - Error: %{stderr} too_many_private_networks: |- There a more private networks configured than can be assigned to the customization spec - rsync_not_found: |- - Warning! Folder sync disabled because the rsync binary is - missing. Make sure rsync is installed and the binary can - be found in PATH. config: host: |- Configuration must specify a vSphere host From 81ca16bea31b1b2eebb266f9ddb55c44c660d84b Mon Sep 17 00:00:00 2001 From: Michael Kuzmin Date: Sun, 18 May 2014 20:11:18 +0400 Subject: [PATCH 2/2] raise minimal Vagrant version to 1.5 --- lib/vSphere/plugin.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/vSphere/plugin.rb b/lib/vSphere/plugin.rb index ebe759f8..88e64e1d 100644 --- a/lib/vSphere/plugin.rb +++ b/lib/vSphere/plugin.rb @@ -1,4 +1,14 @@ -require 'vagrant' +begin + require "vagrant" +rescue LoadError + raise "The Vagrant vSphere plugin must be run within Vagrant." +end + +# This is a sanity check to make sure no one is attempting to install +# this into an early Vagrant version. +if Vagrant::VERSION < "1.5" + raise "The Vagrant vSphere plugin is only compatible with Vagrant 1.5+" +end module VagrantPlugins module VSphere