Class: Omnibus::S3Cache

Inherits:
Object
  • Object
show all
Extended by:
Digestable, S3Helpers
Includes:
Logging
Defined in:
lib/omnibus/s3_cache.rb

Class Method Summary collapse

Methods included from S3Helpers

included

Methods included from Digestable

digest, digest_directory, included

Methods included from Logging

included

Class Method Details

.fetch_missingtrue

Fetch all source tarballs onto the local machine.

Returns:

  • (true)


98
99
100
101
102
103
104
# File 'lib/omnibus/s3_cache.rb', line 98

def fetch_missing
  missing.each do |software|
    without_caching do
      software.fetch
    end
  end
end

.key_for(software) ⇒ String

The key with which to cache the package on S3. This is the name of the package, the version of the package, and its md5 checksum.

Examples:

"zlib-1.2.6-618e944d7c7cd6521551e30b32322f4a"

Parameters:

Returns:

  • (String)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/omnibus/s3_cache.rb', line 119

def key_for(software)
  unless software.name
    raise InsufficientSpecification.new(:name, software)
  end

  unless software.version
    raise InsufficientSpecification.new(:version, software)
  end

  unless software.fetcher.checksum
    raise InsufficientSpecification.new("source md5 checksum", software)
  end

  "#{software.name}-#{software.version}-#{software.fetcher.checksum}"
end

.keysArray<String>

The list of objects in the cache, by their key.

Returns:

  • (Array<String>)


45
46
47
# File 'lib/omnibus/s3_cache.rb', line 45

def keys
  bucket.objects.map(&:key)
end

.listArray<Software>

List all software in the cache.

Returns:



32
33
34
35
36
37
38
# File 'lib/omnibus/s3_cache.rb', line 32

def list
  cached = keys
  softwares.select do |software|
    key = key_for(software)
    cached.include?(key)
  end
end

.missingArray<Software>

List all software missing from the cache.

Returns:



54
55
56
57
58
59
60
# File 'lib/omnibus/s3_cache.rb', line 54

def missing
  cached = keys
  softwares.select do |software|
    key = key_for(software)
    !cached.include?(key)
  end
end

.populatetrue

Populate the cache with the all the missing software definitions.

Returns:

  • (true)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/omnibus/s3_cache.rb', line 67

def populate
  missing.each do |software|
    without_caching do
      software.fetch
    end

    key     = key_for(software)
    fetcher = software.fetcher

    log.info(log_key) do
      "Caching '#{fetcher.downloaded_file}' to '#{Config.s3_bucket}/#{key}'"
    end

    # Fetcher has already verified the downloaded file in software.fetch.
    # Compute the md5 from scratch because the fetcher may have been
    # specified with a different hashing algorithm.
    md5 = digest(fetcher.downloaded_file, :md5)

    File.open(fetcher.downloaded_file, "rb") do |file|
      store_object(key, file, md5, "public-read")
    end
  end

  true
end

.url_for(software) ⇒ Object



135
136
137
# File 'lib/omnibus/s3_cache.rb', line 135

def url_for(software)
  client.bucket(Config.s3_bucket).object(S3Cache.key_for(software)).public_url
end