Class: Dockly::BuildCache::Docker

Inherits:
Base
  • Object
show all
Defined in:
lib/dockly/build_cache/docker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#base_directory, #command_directory, #connection, #file_output, #insert_cache, #insert_latest, #output_directory, #parameter_command, #pull_from_s3, #push_to_s3, #s3_object, #up_to_date?

Instance Attribute Details

#imageObject

Returns the value of attribute image.



2
3
4
# File 'lib/dockly/build_cache/docker.rb', line 2

def image
  @image
end

Instance Method Details

#copy_output_dir(container) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/dockly/build_cache/docker.rb', line 36

def copy_output_dir(container)
  ensure_present! :output_dir
  file_path = File.join(tmp_dir,s3_object(hash_output))
  FileUtils.mkdir_p(File.dirname(file_path))
  file = File.open(file_path, 'w+b')
  container.wait(3600) # 1 hour max timeout
  container.copy(output_directory) { |chunk| file.write(chunk) }
  file.tap(&:rewind)
end

#execute!Object



4
5
6
7
8
# File 'lib/dockly/build_cache/docker.rb', line 4

def execute!
  ensure_present! :image
  super
  image
end

#hash_outputObject



46
47
48
49
50
51
52
53
# File 'lib/dockly/build_cache/docker.rb', line 46

def hash_output
  ensure_present! :image, :hash_command
  @hash_output ||= begin
    status, body, container = run_command(hash_command)
    raise "Hash Command `#{hash_command}` failed to run" unless status.zero?
    body
  end
end

#parameter_output(command) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/dockly/build_cache/docker.rb', line 55

def parameter_output(command)
  ensure_present! :image
  raise "Parameter Command tried to run but not found" unless parameter_commands.keys.include?(command)
  @parameter_commands[command] ||= begin
    status, body, container = run_command(command)
    raise "Parameter Command `#{command}` failed to run" unless status.zero?
    body
  end
end

#push_cache(version) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dockly/build_cache/docker.rb', line 20

def push_cache(version)
  ensure_present! :output_dir
  if cache = pull_from_s3(version)
    debug "inserting to #{output_directory}"
    container = image.run("mkdir -p #{File.dirname(output_directory)}")
    image_with_dir = container.tap { |c| c.wait }.commit
    self.image = image_with_dir.insert_local(
      'localPath' => cache.path,
      'outputPath' => File.dirname(output_directory)
    )
    cache.close
  else
    info "could not find #{s3_object(version)}"
  end
end

#run_buildObject



10
11
12
13
14
15
16
17
18
# File 'lib/dockly/build_cache/docker.rb', line 10

def run_build
  status, body, container = run_command(build_command)
  raise "Build Cache `#{build_command}` failed to run." unless status.zero?
  cache = copy_output_dir(container)
  debug "pushing #{output_directory} to s3"
  push_to_s3(cache)
  cache.close
  self.image = container.commit
end

#run_command(command) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/dockly/build_cache/docker.rb', line 65

def run_command(command)
  resp = ""
  container = image.run(["/bin/bash", "-lc", "cd #{command_directory} && #{command}"])
  container.attach { |source,chunk| resp += chunk }
  status = container.wait['StatusCode']
  [status, resp.strip, container]
end