Class: Linecook::Baker::Docker

Inherits:
Object
  • Object
show all
Includes:
Locking
Defined in:
lib/linecook-gem/baker/docker.rb

Overview

FIXME - refactor into a base class with an interface

Constant Summary collapse

RETAIN_IMAGES =

number of latest images to retain

3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Locking

#clear_lock, #lock, #lock_path, #lockfile, #unlock

Constructor Details

#initialize(image, config) ⇒ Docker

Returns a new instance of Docker.



20
21
22
23
24
# File 'lib/linecook-gem/baker/docker.rb', line 20

def initialize(image, config)
  @image = image
  @config = config
  munge_config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/linecook-gem/baker/docker.rb', line 18

def config
  @config
end

Instance Method Details

#convergeObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/linecook-gem/baker/docker.rb', line 43

def converge
  if @inherited
    begin
      instance.create
    rescue
      puts "Disabling docker cache"
      # Disable the cache and retry if we ran into a problem
      driver_config = instance.driver.send(:config)
      driver_config[:use_cache] = false

      with_retries(5) do
        instance.create
      end
    end
  end
  instance.converge
ensure
  unlock("create_#{@inherited.id}") if @inherited
end

#destroyObject



63
64
65
66
67
68
# File 'lib/linecook-gem/baker/docker.rb', line 63

def destroy
  container.delete(force: true)
  instance.destroy
rescue ::Docker::Error::NotFoundError => e
  puts e.message
end

#exec(command) ⇒ Object



70
71
72
73
# File 'lib/linecook-gem/baker/docker.rb', line 70

def exec(command)
  command = ['/bin/bash', '-c', command]
  container.exec(command, tty: true)
end

#inherit(image) ⇒ Object



75
76
77
78
79
# File 'lib/linecook-gem/baker/docker.rb', line 75

def inherit(image)
  puts "Inheriting from #{image.id}..."
  import(image) unless image_exists?(image)
  @inherited = image
end

#instanceObject



39
40
41
# File 'lib/linecook-gem/baker/docker.rb', line 39

def instance
  @instance ||= @config.instances.find {|x| @image.name == x.suite.name }
end

#saveObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/linecook-gem/baker/docker.rb', line 27

def save
  FileUtils.mkdir_p(File.dirname(@image.path))
  with_retries(5) do
    # You might be wondering "wtf is this"? And how!
    # tl;dr, we want to take the bitwise OR of the return codes for everything in the pipe.
    # so, if any command in the pipe fails, treat the whole pipe to have failed.
    # otherwise, we could end up with xz compressing an invalid export, and treating it as OK.
    status = system("/bin/bash -c 'docker export #{@image.id} | xz -T 0 -0 > #{@image.path}; exit $((${PIPESTATUS[0]} | ${PIPESTATUS[1]}))'")
    fail "Export failed" unless status
  end
end