Class: Hoosegow::ImageBundle

Inherits:
Object
  • Object
show all
Defined in:
lib/hoosegow/image_bundle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dockerfileObject

Public: The source for the Dockerfile. Defaults to Dockerfile in the hoosegow gem.



6
7
8
# File 'lib/hoosegow/image_bundle.rb', line 6

def dockerfile
  @dockerfile
end

#ruby_versionObject

Public: The ruby version to install on the container.



9
10
11
# File 'lib/hoosegow/image_bundle.rb', line 9

def ruby_version
  @ruby_version
end

Instance Method Details

#add(glob, options) ⇒ Object

Public: Include files in the bundle.

To add all files in “root” to the root of the bundle:

add("root/*")

To add all files other than files that start with “.” to the root of the bundle:

add("root/*", :ignore_hidden => true)

To add all files in “lib” to “vendor/lib”:

add("lib/*", :prefix => "vendor/lib")
add("lib", :prefix => "vendor")


22
23
24
# File 'lib/hoosegow/image_bundle.rb', line 22

def add(glob, options)
  definition << options.merge(:glob => glob)
end

#exclude(path) ⇒ Object

Public: Exclude files from the bundle.

To exclude “Gemfile.lock”:

exclude("Gemfile.lock")


30
31
32
# File 'lib/hoosegow/image_bundle.rb', line 30

def exclude(path)
  excludes << path
end

#image_nameObject

Public: The default name of the docker image, based on the tarball’s hash.



35
36
37
# File 'lib/hoosegow/image_bundle.rb', line 35

def image_name
  (tarball && @image_name)
end

#tarballObject

Tarball of this gem and the inmate file. Used for building an image.

Returns the tar file’s bytes.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hoosegow/image_bundle.rb', line 42

def tarball
  return @tarball if defined? @tarball

  require 'open3'
  Dir.mktmpdir do |tmpdir|
    definition.each do |options|
      glob          = options.fetch(:glob)
      prefix        = options[:prefix]
      ignore_hidden = options[:ignore_hidden]

      files = Dir[glob]
      files.reject! { |f| f.start_with?('.') } if ignore_hidden

      dest = prefix ? File.join(tmpdir, prefix) : tmpdir

      FileUtils.mkpath(dest)
      FileUtils.cp_r(files, dest)
    end

    excludes.each do |path|
      full_path = File.join(tmpdir, path)
      if File.file?(full_path)
        File.unlink(File.join(tmpdir, path))
      end
    end

    # Specify the correct ruby version in the Dockerfile.
    bundle_dockerfile = File.join(tmpdir, "Dockerfile")
    content = IO.read(bundle_dockerfile)
    content = content.gsub("{{ruby_version}}", ruby_version)
    IO.write bundle_dockerfile, content

    if dockerfile
      File.unlink bundle_dockerfile
      FileUtils.cp dockerfile, bundle_dockerfile
    end

    # Find hash of all files we're sending over.
    digest = Digest::SHA1.new
    Dir[File.join(tmpdir, '**/*')].each do |path|
      if File.file? path
        open path, 'r' do |file|
          digest.update file.read
        end
      end
    end
    @image_name = "hoosegow:#{digest.hexdigest}"

    # Create tarball of the tmpdir.
    stdout, stderr, status = Open3.capture3 'tar', '-c', '-C', tmpdir, '.'

    raise Hoosegow::ImageBuildError, stderr unless stderr.empty?

    @tarball = stdout
  end
end