class Flock::Sandbox::Docker

Public Class Methods

new(definition) click to toggle source
# File lib/flock/sandbox.rb, line 72
def initialize(definition)
  @definition = definition
  ::Docker.options = {}
end

Public Instance Methods

run() click to toggle source
# File lib/flock/sandbox.rb, line 77
def run
  containers = []
  begin
    unless ENV['ONLY_SCRIPTS']
      @definition.containers.each do |c|
        puts "Start container #{c[:name]} (#{c[:image]})"
        ::Docker::Image.create('fromImage' => c[:image])
        container = ::Docker::Container.create(
          'Image' => c[:image],
          'name' => c[:name],
          'Env' => c[:envs],
          'HostConfig' => { 'Links' => c[:links] })
        containers << container
        container.start
      end
    end
    if ENV['MANUAL_SCRIPTS']
      @definition.scripts.each do |c|
        puts "docker run #{(c[:links] || [])
            .map { |v| "--link #{v}" } * ' ' } #{(c[:envs] || [])
            .map { |e| "-e #{e}" } * ' ' } #{c[:image]} #{c[:cmd] * ' '}"
      end
    else
      @definition.scripts.each do |c|
        puts "Run script #{c[:name]} (#{c[:image]} #{c[:cmd].inspect})"
        ::Docker::Image.create('fromImage' => c[:image])
        container = ::Docker::Container.create(
          'Cmd' => c[:cmd],
          'Image' => c[:image],
          'Env' => c[:envs],
          'HostConfig' => { 'Links' => c[:links] })
        containers << container
        container.start
        res = container.wait(1200)
        logs = container.logs(stdout: true, stderr: true)
        puts logs
        if res['StatusCode'] != 0
          puts "Error in script #{c[:name]}: error code #{res['StatusCode']}"
          exit res['StatusCode']
        end
      end
    end
  ensure
    unless ENV['MANUAL_SCRIPTS']
      containers.each do |c|
        c.kill
        c.delete
      end
    end
  end
end