Skip to content

Commit

Permalink
action: Check the host is connected to Shared network
Browse files Browse the repository at this point in the history
With PD 11.2.1 and higher it will be automatically fixed by connecting host to Shared network.
For older versions the exception will be raised.
  • Loading branch information
legal90 committed Jul 5, 2016
1 parent e657f59 commit 746d299
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 8 deletions.
7 changes: 7 additions & 0 deletions lib/vagrant-parallels/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Action
# a bootup (i.e. not saved).
def self.action_boot
Vagrant::Action::Builder.new.tap do |b|
b.use CheckSharedInterface
b.use SetName
b.use ClearForwardedPorts
b.use Provision
Expand Down Expand Up @@ -74,6 +75,7 @@ def self.action_destroy
def self.action_halt
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use CheckSharedInterface
b.use Call, IsState, :not_created do |env1, b1|
if env1[:result]
b1.use Message, I18n.t('vagrant.commands.common.vm_not_created')
Expand Down Expand Up @@ -122,6 +124,7 @@ def self.action_package
def self.action_provision
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use CheckSharedInterface
b.use Call, IsState, :not_created do |env1, b1|
if env1[:result]
b1.use Message, I18n.t('vagrant.commands.common.vm_not_created')
Expand Down Expand Up @@ -163,6 +166,7 @@ def self.action_reload
def self.action_resume
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use CheckSharedInterface
b.use Call, IsState, :not_created do |env1, b1|
if env1[:result]
b1.use Message, I18n.t('vagrant.commands.common.vm_not_created')
Expand Down Expand Up @@ -227,6 +231,7 @@ def self.action_snapshot_save
def self.action_ssh
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use CheckSharedInterface
b.use Call, IsState, :not_created do |env1, b1|
if env1[:result]
b1.use Message, I18n.t('vagrant.commands.common.vm_not_created')
Expand All @@ -248,6 +253,7 @@ def self.action_ssh
def self.action_ssh_run
Vagrant::Action::Builder.new.tap do |b|
b.use ConfigValidate
b.use CheckSharedInterface
b.use Call, IsState, :not_created do |env1, b1|
if env1[:result]
b1.use Message, I18n.t('vagrant.commands.common.vm_not_created')
Expand Down Expand Up @@ -389,6 +395,7 @@ def self.action_sync_folders
autoload :BoxUnregister, File.expand_path('../action/box_unregister', __FILE__)
autoload :HandleGuestTools, File.expand_path('../action/handle_guest_tools', __FILE__)
autoload :HandleForwardedPortCollisions, File.expand_path('../action/handle_forwarded_port_collisions.rb', __FILE__)
autoload :CheckSharedInterface, File.expand_path('../action/check_shared_interface', __FILE__)
autoload :ClearNetworkInterfaces, File.expand_path('../action/clear_network_interfaces', __FILE__)
autoload :ClearForwardedPorts, File.expand_path('../action/clear_forwarded_ports', __FILE__)
autoload :Customize, File.expand_path('../action/customize', __FILE__)
Expand Down
29 changes: 29 additions & 0 deletions lib/vagrant-parallels/action/check_shared_interface.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module VagrantPlugins
module Parallels
module Action
class CheckSharedInterface
def initialize(app, env)
@app = app
end

def call(env)
shared_iface = env[:machine].provider.driver.read_shared_interface

# Shared interface is connected. Just exit
return @app.call(env) if shared_iface[:status] == 'Up'

# Since PD 11.2.1 Vagrant can fix this automatically
if !env[:machine].provider.pd_version_satisfies?('>= 11.2.1')
raise Errors::SharedInterfaceDisconnected
end

env[:ui].info I18n.t('vagrant_parallels.actions.vm.check_shared_interface.connecting')
iface_name = env[:machine].provider.driver.read_shared_network_id
env[:machine].provider.driver.connect_network_interface(iface_name)

@app.call(env)
end
end
end
end
end
1 change: 0 additions & 1 deletion lib/vagrant-parallels/cap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def self.forwarded_ports(machine)
#
# @return [String] Host's IP address
def self.host_address(machine)

shared_iface = machine.provider.driver.read_shared_interface
return shared_iface[:ip] if shared_iface

Expand Down
10 changes: 9 additions & 1 deletion lib/vagrant-parallels/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ def compact_hdd(hdd_path)
end
end

# Connects the host machine to the specified virtual network interface
# Could be used for Parallels' Shared and Host-Only interfaces only.
#
# @param [<String>] name Network interface name. Example: 'Shared'
def connect_network_interface(name)
raise NotImplementedError
end

# Creates a host only network with the given options.
#
# @param [<Symbol => String>] options Hostonly network options.
Expand Down Expand Up @@ -414,7 +422,7 @@ def read_mac_address
end

if shared_ifaces.empty?
raise Errors::SharedAdapterNotFound
raise Errors::SharedInterfaceNotFound
end

shared_ifaces.values.first.fetch('mac', nil)
Expand Down
2 changes: 2 additions & 0 deletions lib/vagrant-parallels/driver/meta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def initialize(uuid=nil)
:clear_forwarded_ports,
:clear_shared_folders,
:compact_hdd,
:connect_network_interface,
:create_host_only_network,
:create_snapshot,
:delete,
Expand All @@ -99,6 +100,7 @@ def initialize(uuid=nil)
:read_network_interfaces,
:read_shared_interface,
:read_shared_folders,
:read_shared_network_id,
:read_settings,
:read_state,
:read_used_ports,
Expand Down
4 changes: 4 additions & 0 deletions lib/vagrant-parallels/driver/pd_11.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def initialize(uuid)

@logger = Log4r::Logger.new('vagrant_parallels::driver::pd_11')
end

def connect_network_interface(name)
execute_prlsrvctl('net', 'set', name, '--connect-host-to-net', 'on')
end
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/vagrant-parallels/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ class ParallelsUnsupportedVersion < VagrantParallelsError
error_key(:parallels_unsupported_version)
end

class SharedAdapterNotFound < VagrantParallelsError
error_key(:shared_adapter_not_found)
class SharedInterfaceDisconnected < VagrantParallelsError
error_key(:shared_interface_disconnected)
end

class SharedInterfaceNotFound < VagrantParallelsError
error_key(:shared_interface_not_found)
end

class SnapshotIdNotDetected < VagrantParallelsError
Expand Down
16 changes: 12 additions & 4 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,16 @@ en:
check and try again.
Snapshot ID: %{snapshot}
shared_adapter_not_found: |-
Shared network adapter was not found in your virtual machine configuration.
It is required to communicate with VM and forward ports. Please check
network configuration in your Vagrantfile.
shared_interface_not_found: |-
Shared network interface was not found in your virtual machine configuration.
It is required for communications with VM and port forwarding. Please
check network configuration in your Vagrantfile.
shared_interface_disconnected: |-
Your Mac host is not connected to Shared network. It is required for
communications with VM and port forwarding. Please enable this option in GUI:
Parallels Desktop -> Preferences -> Network -> Shared -> Connect Mac to this network
vm_clone_failure: |-
The VM cloning failed! Please ensure that the box you're using is not
corrupted and try again.
Expand Down Expand Up @@ -191,6 +197,8 @@ en:
box:
register: Registering VM image from the base box '%{name}'...
unregister: Unregistering the box VM image...
check_shared_interface:
connecting: Connecting host to Shared network...
clone:
full: Cloning new virtual machine...
linked: Creating new virtual machine as a linked clone...
Expand Down

0 comments on commit 746d299

Please sign in to comment.