2013-11-26 21:48:51 +00:00
|
|
|
require 'digest/sha1'
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module Docker
|
|
|
|
class DockerClient
|
|
|
|
def initialize(machine)
|
|
|
|
@machine = machine
|
|
|
|
end
|
|
|
|
|
|
|
|
def pull_images(*images)
|
|
|
|
@machine.communicate.tap do |comm|
|
|
|
|
images.each do |image|
|
2013-12-03 22:38:38 +00:00
|
|
|
@machine.ui.info(I18n.t("vagrant.docker_pulling_single", name: image))
|
2013-11-26 21:48:51 +00:00
|
|
|
comm.sudo("docker images | grep -q #{image} || docker pull #{image}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_service
|
|
|
|
if !daemon_running? && @machine.guest.capability?(:docker_start_service)
|
|
|
|
@machine.guest.capability(:docker_start_service)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def daemon_running?
|
|
|
|
@machine.communicate.test('test -f /var/run/docker.pid')
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(containers)
|
|
|
|
containers.each do |name, config|
|
2013-11-29 03:26:35 +00:00
|
|
|
cids_dir = "/var/lib/vagrant/cids"
|
2013-11-26 21:48:51 +00:00
|
|
|
config[:cidfile] ||= "#{cids_dir}/#{Digest::SHA1.hexdigest name}"
|
|
|
|
|
2013-12-03 22:33:13 +00:00
|
|
|
@machine.ui.info(I18n.t("vagrant.docker_running", name: name))
|
2013-11-26 21:48:51 +00:00
|
|
|
@machine.communicate.sudo("mkdir -p #{cids_dir}")
|
2013-12-03 22:33:13 +00:00
|
|
|
run_container({
|
|
|
|
name: name
|
|
|
|
}.merge(config))
|
2013-11-26 21:48:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_container(config)
|
2013-12-03 22:36:14 +00:00
|
|
|
raise "Container's cidfile was not provided!" if !config[:cidfile]
|
2013-11-26 21:48:51 +00:00
|
|
|
|
|
|
|
id = "$(cat #{config[:cidfile]})"
|
|
|
|
|
|
|
|
if container_exist?(id)
|
|
|
|
start_container(id)
|
|
|
|
else
|
|
|
|
create_container(config)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def container_exist?(id)
|
|
|
|
@machine.communicate.test("sudo docker ps -a -q | grep -q #{id}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_container(id)
|
2013-12-03 22:36:14 +00:00
|
|
|
if !container_running?(id)
|
2013-11-26 21:48:51 +00:00
|
|
|
@machine.communicate.sudo("docker start #{id}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def container_running?(id)
|
|
|
|
@machine.communicate.test("sudo docker ps -q | grep #{id}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_container(config)
|
|
|
|
args = "-cidfile=#{config[:cidfile]} -d "
|
2013-12-03 22:35:56 +00:00
|
|
|
args << config[:args] if config[:args]
|
2013-11-26 21:48:51 +00:00
|
|
|
@machine.communicate.sudo %[
|
|
|
|
rm -f #{config[:cidfile]}
|
|
|
|
docker run #{args} #{config[:image]} #{config[:cmd]}
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|