2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-23 13:00:41 +00:00
|
|
|
class ContainerRepository < ActiveRecord::Base
|
2016-11-23 16:50:30 +00:00
|
|
|
belongs_to :project
|
2017-03-24 11:31:34 +00:00
|
|
|
|
|
|
|
validates :name, length: { minimum: 0, allow_nil: false }
|
2017-04-03 09:51:13 +00:00
|
|
|
validates :name, uniqueness: { scope: :project_id }
|
2017-03-24 11:31:34 +00:00
|
|
|
|
|
|
|
delegate :client, to: :registry
|
2017-03-31 13:10:15 +00:00
|
|
|
|
|
|
|
before_destroy :delete_tags!
|
2016-11-23 16:50:30 +00:00
|
|
|
|
2017-03-22 11:28:23 +00:00
|
|
|
def registry
|
2017-03-23 13:37:17 +00: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
|
|
|
|
|
|
|
|
def path
|
2017-04-13 09:54:02 +00:00
|
|
|
@path ||= [project.full_path, name]
|
|
|
|
.select(&:present?).join('/').downcase
|
2016-11-02 02:33:35 +00:00
|
|
|
end
|
|
|
|
|
2017-04-10 10:57:19 +00:00
|
|
|
def location
|
|
|
|
File.join(registry.path, path)
|
|
|
|
end
|
|
|
|
|
2016-11-02 02:33:35 +00:00
|
|
|
def tag(tag)
|
|
|
|
ContainerRegistry::Tag.new(self, tag)
|
|
|
|
end
|
|
|
|
|
|
|
|
def manifest
|
2017-04-05 12:44:35 +00:00
|
|
|
@manifest ||= client.repository_tags(path)
|
2016-11-02 02:33:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def tags
|
|
|
|
return @tags if defined?(@tags)
|
|
|
|
return [] unless manifest && manifest['tags']
|
|
|
|
|
|
|
|
@tags = manifest['tags'].map do |tag|
|
|
|
|
ContainerRegistry::Tag.new(self, tag)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def blob(config)
|
|
|
|
ContainerRegistry::Blob.new(self, config)
|
|
|
|
end
|
|
|
|
|
2017-03-29 10:14:29 +00:00
|
|
|
def has_tags?
|
2017-04-05 12:44:35 +00:00
|
|
|
tags.any?
|
2017-03-29 10:14:29 +00:00
|
|
|
end
|
|
|
|
|
2017-04-03 10:49:54 +00:00
|
|
|
def root_repository?
|
|
|
|
name.empty?
|
|
|
|
end
|
|
|
|
|
2017-03-31 09:54:09 +00:00
|
|
|
def delete_tags!
|
|
|
|
return unless has_tags?
|
|
|
|
|
|
|
|
digests = tags.map { |tag| tag.digest }.to_set
|
2016-11-02 02:33:35 +00:00
|
|
|
|
2016-11-23 16:50:30 +00:00
|
|
|
digests.all? do |digest|
|
2017-03-23 13:00:41 +00:00
|
|
|
client.delete_repository_tag(self.path, digest)
|
2016-11-23 16:50:30 +00:00
|
|
|
end
|
2016-11-02 02:33:35 +00:00
|
|
|
end
|
2017-03-30 11:31:33 +00:00
|
|
|
|
2017-04-03 10:49:54 +00:00
|
|
|
def self.build_from_path(path)
|
|
|
|
self.new(project: path.repository_project,
|
|
|
|
name: path.repository_name)
|
|
|
|
end
|
|
|
|
|
2017-03-31 10:27:05 +00:00
|
|
|
def self.create_from_path!(path)
|
2017-04-03 10:49:54 +00:00
|
|
|
build_from_path(path).tap(&:save!)
|
2017-03-30 11:31:33 +00:00
|
|
|
end
|
2017-04-04 10:57:38 +00:00
|
|
|
|
|
|
|
def self.build_root_repository(project)
|
|
|
|
self.new(project: project, name: '')
|
|
|
|
end
|
2016-11-02 02:33:35 +00:00
|
|
|
end
|