This repository has been archived by the owner on Mar 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from realestate-com-au/refactor
Refactor
- Loading branch information
Showing
10 changed files
with
193 additions
and
195 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'tagfish/docker_registry_v1_client' | ||
require 'tagfish/docker_registry_v2_client' | ||
|
||
module Tagfish | ||
module DockerRegistryClient | ||
|
||
def self.for(*args) | ||
[DockerRegistryV2Client, DockerRegistryV1Client].each do |client_class| | ||
begin | ||
return client_class.new(*args) | ||
rescue APIVersionError | ||
end | ||
end | ||
raise APIVersionError, "API version unrecognized!" | ||
end | ||
|
||
class AuthenticationError < StandardError | ||
end | ||
|
||
class APIVersionError < StandardError | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'tagfish/docker_registry_vboth_client' | ||
|
||
module Tagfish | ||
class DockerRegistryV1Client < DockerRegistryVbothClient | ||
|
||
def api_version | ||
'v1' | ||
end | ||
|
||
def search(keyword) | ||
APICall.new(search_uri(keyword)).get_json(http_auth) | ||
end | ||
|
||
def tag_names | ||
tag_map.tag_names | ||
end | ||
|
||
def tag_map | ||
tags_list = tags_api(tags) | ||
Tagfish::Tags.new(tags_list) | ||
end | ||
|
||
private | ||
|
||
def tags | ||
APICall.new(tags_uri).get_json(http_auth) | ||
end | ||
|
||
def tags_api(api_response_data) | ||
case api_response_data | ||
when Hash | ||
api_response_data | ||
when Array | ||
api_response_data.reduce({}) do |images, tag| | ||
images.merge({tag["name"] => tag["layer"]}) | ||
end | ||
else | ||
raise "unexpected type #{api_response_data.class}" | ||
end | ||
end | ||
|
||
def ping_uri | ||
"#{base_uri}/v1/_ping" | ||
end | ||
|
||
def search_uri(keyword) | ||
"#{base_uri}/v1/search?q=#{keyword}" | ||
end | ||
|
||
def tags_uri | ||
"#{base_uri}/v1/repositories/#{docker_uri.repository}/tags" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'tagfish/docker_registry_vboth_client' | ||
|
||
module Tagfish | ||
class DockerRegistryV2Client < DockerRegistryVbothClient | ||
|
||
def api_version | ||
'v2' | ||
end | ||
|
||
def catalog | ||
APICall.new(catalog_uri).get_json(http_auth) | ||
end | ||
|
||
def tag_names | ||
tags["tags"] | ||
end | ||
|
||
def tag_map | ||
Tagfish::Tags.new(tags_logic) | ||
end | ||
|
||
private | ||
|
||
def tags | ||
APICall.new(tags_uri).get_json(http_auth) | ||
end | ||
|
||
def hash(tag) | ||
APICall.new(hash_uri(tag)).get_json(http_auth) | ||
end | ||
|
||
def tags_logic | ||
if tag_names.nil? | ||
abort("No Tags found for this repository") | ||
end | ||
|
||
tags_with_hashes = tag_names.inject({}) do |dict, tag| | ||
dict[tag] = hash(tag)["fsLayers"][0]["blobSum"] | ||
dict | ||
end | ||
end | ||
|
||
def ping_uri | ||
"#{base_uri}/v2/" | ||
end | ||
|
||
def catalog_uri | ||
"#{base_uri}/v2/_catalog" | ||
end | ||
|
||
def tags_uri | ||
"#{base_uri}/v2/#{docker_uri.repository}/tags/list" | ||
end | ||
|
||
def hash_uri(tag) | ||
"#{base_uri}/v2/#{docker_uri.repository}/manifests/#{tag}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'net/http' | ||
require 'json' | ||
require 'tagfish/docker_uri' | ||
require 'tagfish/api_call' | ||
require 'tagfish/tags' | ||
|
||
module Tagfish | ||
class DockerRegistryVbothClient | ||
|
||
attr_accessor :docker_uri | ||
attr_accessor :http_auth | ||
|
||
def initialize(docker_uri) | ||
@docker_uri = docker_uri | ||
code = APICall.new(ping_uri).response_code | ||
if code == 401 | ||
@http_auth = DockerHttpAuth.new(docker_uri.registry) | ||
code = APICall.new(ping_uri).response_code(http_auth) | ||
end | ||
if code == 401 | ||
raise DockerRegistryClient::AuthenticationError, "Please `docker login <REGISTRY>` and try again" | ||
elsif code != 200 | ||
raise DockerRegistryClient::APIVersionError, "Not recognized" | ||
end | ||
end | ||
|
||
def base_uri | ||
"#{docker_uri.protocol}#{docker_uri.registry}" | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require 'tagfish/docker_http_auth' | ||
require 'tagfish/docker_api' | ||
|
||
module Tagfish | ||
class DockerURI | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.