2013-03-10 03:45:27 +00:00
|
|
|
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"
|
2013-07-28 05:17:05 +00:00
|
|
|
require "vagrant-lxc/sudo_wrapper"
|
2013-02-28 03:20:54 +00:00
|
|
|
|
|
|
|
module Vagrant
|
|
|
|
module LXC
|
|
|
|
class Provider < Vagrant.plugin("2", :provider)
|
2013-04-05 05:23:30 +00:00
|
|
|
attr_reader :driver
|
2013-03-01 03:34:51 +00:00
|
|
|
|
2013-02-28 03:20:54 +00:00
|
|
|
def initialize(machine)
|
2013-03-01 03:34:51 +00:00
|
|
|
@logger = Log4r::Logger.new("vagrant::provider::lxc")
|
|
|
|
@machine = machine
|
2013-03-02 04:18:38 +00:00
|
|
|
|
2013-07-28 05:54:58 +00:00
|
|
|
ensure_lxc_installed!
|
2013-03-02 04:18:38 +00:00
|
|
|
machine_id_changed
|
|
|
|
end
|
|
|
|
|
2013-07-28 05:17:05 +00:00
|
|
|
def sudo_wrapper
|
|
|
|
@shell ||= begin
|
2014-04-08 17:58:07 +00:00
|
|
|
wrapper = Pathname.new(LXC.sudo_wrapper_path).exist? &&
|
|
|
|
LXC.sudo_wrapper_path || nil
|
|
|
|
@logger.debug("Found sudo wrapper : #{wrapper}") if wrapper
|
2013-07-28 05:17:05 +00:00
|
|
|
SudoWrapper.new(wrapper)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-28 05:54:58 +00:00
|
|
|
def ensure_lxc_installed!
|
2013-12-18 00:59:32 +00:00
|
|
|
begin
|
2014-02-12 03:41:24 +00:00
|
|
|
sudo_wrapper.run("which", "lxc-create")
|
2013-12-18 00:59:32 +00:00
|
|
|
rescue Vagrant::LXC::Errors::ExecuteError
|
|
|
|
raise Errors::LxcNotInstalled
|
2013-07-28 05:54:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-02 04:18:38 +00:00
|
|
|
# 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}")
|
2013-07-29 15:08:22 +00:00
|
|
|
@driver = Driver.new(id, self.sudo_wrapper)
|
2013-04-05 05:23:30 +00:00
|
|
|
@driver.validate!
|
2013-04-05 05:17:19 +00:00
|
|
|
rescue Driver::ContainerNotFound
|
2013-03-02 04:18:38 +00:00
|
|
|
# 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
|
2013-02-28 03:20:54 +00:00
|
|
|
end
|
|
|
|
|
2013-03-29 15:31:36 +00:00
|
|
|
# @see Vagrant::Plugin::V2::Provider#action
|
2013-02-28 03:21:59 +00:00
|
|
|
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)
|
2013-02-28 03:21:59 +00:00
|
|
|
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
|
|
|
|
|
2013-07-29 15:08:22 +00:00
|
|
|
# Run a custom action called "fetch_ip" which does what it says and puts
|
|
|
|
# the IP found into the `:machine_ip` key in the environment.
|
|
|
|
env = @machine.action("fetch_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]
|
|
|
|
|
2013-03-02 19:45:14 +00:00
|
|
|
{
|
2013-07-29 15:08:22 +00:00
|
|
|
:host => env[:machine_ip],
|
2013-03-29 15:29:24 +00:00
|
|
|
:port => @machine.config.ssh.guest_port
|
2013-03-02 19:45:14 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-02-28 03:20:54 +00:00
|
|
|
def state
|
2013-03-02 04:18:38 +00:00
|
|
|
state_id = nil
|
2013-04-06 01:28:41 +00:00
|
|
|
state_id = :not_created if !@driver.container_name
|
2013-04-05 05:23:30 +00:00
|
|
|
state_id = @driver.state if !state_id
|
2013-03-02 04:18:38 +00:00
|
|
|
state_id = :unknown if !state_id
|
2013-04-08 23:07:06 +00:00
|
|
|
|
|
|
|
short = state_id.to_s.gsub("_", " ")
|
|
|
|
long = I18n.t("vagrant.commands.status.#{state_id}")
|
|
|
|
|
|
|
|
Vagrant::MachineState.new(state_id, short, long)
|
2013-02-28 03:20:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
id = @machine.id ? @machine.id : "new VM"
|
|
|
|
"LXC (#{id})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|