2013-11-26 21:48:51 +00:00
|
|
|
require 'digest/sha1'
|
|
|
|
|
|
|
|
module VagrantPlugins
|
|
|
|
module Docker
|
2013-12-12 19:54:20 +00:00
|
|
|
class Client
|
2013-11-26 21:48:51 +00:00
|
|
|
def initialize(machine)
|
|
|
|
@machine = machine
|
|
|
|
end
|
|
|
|
|
2014-01-14 17:22:55 +00:00
|
|
|
def build_images(images)
|
2013-11-26 21:48:51 +00:00
|
|
|
@machine.communicate.tap do |comm|
|
2014-01-14 17:22:55 +00:00
|
|
|
images.each do |path, opts|
|
|
|
|
@machine.ui.info(I18n.t("vagrant.docker_building_single", path: path))
|
2014-01-16 01:33:55 +00:00
|
|
|
comm.sudo("docker build #{opts[:args]} #{path}")
|
2013-11-26 21:48:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-14 17:22:55 +00:00
|
|
|
def pull_images(*images)
|
2013-12-09 22:52:45 +00:00
|
|
|
@machine.communicate.tap do |comm|
|
|
|
|
images.each do |image|
|
2014-01-14 17:22:55 +00:00
|
|
|
@machine.ui.info(I18n.t("vagrant.docker_pulling_single", name: image))
|
2014-02-03 15:26:38 +00:00
|
|
|
comm.sudo("docker pull #{image}")
|
2013-12-09 22:52:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-26 21:48:51 +00:00
|
|
|
def start_service
|
|
|
|
if !daemon_running? && @machine.guest.capability?(:docker_start_service)
|
|
|
|
@machine.guest.capability(:docker_start_service)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def daemon_running?
|
2014-04-10 20:42:12 +00:00
|
|
|
@machine.guest.capability(:docker_daemon_running)
|
2013-11-26 21:48:51 +00:00
|
|
|
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]})"
|
|
|
|
|
2013-12-05 00:07:41 +00:00
|
|
|
if container_exists?(id)
|
2013-11-26 21:48:51 +00:00
|
|
|
start_container(id)
|
|
|
|
else
|
|
|
|
create_container(config)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-05 00:07:41 +00:00
|
|
|
def container_exists?(id)
|
2013-12-05 00:13:17 +00:00
|
|
|
lookup_container(id, true)
|
2013-11-26 21:48:51 +00:00
|
|
|
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)
|
2013-12-05 00:13:17 +00:00
|
|
|
lookup_container(id)
|
2013-11-26 21:48:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_container(config)
|
2014-03-30 05:49:52 +00:00
|
|
|
args = "--cidfile=#{config[:cidfile]} "
|
2014-02-03 15:14:59 +00:00
|
|
|
args << "-d " if config[:daemonize]
|
2014-03-30 05:49:52 +00:00
|
|
|
args << "--name #{config[:name]} " if config[:name] && config[:auto_assign_name]
|
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]}
|
2014-03-03 15:30:42 +00:00
|
|
|
docker run #{args} #{config[:image]} #{config[:cmd]}
|
2013-11-26 21:48:51 +00:00
|
|
|
]
|
|
|
|
end
|
2013-12-05 00:13:17 +00:00
|
|
|
|
|
|
|
def lookup_container(id, list_all = false)
|
|
|
|
docker_ps = "sudo docker ps -q"
|
|
|
|
docker_ps << " -a" if list_all
|
|
|
|
@machine.communicate.tap do |comm|
|
|
|
|
# Docker < 0.7.0 stores container IDs using its short version while
|
|
|
|
# recent versions use the full container ID
|
|
|
|
# See https://github.com/dotcloud/docker/pull/2140 for more information
|
|
|
|
return comm.test("#{docker_ps} | grep -wFq #{id}") ||
|
|
|
|
comm.test("#{docker_ps} -notrunc | grep -wFq #{id}")
|
|
|
|
end
|
|
|
|
end
|
2013-11-26 21:48:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|