From 9b8dda32de40e48a7f5d2e39516cd4473b78a5e9 Mon Sep 17 00:00:00 2001 From: Mikhail Zholobov Date: Tue, 11 Mar 2014 11:34:22 +0400 Subject: [PATCH 1/6] driver: 'read_ip_dhcp' renamed to 'read_guest_ip' --- lib/vagrant-parallels/driver/meta.rb | 4 ---- lib/vagrant-parallels/driver/pd_8.rb | 21 +++++++++++---------- lib/vagrant-parallels/driver/pd_9.rb | 21 +++++++++++---------- lib/vagrant-parallels/provider.rb | 4 ++-- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/lib/vagrant-parallels/driver/meta.rb b/lib/vagrant-parallels/driver/meta.rb index 1d2b3e2b..4e2a4ba4 100644 --- a/lib/vagrant-parallels/driver/meta.rb +++ b/lib/vagrant-parallels/driver/meta.rb @@ -73,7 +73,6 @@ def initialize(uuid=nil) end def_delegators :@driver, - #:clear_forwarded_ports, :clear_shared_folders, :compact, :create_host_only_network, @@ -83,11 +82,8 @@ def initialize(uuid=nil) :enable_adapters, :execute_command, :export, - #:forward_ports, :halt, :import, - :read_ip_dhcp, - #:read_forwarded_ports, :read_bridged_interfaces, :read_guest_tools_version, :read_guest_ip, diff --git a/lib/vagrant-parallels/driver/pd_8.rb b/lib/vagrant-parallels/driver/pd_8.rb index 181f3675..1490d63c 100644 --- a/lib/vagrant-parallels/driver/pd_8.rb +++ b/lib/vagrant-parallels/driver/pd_8.rb @@ -220,6 +220,17 @@ def read_bridged_interfaces bridged_ifaces end + def read_guest_ip + mac_addr = read_mac_address.downcase + File.foreach("/Library/Preferences/Parallels/parallels_dhcp_leases") do |line| + if line.include? mac_addr + return line[/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/] + end + end + + nil + end + def read_guest_tools_version read_settings.fetch('GuestTools', {}).fetch('version', nil) end @@ -254,16 +265,6 @@ def read_host_only_interfaces hostonly_ifaces end - def read_ip_dhcp - mac_addr = read_mac_address.downcase - File.foreach("/Library/Preferences/Parallels/parallels_dhcp_leases") do |line| - if line.include? mac_addr - ip = line[/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/] - return ip - end - end - end - def read_mac_address read_settings.fetch('Hardware', {}).fetch('net0', {}).fetch('mac', nil) end diff --git a/lib/vagrant-parallels/driver/pd_9.rb b/lib/vagrant-parallels/driver/pd_9.rb index e7176d56..5afcb13f 100644 --- a/lib/vagrant-parallels/driver/pd_9.rb +++ b/lib/vagrant-parallels/driver/pd_9.rb @@ -220,6 +220,17 @@ def read_bridged_interfaces bridged_ifaces end + def read_guest_ip + mac_addr = read_mac_address.downcase + File.foreach("/Library/Preferences/Parallels/parallels_dhcp_leases") do |line| + if line.include? mac_addr + return line[/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/] + end + end + + nil + end + def read_guest_tools_version read_settings.fetch('GuestTools', {}).fetch('version', nil) end @@ -254,16 +265,6 @@ def read_host_only_interfaces hostonly_ifaces end - def read_ip_dhcp - mac_addr = read_mac_address.downcase - File.foreach("/Library/Preferences/Parallels/parallels_dhcp_leases") do |line| - if line.include? mac_addr - ip = line[/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/] - return ip - end - end - end - def read_mac_address read_settings.fetch('Hardware', {}).fetch('net0', {}).fetch('mac', nil) end diff --git a/lib/vagrant-parallels/provider.rb b/lib/vagrant-parallels/provider.rb index 2c018c5b..ea5a95c9 100644 --- a/lib/vagrant-parallels/provider.rb +++ b/lib/vagrant-parallels/provider.rb @@ -52,11 +52,11 @@ def ssh_info # we return nil. return nil if state.id == :not_created - detected_ip = @machine.config.ssh.host || @driver.read_ip_dhcp + detected_ip = @driver.read_guest_ip # If ip couldn't be detected then we cannot possibly SSH into it, # and should return nil too. - return nil if detected_ip.nil? + return nil if !detected_ip # Return ip from running machine, use ip from config if available return { From c6ebbbfabd024b4f050f101578f3c712e76bd0da Mon Sep 17 00:00:00 2001 From: Mikhail Zholobov Date: Tue, 11 Mar 2014 11:37:03 +0400 Subject: [PATCH 2/6] Created 'public_address' provider capability. 'Vagrant Share' is supported now --- lib/vagrant-parallels/cap/public_address.rb | 15 +++++++++++++++ lib/vagrant-parallels/plugin.rb | 5 +++++ 2 files changed, 20 insertions(+) create mode 100644 lib/vagrant-parallels/cap/public_address.rb diff --git a/lib/vagrant-parallels/cap/public_address.rb b/lib/vagrant-parallels/cap/public_address.rb new file mode 100644 index 00000000..016b91d5 --- /dev/null +++ b/lib/vagrant-parallels/cap/public_address.rb @@ -0,0 +1,15 @@ +module VagrantPlugins + module Parallels + module Cap + module PublicAddress + def self.public_address(machine) + return nil if machine.state.id != :running + + ssh_info = machine.ssh_info + return nil if !ssh_info + ssh_info[:host] + end + end + end + end +end diff --git a/lib/vagrant-parallels/plugin.rb b/lib/vagrant-parallels/plugin.rb index 89e19f29..f245be7c 100644 --- a/lib/vagrant-parallels/plugin.rb +++ b/lib/vagrant-parallels/plugin.rb @@ -40,6 +40,11 @@ class Plugin < Vagrant.plugin("2") GuestLinuxCap::MountParallelsSharedFolder end + provider_capability("parallels", "public_address") do + require_relative "cap/public_address" + Cap::PublicAddress + end + synced_folder(:parallels) do require File.expand_path("../synced_folder", __FILE__) SyncedFolder From abce9f4fd357da169d71bf2d53244d50b796ea44 Mon Sep 17 00:00:00 2001 From: Mikhail Zholobov Date: Tue, 11 Mar 2014 23:02:16 +0400 Subject: [PATCH 3/6] driver: Created method 'read_host_info' --- lib/vagrant-parallels/driver/pd_8.rb | 4 ++++ lib/vagrant-parallels/driver/pd_9.rb | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/lib/vagrant-parallels/driver/pd_8.rb b/lib/vagrant-parallels/driver/pd_8.rb index 1490d63c..4b9163df 100644 --- a/lib/vagrant-parallels/driver/pd_8.rb +++ b/lib/vagrant-parallels/driver/pd_8.rb @@ -235,6 +235,10 @@ def read_guest_tools_version read_settings.fetch('GuestTools', {}).fetch('version', nil) end + def read_host_info + json { execute('server', 'info', '--json', retryable: true) } + end + def read_host_only_interfaces net_list = read_virtual_networks net_list.keep_if { |net| net['Type'] == "host-only" } diff --git a/lib/vagrant-parallels/driver/pd_9.rb b/lib/vagrant-parallels/driver/pd_9.rb index 5afcb13f..5b956af6 100644 --- a/lib/vagrant-parallels/driver/pd_9.rb +++ b/lib/vagrant-parallels/driver/pd_9.rb @@ -235,6 +235,10 @@ def read_guest_tools_version read_settings.fetch('GuestTools', {}).fetch('version', nil) end + def read_host_info + json { execute('server', 'info', '--json', retryable: true) } + end + def read_host_only_interfaces net_list = read_virtual_networks net_list.keep_if { |net| net['Type'] == "host-only" } From 92266f8ef7c5bedbd31e80407c23165c61cc630c Mon Sep 17 00:00:00 2001 From: Mikhail Zholobov Date: Tue, 11 Mar 2014 23:03:17 +0400 Subject: [PATCH 4/6] driver: 'read_bridged_interfaces' is using 'read_host_info' [GH-92] --- lib/vagrant-parallels/driver/pd_8.rb | 21 +++++++++------------ lib/vagrant-parallels/driver/pd_9.rb | 19 ++++++++----------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/lib/vagrant-parallels/driver/pd_8.rb b/lib/vagrant-parallels/driver/pd_8.rb index 4b9163df..e81f8652 100644 --- a/lib/vagrant-parallels/driver/pd_8.rb +++ b/lib/vagrant-parallels/driver/pd_8.rb @@ -184,21 +184,18 @@ def import(template_uuid) end def read_bridged_interfaces - net_list = read_virtual_networks - - # Skip 'vnicXXX' and 'Default' interfaces - net_list.delete_if do |net| - net['Type'] != "bridged" or - net['Bound To'] =~ /^(vnic(.+?))$/ or - net['Network ID'] == "Default" + host_hw_info = read_host_info.fetch("Hardware info") + net_list = host_hw_info.select do |name, attrs| + # Get all network interfaces except 'vnicXXX' + attrs.fetch("type") == "net" and name !~ /^(vnic(.+?))$/ end bridged_ifaces = [] - net_list.collect do |iface| + net_list.keys.each do |iface| info = {} - ifconfig = execute(:ifconfig, iface['Bound To']) + ifconfig = execute(:ifconfig, iface) # Assign default values - info[:name] = iface['Bound To'] + info[:name] = iface info[:ip] = "0.0.0.0" info[:netmask] = "0.0.0.0" info[:status] = "Down" @@ -238,13 +235,13 @@ def read_guest_tools_version def read_host_info json { execute('server', 'info', '--json', retryable: true) } end - + def read_host_only_interfaces net_list = read_virtual_networks net_list.keep_if { |net| net['Type'] == "host-only" } hostonly_ifaces = [] - net_list.collect do |iface| + net_list.each do |iface| info = {} net_info = json { execute(:prlsrvctl, 'net', 'info', iface['Network ID'], '--json') } # Really we need to work with bounded virtual interface diff --git a/lib/vagrant-parallels/driver/pd_9.rb b/lib/vagrant-parallels/driver/pd_9.rb index 5b956af6..7b4e7cec 100644 --- a/lib/vagrant-parallels/driver/pd_9.rb +++ b/lib/vagrant-parallels/driver/pd_9.rb @@ -184,21 +184,18 @@ def import(template_uuid) end def read_bridged_interfaces - net_list = read_virtual_networks - - # Skip 'vnicXXX' and 'Default' interfaces - net_list.delete_if do |net| - net['Type'] != "bridged" or - net['Bound To'] =~ /^(vnic(.+?))$/ or - net['Network ID'] == "Default" + host_hw_info = read_host_info.fetch("Hardware info") + net_list = host_hw_info.select do |name, attrs| + # Get all network interfaces except 'vnicXXX' + attrs.fetch("type") == "net" and name !~ /^(vnic(.+?))$/ end bridged_ifaces = [] - net_list.collect do |iface| + net_list.keys.each do |iface| info = {} - ifconfig = execute(:ifconfig, iface['Bound To']) + ifconfig = execute(:ifconfig, iface) # Assign default values - info[:name] = iface['Bound To'] + info[:name] = iface info[:ip] = "0.0.0.0" info[:netmask] = "0.0.0.0" info[:status] = "Down" @@ -244,7 +241,7 @@ def read_host_only_interfaces net_list.keep_if { |net| net['Type'] == "host-only" } hostonly_ifaces = [] - net_list.collect do |iface| + net_list.each do |iface| info = {} net_info = json { execute(:prlsrvctl, 'net', 'info', iface['Network ID'], '--json') } # Really we need to work with bounded virtual interface From 6671cddc0f649479c8fb9143ff8de776d98520d8 Mon Sep 17 00:00:00 2001 From: Mikhail Zholobov Date: Wed, 12 Mar 2014 00:38:54 +0400 Subject: [PATCH 5/6] driver: Handled the case when 'parallels_dhcp_leases' file is inaccessible [GH-74] --- lib/vagrant-parallels/driver/pd_9.rb | 7 +++++-- lib/vagrant-parallels/errors.rb | 4 ++++ locales/en.yml | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/vagrant-parallels/driver/pd_9.rb b/lib/vagrant-parallels/driver/pd_9.rb index 7b4e7cec..dcf5e7a1 100644 --- a/lib/vagrant-parallels/driver/pd_9.rb +++ b/lib/vagrant-parallels/driver/pd_9.rb @@ -219,10 +219,13 @@ def read_bridged_interfaces def read_guest_ip mac_addr = read_mac_address.downcase - File.foreach("/Library/Preferences/Parallels/parallels_dhcp_leases") do |line| - if line.include? mac_addr + leases_file = "/Library/Preferences/Parallels/parallels_dhcp_leases" + begin + File.open(leases_file).grep(/#{mac_addr}/) do |line| return line[/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/] end + rescue Errno::EACCES + raise Errors::DhcpLeasesNotAccessible, :leases_file => leases_file.to_s end nil diff --git a/lib/vagrant-parallels/errors.rb b/lib/vagrant-parallels/errors.rb index 8ff63ee5..5dc6dc84 100644 --- a/lib/vagrant-parallels/errors.rb +++ b/lib/vagrant-parallels/errors.rb @@ -7,6 +7,10 @@ class VagrantParallelsError < Vagrant::Errors::VagrantError error_namespace("vagrant_parallels.errors") end + class DhcpLeasesNotAccessible < VagrantParallelsError + error_key(:dhcp_leases_file_not_accessible) + end + class PrlCtlError < VagrantParallelsError error_key(:prlctl_error) end diff --git a/locales/en.yml b/locales/en.yml index 7e27c6d2..9e387135 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -9,6 +9,12 @@ en: # Translations for exception classes #------------------------------------------------------------------------------- errors: + dhcp_leases_file_not_accessible: |- + Parallels DHCP leases file is not accessible. The Parallels provider + uses it to detect an IP address of virtual machine. This file must be + readable for the user that is running Vagrant. + + Parallels DHCP leases file: %{leases_file} mac_os_x_required: |- Parallels provider only works on OS X (or Mac OS X). prlctl_error: |- From 2c06cf105ff06140d47d6b579a36b1b07e2a893d Mon Sep 17 00:00:00 2001 From: Mikhail Zholobov Date: Wed, 12 Mar 2014 01:01:57 +0400 Subject: [PATCH 6/6] Version bumped to 1.0.1 --- lib/vagrant-parallels/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vagrant-parallels/version.rb b/lib/vagrant-parallels/version.rb index 5e4757dd..690a5fd0 100644 --- a/lib/vagrant-parallels/version.rb +++ b/lib/vagrant-parallels/version.rb @@ -1,5 +1,5 @@ module VagrantPlugins module Parallels - VERSION = "1.0.0" + VERSION = "1.0.1" end end