Class: Vagrant::LXC::Provider

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-lxc/provider.rb,
lib/vagrant-lxc/provider/cap/public_address.rb

Defined Under Namespace

Modules: Cap

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Provider

Returns a new instance of Provider.



19
20
21
22
23
24
25
# File 'lib/vagrant-lxc/provider.rb', line 19

def initialize(machine)
  @logger    = Log4r::Logger.new("vagrant::provider::lxc")
  @machine   = machine

  ensure_lxc_installed!
  machine_id_changed
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



9
10
11
# File 'lib/vagrant-lxc/provider.rb', line 9

def driver
  @driver
end

Class Method Details

.usable?(raise_error = false) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
# File 'lib/vagrant-lxc/provider.rb', line 11

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

  true
end

Instance Method Details

#action(name) ⇒ Object

See Also:

  • Plugin::V2::Provider#action


54
55
56
57
58
59
60
61
# File 'lib/vagrant-lxc/provider.rb', line 54

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 LXC::Action.send(action_method) if LXC::Action.respond_to?(action_method)
  nil
end

#ensure_lxc_installed!Object



27
28
29
30
31
32
33
# File 'lib/vagrant-lxc/provider.rb', line 27

def ensure_lxc_installed!
  begin
    SudoWrapper.new(privileged: @machine.provider_config.privileged).run("which", "lxc-create")
  rescue Vagrant::LXC::Errors::ExecuteError
    raise Errors::LxcNotInstalled
  end
end

#machine_id_changedObject

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vagrant-lxc/provider.rb', line 37

def machine_id_changed
  id = @machine.id

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

#ssh_infoObject

Returns the SSH info for accessing the Container.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/vagrant-lxc/provider.rb', line 64

def ssh_info
  # If the Container is not running then we cannot possibly SSH into it, so
  # we return nil.
  return nil if state.id != :running

  # Run a custom action called "ssh_ip" which does what it says and puts
  # the IP found into the `:machine_ip` key in the environment.
  env = @machine.action("ssh_ip")

  # If we were not able to identify the container's IP, we return nil
  # here and we let Vagrant core deal with it ;)
  return nil unless env[:machine_ip]

  {
    :host => env[:machine_ip],
    :port => @machine.config.ssh.guest_port
  }
end

#stateObject



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/vagrant-lxc/provider.rb', line 83

def state
  state_id = nil
  state_id = :not_created if !@driver.container_name
  state_id = @driver.state if !state_id
  state_id = :unknown if !state_id

  short = state_id.to_s.gsub("_", " ")
  long  = I18n.t("vagrant.commands.status.#{state_id}")

  Vagrant::MachineState.new(state_id, short, long)
end

#to_sObject



95
96
97
98
# File 'lib/vagrant-lxc/provider.rb', line 95

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