Class: OsCtl::Repo::Downloader::Cached

Inherits:
Base
  • Object
show all
Defined in:
lib/osctl/repo/downloader/cached.rb

Overview

Download image in a specified format and cache it locally

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#connect, #index_uri, #initialize

Constructor Details

This class inherits a constructor from OsCtl::Repo::Downloader::Base

Instance Attribute Details

#repoObject (readonly, protected)

Returns the value of attribute repo.



71
72
73
# File 'lib/osctl/repo/downloader/cached.rb', line 71

def repo
  @repo
end

Instance Method Details

#download(vendor, variant, arch, dist, vtag, format, open) ⇒ Array(String, [IO, nil]) (protected)

Returns ‘[image_path, io|nil]`.

Returns:

  • (Array(String, [IO, nil]))

    ‘[image_path, io|nil]`



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/osctl/repo/downloader/cached.rb', line 74

def download(vendor, variant, arch, dist, vtag, format, open)
  path = fh = nil

  ensure_cache_dir

  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

  [path, fh]
end

#ensure_cache_dirObject (protected)



234
235
236
# File 'lib/osctl/repo/downloader/cached.rb', line 234

def ensure_cache_dir
  FileUtils.mkpath(repo.path)
end

#fetch_image(http, t, format) ⇒ Object (protected)



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
204
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
# File 'lib/osctl/repo/downloader/cached.rb', line 179

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 }

    http.request_get(uri.path, 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
    http.request_get(uri.path) 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]

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :force_check (Boolean)

Yield Parameters:

  • downloaded (String)

    data

Returns:

  • (String)

    path to image



29
30
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
# File 'lib/osctl/repo/downloader/cached.rb', line 29

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 SystemCallError,
         OpenSSL::SSL::SSLError,
         BadHttpResponse => e
    if force_check
      raise
    elsif block
      begin
        fh = read_from_cache(vendor, variant, arch, dist, vtag, format)
      rescue CacheMiss => cache_e
        raise "Unable to reach remote repository: #{e.message}; " \
              "not found in cache: #{cache_e.message}"
      end
    end
  end

  if block
    block.call(fh.read(32 * 1024)) until fh.eof?
    fh.close
  end

  File.absolute_path(image_path)
end

#listArray<Remote::Image>

Returns:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/osctl/repo/downloader/cached.rb', line 10

def list
  ensure_cache_dir

  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

#read_from_cache(vendor, variant, arch, dist, vtag, format) ⇒ Object (protected)

Raises:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/osctl/repo/downloader/cached.rb', line 103

def read_from_cache(vendor, variant, arch, dist, vtag, format)
  fh = nil
  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
    unless t.cached?(format)
      raise CacheMiss, "Image #{t} not found in cache"
    end

    fh = File.open(t.abs_cache_path(format), 'r')
  end

  fh
end

#update_index(http) ⇒ Object (protected)



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/osctl/repo/downloader/cached.rb', line 129

def update_index(http)
  uri = index_uri

  if repo.has_index?
    headers = { 'If-Modified-Since' => File.stat(repo.index_path).mtime.httpdate }

    http.request_get(uri.path, headers) do |res|
      case res.code
      when '200'
        File.open(repo.index_path, 'w') do |f|
          res.read_body { |fragment| f.write(fragment) }
        end

        if res['last-modified']
          # Save the modtime for later requests
          FileUtils.touch(
            repo.index_path,
            mtime: Time.httpdate(res['last-modified'])
          )
        end

      when '304'
        # index unchanged

      else
        raise BadHttpResponse, res.code
      end
    end

  else
    http.request_get(uri.path) do |res|
      raise BadHttpResponse, res.code if res.code != '200'

      File.open(repo.index_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(
          repo.index_path,
          mtime: Time.httpdate(res['last-modified'])
        )
      end
    end
  end
end