2015-09-22 15:12:14 +00:00
|
|
|
# encoding: utf-8
|
2015-10-06 16:55:44 +00:00
|
|
|
# author: Dominik Richter
|
|
|
|
|
2015-09-22 15:12:14 +00:00
|
|
|
require 'docker'
|
|
|
|
require 'yaml'
|
|
|
|
require 'concurrent'
|
|
|
|
|
|
|
|
class DockerRunner
|
|
|
|
def initialize(conf_path = nil)
|
|
|
|
@conf_path = conf_path ||
|
2015-09-24 01:10:12 +00:00
|
|
|
ENV['config']
|
2015-09-22 15:12:14 +00:00
|
|
|
unless File.file?(@conf_path)
|
|
|
|
fail "Can't find configuration in #{@conf_path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@conf = YAML.load_file(@conf_path)
|
|
|
|
if @conf.nil? or @conf.empty?
|
|
|
|
fail "Can't read coniguration in #{@conf_path}"
|
|
|
|
end
|
|
|
|
if @conf['images'].nil?
|
|
|
|
fail "You must configure test images in your #{@conf_path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@images = docker_images_by_tag
|
2015-09-24 09:20:45 +00:00
|
|
|
@image_pull_tickets = Concurrent::Semaphore.new(2)
|
2015-09-24 09:14:06 +00:00
|
|
|
@docker_run_tickets = Concurrent::Semaphore.new(5)
|
2015-09-22 15:12:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_all(&block)
|
|
|
|
fail 'You must provide a block for run_all' unless block_given?
|
|
|
|
|
|
|
|
promises = @conf['images'].map do |id|
|
|
|
|
run_on_target(id, &block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# wait for all tests to be finished
|
|
|
|
sleep(0.1) until promises.all?(&:fulfilled?)
|
|
|
|
|
|
|
|
# return resulting values
|
|
|
|
promises.map(&:value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_on_target(name, &block)
|
|
|
|
pr = Concurrent::Promise.new {
|
2015-09-22 16:00:05 +00:00
|
|
|
begin
|
|
|
|
container = start_container(name)
|
|
|
|
res = block.call(name, container)
|
|
|
|
# special rescue block to handle not implemented error
|
|
|
|
rescue NotImplementedError => err
|
|
|
|
raise err.message
|
|
|
|
end
|
|
|
|
# always stop the container
|
2015-09-22 15:12:14 +00:00
|
|
|
stop_container(container)
|
|
|
|
res
|
|
|
|
}.execute
|
|
|
|
|
|
|
|
# failure handling
|
|
|
|
pr.rescue do |err|
|
2015-09-22 15:21:07 +00:00
|
|
|
msg = "\033[31;1m#{err.message}\033[0m"
|
|
|
|
puts msg
|
|
|
|
msg + "\n" + err.backtrace.join("\n")
|
2015-09-22 15:12:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-23 23:37:14 +00:00
|
|
|
def provision_image(image, prov, files)
|
|
|
|
return image if prov['script'].nil?
|
|
|
|
path = File.join(File.dirname(@conf_path), prov['script'])
|
|
|
|
unless File.file?(path)
|
|
|
|
puts "Can't find script file #{path}"
|
|
|
|
return image
|
|
|
|
end
|
|
|
|
puts " script #{path}"
|
|
|
|
dst = "/bootstrap#{files.length}.sh"
|
|
|
|
files.push(dst)
|
|
|
|
image.insert_local('localPath' => path, 'outputPath' => dst)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bootstrap_image(name, image)
|
|
|
|
files = []
|
|
|
|
provisions = Array(@conf['provision'])
|
|
|
|
puts "--> provision docker #{name}" unless provisions.empty?
|
|
|
|
provisions.each do |prov|
|
|
|
|
image = provision_image(image, prov, files)
|
|
|
|
end
|
|
|
|
[image, files]
|
|
|
|
end
|
|
|
|
|
2015-09-22 15:12:14 +00:00
|
|
|
def start_container(name, version = nil)
|
|
|
|
unless name.include?(':')
|
|
|
|
version ||= 'latest'
|
|
|
|
name = "#{name}:#{version}"
|
|
|
|
end
|
2015-09-24 09:14:06 +00:00
|
|
|
puts "--> schedule docker #{name}"
|
2015-09-22 15:12:14 +00:00
|
|
|
|
|
|
|
image = @images[name]
|
2015-09-22 15:31:32 +00:00
|
|
|
if image.nil?
|
2015-09-22 15:41:03 +00:00
|
|
|
puts "\033[35;1m--> pull docker images #{name} "\
|
|
|
|
"(this may take a while)\033[0m"
|
|
|
|
|
2015-09-24 09:20:45 +00:00
|
|
|
@image_pull_tickets.acquire(1)
|
|
|
|
puts "... start pull image #{name}"
|
|
|
|
image = Docker::Image.create('fromImage' => name)
|
|
|
|
@image_pull_tickets.release(1)
|
2015-09-22 15:41:03 +00:00
|
|
|
|
|
|
|
unless image.nil?
|
|
|
|
puts "\033[35;1m--> pull docker images finished for #{name}\033[0m"
|
|
|
|
end
|
2015-09-22 15:31:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
fail "Can't find nor pull docker image #{name}" if image.nil?
|
2015-09-22 15:12:14 +00:00
|
|
|
|
2015-09-23 23:37:14 +00:00
|
|
|
image, scripts = bootstrap_image(name, image)
|
|
|
|
|
2015-09-24 09:20:45 +00:00
|
|
|
@docker_run_tickets.acquire(1)
|
|
|
|
|
2015-09-22 15:12:14 +00:00
|
|
|
puts "--> start docker #{name}"
|
|
|
|
container = Docker::Container.create(
|
2015-09-22 23:02:48 +00:00
|
|
|
'Cmd' => %w{sleep 3600},
|
2015-09-22 15:12:14 +00:00
|
|
|
'Image' => image.id,
|
|
|
|
'OpenStdin' => true,
|
|
|
|
)
|
|
|
|
container.start
|
2015-09-23 23:37:14 +00:00
|
|
|
|
|
|
|
scripts.each do |script|
|
|
|
|
container.exec(%w{chmod +x}.push(script))
|
|
|
|
container.exec(%w{sh -c}.push(script))
|
|
|
|
end
|
|
|
|
|
2015-09-22 15:12:14 +00:00
|
|
|
container
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_container(container)
|
2015-09-24 09:20:45 +00:00
|
|
|
@docker_run_tickets.release(1)
|
2015-09-22 15:12:14 +00:00
|
|
|
puts "--> killrm docker #{container.id}"
|
|
|
|
container.kill
|
|
|
|
container.delete(force: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# get all docker image tags
|
|
|
|
def docker_images_by_tag
|
|
|
|
images = {}
|
|
|
|
Docker::Image.all.map do |img|
|
|
|
|
Array(img.info['RepoTags']).each do |tag|
|
|
|
|
images[tag] = img
|
|
|
|
end
|
|
|
|
end
|
|
|
|
images
|
|
|
|
end
|
|
|
|
end
|