Class: Vagrant::Guest

Inherits:
Object
  • Object
show all
Includes:
CapabilityHost
Defined in:
lib/vagrant/guest.rb

Overview

This class handles guest-OS specific interactions with a machine. It is primarily responsible for detecting the proper guest OS implementation and then delegating capabilities.

Vagrant has many tasks which require specific guest OS knowledge. These are implemented using a guest/capability system. Various plugins register as "guests" which determine the underlying OS of the system. Then, "guest capabilities" register themselves for a specific OS (one or more), and these capabilities are called.

Example capabilities might be "mount_virtualbox_shared_folder" or "configure_networks".

This system allows for maximum flexibility and pluginability for doing guest OS specific operations.

Instance Method Summary collapse

Methods included from CapabilityHost

#capability?, #capability_host_chain, #initialize_capabilities!

Constructor Details

#initialize(machine, guests, capabilities) ⇒ Guest

Returns a new instance of Guest.


24
25
26
27
28
# File 'lib/vagrant/guest.rb', line 24

def initialize(machine, guests, capabilities)
  @capabilities = capabilities
  @guests       = guests
  @machine      = machine
end

Instance Method Details

#capability(*args) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vagrant/guest.rb', line 42

def capability(*args)
  super
rescue Errors::CapabilityNotFound => e
  raise Errors::GuestCapabilityNotFound,
    cap: e.extra_data[:cap],
    guest: name
rescue Errors::CapabilityInvalid => e
  raise Errors::GuestCapabilityInvalid,
    cap: e.extra_data[:cap],
    guest: name
end

#detect!Object

This will detect the proper guest OS for the machine and set up the class to actually execute capabilities.


32
33
34
35
36
37
38
39
# File 'lib/vagrant/guest.rb', line 32

def detect!
  guest_name = @machine.config.vm.guest
  initialize_capabilities!(guest_name, @guests, @capabilities, @machine)
rescue Errors::CapabilityHostExplicitNotDetected => e
  raise Errors::GuestExplicitNotDetected, value: e.extra_data[:value]
rescue Errors::CapabilityHostNotDetected
  raise Errors::GuestNotDetected
end

#nameSymbol

Returns the specified or detected guest type name.

Returns:

  • (Symbol)

57
58
59
# File 'lib/vagrant/guest.rb', line 57

def name
  capability_host_chain[0][0]
end

#ready?Boolean

This returns whether the guest is ready to work. If this returns false, then #detect! should be called in order to detect the guest OS.

Returns:

  • (Boolean)

66
67
68
# File 'lib/vagrant/guest.rb', line 66

def ready?
  !!capability_host_chain
end