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
|
|
|
|
|
|
|
|
def create(params)
|
|
|
|
image = params.fetch(:image)
|
|
|
|
ports = Array(params[:ports])
|
|
|
|
volumes = Array(params[:volumes])
|
|
|
|
name = params.fetch(:name)
|
|
|
|
cmd = Array(params.fetch(:cmd))
|
|
|
|
|
2014-03-27 23:51:44 +00:00
|
|
|
run_cmd = %W(docker run --name #{name} -d)
|
2014-03-26 23:45:46 +00:00
|
|
|
run_cmd += ports.map { |p| ['-p', p.to_s] }
|
|
|
|
run_cmd += volumes.map { |v| ['-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]
|
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-03-28 00:11:19 +00:00
|
|
|
execute(*run_cmd.flatten).chomp
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
# TODO: Add spec to verify this behavior
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop(cid)
|
|
|
|
if running?(cid)
|
|
|
|
execute('docker', 'stop', '-t', '1', cid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rm(cid)
|
|
|
|
if created?(cid)
|
|
|
|
execute('docker', 'rm', '-v', cid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def execute(*cmd, &block)
|
2014-04-11 01:26:19 +00:00
|
|
|
@executor.execute(*cmd, &block)
|
2014-03-26 23:45:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|