BigW Consortium Gitlab

container_repository.rb 1.67 KB
Newer Older
1
class ContainerRepository < ActiveRecord::Base
2
  belongs_to :project
3 4

  validates :name, length: { minimum: 0, allow_nil: false }
5
  validates :name, uniqueness: { scope: :project_id }
6 7

  delegate :client, to: :registry
8 9

  before_destroy :delete_tags!
10

11
  def registry
12 13 14 15 16 17 18 19 20 21 22
    @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
23 24
    @path ||= [project.full_path, name]
      .select(&:present?).join('/').downcase
25 26
  end

27 28 29 30
  def location
    File.join(registry.path, path)
  end

31 32 33 34 35
  def tag(tag)
    ContainerRegistry::Tag.new(self, tag)
  end

  def manifest
36
    @manifest ||= client.repository_tags(path)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  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

52
  def has_tags?
53
    tags.any?
54 55
  end

56 57 58 59
  def root_repository?
    name.empty?
  end

60 61 62 63
  def delete_tags!
    return unless has_tags?

    digests = tags.map { |tag| tag.digest }.to_set
64

65
    digests.all? do |digest|
66
      client.delete_repository_tag(self.path, digest)
67
    end
68
  end
69

70 71 72 73 74
  def self.build_from_path(path)
    self.new(project: path.repository_project,
             name: path.repository_name)
  end

75
  def self.create_from_path!(path)
76
    build_from_path(path).tap(&:save!)
77
  end
78 79 80 81

  def self.build_root_repository(project)
    self.new(project: project, name: '')
  end
82
end