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



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

def base_directory
  base_dir || docker.git_archive
end

#command_directoryObject



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

def command_directory
  File.join(base_directory, command_dir)
end

#connectionObject



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

def connection
  Dockly::AWS.s3
end

#execute!Object



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

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



95
96
97
# File 'lib/dockly/build_cache/base.rb', line 95

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

#hash_outputObject



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

def hash_output
end

#insert_cacheObject



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

def insert_cache
  push_cache(hash_output)
end

#insert_latestObject



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

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



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

def output_directory
  File.join(base_directory, output_dir)
end

#parameter_command(command) ⇒ Object



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

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

#parameter_output(command) ⇒ Object



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

def parameter_output(command)
end

#pull_from_s3(version) ⇒ Object



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

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(s3_bucket, file_name)
    debug 'Pulled build cache from s3'

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

#push_to_s3(file) ⇒ Object



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

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



99
100
101
102
103
104
105
# File 'lib/dockly/build_cache/base.rb', line 99

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)


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

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