debian-mirror-gitlab/app/models/container_repository.rb

100 lines
2.1 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class ContainerRepository < ApplicationRecord
2019-03-02 22:35:43 +05:30
include Gitlab::Utils::StrongMemoize
2017-08-17 22:00:37 +05:30
belongs_to :project
validates :name, length: { minimum: 0, allow_nil: false }
validates :name, uniqueness: { scope: :project_id }
delegate :client, to: :registry
2019-03-02 22:35:43 +05:30
scope :ordered, -> { order(:name) }
2019-12-21 20:55:43 +05:30
scope :with_api_entity_associations, -> { preload(:project) }
2019-03-02 22:35:43 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ServiceClass
2017-08-17 22:00:37 +05:30
def registry
@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-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ServiceClass
2017-08-17 22:00:37 +05:30
def path
@path ||= [project.full_path, name]
.select(&:present?).join('/').downcase
end
def location
File.join(registry.path, path)
end
def tag(tag)
ContainerRegistry::Tag.new(self, tag)
end
def manifest
@manifest ||= client.repository_tags(path)
end
def tags
return [] unless manifest && manifest['tags']
2019-03-02 22:35:43 +05:30
strong_memoize(:tags) do
manifest['tags'].sort.map do |tag|
ContainerRegistry::Tag.new(self, tag)
end
2017-08-17 22:00:37 +05:30
end
end
def blob(config)
ContainerRegistry::Blob.new(self, config)
end
def has_tags?
tags.any?
end
def root_repository?
name.empty?
end
def delete_tags!
return unless has_tags?
2019-12-21 20:55:43 +05:30
digests = tags.map { |tag| tag.digest }.compact.to_set
2017-08-17 22:00:37 +05:30
2019-12-21 20:55:43 +05:30
digests.map(&method(:delete_tag_by_digest)).all?
2017-08-17 22:00:37 +05:30
end
2019-10-12 21:52:04 +05:30
def delete_tag_by_digest(digest)
client.delete_repository_tag(self.path, digest)
end
2017-08-17 22:00:37 +05:30
def self.build_from_path(path)
self.new(project: path.repository_project,
name: path.repository_name)
end
def self.create_from_path!(path)
build_from_path(path).tap(&:save!)
end
def self.build_root_repository(project)
self.new(project: project, name: '')
end
2019-10-12 21:52:04 +05:30
def self.find_by_path!(path)
self.find_by!(project: path.repository_project,
name: path.repository_name)
end
2017-08-17 22:00:37 +05:30
end
2019-12-04 20:38:33 +05:30
ContainerRepository.prepend_if_ee('EE::ContainerRepository')