Class: CookbookOmnifetch::MetadataBasedInstaller
- Inherits:
-
Object
- Object
- CookbookOmnifetch::MetadataBasedInstaller
- Defined in:
- lib/cookbook-omnifetch/metadata_based_installer.rb
Defined Under Namespace
Classes: CookbookMetadata
Instance Attribute Summary collapse
-
#http_client ⇒ Object
readonly
Returns the value of attribute http_client.
-
#install_path ⇒ Object
readonly
Returns the value of attribute install_path.
-
#slug ⇒ Object
readonly
Returns the value of attribute slug.
-
#url_path ⇒ Object
readonly
Returns the value of attribute url_path.
Instance Method Summary collapse
-
#clean_cache(staging_path, metadata) ⇒ Object
Removes files from cache that are not supposed to be there, based on files in metadata.
-
#file_outdated?(path, expected_md5sum) ⇒ TrueClass, FalseClass
Check if a given file (at absolute path) is missing or does has a mismatched md5sum.
-
#initialize(http_client:, url_path:, install_path:) ⇒ MetadataBasedInstaller
constructor
A new instance of MetadataBasedInstaller.
- #install ⇒ Object
-
#sync_cache(staging_path, metadata) ⇒ Object
Downloads any out-of-date files into installer cache, overwriting those that don’t match the checksum provided the metadata @ url_path.
Constructor Details
#initialize(http_client:, url_path:, install_path:) ⇒ MetadataBasedInstaller
Returns a new instance of MetadataBasedInstaller.
43 44 45 46 47 48 |
# File 'lib/cookbook-omnifetch/metadata_based_installer.rb', line 43 def initialize(http_client:, url_path:, install_path:) @http_client = http_client @url_path = url_path @install_path = install_path @slug = Kernel.rand(1_000_000_000).to_s end |
Instance Attribute Details
#http_client ⇒ Object (readonly)
Returns the value of attribute http_client.
38 39 40 |
# File 'lib/cookbook-omnifetch/metadata_based_installer.rb', line 38 def http_client @http_client end |
#install_path ⇒ Object (readonly)
Returns the value of attribute install_path.
40 41 42 |
# File 'lib/cookbook-omnifetch/metadata_based_installer.rb', line 40 def install_path @install_path end |
#slug ⇒ Object (readonly)
Returns the value of attribute slug.
41 42 43 |
# File 'lib/cookbook-omnifetch/metadata_based_installer.rb', line 41 def slug @slug end |
#url_path ⇒ Object (readonly)
Returns the value of attribute url_path.
39 40 41 |
# File 'lib/cookbook-omnifetch/metadata_based_installer.rb', line 39 def url_path @url_path end |
Instance Method Details
#clean_cache(staging_path, metadata) ⇒ Object
Removes files from cache that are not supposed to be there, based on files in metadata.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cookbook-omnifetch/metadata_based_installer.rb', line 61 def clean_cache(staging_path, ) actual_file_list = Dir.glob(File.join(staging_path, "**/*")) expected_file_list = [] CookbookMetadata.new().files { |_, path, _| expected_file_list << File.join(staging_path, path) } extra_files = actual_file_list - expected_file_list extra_files.each do |path| if File.file?(path) FileUtils.rm(path) end end end |
#file_outdated?(path, expected_md5sum) ⇒ TrueClass, FalseClass
Check if a given file (at absolute path) is missing or does has a mismatched md5sum
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/cookbook-omnifetch/metadata_based_installer.rb', line 96 def file_outdated?(path, expected_md5sum) return true unless File.exist?(path) md5 = Digest::MD5.new File.open(path, "r") do |file| while (chunk = file.read(1024)) md5.update chunk end end md5.to_s != expected_md5sum end |
#install ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/cookbook-omnifetch/metadata_based_installer.rb', line 50 def install StagingArea.stage(install_path) do |staging_path| FileUtils.cp_r("#{install_path}/.", staging_path) if Dir.exist?(install_path) = http_client.get(url_path) clean_cache(staging_path, ) sync_cache(staging_path, ) end end |
#sync_cache(staging_path, metadata) ⇒ Object
Downloads any out-of-date files into installer cache, overwriting those that don’t match the checksum provided the metadata @ url_path
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/cookbook-omnifetch/metadata_based_installer.rb', line 76 def sync_cache(staging_path, ) queue = ThreadedJobQueue.new CookbookMetadata.new().files do |url, path, checksum| dest_path = File.join(staging_path, path) FileUtils.mkdir_p(File.dirname(dest_path)) if file_outdated?(dest_path, checksum) queue << lambda do |_lock| http_client.streaming_request(url) do |tempfile| tempfile.close FileUtils.mv(tempfile.path, dest_path) end end end end queue.process(CookbookOmnifetch.chef_server_download_concurrency) end |