Class: DockerRegistry2::Registry
- Inherits:
-
Object
- Object
- DockerRegistry2::Registry
- Defined in:
- lib/registry/registry.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Method Summary collapse
- #_pull_v1(repo, manifest, dir) ⇒ Object
- #_pull_v2(repo, manifest, dir) ⇒ Object
- #blob(repo, digest, outpath = nil) ⇒ Object
-
#blob_size(repo, blobSum) ⇒ Object
gets the size of a particular blob, given the repo and the content-addressable hash usually unneeded, since manifest includes it.
- #copy(repo, tag, newregistry, newrepo, newtag) ⇒ Object
- #digest(image, tag, architecture = nil, os = nil, variant = nil) ⇒ Object
- #dodelete(url) ⇒ Object
- #doget(url, accept: nil) ⇒ Object
- #dohead(url) ⇒ Object
- #doput(url, payload = nil) ⇒ Object
-
#initialize(uri, options = {}) ⇒ Registry
constructor
A new instance of Registry.
- #last(header) ⇒ Object
- #manifest(repo, tag) ⇒ Object
- #manifest_digest(repo, tag) ⇒ Object
- #manifest_sum(manifest) ⇒ Object
-
#paginate_doget(url) ⇒ Object
When a result set is too large, the Docker registry returns only the first items and adds a Link header in the response with the URL of the next page.
-
#parse_link_header(header) ⇒ Object
Parse the value of the Link HTTP header and return a Hash whose keys are the rel values turned into symbols, and the values are URLs.
- #pull(repo, tag, dir) ⇒ Object
- #push(manifest, dir) ⇒ Object
- #rmtag(image, tag) ⇒ Object
- #search(query = '') ⇒ Object
- #tag(repo, tag, newrepo, newtag) ⇒ Object
- #tags(repo, count = nil, last = '', withHashes = false, auto_paginate: false) ⇒ Object
Constructor Details
#initialize(uri, options = {}) ⇒ Registry
Returns a new instance of Registry.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/registry/registry.rb', line 24 def initialize(uri, = {}) @uri = URI.parse(uri) @base_uri = "#{@uri.scheme}://#{@uri.host}:#{@uri.port}#{@uri.path}" # `URI.join("https://example.com/foo/bar", "v2")` drops `bar` in the base URL. A trailing slash prevents that. @base_uri << '/' unless @base_uri.end_with? '/' @user = [:user] @password = [:password] @http_options = [:http_options] || {} apply_timeout_defaults() @connection = nil end |
Instance Method Details
#_pull_v1(repo, manifest, dir) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/registry/registry.rb', line 211 def _pull_v1(repo, manifest, dir) # make sure the directory exists FileUtils.mkdir_p dir return false unless manifest['schemaVersion'] == 1 # pull each of the layers manifest['fsLayers'].each do |layer| # define path of file to save layer in layer_file = "#{dir}/#{layer['blobSum']}" # skip layer if we already got it next if File.file? layer_file # download layer # puts "getting layer (v1) #{layer['blobSum']}" blob(repo, layer['blobSum'], layer_file) # return layer file end end |
#_pull_v2(repo, manifest, dir) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/registry/registry.rb', line 193 def _pull_v2(repo, manifest, dir) # make sure the directory exists FileUtils.mkdir_p dir return false unless manifest['schemaVersion'] == 2 # pull each of the layers manifest['layers'].each do |layer| # define path of file to save layer in layer_file = "#{dir}/#{layer['digest']}" # skip layer if we already got it next if File.file? layer_file # download layer # puts "getting layer (v2) #{layer['digest']}" blob(repo, layer['digest'], layer_file) end end |
#blob(repo, digest, outpath = nil) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/registry/registry.rb', line 125 def blob(repo, digest, outpath = nil) blob_url = "v2/#{repo}/blobs/#{digest}" if outpath.nil? response = doget(blob_url) DockerRegistry2::Blob.new(response.headers, response.body) else File.open(outpath, 'w') do |fd| doreq('get', blob_url, fd) end outpath end end |
#blob_size(repo, blobSum) ⇒ Object
gets the size of a particular blob, given the repo and the content-addressable hash usually unneeded, since manifest includes it
244 245 246 247 |
# File 'lib/registry/registry.rb', line 244 def blob_size(repo, blobSum) response = dohead "v2/#{repo}/blobs/#{blobSum}" Integer(response.headers[:content_length], 10) end |
#copy(repo, tag, newregistry, newrepo, newtag) ⇒ Object
240 |
# File 'lib/registry/registry.rb', line 240 def copy(repo, tag, newregistry, newrepo, newtag); end |
#digest(image, tag, architecture = nil, os = nil, variant = nil) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/registry/registry.rb', line 147 def digest(image, tag, architecture = nil, os = nil, variant = nil) manifest = manifest(image, tag) parsed_manifest = JSON.parse(manifest.body) # Multi-arch images if parsed_manifest.key?('manifests') manifests = parsed_manifest['manifests'] return manifests if architecture.nil? || os.nil? manifests.each do |entry| if !variant.nil? return entry['digest'] if entry['platform']['architecture'] == architecture && entry['platform']['os'] == os && entry['platform']['variant'] == variant elsif entry['platform']['architecture'] == architecture && entry['platform']['os'] == os return entry['digest'] end end raise DockerRegistry2::NotFound, "No matches found for the image=#{image} tag=#{tag} os=#{os} architecture=#{architecture}" end manifest.headers[:docker_content_digest] end |
#dodelete(url) ⇒ Object
44 45 46 |
# File 'lib/registry/registry.rb', line 44 def dodelete(url) doreq 'delete', url end |
#doget(url, accept: nil) ⇒ Object
36 37 38 |
# File 'lib/registry/registry.rb', line 36 def doget(url, accept: nil) doreq 'get', url, nil, nil, accept: accept end |
#dohead(url) ⇒ Object
48 49 50 |
# File 'lib/registry/registry.rb', line 48 def dohead(url) doreq 'head', url end |
#doput(url, payload = nil) ⇒ Object
40 41 42 |
# File 'lib/registry/registry.rb', line 40 def doput(url, payload = nil) doreq 'put', url, nil, payload end |
#last(header) ⇒ Object
266 267 268 269 270 271 272 273 |
# File 'lib/registry/registry.rb', line 266 def last(header) links = parse_link_header(header) if links[:next] query = URI(links[:next]).query last = URI.decode_www_form(query).to_h['last'] end last end |
#manifest(repo, tag) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/registry/registry.rb', line 115 def manifest(repo, tag) # first get the manifest response = doget_with_legacy_fallback("v2/#{repo}/manifests/#{tag}") parsed = JSON.parse response.body manifest = DockerRegistry2::Manifest[parsed] manifest.body = response.body manifest.headers = response.headers manifest end |
#manifest_digest(repo, tag) ⇒ Object
139 140 141 142 143 144 145 |
# File 'lib/registry/registry.rb', line 139 def manifest_digest(repo, tag) tag_path = "v2/#{repo}/manifests/#{tag}" dohead(tag_path).headers[:docker_content_digest] rescue DockerRegistry2::InvalidMethod # Pre-2.3.0 registries didn't support manifest HEAD requests doget(tag_path).headers[:docker_content_digest] end |
#manifest_sum(manifest) ⇒ Object
275 276 277 278 279 280 281 |
# File 'lib/registry/registry.rb', line 275 def manifest_sum(manifest) size = 0 manifest['layers'].each do |layer| size += layer['size'] end size end |
#paginate_doget(url) ⇒ Object
When a result set is too large, the Docker registry returns only the first items and adds a Link header in the response with the URL of the next page. See https://docs.docker.com/registry/spec/api/#pagination. This method iterates over the pages and calls the given block with each response.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/registry/registry.rb', line 55 def paginate_doget(url) loop do response = doget(url) yield response link_header = response.headers[:link] or break next_url = parse_link_header(link_header)[:next] or break # The next URL in the Link header may be relative to the request URL, or absolute. # URI.join handles both cases nicely. url = URI.join(response.request_url, next_url) end end |
#parse_link_header(header) ⇒ Object
Parse the value of the Link HTTP header and return a Hash whose keys are the rel values turned into symbols, and
the values are URLs. For example, { next: '/v2/_catalog?n=100&last=x' }.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/registry/registry.rb', line 251 def parse_link_header(header) parts = header.split(',') links = {} # Parse each part into a named link parts.each do |part, _index| section = part.split(';') url = section[0][/<(.*)>/, 1] name = section[1][/rel="?([^"]*)"?/, 1].to_sym links[name] = url end links end |
#pull(repo, tag, dir) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/registry/registry.rb', line 179 def pull(repo, tag, dir) # make sure the directory exists FileUtils.mkdir_p dir # get the manifest m = manifest repo, tag # puts "pulling #{repo}:#{tag} into #{dir}" # manifest can contain multiple manifests one for each API version downloaded_layers = [] downloaded_layers += _pull_v2(repo, m, dir) if m['schemaVersion'] == 2 downloaded_layers += _pull_v1(repo, m, dir) if m['schemaVersion'] == 1 # return downloaded_layers downloaded_layers end |
#push(manifest, dir) ⇒ Object
230 |
# File 'lib/registry/registry.rb', line 230 def push(manifest, dir); end |
#rmtag(image, tag) ⇒ Object
172 173 174 175 176 177 |
# File 'lib/registry/registry.rb', line 172 def rmtag(image, tag) # TODO: Need full response back. Rewrite other manifests() calls without JSON? reference = doget("v2/#{image}/manifests/#{tag}").headers[:docker_content_digest] dodelete("v2/#{image}/manifests/#{reference}").code end |
#search(query = '') ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/registry/registry.rb', line 69 def search(query = '') all_repos = [] paginate_doget('v2/_catalog') do |response| repos = JSON.parse(response.body)['repositories'] repos.select! { |repo| repo.match?(/#{query}/) } unless query.empty? all_repos += repos end all_repos end |
#tag(repo, tag, newrepo, newtag) ⇒ Object
232 233 234 235 236 237 238 |
# File 'lib/registry/registry.rb', line 232 def tag(repo, tag, newrepo, newtag) manifest = manifest(repo, tag) raise DockerRegistry2::RegistryVersionException unless manifest['schemaVersion'] == 2 doput "v2/#{newrepo}/manifests/#{newtag}", manifest.to_json end |
#tags(repo, count = nil, last = '', withHashes = false, auto_paginate: false) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/registry/registry.rb', line 79 def (repo, count = nil, last = '', withHashes = false, auto_paginate: false) # create query params params = [] params.push(['last', last]) if last && last != '' params.push(['n', count]) unless count.nil? query_vars = '' query_vars = "?#{URI.encode_www_form(params)}" if params.length.positive? response = doget "v2/#{repo}/tags/list#{query_vars}" # parse the response resp = JSON.parse response.body # parse out next page link if necessary resp['last'] = last(response.headers[:link]) if response.headers[:link] # do we include the hashes? if withHashes resp['hashes'] = {} resp['tags'].each do |tag| resp['hashes'][tag] = digest(repo, tag) end end return resp unless auto_paginate while (last_tag = resp.delete('last')) = (repo, count, last_tag, withHashes) resp['last'] = ['last'] resp['tags'] += ['tags'] resp['tags'] = resp['tags'].uniq resp['hashes'].merge!(['hashes']) if withHashes end resp end |