2019-03-02 22:35:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Projects
|
|
|
|
module ContainerRepository
|
|
|
|
class CleanupTagsService < BaseService
|
|
|
|
def execute(container_repository)
|
|
|
|
return error('feature disabled') unless can_use?
|
2020-03-13 15:44:24 +05:30
|
|
|
return error('access denied') unless can_destroy?
|
2020-06-23 00:09:42 +05:30
|
|
|
return error('invalid regex') unless valid_regex?
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
tags = container_repository.tags
|
|
|
|
tags = without_latest(tags)
|
|
|
|
tags = filter_by_name(tags)
|
|
|
|
tags = filter_keep_n(tags)
|
|
|
|
tags = filter_by_older_than(tags)
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
delete_tags(container_repository, tags)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
def delete_tags(container_repository, tags)
|
|
|
|
return success(deleted: []) unless tags.any?
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
tag_names = tags.map(&:name)
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
Projects::ContainerRepository::DeleteTagsService
|
2020-11-24 15:15:51 +05:30
|
|
|
.new(container_repository.project,
|
|
|
|
current_user,
|
|
|
|
tags: tag_names,
|
|
|
|
container_expiration_policy: params['container_expiration_policy'])
|
2020-04-22 19:07:51 +05:30
|
|
|
.execute(container_repository)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def without_latest(tags)
|
|
|
|
tags.reject(&:latest?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def order_by_date(tags)
|
2020-05-24 23:13:21 +05:30
|
|
|
now = DateTime.current
|
2019-03-02 22:35:43 +05:30
|
|
|
tags.sort_by { |tag| tag.created_at || now }.reverse
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_name(tags)
|
2020-10-24 23:57:45 +05:30
|
|
|
regex_delete = ::Gitlab::UntrustedRegexp.new("\\A#{params['name_regex_delete'] || params['name_regex']}\\z")
|
|
|
|
regex_retain = ::Gitlab::UntrustedRegexp.new("\\A#{params['name_regex_keep']}\\z")
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
tags.select do |tag|
|
2020-04-08 14:13:33 +05:30
|
|
|
# regex_retain will override any overlapping matches by regex_delete
|
|
|
|
regex_delete.match?(tag.name) && !regex_retain.match?(tag.name)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_keep_n(tags)
|
2020-04-22 19:07:51 +05:30
|
|
|
return tags unless params['keep_n']
|
|
|
|
|
|
|
|
tags = order_by_date(tags)
|
2019-03-02 22:35:43 +05:30
|
|
|
tags.drop(params['keep_n'].to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_by_older_than(tags)
|
|
|
|
return tags unless params['older_than']
|
|
|
|
|
|
|
|
older_than = ChronicDuration.parse(params['older_than']).seconds.ago
|
|
|
|
|
|
|
|
tags.select do |tag|
|
|
|
|
tag.created_at && tag.created_at < older_than
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def can_destroy?
|
|
|
|
return true if params['container_expiration_policy']
|
|
|
|
|
|
|
|
can?(current_user, :destroy_container_image, project)
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def can_use?
|
|
|
|
Feature.enabled?(:container_registry_cleanup, project, default_enabled: true)
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
def valid_regex?
|
|
|
|
%w(name_regex_delete name_regex name_regex_keep).each do |param_name|
|
|
|
|
regex = params[param_name]
|
2020-10-24 23:57:45 +05:30
|
|
|
::Gitlab::UntrustedRegexp.new(regex) unless regex.blank?
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
true
|
|
|
|
rescue RegexpError => e
|
2020-10-24 23:57:45 +05:30
|
|
|
::Gitlab::ErrorTracking.log_exception(e, project_id: project.id)
|
2020-06-23 00:09:42 +05:30
|
|
|
false
|
|
|
|
end
|
2019-03-02 22:35:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|