Class: VagrantPlugins::Parallels::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-parallels/provider.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Provider

Returns a new instance of Provider.



23
24
25
26
27
28
29
30
# File 'lib/vagrant-parallels/provider.rb', line 23

def initialize(machine)
  @logger = Log4r::Logger.new('vagrant::provider::parallels')
  @machine = machine

  # This method will load in our driver, so we call it now to
  # initialize it.
  machine_id_changed
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



7
8
9
# File 'lib/vagrant-parallels/provider.rb', line 7

def driver
  @driver
end

Class Method Details

.usable?(raise_error = false) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/vagrant-parallels/provider.rb', line 9

def self.usable?(raise_error=false)
  if !Vagrant::Util::Platform.darwin?
    raise Errors::MacOSXRequired
  end

  # Instantiate the driver, which will determine the Parallels Desktop
  # version and all that, which checks for Parallels Desktop being present
  Driver::Meta.new
  true
rescue Errors::VagrantParallelsError
  raise if raise_error
  return false
end

Instance Method Details

#action(name) ⇒ Object

See Also:

  • Vagrant::Plugin::V2::Provider#action


33
34
35
36
37
38
39
40
# File 'lib/vagrant-parallels/provider.rb', line 33

def action(name)
  # Attempt to get the action method from the Action class if it
  # exists, otherwise return nil to show that we don't support the
  # given action.
  action_method = "action_#{name}"
  return Action.send(action_method) if Action.respond_to?(action_method)
  nil
end

#machine_id_changedObject

If the machine ID changed, then we need to rebuild our underlying driver.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vagrant-parallels/provider.rb', line 44

def machine_id_changed
  id = @machine.id

  begin
    @logger.debug("Instantiating the driver for machine ID: #{@machine.id.inspect}")
    @driver = VagrantPlugins::Parallels::Driver::Meta.new(id)
  rescue VagrantPlugins::Parallels::Driver::Meta::VMNotFound
    # The virtual machine doesn't exist, so we probably have a stale
    # ID. Just clear the id out of the machine and reload it.
    @logger.debug('VM not found! Clearing saved machine ID and reloading.')
    id = nil
    retry
  end
end

#pd_version_satisfies?(*constraints) ⇒ Boolean

Determines if the installed Parallels Desktop version is satisfied by the given constraint or group of constraints.

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/vagrant-parallels/provider.rb', line 105

def pd_version_satisfies?(*constraints)
  pd_version = Gem::Version.new(@driver.version)
  Gem::Requirement.new(*constraints).satisfied_by?(pd_version)
end

#ssh_infoObject

Returns the SSH info for accessing the Parallels VM.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/vagrant-parallels/provider.rb', line 60

def ssh_info
  # If the VM is not running that we can't possibly SSH into it
  return nil if state.id != :running

  detected_ip = @driver.ssh_ip

  # If ip couldn't be detected then we cannot possibly SSH into it,
  # and should return nil too.
  return nil if !detected_ip

  # Return ip from running machine, use ip from config if available
  {
    host: detected_ip,
    port: @driver.ssh_port(@machine.config.ssh.guest_port)
  }
end

#stateSymbol

Return the state of Parallels virtual machine by actually querying PrlCtl.

Returns:

  • (Symbol)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vagrant-parallels/provider.rb', line 81

def state
  # Determine the ID of the state here.
  state_id = nil
  state_id = :not_created if !@driver.uuid
  state_id = @driver.read_state if !state_id
  state_id = :unknown if !state_id

  # Translate into short/long descriptions
  short = state_id.to_s.gsub('_', ' ')
  long  = I18n.t("vagrant_parallels.commands.status.#{state_id}")

  # If machine is not created, then specify the special ID flag
  if state_id == :not_created
    state_id = Vagrant::MachineState::NOT_CREATED_ID
  end

  # Return the state
  Vagrant::MachineState.new(state_id, short, long)
end

#to_sString

Returns a human-friendly string version of this provider which includes the machine’s ID that this provider represents, if it has one.

Returns:

  • (String)


115
116
117
118
# File 'lib/vagrant-parallels/provider.rb', line 115

def to_s
  id = @machine.id ? @machine.id : 'new VM'
  "Parallels (#{id})"
end