Class: UffizziCore::DockerHubClient
- Inherits:
-
Object
- Object
- UffizziCore::DockerHubClient
show all
- Defined in:
- app/clients/uffizzi_core/docker_hub_client.rb,
app/clients/uffizzi_core/docker_hub_client/request_result.rb,
app/clients/uffizzi_core/docker_hub_client/not_authorized_error.rb
Defined Under Namespace
Classes: NotAuthorizedError, RequestResult
Constant Summary
collapse
- BASE_URL =
'https://hub.docker.com'
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#accounts ⇒ Object
-
#authenticate ⇒ Object
-
#authenticated? ⇒ Boolean
-
#digest(image:, tag:, token:) ⇒ Object
-
#get_token(repository) ⇒ Object
-
#initialize(credential = nil) ⇒ DockerHubClient
constructor
A new instance of DockerHubClient.
-
#metadata(namespace:, image:) ⇒ Object
-
#private_images(account:, page: 1, per_page: 25) ⇒ Object
-
#public_images(q:, page: 1, per_page: 25) ⇒ Object
-
#repository(namespace:, image:) ⇒ Object
-
#tags(namespace:, image:, q: '', page: 1, per_page: 10) ⇒ Object
Constructor Details
#initialize(credential = nil) ⇒ DockerHubClient
Returns a new instance of DockerHubClient.
8
9
10
11
12
13
14
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 8
def initialize(credential = nil)
@connection = build_connection
@credential = credential
return unless credential
@jwt = authenticate
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
4
5
6
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 4
def connection
@connection
end
|
#credential ⇒ Object
Returns the value of attribute credential.
4
5
6
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 4
def credential
@credential
end
|
#jwt ⇒ Object
Returns the value of attribute jwt.
4
5
6
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 4
def jwt
@jwt
end
|
Instance Method Details
#accounts ⇒ Object
55
56
57
58
59
60
61
62
63
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 55
def accounts
raise NotAuthorizedError if !authenticated?
url = "#{BASE_URL}/v2/repositories/namespaces/"
response = connection.get(url) do |request|
request.['Authorization'] = "JWT #{jwt}"
end
RequestResult.new(result: response.body)
end
|
#authenticate ⇒ Object
16
17
18
19
20
21
22
23
24
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 16
def authenticate
params = { username: credential.username, password: credential.password }
url = "#{BASE_URL}/v2/users/login/"
response = connection.post(url, params)
request_result = RequestResult.new(result: response.body)
request_result.result.token
rescue NoMethodError
nil
end
|
#authenticated? ⇒ Boolean
99
100
101
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 99
def authenticated?
jwt.present?
end
|
#digest(image:, tag:, token:) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 82
def digest(image:, tag:, token:)
url = "https://index.docker.io/v2/#{image}/manifests/#{tag}"
response = connection.get(url) do |request|
request.['Accept'] = 'application/vnd.docker.distribution.manifest.v2+json'
request.['Authorization'] = "Bearer #{token}"
end
RequestResult.quiet.new(result: response.body, headers: response.)
end
|
#get_token(repository) ⇒ Object
92
93
94
95
96
97
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 92
def get_token(repository)
params = { username: credential.username, password: credential.password }
url = "https://auth.docker.io/token?service=registry.docker.io&scope=repository:#{repository}:pull"
response = connection.get(url, params)
RequestResult.new(result: response.body)
end
|
65
66
67
68
69
70
71
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 65
def metadata(namespace:, image:)
url = BASE_URL + "/v2/repositories/#{namespace}/#{image}/"
response = connection.get(url) do |request|
request.['Authorization'] = "JWT #{jwt}"
end
RequestResult.quiet.new(result: response.body)
end
|
#private_images(account:, page: 1, per_page: 25) ⇒ Object
44
45
46
47
48
49
50
51
52
53
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 44
def private_images(account:, page: 1, per_page: 25)
raise NotAuthorizedError if !authenticated? || account.empty?
url = BASE_URL + "/v2/repositories/#{account}/"
params = { page_size: per_page, page: page }
response = connection.get(url, params) do |request|
request.['Authorization'] = "JWT #{jwt}"
end
RequestResult.new(result: response.body)
end
|
#public_images(q:, page: 1, per_page: 25) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 35
def public_images(q:, page: 1, per_page: 25)
url = "#{BASE_URL}/api/content/v1/products/search"
params = { page_size: per_page, q: q, type: :image, page: page }
response = connection.get(url, params) do |request|
request.['Search-Version'] = 'v3'
end
RequestResult.new(result: response.body)
end
|
#repository(namespace:, image:) ⇒ Object
26
27
28
29
30
31
32
33
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 26
def repository(namespace:, image:)
url = "#{BASE_URL}/v2/repositories/#{namespace}/#{image}"
response = connection.get(url) do |request|
request.['Authorization'] = "JWT #{jwt}"
end
RequestResult.new(status: response.status, result: response.body)
end
|
73
74
75
76
77
78
79
80
|
# File 'app/clients/uffizzi_core/docker_hub_client.rb', line 73
def tags(namespace:, image:, q: '', page: 1, per_page: 10)
url = BASE_URL + "/v2/repositories/#{namespace}/#{image}/tags"
params = { page_size: per_page, page: page, name: q }
response = connection.get(url, params) do |request|
request.['Authorization'] = "JWT #{jwt}"
end
RequestResult.quiet.new(result: response.body)
end
|