Class: Flock::Sandbox::Docker

Inherits:
Object
  • Object
show all
Defined in:
lib/flock/sandbox.rb

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ Docker

Returns a new instance of Docker.



72
73
74
75
# File 'lib/flock/sandbox.rb', line 72

def initialize(definition)
  @definition = definition
  ::Docker.options = {}
end

Instance Method Details

#runObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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