Class: OsCtl::Repo::Downloader::Cached
- Defined in:
- lib/osctl/repo/downloader/cached.rb
Overview
Download image in a specified format and cache it locally
Constant Summary
Constants inherited from Base
Base::DEFAULT_ATTEMPTS, Base::DEFAULT_WAIT, Base::NETWORK_EXCEPTIONS
Instance Attribute Summary collapse
-
#repo ⇒ Object
readonly
protected
Returns the value of attribute repo.
Instance Method Summary collapse
- #cached_image_path(vendor, variant, arch, dist, vtag, format) ⇒ Object protected
-
#download(vendor, variant, arch, dist, vtag, format, open) ⇒ Array(String, [IO, nil])
protected
‘[image_path, io|nil]`.
- #ensure_cache_dir ⇒ Object protected
- #fetch_image(http, t, format) ⇒ Object protected
-
#get(vendor, variant, arch, dist, vtag, format, opts = {}) {|downloaded| ... } ⇒ String
param opts [Hash].
- #list ⇒ Array<Remote::Image>
- #read_from_cache(vendor, variant, arch, dist, vtag, format) ⇒ Object protected
- #update_index(http) ⇒ Object protected
- #with_cached_image(vendor, variant, arch, dist, vtag, format) ⇒ Object protected
- #write_index(path, res) ⇒ Object protected
Methods inherited from Base
#connect, #index_uri, #initialize, #request_get, #retryable_http_code?, #with_retries
Constructor Details
This class inherits a constructor from OsCtl::Repo::Downloader::Base
Instance Attribute Details
#repo ⇒ Object (readonly, protected)
Returns the value of attribute repo.
92 93 94 |
# File 'lib/osctl/repo/downloader/cached.rb', line 92 def repo @repo end |
Instance Method Details
#cached_image_path(vendor, variant, arch, dist, vtag, format) ⇒ Object (protected)
141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/osctl/repo/downloader/cached.rb', line 141 def cached_image_path(vendor, variant, arch, dist, vtag, format) path = nil with_cached_image(vendor, variant, arch, dist, vtag, format) do |t| unless t.cached?(format) raise CacheMiss, "Image #{t} not found in cache" end path = t.abs_cache_path(format) end path end |
#download(vendor, variant, arch, dist, vtag, format, open) ⇒ Array(String, [IO, nil]) (protected)
Returns ‘[image_path, io|nil]`.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/osctl/repo/downloader/cached.rb', line 95 def download(vendor, variant, arch, dist, vtag, format, open) path = fh = nil ensure_cache_dir with_retries do connect do |http| index = nil repo.lock_index do update_index(http) index = Remote::Index.from_file(repo, repo.index_path) end t = index.lookup(vendor, variant, arch, dist, vtag) raise ImageNotFound, t unless t raise FormatNotFound.new(t, format) unless t.has_image?(format) FileUtils.mkdir_p(t.abs_dir_path) t.lock(format) do path = fetch_image(http, t, format) fh = File.open(path, 'r') if open end end end [path, fh] end |
#ensure_cache_dir ⇒ Object (protected)
260 261 262 |
# File 'lib/osctl/repo/downloader/cached.rb', line 260 def ensure_cache_dir FileUtils.mkpath(repo.path) end |
#fetch_image(http, t, format) ⇒ Object (protected)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/osctl/repo/downloader/cached.rb', line 205 def fetch_image(http, t, format) uri = URI(t.abs_image_url(format)) t_path = t.abs_cache_path(format) t_tmp_path = "#{t_path}.new" if t.cached?(format) headers = { 'If-Modified-Since' => File.stat(t_path).mtime.httpdate } request_get(http, uri, headers) do |res| case res.code when '200' File.open(t_tmp_path, 'w') do |f| res.read_body do |fragment| f.write(fragment) end end if res['last-modified'] # Save the modtime for later requests FileUtils.touch(t_tmp_path, mtime: Time.httpdate(res['last-modified'])) end File.rename(t_tmp_path, t_path) return t_path when '304' # image unchanged else raise BadHttpResponse, res.code end end else # download it request_get(http, uri) do |res| File.open(t_tmp_path, 'w') do |f| raise BadHttpResponse, res.code if res.code != '200' res.read_body do |fragment| f.write(fragment) end end if res['last-modified'] # Save the modtime for later requests FileUtils.touch(t_tmp_path, mtime: Time.httpdate(res['last-modified'])) end File.rename(t_tmp_path, t_path) end end t_path end |
#get(vendor, variant, arch, dist, vtag, format, opts = {}) {|downloaded| ... } ⇒ String
param opts [Hash]
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/osctl/repo/downloader/cached.rb', line 31 def get(vendor, variant, arch, dist, vtag, format, opts = {}, &block) force_check = if opts.has_key?(:force_check) opts[:force_check] else false end begin image_path, fh = download( vendor, variant, arch, dist, vtag, format, block ? true : false ) rescue NetworkError, BadHttpResponse => e if force_check raise elsif block begin image_path, fh = read_from_cache( vendor, variant, arch, dist, vtag, format ) rescue CacheMiss raise e end else begin image_path = cached_image_path( vendor, variant, arch, dist, vtag, format ) rescue CacheMiss raise e end end end if block block.call(fh.read(32 * 1024)) until fh.eof? fh.close end File.absolute_path(image_path) end |
#list ⇒ Array<Remote::Image>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/osctl/repo/downloader/cached.rb', line 10 def list ensure_cache_dir with_retries do connect do |http| index = nil repo.lock_index do update_index(http) index = Remote::Index.from_file(repo, repo.index_path) end index.images end end end |
#read_from_cache(vendor, variant, arch, dist, vtag, format) ⇒ Object (protected)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/osctl/repo/downloader/cached.rb', line 126 def read_from_cache(vendor, variant, arch, dist, vtag, format) path = fh = nil with_cached_image(vendor, variant, arch, dist, vtag, format) do |t| unless t.cached?(format) raise CacheMiss, "Image #{t} not found in cache" end path = t.abs_cache_path(format) fh = File.open(path, 'r') end [path, fh] end |
#update_index(http) ⇒ Object (protected)
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/osctl/repo/downloader/cached.rb', line 174 def update_index(http) uri = index_uri tmp_path = "#{repo.index_path}.new" if repo.has_index? headers = { 'If-Modified-Since' => File.stat(repo.index_path).mtime.httpdate } request_get(http, uri, headers) do |res| case res.code when '200' write_index(tmp_path, res) File.rename(tmp_path, repo.index_path) when '304' # index unchanged else raise BadHttpResponse, res.code end end else request_get(http, uri) do |res| raise BadHttpResponse, res.code if res.code != '200' write_index(tmp_path, res) File.rename(tmp_path, repo.index_path) end end end |
#with_cached_image(vendor, variant, arch, dist, vtag, format) ⇒ Object (protected)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/osctl/repo/downloader/cached.rb', line 155 def with_cached_image(vendor, variant, arch, dist, vtag, format) index = nil repo.lock_index do index = Remote::Index.from_file(repo, repo.index_path) rescue Errno::ENOENT raise CacheMiss, 'Repository index not found in cache' end t = index.lookup(vendor, variant, arch, dist, vtag) raise ImageNotFound, t unless t raise FormatNotFound.new(t, format) unless t.has_image?(format) t.lock(format) do yield(t) end end |
#write_index(path, res) ⇒ Object (protected)
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/osctl/repo/downloader/cached.rb', line 264 def write_index(path, res) File.open(path, 'w') do |f| res.read_body do |fragment| f.write(fragment) end end return unless res['last-modified'] # Save the modtime for later requests FileUtils.touch( path, mtime: Time.httpdate(res['last-modified']) ) rescue StandardError FileUtils.rm_f(path) raise end |