2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
module Projects
|
|
|
|
class DestroyService < BaseService
|
2015-09-11 14:41:01 +05:30
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
DestroyError = Class.new(StandardError)
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def async_execute
|
2017-09-10 17:25:29 +05:30
|
|
|
project.update_attribute(:pending_delete, true)
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
# Ensure no repository +deleted paths are kept,
|
|
|
|
# regardless of any issue with the ProjectDestroyWorker
|
|
|
|
# job process.
|
|
|
|
schedule_stale_repos_removal
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
job_id = ProjectDestroyWorker.perform_async(project.id, current_user.id, params)
|
2020-03-13 15:44:24 +05:30
|
|
|
log_info("User #{current_user.id} scheduled destruction of project #{project.full_path} with job ID #{job_id}")
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def execute
|
|
|
|
return false unless can?(current_user, :remove_project, project)
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
# Flush the cache for both repositories. This has to be done _before_
|
|
|
|
# removing the physical repositories as some expiration code depends on
|
|
|
|
# Git data (e.g. a list of branch names).
|
2017-09-10 17:25:29 +05:30
|
|
|
flush_caches(project)
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
Projects::UnlinkForkService.new(project, current_user).execute
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
attempt_destroy_transaction(project)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
system_hook_service.execute_hooks_for(project, :destroy)
|
2017-09-10 17:25:29 +05:30
|
|
|
log_info("Project \"#{project.full_path}\" was removed")
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
current_user.invalidate_personal_projects_count
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
true
|
2017-09-10 17:25:29 +05:30
|
|
|
rescue => error
|
|
|
|
attempt_rollback(project, error.message)
|
|
|
|
false
|
|
|
|
rescue Exception => error # rubocop:disable Lint/RescueException
|
|
|
|
# Project.transaction can raise Exception
|
|
|
|
attempt_rollback(project, error.message)
|
|
|
|
raise
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def trash_project_repositories!
|
2020-03-13 15:44:24 +05:30
|
|
|
unless remove_repository(project.repository)
|
2019-07-31 22:56:46 +05:30
|
|
|
raise_error(s_('DeleteProject|Failed to remove project repository. Please try again or contact administrator.'))
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
unless remove_repository(project.wiki.repository)
|
2019-07-31 22:56:46 +05:30
|
|
|
raise_error(s_('DeleteProject|Failed to remove wiki repository. Please try again or contact administrator.'))
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
def trash_relation_repositories!
|
|
|
|
unless remove_snippets
|
|
|
|
raise_error(s_('DeleteProject|Failed to remove project snippets. Please try again or contact administrator.'))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_snippets
|
2020-06-23 00:09:42 +05:30
|
|
|
response = ::Snippets::BulkDestroyService.new(current_user, project.snippets).execute
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
response.success?
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def remove_repository(repository)
|
|
|
|
return true unless repository
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
result = Repositories::DestroyService.new(repository).execute
|
2015-09-11 14:41:01 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
result[:status] == :success
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
def schedule_stale_repos_removal
|
2020-03-13 15:44:24 +05:30
|
|
|
repos = [project.repository, project.wiki.repository]
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
repos.each do |repository|
|
|
|
|
next unless repository
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
Repositories::ShellDestroyService.new(repository).execute(Repositories::ShellDestroyService::STALE_REMOVAL_DELAY)
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def attempt_rollback(project, message)
|
|
|
|
return unless project
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
# It's possible that the project was destroyed, but some after_commit
|
|
|
|
# hook failed and caused us to end up here. A destroyed model will be a frozen hash,
|
|
|
|
# which cannot be altered.
|
2018-11-18 11:00:15 +05:30
|
|
|
project.update(delete_error: message, pending_delete: false) unless project.destroyed?
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
log_error("Deletion failed on #{project.full_path} with the following message: #{message}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def attempt_destroy_transaction(project)
|
2018-12-05 23:21:45 +05:30
|
|
|
unless remove_registry_tags
|
2019-07-31 22:56:46 +05:30
|
|
|
raise_error(s_('DeleteProject|Failed to remove some tags in project container registry. Please try again or contact administrator.'))
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
project.leave_pool_repository
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
Project.transaction do
|
2018-11-08 19:23:39 +05:30
|
|
|
log_destroy_event
|
2020-04-08 14:13:33 +05:30
|
|
|
trash_relation_repositories!
|
|
|
|
trash_project_repositories!
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
# Rails attempts to load all related records into memory before
|
|
|
|
# destroying: https://github.com/rails/rails/issues/22510
|
|
|
|
# This ensures we delete records in batches.
|
|
|
|
#
|
|
|
|
# Exclude container repositories because its before_destroy would be
|
|
|
|
# called multiple times, and it doesn't destroy any database records.
|
2020-04-08 14:13:33 +05:30
|
|
|
project.destroy_dependent_associations_in_batches(exclude: [:container_repositories, :snippets])
|
2017-09-10 17:25:29 +05:30
|
|
|
project.destroy!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def log_destroy_event
|
|
|
|
log_info("Attempting to destroy #{project.full_path} (#{project.id})")
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def remove_registry_tags
|
2019-10-12 21:52:04 +05:30
|
|
|
return true unless Gitlab.config.registry.enabled
|
2018-12-05 23:21:45 +05:30
|
|
|
return false unless remove_legacy_registry_tags
|
|
|
|
|
|
|
|
project.container_repositories.find_each do |container_repository|
|
|
|
|
service = Projects::ContainerRepository::DestroyService.new(project, current_user)
|
|
|
|
service.execute(container_repository)
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
##
|
|
|
|
# This method makes sure that we correctly remove registry tags
|
|
|
|
# for legacy image repository (when repository path equals project path).
|
|
|
|
#
|
|
|
|
def remove_legacy_registry_tags
|
2016-06-02 11:05:42 +05:30
|
|
|
return true unless Gitlab.config.registry.enabled
|
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
::ContainerRepository.build_root_repository(project).tap do |repository|
|
2018-10-15 14:42:47 +05:30
|
|
|
break repository.has_tags? ? repository.delete_tags! : true
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def raise_error(message)
|
|
|
|
raise DestroyError.new(message)
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def flush_caches(project)
|
|
|
|
Projects::ForksCountService.new(project).delete_cache
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
Projects::DestroyService.prepend_if_ee('EE::Projects::DestroyService')
|