Class: Vagrant::LXC::Provider

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine) ⇒ Provider

Returns a new instance of Provider.



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

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

  machine_id_changed
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



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

def container
  @container
end

Instance Method Details

#action(name) ⇒ Object

See Also:

  • Plugin::V2::Provider#action


38
39
40
41
42
43
44
45
46
# File 'lib/vagrant-lxc/provider.rb', line 38

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}"
  # TODO: Rename to singular
  return LXC::Action.send(action_method) if LXC::Action.respond_to?(action_method)
  nil
end

#machine_id_changedObject

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vagrant-lxc/provider.rb', line 21

def machine_id_changed
  id = @machine.id

  begin
    @logger.debug("Instantiating the container for: #{id.inspect}")
    @container = Container.new(id)
    @container.validate!
  rescue Container::NotFound
    # 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.



49
50
51
52
53
54
55
56
57
58
# File 'lib/vagrant-lxc/provider.rb', line 49

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

  {
    :host => @container.assigned_ip,
    :port => @machine.config.ssh.guest_port
  }
end

#stateObject



60
61
62
63
64
65
66
# File 'lib/vagrant-lxc/provider.rb', line 60

def state
  state_id = nil
  state_id = :not_created if !@container.name
  state_id = @container.state if !state_id
  state_id = :unknown if !state_id
  LXC::MachineState.new(state_id)
end

#to_sObject



68
69
70
71
# File 'lib/vagrant-lxc/provider.rb', line 68

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