2014-04-16 21:46:37 +00:00
|
|
|
require "digest/md5"
|
2014-04-11 01:26:19 +00:00
|
|
|
require "fileutils"
|
2014-04-16 23:00:57 +00:00
|
|
|
require "thread"
|
2014-04-11 01:26:19 +00:00
|
|
|
|
2014-03-26 23:45:46 +00:00
|
|
|
require "log4r"
|
|
|
|
|
2014-04-11 01:26:19 +00:00
|
|
|
require "vagrant/util/silence_warnings"
|
|
|
|
|
2014-03-26 23:45:46 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module DockerProvider
|
|
|
|
class Provider < Vagrant.plugin("2", :provider)
|
2014-04-16 23:00:57 +00:00
|
|
|
@@host_vm_mutex = Mutex.new
|
|
|
|
|
2014-03-26 23:45:46 +00:00
|
|
|
def initialize(machine)
|
|
|
|
@logger = Log4r::Logger.new("vagrant::provider::docker")
|
|
|
|
@machine = machine
|
2014-04-16 03:52:06 +00:00
|
|
|
|
|
|
|
if host_vm?
|
|
|
|
# We need to use a special communicator that proxies our
|
|
|
|
# SSH requests over our host VM to the container itself.
|
|
|
|
@machine.config.vm.communicator = :docker_hostvm
|
|
|
|
end
|
2014-03-26 23:45:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# @see Vagrant::Plugin::V2::Provider#action
|
|
|
|
def action(name)
|
|
|
|
action_method = "action_#{name}"
|
|
|
|
return Action.send(action_method) if Action.respond_to?(action_method)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2014-04-11 01:26:19 +00:00
|
|
|
# Returns the driver instance for this provider.
|
|
|
|
def driver
|
|
|
|
return @driver if @driver
|
|
|
|
@driver = Driver.new
|
|
|
|
|
|
|
|
# If we are running on a host machine, then we set the executor
|
|
|
|
# to execute remotely.
|
|
|
|
if host_vm?
|
|
|
|
@driver.executor = Executor::Vagrant.new(host_vm)
|
|
|
|
end
|
|
|
|
|
|
|
|
@driver
|
|
|
|
end
|
|
|
|
|
|
|
|
# This returns the {Vagrant::Machine} that is our host machine.
|
|
|
|
# It does not perform any action on the machine or verify it is
|
|
|
|
# running.
|
|
|
|
#
|
|
|
|
# @return [Vagrant::Machine]
|
|
|
|
def host_vm
|
|
|
|
return @host_vm if @host_vm
|
|
|
|
|
|
|
|
vf_path = @machine.provider_config.vagrant_vagrantfile
|
|
|
|
host_machine_name = @machine.provider_config.vagrant_machine || :default
|
|
|
|
if !vf_path
|
|
|
|
# We don't have a Vagrantfile path set, so we're going to use
|
|
|
|
# the default but we need to copy it into the data dir so that
|
|
|
|
# we don't write into our installation dir (we can't).
|
|
|
|
default_path = File.expand_path("../hostmachine/Vagrantfile", __FILE__)
|
|
|
|
vf_path = @machine.env.data_dir.join("docker-host", "Vagrantfile")
|
2014-04-15 21:03:51 +00:00
|
|
|
begin
|
2014-04-20 15:45:51 +00:00
|
|
|
@machine.env.lock("docker-provider-hostvm") do
|
|
|
|
vf_path.dirname.mkpath
|
|
|
|
FileUtils.cp(default_path, vf_path)
|
2014-04-15 21:03:51 +00:00
|
|
|
end
|
|
|
|
rescue Vagrant::Errors::EnvironmentLockedError
|
|
|
|
# Lock contention, just retry
|
|
|
|
retry
|
|
|
|
end
|
2014-04-11 01:26:19 +00:00
|
|
|
|
|
|
|
# Set the machine name since we hardcode that for the default
|
|
|
|
host_machine_name = :default
|
|
|
|
end
|
|
|
|
|
|
|
|
vf_file = File.basename(vf_path)
|
|
|
|
vf_path = File.dirname(vf_path)
|
|
|
|
|
|
|
|
# Create the env to manage this machine
|
|
|
|
@host_vm = Vagrant::Util::SilenceWarnings.silence! do
|
|
|
|
host_env = Vagrant::Environment.new(
|
|
|
|
cwd: vf_path,
|
|
|
|
home_path: @machine.env.home_path,
|
|
|
|
ui_class: @machine.env.ui_class,
|
|
|
|
vagrantfile_name: vf_file,
|
|
|
|
)
|
|
|
|
|
2014-05-01 17:22:43 +00:00
|
|
|
host_env.machine(
|
|
|
|
host_machine_name,
|
|
|
|
host_env.default_provider(exclude: [:docker]))
|
2014-04-11 01:26:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@host_vm
|
|
|
|
end
|
|
|
|
|
2014-04-16 21:46:37 +00:00
|
|
|
# This acquires a lock on the host VM.
|
|
|
|
def host_vm_lock
|
|
|
|
hash = Digest::MD5.hexdigest(host_vm.data_dir.to_s)
|
2014-04-16 23:00:57 +00:00
|
|
|
|
|
|
|
# We do a process-level mutex on the outside, since we can
|
|
|
|
# wait for that a short amount of time. Then, we do a process lock
|
|
|
|
# on the inside, which will raise an exception if locked.
|
|
|
|
host_vm_mutex.synchronize do
|
|
|
|
@machine.env.lock(hash) do
|
|
|
|
return yield
|
|
|
|
end
|
2014-04-16 21:46:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-16 23:00:57 +00:00
|
|
|
# This is a process-local mutex that can be used by parallel
|
|
|
|
# providers to lock the host VM access.
|
|
|
|
def host_vm_mutex
|
|
|
|
@@host_vm_mutex
|
|
|
|
end
|
|
|
|
|
2014-04-11 01:26:19 +00:00
|
|
|
# This says whether or not Docker will be running within a VM
|
|
|
|
# rather than directly on our system. Docker needs to run in a VM
|
|
|
|
# when we're not on Linux, or not on a Linux that supports Docker.
|
|
|
|
def host_vm?
|
2014-04-19 02:58:26 +00:00
|
|
|
@machine.provider_config.force_host_vm ||
|
|
|
|
!Vagrant::Util::Platform.linux?
|
2014-04-11 01:26:19 +00:00
|
|
|
end
|
|
|
|
|
2014-03-26 23:45:46 +00:00
|
|
|
# Returns the SSH info for accessing the Container.
|
|
|
|
def ssh_info
|
2014-04-16 02:58:28 +00:00
|
|
|
# If the container isn't running, we can't SSH into it
|
|
|
|
return nil if state.id != :running
|
2014-03-26 23:45:46 +00:00
|
|
|
|
2014-04-11 01:26:19 +00:00
|
|
|
network = driver.inspect_container(@machine.id)['NetworkSettings']
|
2014-03-26 23:45:46 +00:00
|
|
|
ip = network['IPAddress']
|
|
|
|
|
|
|
|
# If we were not able to identify the container's IP, we return nil
|
|
|
|
# here and we let Vagrant core deal with it ;)
|
2014-04-16 02:58:28 +00:00
|
|
|
return nil if !ip
|
2014-03-26 23:45:46 +00:00
|
|
|
|
2014-04-16 02:58:28 +00:00
|
|
|
{ host: ip }
|
2014-03-26 23:45:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def state
|
|
|
|
state_id = nil
|
2014-04-18 00:11:00 +00:00
|
|
|
state_id = :not_created if !@machine.id
|
|
|
|
state_id = :host_state_unknown if !state_id && \
|
|
|
|
host_vm? && !host_vm.communicate.ready?
|
2014-04-11 01:26:19 +00:00
|
|
|
state_id = :not_created if !state_id && \
|
|
|
|
(!@machine.id || !driver.created?(@machine.id))
|
|
|
|
state_id = driver.state(@machine.id) if @machine.id && !state_id
|
2014-03-26 23:45:46 +00:00
|
|
|
state_id = :unknown if !state_id
|
|
|
|
|
|
|
|
short = state_id.to_s.gsub("_", " ")
|
2014-04-11 01:26:19 +00:00
|
|
|
long = I18n.t("docker_provider.status.#{state_id}")
|
2014-03-26 23:45:46 +00:00
|
|
|
|
|
|
|
Vagrant::MachineState.new(state_id, short, long)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
id = @machine.id ? @machine.id : "new container"
|
|
|
|
"Docker (#{id})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|