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



110
111
112
# File 'lib/dockly/build_cache/base.rb', line 110

def base_directory
  base_dir || docker.git_archive
end

#command_directoryObject



102
103
104
# File 'lib/dockly/build_cache/base.rb', line 102

def command_directory
  File.join(base_directory, command_dir)
end

#connectionObject



114
115
116
# File 'lib/dockly/build_cache/base.rb', line 114

def connection
  Dockly::AWS.s3
end

#execute!Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dockly/build_cache/base.rb', line 19

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



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

def file_output(file)
  File.join(File.dirname(output_dir), File.basename(file.path))
end

#hash_outputObject



74
75
# File 'lib/dockly/build_cache/base.rb', line 74

def hash_output
end

#insert_cacheObject



32
33
34
# File 'lib/dockly/build_cache/base.rb', line 32

def insert_cache
  push_cache(hash_output)
end

#insert_latestObject



36
37
38
39
40
41
42
43
44
# File 'lib/dockly/build_cache/base.rb', line 36

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



106
107
108
# File 'lib/dockly/build_cache/base.rb', line 106

def output_directory
  File.join(base_directory, output_dir)
end

#parameter_command(command) ⇒ Object



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

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

#parameter_output(command) ⇒ Object



77
78
# File 'lib/dockly/build_cache/base.rb', line 77

def parameter_output(command)
end

#pull_from_s3(version) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dockly/build_cache/base.rb', line 54

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)
    object = connection.get_object(s3_bucket, file_name)

    file = File.open(file_path, 'w+b')
    file.write(object.body)
    file.tap(&:rewind)
  else
    File.open(file_path, 'rb')
  end
rescue Excon::Errors::NotFound
  nil
end

#push_to_s3(file) ⇒ Object



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

def push_to_s3(file)
  ensure_present! :s3_bucket, :s3_object_prefix
  connection.put_object(s3_bucket, s3_object(hash_output), file.read)
  connection.copy_object(s3_bucket, s3_object(hash_output), s3_bucket, s3_object("latest"))
end

#s3_object(file) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/dockly/build_cache/base.rb', line 94

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)


46
47
48
49
50
51
52
# File 'lib/dockly/build_cache/base.rb', line 46

def up_to_date?
  ensure_present! :s3_bucket, :s3_object_prefix
  connection.head_object(s3_bucket, s3_object(hash_output))
  true
rescue Excon::Errors::NotFound
  false
end