Class: Dockly::Docker

Inherits:
Object
  • Object
show all
Includes:
Util::DSL, Util::Logger::Mixin
Defined in:
lib/dockly/docker.rb

Instance Method Summary collapse

Instance Method Details

#add_git_archive(image) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/dockly/docker.rb', line 105

def add_git_archive(image)
  return image if git_archive.nil?

  image.insert_local(
    'localPath' => git_archive_tar,
    'outputPath' => '/'
  )
end

#build_image(image) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/dockly/docker.rb', line 114

def build_image(image)
  ensure_present! :name, :build
  info "starting build from #{image.id}"
  out_image = ::Docker::Image.build("from #{image.id}\n#{build}")
  info "built the image: #{out_image.id}"
  out_image.tag(:repo => name, :tag => tag)
  out_image
end

#ensure_tar(file_name) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/dockly/docker.rb', line 60

def ensure_tar(file_name)
  if Dockly::Util::Tar.is_tar?(file_name)
    file_name
  elsif Dockly::Util::Tar.is_gzip?(file_name)
    file_name
  else
    raise "Expected a (possibly gzipped) tar: #{file_name}"
  end
end

#export_filenameObject



42
43
44
# File 'lib/dockly/docker.rb', line 42

def export_filename
  "#{name}-image.tgz"
end

#export_image(image) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dockly/docker.rb', line 123

def export_image(image)
  ensure_present! :name, :build_dir
  container = ::Docker::Container.create('Image' => image.id, 'Cmd' => %w[true])
  info "created the container: #{container.id}"
  Zlib::GzipWriter.open(tar_path) do |file|
    container.export do |chunk, remaining, total|
      file.write(chunk)
    end
  end
  info "done writing the docker tar: #{export_filename}"
end

#fetch_importObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/dockly/docker.rb', line 135

def fetch_import
  ensure_present! :import
  path = "/tmp/dockly-docker-import.#{name}.#{File.basename(import)}"

  if File.exist?(path)
    debug "already fetched #{import}"
  else
    debug "fetching #{import}"
    File.open("#{path}.tmp", 'wb') do |file|
      case import
        when /^s3:\/\/(?<bucket_name>.+?)\/(?<object_path>.+)$/
          connection.get_object(Regexp.last_match[:bucket_name],
                                Regexp.last_match[:object_path]) do |chunk, remaining, total|
            file.write(chunk)
          end
        when /^https?:\/\//
          Excon.get(import, :response_block => lambda { |chunk, remaining, total|
            file.write(chunk)
          })
        else
          raise "You can only import from S3 or a public url"
      end
    end
    FileUtils.mv("#{path}.tmp", path, :force => true)
  end
  path
end

#generate!Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dockly/docker.rb', line 25

def generate!
  Docker.options = { :read_timeout => timeout, :write_timeout => timeout }
  docker_tar = File.absolute_path(ensure_tar(fetch_import))

  import = import_base(docker_tar)

  cleanup = add_git_archive(import)
  cleanup = run_build_caches(cleanup)
  cleanup = build_image(cleanup)

  export_image(cleanup)

  true
ensure
  cleanup.remove if cleanup_images && !cleanup.nil?
end

#git_archive_dirObject



86
87
88
# File 'lib/dockly/docker.rb', line 86

def git_archive_dir
  @git_archive_dir ||= File.join(build_dir, "gitarc")
end

#git_archive_pathObject



90
91
92
# File 'lib/dockly/docker.rb', line 90

def git_archive_path
  "#{git_archive_dir}/#{name}.tar"
end

#git_archive_tarObject



94
95
96
# File 'lib/dockly/docker.rb', line 94

def git_archive_tar
  git_archive && File.absolute_path(make_git_archive)
end

#import_base(docker_tar) ⇒ Object



98
99
100
101
102
103
# File 'lib/dockly/docker.rb', line 98

def import_base(docker_tar)
  info "importing the docker image from #{docker_tar}"
  image = ::Docker::Image.import(docker_tar)
  info "imported docker image: #{image.id}"
  image
end

#make_git_archiveObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dockly/docker.rb', line 70

def make_git_archive
  ensure_present! :git_archive
  info "initializing"

  prefix = git_archive
  prefix += '/' unless prefix.end_with?('/')

  FileUtils.rm_rf(git_archive_dir)
  FileUtils.mkdir_p(git_archive_dir)
  info "archiving #{Dockly::Util::Git.git_sha}"
  Grit::Git.with_timeout(120) do
    Dockly::Util::Git.git_repo.archive_to_file(Dockly::Util::Git.git_sha, prefix, git_archive_path, 'tar', 'cat')
  end
  git_archive_path
end

#repository(value = nil) ⇒ Object



163
164
165
# File 'lib/dockly/docker.rb', line 163

def repository(value = nil)
  name(value)
end

#run_build_caches(image) ⇒ Object



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

def run_build_caches(image)
  info "starting build caches"
  (build_cache || []).each do |cache|
    cache.image = image
    image = cache.execute!
  end
  info "finished build caches"
  image
end

#tar_pathObject



56
57
58
# File 'lib/dockly/docker.rb', line 56

def tar_path
  File.join(build_dir, export_filename)
end