Class: UffizziCore::ContainerRegistryService

Inherits:
Object
  • Object
show all
Defined in:
app/services/uffizzi_core/container_registry_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, container_data = {}) ⇒ ContainerRegistryService

Returns a new instance of ContainerRegistryService.



49
50
51
52
53
54
# File 'app/services/uffizzi_core/container_registry_service.rb', line 49

def initialize(type, container_data = {})
  @type = type
  @container_data = container_data

  raise ::UffizziCore::RegistryNotSupportedError unless self.class.sources.include?(type)
end

Instance Attribute Details

#container_dataObject

Returns the value of attribute container_data.



4
5
6
# File 'app/services/uffizzi_core/container_registry_service.rb', line 4

def container_data
  @container_data
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'app/services/uffizzi_core/container_registry_service.rb', line 4

def type
  @type
end

Class Method Details

.additional_sourcesObject



44
45
46
# File 'app/services/uffizzi_core/container_registry_service.rb', line 44

def additional_sources
  []
end

.docker_registry?(container) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
# File 'app/services/uffizzi_core/container_registry_service.rb', line 29

def docker_registry?(container)
  registry_url = container.dig(:image, :registry_url)
  return false if registry_url.nil?

  registry_domain_regexp = /(\w+\.\w{2,})(?::\d+)?\z/
  registry_domain = registry_url.match(registry_domain_regexp)&.to_a&.last
  return false if registry_domain.nil?

  ['amazonaws.com', 'azurecr.io', 'gcr.io', 'ghcr.io'].exclude?(registry_domain)
end

.init_by_container(container, credentials) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'app/services/uffizzi_core/container_registry_service.rb', line 12

def init_by_container(container, credentials)
  registry_url = container.dig(:image, :registry_url)

  return new(:docker_hub, container) if registry_url.include?('docker.io')
  return new(:azure, container) if registry_url.include?('azurecr.io')
  return new(:google, container) if registry_url.include?('gcr.io')
  return init_by_credentials(container, credentials) if registry_url.include?('amazonaws.com')
  return new(:github_container_registry, container) if registry_url.include?('ghcr.io')
  return new(:docker_registry, container) if docker_registry?(container)
end

.init_by_credentials(container, credentials) ⇒ Object



23
24
25
26
27
# File 'app/services/uffizzi_core/container_registry_service.rb', line 23

def init_by_credentials(container, credentials)
  return new(:docker_registry, container) if credentials && credentials.docker_registry.where(username: 'AWS').exists?

  new(:amazon, container)
end

.init_by_subclass(credential_type) ⇒ Object



7
8
9
10
# File 'app/services/uffizzi_core/container_registry_service.rb', line 7

def init_by_subclass(credential_type)
  type = credential_type.demodulize.underscore
  new(type.to_sym)
end

.sourcesObject



40
41
42
# File 'app/services/uffizzi_core/container_registry_service.rb', line 40

def sources
  [:azure, :google, :amazon, :github_container_registry, :docker_registry, :docker_hub, *additional_sources]
end

Instance Method Details

#credential(credentials_scope) ⇒ Object



90
91
92
# File 'app/services/uffizzi_core/container_registry_service.rb', line 90

def credential(credentials_scope)
  credentials_scope.send(type).first
end

#credential_correct?(credential) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
# File 'app/services/uffizzi_core/container_registry_service.rb', line 68

def credential_correct?(credential)
  service.credential_correct?(credential)
rescue URI::InvalidURIError, Faraday::ConnectionFailed, UffizziCore::ContainerRegistryError
  false
end

#digest(credential, image, tag) ⇒ Object



56
57
58
# File 'app/services/uffizzi_core/container_registry_service.rb', line 56

def digest(credential, image, tag)
  service.digest(credential, image, tag)
end

#image_available?(credentials_scope) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
# File 'app/services/uffizzi_core/container_registry_service.rb', line 94

def image_available?(credentials_scope)
  credential = credential(credentials_scope)
  service.image_available?(credential, image_data)
end

#image_dataObject



74
75
76
# File 'app/services/uffizzi_core/container_registry_service.rb', line 74

def image_data
  @image_data ||= container_data[:image]
end

#image_name(credentials) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'app/services/uffizzi_core/container_registry_service.rb', line 78

def image_name(credentials)
  if image_data[:registry_url].present? && [:google, :github_container_registry, :docker_registry, :docker_hub].exclude?(type)
    return image_data[:name]
  end

  if type == :docker_registry && credential(credentials).nil?
    return [image_data[:registry_url], image_data[:namespace], image_data[:name]].compact.join('/')
  end

  "#{image_data[:namespace]}/#{image_data[:name]}"
end

#repo_typeObject



64
65
66
# File 'app/services/uffizzi_core/container_registry_service.rb', line 64

def repo_type
  @repo_type ||= "UffizziCore::Repo::#{type.to_s.camelize}".safe_constantize
end

#serviceObject



60
61
62
# File 'app/services/uffizzi_core/container_registry_service.rb', line 60

def service
  @service ||= "UffizziCore::ContainerRegistry::#{type.to_s.camelize}Service".safe_constantize
end