class Dockly::BuildCache::Base

Public Instance Methods

base_directory() click to toggle source
# File lib/dockly/build_cache/base.rb, line 126
def base_directory
  base_dir || docker.git_archive
end
command_directory() click to toggle source
# File lib/dockly/build_cache/base.rb, line 118
def command_directory
  File.join(base_directory, command_dir)
end
connection() click to toggle source
# File lib/dockly/build_cache/base.rb, line 130
def connection
  Dockly.s3
end
execute!() click to toggle source
# File lib/dockly/build_cache/base.rb, line 22
def execute!
  debug "Looking for cache for hash: #{hash_output}"
  if up_to_date?
    debug "build cache up to date, pulling from s3"
    insert_cache
  else
    insert_latest
    debug "build cache out of date, running build"
    run_build
  end
  debug "finished build cache"
end
file_output(file) click to toggle source
# File lib/dockly/build_cache/base.rb, line 106
def file_output(file)
  File.join(File.dirname(output_dir), File.basename(file.path))
end
hash_output() click to toggle source
# File lib/dockly/build_cache/base.rb, line 80
def hash_output
end
insert_cache() click to toggle source
# File lib/dockly/build_cache/base.rb, line 35
def insert_cache
  push_cache(hash_output)
end
insert_latest() click to toggle source
# File lib/dockly/build_cache/base.rb, line 39
def insert_latest
  if use_latest
    debug "attempting to push latest"
    if cache = push_cache("latest")
      debug "pushed latest, removing local file"
      File.delete(cache.path)
    end
  end
end
output_directory() click to toggle source
# File lib/dockly/build_cache/base.rb, line 122
def output_directory
  File.join(base_directory, output_dir)
end
parameter_command(command) click to toggle source
# File lib/dockly/build_cache/base.rb, line 86
def parameter_command(command)
  parameter_commands[command] = nil
end
parameter_output(command) click to toggle source
# File lib/dockly/build_cache/base.rb, line 83
def parameter_output(command)
end
pull_from_s3(version) click to toggle source
# File lib/dockly/build_cache/base.rb, line 57
def pull_from_s3(version)
  ensure_present! :s3_bucket, :s3_object_prefix

  file_name = s3_object(version)
  file_path = File.join(tmp_dir,file_name)

  FileUtils.mkdir_p(File.dirname(file_path))
  unless File.exist?(file_path)
    debug 'Pulling build cache from s3'
    object = connection.get_object(bucket: s3_bucket, key: file_name)
    debug 'Pulled build cache from s3'

    file = File.open(file_path, 'w+b')
    file.write(object.body.read)
    file.tap(&:rewind)
  else
    info 'Build cache already exists locally'
    File.open(file_path, 'rb')
  end
rescue Aws::S3::Errors::NotFound, Aws::S3::Errors::NoSuchKey
  nil
end
push_to_s3(file) click to toggle source
# File lib/dockly/build_cache/base.rb, line 90
def push_to_s3(file)
  ensure_present! :s3_bucket, :s3_object_prefix
  connection.put_object(
    bucket: s3_bucket,
    key: s3_object(hash_output),
    body: file,
    acl: 'bucket-owner-full-control',
  )
  connection.copy_object(
    copy_source: [s3_bucket, s3_object(hash_output)].join('/'),
    bucket: s3_bucket,
    key: s3_object('latest'),
    acl: 'bucket-owner-full-control',
  )
end
s3_object(file) click to toggle source
# File lib/dockly/build_cache/base.rb, line 110
def s3_object(file)
  output = "#{s3_object_prefix}"
  parameter_commands.each do |parameter_command, _|
    output << "#{parameter_output(parameter_command)}_" unless parameter_output(parameter_command).nil?
  end
  output << "#{file}"
end
up_to_date?() click to toggle source
# File lib/dockly/build_cache/base.rb, line 49
def up_to_date?
  ensure_present! :s3_bucket, :s3_object_prefix
  connection.head_object(bucket: s3_bucket, key: s3_object(hash_output))
  true
rescue Aws::S3::Errors::NotFound, Aws::S3::Errors::NoSuchKey
  false
end