Class: Dockly::BuildCache::Base

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

Direct Known Subclasses

Docker, Local

Instance Method Summary collapse

Instance Method Details

#base_directoryObject



126
127
128
# File 'lib/dockly/build_cache/base.rb', line 126

def base_directory
  base_dir || docker.git_archive
end

#command_directoryObject



118
119
120
# File 'lib/dockly/build_cache/base.rb', line 118

def command_directory
  File.join(base_directory, command_dir)
end

#connectionObject



130
131
132
# File 'lib/dockly/build_cache/base.rb', line 130

def connection
  Dockly.s3
end

#execute!Object



22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



106
107
108
# 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_outputObject



80
81
# File 'lib/dockly/build_cache/base.rb', line 80

def hash_output
end

#insert_cacheObject



35
36
37
# File 'lib/dockly/build_cache/base.rb', line 35

def insert_cache
  push_cache(hash_output)
end

#insert_latestObject



39
40
41
42
43
44
45
46
47
# 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_directoryObject



122
123
124
# File 'lib/dockly/build_cache/base.rb', line 122

def output_directory
  File.join(base_directory, output_dir)
end

#parameter_command(command) ⇒ Object



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

def parameter_command(command)
  parameter_commands[command] = nil
end

#parameter_output(command) ⇒ Object



83
84
# File 'lib/dockly/build_cache/base.rb', line 83

def parameter_output(command)
end

#pull_from_s3(version) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Object



110
111
112
113
114
115
116
# 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?Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# 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