gitlab-org--gitlab-foss/lib/container_registry/client.rb

69 lines
2 KiB
Ruby
Raw Normal View History

require 'faraday'
require 'faraday_middleware'
module ContainerRegistry
class Client
attr_accessor :uri
MANIFEST_VERSION = 'application/vnd.docker.distribution.manifest.v2+json'
def initialize(base_uri, options = {})
@base_uri = base_uri
2016-05-09 15:34:24 -04:00
@faraday = Faraday.new(@base_uri) do |conn|
2016-05-09 16:32:18 -04:00
initialize_connection(conn, options)
end
end
def repository_tags(name)
2016-06-17 11:33:03 -04:00
response_body @faraday.get("/v2/#{name}/tags/list")
end
def repository_manifest(name, reference)
2016-06-17 11:33:03 -04:00
response_body @faraday.get("/v2/#{name}/manifests/#{reference}")
end
def repository_tag_digest(name, reference)
response = @faraday.head("/v2/#{name}/manifests/#{reference}")
response.headers['docker-content-digest'] if response.success?
end
def delete_repository_tag(name, reference)
@faraday.delete("/v2/#{name}/manifests/#{reference}").success?
end
def blob(name, digest, type = nil)
headers = {}
headers['Accept'] = type if type
2016-06-17 11:33:03 -04:00
response_body @faraday.get("/v2/#{name}/blobs/#{digest}", nil, headers)
end
def delete_blob(name, digest)
@faraday.delete("/v2/#{name}/blobs/#{digest}").success?
end
2016-05-09 15:34:24 -04:00
private
2016-05-09 16:32:18 -04:00
def initialize_connection(conn, options)
2016-05-09 15:34:24 -04:00
conn.request :json
conn.headers['Accept'] = MANIFEST_VERSION
conn.response :json, content_type: 'application/json'
conn.response :json, content_type: 'application/vnd.docker.distribution.manifest.v1+prettyjws'
conn.response :json, content_type: 'application/vnd.docker.distribution.manifest.v1+json'
conn.response :json, content_type: 'application/vnd.docker.distribution.manifest.v2+json'
2016-05-09 15:34:24 -04:00
if options[:user] && options[:password]
conn.request(:basic_auth, options[:user].to_s, options[:password].to_s)
elsif options[:token]
conn.request(:authorization, :bearer, options[:token].to_s)
end
conn.adapter :net_http
end
2016-06-17 11:33:03 -04:00
def response_body(response)
response.body if response.success?
end
end
end