2016-05-04 08:22:54 -04:00
|
|
|
require 'faraday'
|
|
|
|
require 'faraday_middleware'
|
|
|
|
|
2016-05-09 15:14:46 -04:00
|
|
|
module ContainerRegistry
|
2016-05-04 08:22:54 -04:00
|
|
|
class Client
|
|
|
|
attr_accessor :uri
|
|
|
|
|
2017-02-21 18:32:18 -05:00
|
|
|
MANIFEST_VERSION = 'application/vnd.docker.distribution.manifest.v2+json'.freeze
|
2016-05-04 08:22:54 -04:00
|
|
|
|
2016-06-29 08:18:55 -04:00
|
|
|
# Taken from: FaradayMiddleware::FollowRedirects
|
2016-07-15 12:05:39 -04:00
|
|
|
REDIRECT_CODES = Set.new [301, 302, 303, 307]
|
2016-06-29 08:18:55 -04:00
|
|
|
|
2016-05-04 08:22:54 -04:00
|
|
|
def initialize(base_uri, options = {})
|
|
|
|
@base_uri = base_uri
|
2016-06-30 08:49:58 -04:00
|
|
|
@options = options
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository_tags(name)
|
2016-06-30 08:49:58 -04:00
|
|
|
response_body faraday.get("/v2/#{name}/tags/list")
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository_manifest(name, reference)
|
2016-06-30 08:49:58 -04:00
|
|
|
response_body faraday.get("/v2/#{name}/manifests/#{reference}")
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository_tag_digest(name, reference)
|
2016-06-30 08:49:58 -04:00
|
|
|
response = faraday.head("/v2/#{name}/manifests/#{reference}")
|
2016-05-04 08:22:54 -04:00
|
|
|
response.headers['docker-content-digest'] if response.success?
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_repository_tag(name, reference)
|
2016-06-30 08:49:58 -04:00
|
|
|
faraday.delete("/v2/#{name}/manifests/#{reference}").success?
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def blob(name, digest, type = nil)
|
2016-06-30 08:49:58 -04:00
|
|
|
type ||= 'application/octet-stream'
|
|
|
|
response_body faraday_blob.get("/v2/#{name}/blobs/#{digest}", nil, 'Accept' => type), allow_redirect: true
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_blob(name, digest)
|
2016-06-30 08:49:58 -04:00
|
|
|
faraday.delete("/v2/#{name}/blobs/#{digest}").success?
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
2016-06-30 08:49:58 -04:00
|
|
|
|
2016-05-09 15:34:24 -04:00
|
|
|
private
|
2016-06-30 08:49:58 -04:00
|
|
|
|
2016-05-09 16:32:18 -04:00
|
|
|
def initialize_connection(conn, options)
|
2016-05-09 15:34:24 -04:00
|
|
|
conn.request :json
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-06-30 08:49:58 -04:00
|
|
|
def accept_manifest(conn)
|
|
|
|
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'
|
|
|
|
end
|
|
|
|
|
2016-06-29 08:18:55 -04:00
|
|
|
def response_body(response, allow_redirect: false)
|
|
|
|
if allow_redirect && REDIRECT_CODES.include?(response.status)
|
2016-06-30 08:49:58 -04:00
|
|
|
response = redirect_response(response.headers['location'])
|
2016-06-29 08:18:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
response.body if response && response.success?
|
|
|
|
end
|
|
|
|
|
2016-06-30 08:49:58 -04:00
|
|
|
def redirect_response(location)
|
2016-06-29 08:18:55 -04:00
|
|
|
return unless location
|
|
|
|
|
2017-05-17 01:51:56 -04:00
|
|
|
faraday_redirect.get(location)
|
2016-06-30 08:49:58 -04:00
|
|
|
end
|
2016-06-29 08:18:55 -04:00
|
|
|
|
2016-06-30 08:49:58 -04:00
|
|
|
def faraday
|
|
|
|
@faraday ||= Faraday.new(@base_uri) do |conn|
|
|
|
|
initialize_connection(conn, @options)
|
|
|
|
accept_manifest(conn)
|
|
|
|
end
|
2016-06-29 08:18:55 -04:00
|
|
|
end
|
|
|
|
|
2016-06-30 08:49:58 -04:00
|
|
|
def faraday_blob
|
|
|
|
@faraday_blob ||= Faraday.new(@base_uri) do |conn|
|
|
|
|
initialize_connection(conn, @options)
|
|
|
|
end
|
2016-06-17 11:33:03 -04:00
|
|
|
end
|
2017-05-17 01:51:56 -04:00
|
|
|
|
|
|
|
# Create a new request to make sure the Authorization header is not inserted
|
|
|
|
# via the Faraday middleware
|
|
|
|
def faraday_redirect
|
|
|
|
@faraday_redirect ||= Faraday.new(@base_uri) do |conn|
|
|
|
|
conn.request :json
|
|
|
|
conn.adapter :net_http
|
|
|
|
end
|
|
|
|
end
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
end
|