2016-11-02 02:33:35 +00:00
|
|
|
class ContainerImage < ActiveRecord::Base
|
2016-11-23 16:50:30 +00:00
|
|
|
belongs_to :project
|
2016-11-02 02:33:35 +00:00
|
|
|
|
2017-03-22 11:28:23 +00:00
|
|
|
delegate :container_registry, to: :project
|
2016-11-23 16:50:30 +00:00
|
|
|
delegate :client, to: :container_registry
|
2016-11-02 02:33:35 +00:00
|
|
|
|
|
|
|
validates :manifest, presence: true
|
|
|
|
|
2016-11-23 16:50:30 +00:00
|
|
|
before_destroy :delete_tags
|
|
|
|
|
2017-03-22 11:28:23 +00:00
|
|
|
def registry
|
|
|
|
# TODO, container registry with image access level
|
|
|
|
token = Auth::ContainerRegistryAuthenticationService.image_token(self)
|
2016-11-02 02:33:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def tag(tag)
|
|
|
|
ContainerRegistry::Tag.new(self, tag)
|
|
|
|
end
|
|
|
|
|
|
|
|
def manifest
|
|
|
|
@manifest ||= client.repository_tags(name_with_namespace)
|
|
|
|
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
|
|
|
|
|
|
|
|
def delete_tags
|
|
|
|
return unless tags
|
|
|
|
|
2016-11-23 16:50:30 +00:00
|
|
|
digests = tags.map {|tag| tag.digest }.to_set
|
|
|
|
digests.all? do |digest|
|
|
|
|
client.delete_repository_tag(name_with_namespace, digest)
|
|
|
|
end
|
2016-11-02 02:33:35 +00:00
|
|
|
end
|
|
|
|
|
2017-03-22 15:07:27 +00:00
|
|
|
def self.from_path(full_path)
|
|
|
|
return unless full_path.include?('/')
|
2016-12-16 03:24:05 +00:00
|
|
|
|
2017-03-22 15:07:27 +00:00
|
|
|
path = full_path[0...full_path.rindex('/')]
|
|
|
|
name = full_path[full_path.rindex('/')+1..-1]
|
|
|
|
project = Project.find_by_full_path(path)
|
|
|
|
|
|
|
|
self.new(name: name, path: path, project: project)
|
2016-11-02 02:33:35 +00:00
|
|
|
end
|
|
|
|
end
|