2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-23 09:00:41 -04:00
|
|
|
class ContainerRepository < ActiveRecord::Base
|
2019-01-10 09:22:58 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2016-11-23 11:50:30 -05:00
|
|
|
belongs_to :project
|
2017-03-24 07:31:34 -04:00
|
|
|
|
|
|
|
validates :name, length: { minimum: 0, allow_nil: false }
|
2017-04-03 05:51:13 -04:00
|
|
|
validates :name, uniqueness: { scope: :project_id }
|
2017-03-24 07:31:34 -04:00
|
|
|
|
|
|
|
delegate :client, to: :registry
|
2017-03-31 09:10:15 -04:00
|
|
|
|
2019-01-10 09:22:58 -05:00
|
|
|
scope :ordered, -> { order(:name) }
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2017-03-22 07:28:23 -04:00
|
|
|
def registry
|
2017-03-23 09:37:17 -04:00
|
|
|
@registry ||= begin
|
|
|
|
token = Auth::ContainerRegistryAuthenticationService.full_access_token(path)
|
|
|
|
|
|
|
|
url = Gitlab.config.registry.api_url
|
|
|
|
host_port = Gitlab.config.registry.host_port
|
|
|
|
|
|
|
|
ContainerRegistry::Registry.new(url, token: token, path: host_port)
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2017-03-23 09:37:17 -04:00
|
|
|
|
|
|
|
def path
|
2017-04-13 05:54:02 -04:00
|
|
|
@path ||= [project.full_path, name]
|
|
|
|
.select(&:present?).join('/').downcase
|
2016-11-01 22:33:35 -04:00
|
|
|
end
|
|
|
|
|
2017-04-10 06:57:19 -04:00
|
|
|
def location
|
|
|
|
File.join(registry.path, path)
|
|
|
|
end
|
|
|
|
|
2016-11-01 22:33:35 -04:00
|
|
|
def tag(tag)
|
|
|
|
ContainerRegistry::Tag.new(self, tag)
|
|
|
|
end
|
|
|
|
|
|
|
|
def manifest
|
2017-04-05 08:44:35 -04:00
|
|
|
@manifest ||= client.repository_tags(path)
|
2016-11-01 22:33:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tags
|
|
|
|
return [] unless manifest && manifest['tags']
|
|
|
|
|
2019-01-10 09:22:58 -05:00
|
|
|
strong_memoize(:tags) do
|
|
|
|
manifest['tags'].sort.map do |tag|
|
|
|
|
ContainerRegistry::Tag.new(self, tag)
|
|
|
|
end
|
2016-11-01 22:33:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def blob(config)
|
|
|
|
ContainerRegistry::Blob.new(self, config)
|
|
|
|
end
|
|
|
|
|
2017-03-29 06:14:29 -04:00
|
|
|
def has_tags?
|
2017-04-05 08:44:35 -04:00
|
|
|
tags.any?
|
2017-03-29 06:14:29 -04:00
|
|
|
end
|
|
|
|
|
2017-04-03 06:49:54 -04:00
|
|
|
def root_repository?
|
|
|
|
name.empty?
|
|
|
|
end
|
|
|
|
|
2017-03-31 05:54:09 -04:00
|
|
|
def delete_tags!
|
|
|
|
return unless has_tags?
|
|
|
|
|
|
|
|
digests = tags.map { |tag| tag.digest }.to_set
|
2016-11-01 22:33:35 -04:00
|
|
|
|
2016-11-23 11:50:30 -05:00
|
|
|
digests.all? do |digest|
|
2017-03-23 09:00:41 -04:00
|
|
|
client.delete_repository_tag(self.path, digest)
|
2016-11-23 11:50:30 -05:00
|
|
|
end
|
2016-11-01 22:33:35 -04:00
|
|
|
end
|
2017-03-30 07:31:33 -04:00
|
|
|
|
2017-04-03 06:49:54 -04:00
|
|
|
def self.build_from_path(path)
|
|
|
|
self.new(project: path.repository_project,
|
|
|
|
name: path.repository_name)
|
|
|
|
end
|
|
|
|
|
2017-03-31 06:27:05 -04:00
|
|
|
def self.create_from_path!(path)
|
2017-04-03 06:49:54 -04:00
|
|
|
build_from_path(path).tap(&:save!)
|
2017-03-30 07:31:33 -04:00
|
|
|
end
|
2017-04-04 06:57:38 -04:00
|
|
|
|
|
|
|
def self.build_root_repository(project)
|
|
|
|
self.new(project: project, name: '')
|
|
|
|
end
|
2016-11-01 22:33:35 -04:00
|
|
|
end
|