Skip to content

Commit

Permalink
Release v.1.3.4
Browse files Browse the repository at this point in the history
  Use Shared IP from DHCP instead of detecting it via VM option.
  Optimized error exceptions.
  Ported changes from hashicorp/vagrant@7ccec33
  action/prepare_nfs_settings: Use Shared network instead of Host-Only for NFS exports
  driver: Raise exception if option value is not available (#read_vm_option)
  • Loading branch information
legal90 committed Oct 31, 2014
2 parents 448c90d + 94921b7 commit d42ed19
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 67 deletions.
7 changes: 4 additions & 3 deletions lib/vagrant-parallels/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,9 @@ def self.action_simple_reboot
end
end

# This action sync folders on a running provider. It is used by the docker provider
# to link synced folders on the host machine as volumes into the docker containers
# This is the action that is called to sync folders to a running machine
# without a reboot. It is used by the docker provider to link synced
# folders on the host machine as volumes into the docker containers.
def self.action_sync_folders
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
Expand All @@ -320,7 +321,7 @@ def self.action_sync_folders
b2.use Message, I18n.t("vagrant.commands.common.vm_not_running")
next
end

b2.use PrepareNFSValidIds
b2.use SyncedFolders
b2.use PrepareNFSSettings
end
Expand Down
75 changes: 21 additions & 54 deletions lib/vagrant-parallels/action/prepare_nfs_settings.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
require 'ipaddr'
require "vagrant/action/builtin/mixin_synced_folders"

module VagrantPlugins
module Parallels
module Action
class PrepareNFSSettings
include Vagrant::Action::Builtin::MixinSyncedFolders
include Vagrant::Util::Retryable

def initialize(app, env)
@app = app
@logger = Log4r::Logger.new('vagrant_parallels::action::nfs')
Expand All @@ -11,74 +17,35 @@ def call(env)
@machine = env[:machine]
@app.call(env)

if using_nfs?(@machine.config.vm) || using_nfs?(env[:synced_folders_config])
opts = {
cached: !!env[:synced_folders_cached],
config: env[:synced_folders_config],
disable_usable_check: !!env[:test],
}
folders = synced_folders(env[:machine], **opts)

if folders.has_key?(:nfs)
@logger.info("Using NFS, preparing NFS settings by reading host IP and machine IP")
add_ips_to_env!(env)
end
end

# We're using NFS if we have any synced folder with NFS configured. If
# we are not using NFS we don't need to do the extra work to
# populate these fields in the environment.
def using_nfs?(env)
env && env.synced_folders.any? { |_, opts| opts[:type] == :nfs }
end

# Extracts the proper host and guest IPs for NFS mounts and stores them
# in the environment for the SyncedFolder action to use them in
# mounting.
#
# The ! indicates that this method modifies its argument.
def add_ips_to_env!(env)
host_ip = find_host_only_adapter
machine_ip = read_machine_ip
host_ip = @machine.provider.driver.read_shared_interface[:ip]
guest_ip = @machine.provider.driver.read_guest_ip

raise Vagrant::Errors::NFSNoHostonlyNetwork if !host_ip || !machine_ip
# If we couldn't determine either guest's or host's IP, then
# it is probably a bug. Display an appropriate error message.
raise Vagrant::Errors::NFSNoHostIP if !host_ip
raise Vagrant::Errors::NFSNoGuestIP if !guest_ip

env[:nfs_host_ip] = host_ip
env[:nfs_machine_ip] = machine_ip
end

# Finds first host only network adapter and returns its IP address
#
# @return [String] ip address of found host-only adapter
def find_host_only_adapter
host_only_all = @machine.provider.driver.read_host_only_interfaces
host_only_used = @machine.provider.driver.read_network_interfaces.
select { |_, opts| opts[:type] == :hostonly }

host_only_used.each do |_, opts|
host_only_all.each do |interface|
if @machine.provider.pd_version_satisfies?('>= 10')
name_matched = interface[:name] == opts[:hostonly]
else
name_matched = interface[:bound_to] == opts[:hostonly]
end

return interface[:ip] if name_matched
end
end

nil
end

# Returns the IP address of the guest by looking at the first
# enabled host only network.
#
# @return [String] ip address of adapter in guest
def read_machine_ip
ips = []
@machine.config.vm.networks.each do |type, options|
if type == :private_network && options[:ip].is_a?(String)
ips << options[:ip]
end
end

if ips.empty?
raise Vagrant::Errors::NFSNoGuestIP
end

ips
env[:nfs_machine_ip] = guest_ip
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/vagrant-parallels/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ def read_shared_folders
def read_state
end

# Returns a value of specified VM option
# Returns a value of specified VM option. Raises an exception if value
# is not available
#
# @param [String] option Name of option (See all: `prlctl list -L`)
# @param [String] uuid Virtual machine UUID
Expand Down
5 changes: 3 additions & 2 deletions lib/vagrant-parallels/driver/meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def initialize(uuid=nil)
# specific driver to instantiate.
begin
@version = read_version || ""
rescue Vagrant::Errors::CommandUnavailable,
Vagrant::Errors::CommandUnavailableWindows
rescue Vagrant::Errors::CommandUnavailable
# This means that Parallels Desktop was not found, so we raise this
# error here.
raise VagrantPlugins::Parallels::Errors::ParallelsNotDetected
Expand Down Expand Up @@ -89,6 +88,7 @@ def initialize(uuid=nil)
:import,
:read_bridged_interfaces,
:read_forwarded_ports,
:read_guest_ip,
:read_guest_tools_state,
:read_guest_tools_iso_path,
:read_host_only_interfaces,
Expand All @@ -101,6 +101,7 @@ def initialize(uuid=nil)
:read_state,
:read_used_ports,
:read_virtual_networks,
:read_vm_option,
:read_vms,
:read_vms_info,
:read_vms_paths,
Expand Down
7 changes: 6 additions & 1 deletion lib/vagrant-parallels/driver/pd_8.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,12 @@ def read_virtual_networks
end

def read_vm_option(option, uuid=@uuid)
execute_prlctl('list', uuid,'--no-header', '-o', option)
out = execute_prlctl('list', uuid,'--no-header', '-o', option).strip
if out.empty?
raise Errors::ParallelsVMOptionNotFound, vm_option: option
end

out
end

def read_vms
Expand Down
4 changes: 4 additions & 0 deletions lib/vagrant-parallels/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class ParallelsTplNameNotFound < VagrantParallelsError
error_key(:parallels_tpl_name_not_found)
end

class ParallelsVMOptionNotFound < VagrantParallelsError
error_key(:parallels_vm_option_not_found)
end

class VMImportFailure < VagrantParallelsError
error_key(:vm_import_failure)
end
Expand Down
5 changes: 1 addition & 4 deletions lib/vagrant-parallels/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ def self.usable?(raise_error=false)
# version and all that, which checks for Parallels Desktop being present
Driver::Meta.new
true
rescue VagrantPlugins::Parallels::Errors::ParallelsInvalidVersion
raise if raise_error
return false
rescue VagrantPlugins::Parallels::Errors::ParallelsNotDetected
rescue Errors::VagrantParallelsError
raise if raise_error
return false
end
Expand Down
2 changes: 1 addition & 1 deletion lib/vagrant-parallels/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module VagrantPlugins
module Parallels
VERSION = "1.3.3"
VERSION = "1.3.4"
end
end
5 changes: 5 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ en:
the box you're using is not corrupted and try again.
Template config path: "%{config_path}"
parallels_vm_option_not_found: |-
Could not find a required option of Parallels Desktop virtual machine:
%{vm_option}
This is an internal error that should be reported as a bug.
vm_import_failure: |-
The VM import failed! Please verify that the box you're using is not
corrupted and try again.
Expand Down
23 changes: 22 additions & 1 deletion test/unit/support/shared/pd_driver_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,27 @@
end
end

describe 'read_vm_option' do
it 'returns stripped value' do
subprocess.stub(:execute).
with('prlctl', 'list', uuid, '--no-header', '-o', an_instance_of(String),
an_instance_of(Hash)).
and_return(subprocess_result(stdout: "opt_val \n"))

subject.read_vm_option('supported_option').should == 'opt_val'
end

it 'raises an exception in option is not available' do
subprocess.stub(:execute).
with('prlctl', 'list', uuid, '--no-header', '-o', an_instance_of(String),
an_instance_of(Hash)).
and_return(subprocess_result(stdout: " \n"))

expect { subject.read_vm_option('invalid_option') }.
to raise_error(VagrantPlugins::Parallels::Errors::ParallelsVMOptionNotFound)
end
end

describe "read_vms" do
it "returns the list of all registered VMs and templates" do
subject.read_vms.should be_kind_of(Hash)
Expand Down Expand Up @@ -295,7 +316,7 @@
subject.version.should match(/^#{parallels_version}.\d+\.\d+$/)
end

it "rises ParallelsInvalidVersion exception for unsupported version" do
it "raises ParallelsInvalidVersion exception for unsupported version" do
subprocess.should_receive(:execute).
with("prlctl", "--version", an_instance_of(Hash)).
and_return(subprocess_result(stdout: "prlctl version 7.0.12345"))
Expand Down

0 comments on commit d42ed19

Please sign in to comment.