vagrant-lxc-ng/lib/vagrant-lxc/provider.rb

74 lines
2 KiB
Ruby
Raw Normal View History

require "log4r"
2013-03-02 06:57:03 +00:00
require "vagrant-lxc/action"
2013-04-05 05:17:19 +00:00
require "vagrant-lxc/driver"
require "vagrant-lxc/machine_state"
module Vagrant
module LXC
class Provider < Vagrant.plugin("2", :provider)
attr_reader :driver
2013-03-01 03:34:51 +00:00
def initialize(machine)
2013-03-01 03:34:51 +00:00
@logger = Log4r::Logger.new("vagrant::provider::lxc")
@machine = machine
machine_id_changed
end
# If the machine ID changed, then we need to rebuild our underlying
# container.
def machine_id_changed
id = @machine.id
begin
@logger.debug("Instantiating the container for: #{id.inspect}")
@driver = Driver.new(id)
@driver.validate!
2013-04-05 05:17:19 +00:00
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
2013-03-29 15:31:36 +00:00
# @see Vagrant::Plugin::V2::Provider#action
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}"
2013-03-02 06:57:03 +00:00
return LXC::Action.send(action_method) if LXC::Action.respond_to?(action_method)
nil
end
2013-03-02 19:45:14 +00:00
# Returns the SSH info for accessing the Container.
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 => @driver.assigned_ip,
:port => @machine.config.ssh.guest_port
2013-03-02 19:45:14 +00:00
}
end
def state
state_id = nil
state_id = :not_created if !@driver.name
state_id = @driver.state if !state_id
state_id = :unknown if !state_id
LXC::MachineState.new(state_id)
end
def to_s
id = @machine.id ? @machine.id : "new VM"
"LXC (#{id})"
end
end
end
end