debian-mirror-gitlab/lib/container_registry/client.rb

98 lines
2.8 KiB
Ruby
Raw Normal View History

2016-06-02 11:05:42 +05:30
require 'faraday'
require 'faraday_middleware'
module ContainerRegistry
class Client
attr_accessor :uri
2017-08-17 22:00:37 +05:30
MANIFEST_VERSION = 'application/vnd.docker.distribution.manifest.v2+json'.freeze
2016-06-02 11:05:42 +05:30
2016-08-24 12:49:21 +05:30
# Taken from: FaradayMiddleware::FollowRedirects
REDIRECT_CODES = Set.new [301, 302, 303, 307]
2016-06-02 11:05:42 +05:30
def initialize(base_uri, options = {})
@base_uri = base_uri
2016-08-24 12:49:21 +05:30
@options = options
2016-06-02 11:05:42 +05:30
end
def repository_tags(name)
2016-08-24 12:49:21 +05:30
response_body faraday.get("/v2/#{name}/tags/list")
2016-06-02 11:05:42 +05:30
end
def repository_manifest(name, reference)
2016-08-24 12:49:21 +05:30
response_body faraday.get("/v2/#{name}/manifests/#{reference}")
2016-06-02 11:05:42 +05:30
end
def repository_tag_digest(name, reference)
2016-08-24 12:49:21 +05:30
response = faraday.head("/v2/#{name}/manifests/#{reference}")
2016-06-02 11:05:42 +05:30
response.headers['docker-content-digest'] if response.success?
end
def delete_repository_tag(name, reference)
2016-08-24 12:49:21 +05:30
faraday.delete("/v2/#{name}/manifests/#{reference}").success?
2016-06-02 11:05:42 +05:30
end
def blob(name, digest, type = nil)
2016-08-24 12:49:21 +05:30
type ||= 'application/octet-stream'
response_body faraday_blob.get("/v2/#{name}/blobs/#{digest}", nil, 'Accept' => type), allow_redirect: true
2016-06-02 11:05:42 +05:30
end
def delete_blob(name, digest)
2016-08-24 12:49:21 +05:30
faraday.delete("/v2/#{name}/blobs/#{digest}").success?
2016-06-02 11:05:42 +05:30
end
2016-08-24 12:49:21 +05:30
2016-06-02 11:05:42 +05:30
private
2016-08-24 12:49:21 +05:30
2016-06-02 11:05:42 +05:30
def initialize_connection(conn, options)
conn.request :json
2016-08-24 12:49:21 +05:30
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
def accept_manifest(conn)
2016-06-02 11:05:42 +05:30
conn.headers['Accept'] = MANIFEST_VERSION
2016-06-22 15:30:34 +05:30
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-08-24 12:49:21 +05:30
end
2016-06-02 11:05:42 +05:30
2016-08-24 12:49:21 +05:30
def response_body(response, allow_redirect: false)
if allow_redirect && REDIRECT_CODES.include?(response.status)
response = redirect_response(response.headers['location'])
2016-06-02 11:05:42 +05:30
end
2016-08-24 12:49:21 +05:30
response.body if response && response.success?
end
def redirect_response(location)
return unless location
# We explicitly remove authorization token
faraday_blob.get(location) do |req|
req['Authorization'] = ''
end
2016-06-02 11:05:42 +05:30
end
2016-06-22 15:30:34 +05:30
2016-08-24 12:49:21 +05:30
def faraday
@faraday ||= Faraday.new(@base_uri) do |conn|
initialize_connection(conn, @options)
accept_manifest(conn)
end
end
def faraday_blob
@faraday_blob ||= Faraday.new(@base_uri) do |conn|
initialize_connection(conn, @options)
end
2016-06-22 15:30:34 +05:30
end
2016-06-02 11:05:42 +05:30
end
end