2014-04-11 01:26:19 +00:00
|
|
|
require "json"
|
2014-03-26 23:45:46 +00:00
|
|
|
|
2014-04-11 01:26:19 +00:00
|
|
|
require "log4r"
|
2014-03-26 23:45:46 +00:00
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module DockerProvider
|
|
|
|
class Driver
|
2014-04-11 01:26:19 +00:00
|
|
|
# The executor is responsible for actually executing Docker commands.
|
|
|
|
# This is set by the provider, but defaults to local execution.
|
|
|
|
attr_accessor :executor
|
2014-03-26 23:45:46 +00:00
|
|
|
|
|
|
|
def initialize
|
2014-04-11 01:26:19 +00:00
|
|
|
@logger = Log4r::Logger.new("vagrant::docker::driver")
|
|
|
|
@executor = Executor::Local.new
|
2014-03-26 23:45:46 +00:00
|
|
|
end
|
|
|
|
|
2014-10-23 22:02:46 +00:00
|
|
|
def build(dir, **opts, &block)
|
2014-05-08 01:41:24 +00:00
|
|
|
args = Array(opts[:extra_args])
|
|
|
|
args << dir
|
2014-10-23 22:02:46 +00:00
|
|
|
result = execute('docker', 'build', *args, &block)
|
2014-10-22 00:44:48 +00:00
|
|
|
matches = result.scan(/Successfully built (.+)$/i)
|
|
|
|
if matches.empty?
|
2014-04-29 23:50:58 +00:00
|
|
|
# This will cause a stack trace in Vagrant, but it is a bug
|
|
|
|
# if this happens anyways.
|
|
|
|
raise "UNKNOWN OUTPUT: #{result}"
|
2014-04-18 22:53:12 +00:00
|
|
|
end
|
|
|
|
|
2014-10-22 00:44:48 +00:00
|
|
|
# Return the last match, and the capture of it
|
|
|
|
matches[-1][0]
|
2014-04-18 22:53:12 +00:00
|
|
|
end
|
|
|
|
|
2014-04-29 23:27:22 +00:00
|
|
|
def create(params, **opts, &block)
|
2014-03-26 23:45:46 +00:00
|
|
|
image = params.fetch(:image)
|
2014-05-08 18:10:37 +00:00
|
|
|
links = params.fetch(:links)
|
2014-03-26 23:45:46 +00:00
|
|
|
ports = Array(params[:ports])
|
|
|
|
volumes = Array(params[:volumes])
|
|
|
|
name = params.fetch(:name)
|
|
|
|
cmd = Array(params.fetch(:cmd))
|
2014-04-18 01:07:57 +00:00
|
|
|
env = params.fetch(:env)
|
2014-04-28 01:37:25 +00:00
|
|
|
expose = Array(params[:expose])
|
2014-03-26 23:45:46 +00:00
|
|
|
|
2014-04-28 01:23:31 +00:00
|
|
|
run_cmd = %W(docker run --name #{name})
|
2014-04-28 01:10:41 +00:00
|
|
|
run_cmd << "-d" if params[:detach]
|
2014-04-18 01:07:57 +00:00
|
|
|
run_cmd += env.map { |k,v| ['-e', "#{k}=#{v}"] }
|
2014-04-28 01:37:25 +00:00
|
|
|
run_cmd += expose.map { |p| ['--expose', "#{p}"] }
|
2014-05-08 18:10:37 +00:00
|
|
|
run_cmd += links.map { |k, v| ['--link', "#{k}:#{v}"] }
|
2014-03-26 23:45:46 +00:00
|
|
|
run_cmd += ports.map { |p| ['-p', p.to_s] }
|
2017-05-12 21:05:12 +00:00
|
|
|
run_cmd += volumes.map { |v|
|
|
|
|
v = v.to_s
|
|
|
|
if v.include?(":") && (Vagrant::Util::Platform.windows? || Vagrant::Util::Platform.wsl?)
|
|
|
|
host, guest = v.split(":", 2)
|
|
|
|
host = Vagrant::Util::Platform.windows_path(host)
|
|
|
|
# NOTE: Docker does not support UNC style paths (which also
|
|
|
|
# means that there's no long path support). Hopefully this
|
|
|
|
# will be fixed someday and the gsub below can be removed.
|
|
|
|
host.gsub!(/^[^A-Za-z]+/, "")
|
|
|
|
v = [host, guest].join(":")
|
|
|
|
end
|
|
|
|
['-v', v.to_s]
|
|
|
|
}
|
2014-03-27 23:51:44 +00:00
|
|
|
run_cmd += %W(--privileged) if params[:privileged]
|
2014-03-26 23:45:46 +00:00
|
|
|
run_cmd += %W(-h #{params[:hostname]}) if params[:hostname]
|
2016-08-07 17:37:50 +00:00
|
|
|
run_cmd << "-t" if params[:pty]
|
2014-08-09 19:03:35 +00:00
|
|
|
run_cmd << "--rm=true" if params[:rm]
|
2014-04-11 01:26:19 +00:00
|
|
|
run_cmd += params[:extra_args] if params[:extra_args]
|
2014-03-26 23:45:46 +00:00
|
|
|
run_cmd += [image, cmd]
|
|
|
|
|
2014-10-22 16:33:57 +00:00
|
|
|
execute(*run_cmd.flatten, **opts, &block).chomp.lines.last
|
2014-03-26 23:45:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def state(cid)
|
|
|
|
case
|
|
|
|
when running?(cid)
|
|
|
|
:running
|
|
|
|
when created?(cid)
|
|
|
|
:stopped
|
|
|
|
else
|
|
|
|
:not_created
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def created?(cid)
|
2014-03-27 23:51:44 +00:00
|
|
|
result = execute('docker', 'ps', '-a', '-q', '--no-trunc').to_s
|
2014-03-26 23:45:46 +00:00
|
|
|
result =~ /^#{Regexp.escape cid}$/
|
|
|
|
end
|
|
|
|
|
2014-04-20 15:24:43 +00:00
|
|
|
def image?(id)
|
|
|
|
result = execute('docker', 'images', '-q').to_s
|
|
|
|
result =~ /^#{Regexp.escape(id)}$/
|
|
|
|
end
|
|
|
|
|
2014-03-26 23:45:46 +00:00
|
|
|
def running?(cid)
|
2014-03-27 23:51:44 +00:00
|
|
|
result = execute('docker', 'ps', '-q', '--no-trunc')
|
2014-03-26 23:45:46 +00:00
|
|
|
result =~ /^#{Regexp.escape cid}$/m
|
|
|
|
end
|
|
|
|
|
|
|
|
def privileged?(cid)
|
|
|
|
inspect_container(cid)['HostConfig']['Privileged']
|
|
|
|
end
|
|
|
|
|
2014-10-22 20:12:31 +00:00
|
|
|
def login(email, username, password, server)
|
|
|
|
cmd = %W(docker login)
|
|
|
|
cmd += ["-e", email] if email != ""
|
|
|
|
cmd += ["-u", username] if username != ""
|
|
|
|
cmd += ["-p", password] if password != ""
|
|
|
|
cmd << server if server && server != ""
|
|
|
|
|
|
|
|
execute(*cmd.flatten)
|
|
|
|
end
|
|
|
|
|
|
|
|
def logout(server)
|
|
|
|
cmd = %W(docker logout)
|
|
|
|
cmd << server if server && server != ""
|
|
|
|
execute(*cmd.flatten)
|
|
|
|
end
|
|
|
|
|
2015-07-08 16:09:15 +00:00
|
|
|
def pull(image)
|
|
|
|
execute('docker', 'pull', image)
|
|
|
|
end
|
|
|
|
|
2014-03-26 23:45:46 +00:00
|
|
|
def start(cid)
|
2014-04-17 23:20:49 +00:00
|
|
|
if !running?(cid)
|
2014-03-26 23:45:46 +00:00
|
|
|
execute('docker', 'start', cid)
|
|
|
|
# This resets the cached information we have around, allowing `vagrant reload`s
|
|
|
|
# to work properly
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-22 00:50:45 +00:00
|
|
|
def stop(cid, timeout)
|
2014-03-26 23:45:46 +00:00
|
|
|
if running?(cid)
|
2014-10-22 00:50:45 +00:00
|
|
|
execute('docker', 'stop', '-t', timeout.to_s, cid)
|
2014-03-26 23:45:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rm(cid)
|
|
|
|
if created?(cid)
|
2014-08-29 08:51:31 +00:00
|
|
|
execute('docker', 'rm', '-f', '-v', cid)
|
2014-03-26 23:45:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-18 23:29:56 +00:00
|
|
|
def rmi(id)
|
|
|
|
execute('docker', 'rmi', id)
|
2014-04-28 16:36:55 +00:00
|
|
|
return true
|
2014-04-18 23:29:56 +00:00
|
|
|
rescue Exception => e
|
2014-04-28 16:36:55 +00:00
|
|
|
return false if e.to_s.include?("is using it")
|
2014-04-18 23:29:56 +00:00
|
|
|
raise if !e.to_s.include?("No such image")
|
|
|
|
end
|
|
|
|
|
2014-03-26 23:45:46 +00:00
|
|
|
def inspect_container(cid)
|
|
|
|
# DISCUSS: Is there a chance that this json will change after the container
|
|
|
|
# has been brought up?
|
|
|
|
@data ||= JSON.parse(execute('docker', 'inspect', cid)).first
|
|
|
|
end
|
|
|
|
|
|
|
|
def all_containers
|
2014-03-27 23:51:44 +00:00
|
|
|
execute('docker', 'ps', '-a', '-q', '--no-trunc').to_s.split
|
2014-03-26 23:45:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def docker_bridge_ip
|
|
|
|
output = execute('/sbin/ip', '-4', 'addr', 'show', 'scope', 'global', 'docker0')
|
|
|
|
if output =~ /^\s+inet ([0-9.]+)\/[0-9]+\s+/
|
|
|
|
return $1.to_s
|
|
|
|
else
|
|
|
|
# TODO: Raise an user friendly message
|
|
|
|
raise 'Unable to fetch docker bridge IP!'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-29 23:27:22 +00:00
|
|
|
def execute(*cmd, **opts, &block)
|
|
|
|
@executor.execute(*cmd, **opts, &block)
|
2014-03-26 23:45:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|