uffizzi_core 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0339ca44a1964075ce650d5e830911c67b91242cdfb8c899a7a21a0277a81025'
4
- data.tar.gz: 20cb6887c0fb29b7335faef953a67f31645c96b2ee4b7817bcd32d095401a107
3
+ metadata.gz: 6eee58a81e1cc3ebb107400567a7639a31656fba4e059b4f2d5a27233693b421
4
+ data.tar.gz: 2c9955231622ac80c6309d5307ae9c3b2d799f00500976b5344ab56590e7c02a
5
5
  SHA512:
6
- metadata.gz: b2901c9c5a66c41344a9fff26031e516bbe7f983d084ee9aad6365ecbc1b7a8509f031d3bfb7beeb7436fcf50c1ce32d6631197f9860cffd18dbc541103d5327
7
- data.tar.gz: 8dc9721c025a2e9f434922295cfd287f0c1e83c1a4ea1574f6afe681b2fb5ea2e4e3fe83a62df37156cf7aaaa0089270a0deb47ed4e59939b93dbbe71329f5ce
6
+ metadata.gz: 8d5723f5f07e1504ab7e922c2a6d2e0fa68b57dd613e1bfb8771825f6c3a2ff4df61b7f6948c4517d5ebe7b38c070f640e1c41f50b5ef1aeb5c055d14c07bac6
7
+ data.tar.gz: 9395fd8c776b31d6483e3a239459cd3d37905a8e2eee213625f05678b8964bc0253f3cb185ed31106d63416f93f1b3dbadc867323de59118685c37711b9f1944
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class UffizziCore::DockerRegistryClient
4
- def initialize(credentials)
5
- @registry_url = credentials.registry_url
6
- @connection = build_connection(@registry_url, credentials.username, credentials.password)
4
+ def initialize(registry_url:, username: nil, password: nil)
5
+ @registry_url = registry_url
6
+ @connection = build_connection(@registry_url, username, password)
7
7
  end
8
8
 
9
9
  def authenticated?
@@ -11,13 +11,22 @@ class UffizziCore::DockerRegistryClient
11
11
  response.status == 200
12
12
  end
13
13
 
14
+ def manifests(image:, tag:, namespace: nil)
15
+ full_image = [namespace, image].compact.join('/')
16
+ url = "/v2/#{full_image}/manifests/#{tag}"
17
+ response = @connection.get(url)
18
+
19
+ RequestResult.new(status: response.status, result: response.body)
20
+ end
21
+
14
22
  private
15
23
 
16
24
  def build_connection(registry_url, username, password)
17
25
  Faraday.new(registry_url) do |conn|
18
- conn.request(:basic_auth, username, password)
26
+ conn.request(:basic_auth, username, password) if username.present? && password.present?
19
27
  conn.request(:json)
20
28
  conn.response(:json)
29
+ conn.response(:follow_redirects)
21
30
  conn.adapter(Faraday.default_adapter)
22
31
  end
23
32
  end
@@ -32,9 +32,7 @@ class UffizziCore::Controller::DeployContainers::ContainerSerializer < UffizziCo
32
32
  UffizziCore::Repo::GithubContainerRegistry.name,
33
33
  UffizziCore::Repo::DockerRegistry.name
34
34
 
35
- credential = UffizziCore::RepoService.credential(repo)
36
- registry_host = URI.parse(credential.registry_url).host
37
- "#{registry_host}/#{object.image}"
35
+ build_registry_image(repo)
38
36
  else
39
37
  object.image
40
38
  end
@@ -65,4 +63,15 @@ class UffizziCore::Controller::DeployContainers::ContainerSerializer < UffizziCo
65
63
 
66
64
  object.healthcheck.merge('test' => new_command)
67
65
  end
66
+
67
+ private
68
+
69
+ def build_registry_image(repo)
70
+ credential = UffizziCore::RepoService.credential(repo)
71
+ return object.image if credential.blank?
72
+
73
+ registry_host = URI.parse(credential.registry_url).host
74
+
75
+ "#{registry_host}/#{object.image}"
76
+ end
68
77
  end
@@ -31,7 +31,7 @@ class UffizziCore::ComposeFile::Builders::ContainerBuilderService
31
31
  {
32
32
  tag: tag(image_data, repo_attributes),
33
33
  port: port(container_name, ingress_data),
34
- image: image(container_data, image_data, build_data),
34
+ image: image(container_data, image_data, build_data, credentials),
35
35
  public: is_ingress,
36
36
  entrypoint: entrypoint(container_data),
37
37
  command: command(container_data),
@@ -96,20 +96,23 @@ class UffizziCore::ComposeFile::Builders::ContainerBuilderService
96
96
  ingress[:port]
97
97
  end
98
98
 
99
- def image(container_data, image_data, build_data)
99
+ def image(container_data, image_data, build_data, credentials)
100
100
  if image_data.present?
101
- image_name(container_data, image_data)
101
+ image_name(container_data, image_data, credentials)
102
102
  else
103
103
  "#{build_data[:account_name]}/#{build_data[:repository_name]}"
104
104
  end
105
105
  end
106
106
 
107
- def image_name(container_data, image_data)
107
+ def image_name(container_data, image_data, credentials)
108
108
  if image_data[:registry_url].present? &&
109
109
  !UffizziCore::ComposeFile::ContainerService.google?(container_data) &&
110
110
  !UffizziCore::ComposeFile::ContainerService.github_container_registry?(container_data) &&
111
111
  !UffizziCore::ComposeFile::ContainerService.docker_registry?(container_data)
112
112
  image_data[:name]
113
+ elsif UffizziCore::ComposeFile::ContainerService.docker_registry?(container_data) &&
114
+ credential_by_scope(credentials, :docker_registry).nil?
115
+ [image_data[:registry_url], image_data[:namespace], image_data[:name]].compact.join('/')
113
116
  else
114
117
  "#{image_data[:namespace]}/#{image_data[:name]}"
115
118
  end
@@ -205,7 +208,7 @@ class UffizziCore::ComposeFile::Builders::ContainerBuilderService
205
208
  end
206
209
 
207
210
  def build_docker_repo_attributes(image_data, credentials, scope, repo_type)
208
- credential = credentials.send(scope).first
211
+ credential = credential_by_scope(credentials, scope)
209
212
  if UffizziCore::ComposeFile::ContainerService.image_available?(credential, image_data, scope)
210
213
  return docker_builder(repo_type).build_attributes(image_data)
211
214
  end
@@ -234,4 +237,8 @@ class UffizziCore::ComposeFile::Builders::ContainerBuilderService
234
237
  def variables_builder
235
238
  @variables_builder ||= UffizziCore::ComposeFile::Builders::VariablesBuilderService.new(project)
236
239
  end
240
+
241
+ def credential_by_scope(credentials, scope)
242
+ credentials.send(scope).first
243
+ end
237
244
  end
@@ -29,9 +29,11 @@ class UffizziCore::ComposeFile::ContainerService
29
29
 
30
30
  def docker_registry?(container)
31
31
  registry_url = container.dig(:image, :registry_url)
32
+ return false if registry_url.nil?
33
+
32
34
  registry_domain_regexp = /(\w+\.\w{2,})(?::\d+)?\z/
33
35
  registry_domain = registry_url.match(registry_domain_regexp)&.to_a&.last
34
- return false if registry_url.nil? || registry_domain.nil?
36
+ return false if registry_domain.nil?
35
37
 
36
38
  ['amazonaws.com', 'azurecr.io', 'gcr.io', 'ghcr.io'].exclude?(registry_domain)
37
39
  end
@@ -60,13 +62,13 @@ class UffizziCore::ComposeFile::ContainerService
60
62
  detect_credential(container, credentials, :docker_hub)
61
63
  elsif UffizziCore::ComposeFile::ContainerService.google?(container)
62
64
  detect_credential(container, credentials, :google)
65
+ else
66
+ detect_credential(container, credentials, :docker_registry)
63
67
  end
64
68
  end
65
69
 
66
70
  def detect_credential(container, credentials, type)
67
- credential = credentials.detect do |item|
68
- item.send("#{type}?")
69
- end
71
+ credential = credentials.detect { |item| item.send("#{type}?") }
70
72
 
71
73
  return credential if image_available?(credential, container[:image], type)
72
74
 
@@ -77,6 +79,8 @@ class UffizziCore::ComposeFile::ContainerService
77
79
  case type
78
80
  when :docker_hub
79
81
  UffizziCore::DockerHubService.image_available?(credential, image_data)
82
+ when :docker_registry
83
+ UffizziCore::DockerRegistryService.image_available?(credential, image_data)
80
84
  else
81
85
  # TODO check image availability in other registry types
82
86
  credential.present?
@@ -9,7 +9,13 @@ class UffizziCore::DockerRegistry::CredentialService
9
9
  private
10
10
 
11
11
  def client(credential)
12
- UffizziCore::DockerRegistryClient.new(credential)
12
+ params = {
13
+ registry_url: credential.registry_url,
14
+ username: credential.username,
15
+ password: credential.password,
16
+ }
17
+
18
+ UffizziCore::DockerRegistryClient.new(params)
13
19
  end
14
20
  end
15
21
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UffizziCore::DockerRegistryService
4
+ class << self
5
+ def image_available?(credential, image_data)
6
+ client_params = build_client_params(credential, image_data)
7
+ client = UffizziCore::DockerRegistryClient.new(**client_params)
8
+ response = client.manifests(namespace: image_data[:namespace], image: image_data[:name], tag: image_data[:tag])
9
+
10
+ response.status < 400
11
+ end
12
+
13
+ private
14
+
15
+ def build_client_params(credential, image_data)
16
+ registry_url = credential&.registry_url || image_data[:registry_url]
17
+ new_registry_url = registry_url.start_with?('https://', 'http://') ? registry_url : "https://#{registry_url}"
18
+
19
+ {
20
+ registry_url: new_registry_url,
21
+ username: credential&.username,
22
+ password: credential&.password,
23
+ }
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module UffizziCore
4
- VERSION = '1.0.3'
4
+ VERSION = '1.1.0'
5
5
  end
data/lib/uffizzi_core.rb CHANGED
@@ -25,6 +25,7 @@ require 'rswag/api'
25
25
  require 'rswag/ui'
26
26
  require 'sidekiq'
27
27
  require 'virtus'
28
+ require 'faraday/follow_redirects'
28
29
 
29
30
  module UffizziCore
30
31
  mattr_accessor :dependencies, default: {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uffizzi_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Thurman
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-09-14 00:00:00.000000000 Z
12
+ date: 2022-09-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aasm
@@ -193,6 +193,20 @@ dependencies:
193
193
  - - ">="
194
194
  - !ruby/object:Gem::Version
195
195
  version: '0'
196
+ - !ruby/object:Gem::Dependency
197
+ name: faraday-follow_redirects
198
+ requirement: !ruby/object:Gem::Requirement
199
+ requirements:
200
+ - - ">="
201
+ - !ruby/object:Gem::Version
202
+ version: '0'
203
+ type: :runtime
204
+ prerelease: false
205
+ version_requirements: !ruby/object:Gem::Requirement
206
+ requirements:
207
+ - - ">="
208
+ - !ruby/object:Gem::Version
209
+ version: '0'
196
210
  - !ruby/object:Gem::Dependency
197
211
  name: faraday_middleware
198
212
  requirement: !ruby/object:Gem::Requirement
@@ -937,6 +951,7 @@ files:
937
951
  - app/services/uffizzi_core/docker_hub/credential_service.rb
938
952
  - app/services/uffizzi_core/docker_hub_service.rb
939
953
  - app/services/uffizzi_core/docker_registry/credential_service.rb
954
+ - app/services/uffizzi_core/docker_registry_service.rb
940
955
  - app/services/uffizzi_core/github_container_registry/credential_service.rb
941
956
  - app/services/uffizzi_core/google/credential_service.rb
942
957
  - app/services/uffizzi_core/google_service.rb