2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-06-17 14:53:26 -04:00
|
|
|
module Projects
|
2014-06-17 16:49:17 -04:00
|
|
|
class DestroyService < BaseService
|
2015-06-03 05:50:08 -04:00
|
|
|
include Gitlab::ShellAdapter
|
|
|
|
|
2017-03-01 06:00:37 -05:00
|
|
|
DestroyError = Class.new(StandardError)
|
2015-06-03 05:50:08 -04:00
|
|
|
|
2016-08-06 10:25:51 -04:00
|
|
|
def async_execute
|
2017-06-01 17:36:04 -04:00
|
|
|
project.update_attribute(:pending_delete, true)
|
2019-01-16 14:44:52 -05:00
|
|
|
|
|
|
|
# Ensure no repository +deleted paths are kept,
|
|
|
|
# regardless of any issue with the ProjectDestroyWorker
|
|
|
|
# job process.
|
|
|
|
schedule_stale_repos_removal
|
|
|
|
|
2017-06-01 17:36:04 -04:00
|
|
|
job_id = ProjectDestroyWorker.perform_async(project.id, current_user.id, params)
|
2020-01-27 22:08:39 -05:00
|
|
|
log_info("User #{current_user.id} scheduled destruction of project #{project.full_path} with job ID #{job_id}")
|
2016-01-22 14:13:37 -05:00
|
|
|
end
|
|
|
|
|
2014-06-17 16:49:17 -04:00
|
|
|
def execute
|
2014-06-17 14:53:26 -04:00
|
|
|
return false unless can?(current_user, :remove_project, project)
|
|
|
|
|
2016-02-16 17:11:56 -05:00
|
|
|
# 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-07-18 11:09:14 -04:00
|
|
|
flush_caches(project)
|
2016-02-16 17:11:56 -05:00
|
|
|
|
2016-09-01 07:42:17 -04:00
|
|
|
Projects::UnlinkForkService.new(project, current_user).execute
|
|
|
|
|
2020-08-20 17:10:18 -04:00
|
|
|
attempt_destroy(project)
|
2015-06-03 05:50:08 -04:00
|
|
|
|
|
|
|
system_hook_service.execute_hooks_for(project, :destroy)
|
2020-08-12 17:09:54 -04:00
|
|
|
log_info("Project \"#{project.full_path}\" was deleted")
|
2017-07-18 11:09:14 -04:00
|
|
|
|
2018-04-04 11:14:19 -04:00
|
|
|
current_user.invalidate_personal_projects_count
|
|
|
|
|
2015-06-03 05:50:08 -04:00
|
|
|
true
|
2017-07-18 11:09:14 -04:00
|
|
|
rescue => error
|
|
|
|
attempt_rollback(project, error.message)
|
2017-07-06 09:43:07 -04:00
|
|
|
false
|
2017-07-18 11:09:14 -04:00
|
|
|
rescue Exception => error # rubocop:disable Lint/RescueException
|
|
|
|
# Project.transaction can raise Exception
|
|
|
|
attempt_rollback(project, error.message)
|
|
|
|
raise
|
2015-06-03 05:50:08 -04:00
|
|
|
end
|
2014-06-17 14:53:26 -04:00
|
|
|
|
2015-06-03 05:50:08 -04:00
|
|
|
private
|
2014-06-17 14:53:26 -04:00
|
|
|
|
2020-02-27 19:09:08 -05:00
|
|
|
def trash_project_repositories!
|
2020-01-27 22:08:39 -05:00
|
|
|
unless remove_repository(project.repository)
|
2019-04-15 08:25:48 -04:00
|
|
|
raise_error(s_('DeleteProject|Failed to remove project repository. Please try again or contact administrator.'))
|
2017-07-18 11:09:14 -04:00
|
|
|
end
|
|
|
|
|
2020-01-27 22:08:39 -05:00
|
|
|
unless remove_repository(project.wiki.repository)
|
2019-04-15 08:25:48 -04:00
|
|
|
raise_error(s_('DeleteProject|Failed to remove wiki repository. Please try again or contact administrator.'))
|
2017-07-18 11:09:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-27 19:09:08 -05:00
|
|
|
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-16 11:08:32 -04:00
|
|
|
response = ::Snippets::BulkDestroyService.new(current_user, project.snippets).execute
|
2020-02-27 19:09:08 -05:00
|
|
|
|
|
|
|
response.success?
|
|
|
|
end
|
|
|
|
|
2020-01-27 22:08:39 -05:00
|
|
|
def remove_repository(repository)
|
|
|
|
return true unless repository
|
2018-05-23 10:54:07 -04:00
|
|
|
|
2020-01-27 22:08:39 -05:00
|
|
|
result = Repositories::DestroyService.new(repository).execute
|
2015-06-03 05:50:08 -04:00
|
|
|
|
2020-01-27 22:08:39 -05:00
|
|
|
result[:status] == :success
|
2015-06-03 05:50:08 -04:00
|
|
|
end
|
|
|
|
|
2019-01-16 14:44:52 -05:00
|
|
|
def schedule_stale_repos_removal
|
2020-01-27 22:08:39 -05:00
|
|
|
repos = [project.repository, project.wiki.repository]
|
2019-01-16 14:44:52 -05:00
|
|
|
|
2020-01-27 22:08:39 -05:00
|
|
|
repos.each do |repository|
|
|
|
|
next unless repository
|
2019-01-16 14:44:52 -05:00
|
|
|
|
2020-01-27 22:08:39 -05:00
|
|
|
Repositories::ShellDestroyService.new(repository).execute(Repositories::ShellDestroyService::STALE_REMOVAL_DELAY)
|
|
|
|
end
|
2018-04-06 11:23:49 -04:00
|
|
|
end
|
|
|
|
|
2017-07-18 11:09:14 -04:00
|
|
|
def attempt_rollback(project, message)
|
|
|
|
return unless project
|
|
|
|
|
2018-03-17 02:14:12 -04:00
|
|
|
# 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-07-02 06:43:06 -04:00
|
|
|
project.update(delete_error: message, pending_delete: false) unless project.destroyed?
|
2018-03-17 02:14:12 -04:00
|
|
|
|
2017-07-18 11:09:14 -04:00
|
|
|
log_error("Deletion failed on #{project.full_path} with the following message: #{message}")
|
|
|
|
end
|
|
|
|
|
2020-08-20 17:10:18 -04:00
|
|
|
def attempt_destroy(project)
|
2018-09-19 08:37:02 -04:00
|
|
|
unless remove_registry_tags
|
2019-04-15 08:25:48 -04:00
|
|
|
raise_error(s_('DeleteProject|Failed to remove some tags in project container registry. Please try again or contact administrator.'))
|
2018-09-19 08:37:02 -04:00
|
|
|
end
|
2017-07-06 09:43:07 -04:00
|
|
|
|
2018-12-17 03:49:38 -05:00
|
|
|
project.leave_pool_repository
|
|
|
|
|
2020-08-20 17:10:18 -04:00
|
|
|
if Gitlab::Ci::Features.project_transactionless_destroy?(project)
|
|
|
|
destroy_project_related_records(project)
|
|
|
|
else
|
|
|
|
Project.transaction { destroy_project_related_records(project) }
|
2017-07-06 09:43:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-20 17:10:18 -04:00
|
|
|
def destroy_project_related_records(project)
|
|
|
|
log_destroy_event
|
|
|
|
trash_relation_repositories!
|
|
|
|
trash_project_repositories!
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
project.destroy_dependent_associations_in_batches(exclude: [:container_repositories, :snippets])
|
|
|
|
project.destroy!
|
|
|
|
end
|
|
|
|
|
2018-06-08 16:33:51 -04:00
|
|
|
def log_destroy_event
|
|
|
|
log_info("Attempting to destroy #{project.full_path} (#{project.id})")
|
|
|
|
end
|
|
|
|
|
2018-09-19 08:37:02 -04:00
|
|
|
def remove_registry_tags
|
2019-08-06 16:36:07 -04:00
|
|
|
return true unless Gitlab.config.registry.enabled
|
2018-09-19 08:37:02 -04:00
|
|
|
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-04-04 06:57:38 -04:00
|
|
|
##
|
|
|
|
# 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
|
|
|
|
return true unless Gitlab.config.registry.enabled
|
|
|
|
|
2018-09-08 01:36:19 -04:00
|
|
|
::ContainerRepository.build_root_repository(project).tap do |repository|
|
2018-04-18 05:19:40 -04:00
|
|
|
break repository.has_tags? ? repository.delete_tags! : true
|
2017-04-04 06:57:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-03 05:50:08 -04:00
|
|
|
def raise_error(message)
|
|
|
|
raise DestroyError.new(message)
|
|
|
|
end
|
|
|
|
|
2017-07-18 11:09:14 -04:00
|
|
|
def flush_caches(project)
|
2017-08-14 09:22:09 -04:00
|
|
|
Projects::ForksCountService.new(project).delete_cache
|
2016-02-16 17:11:56 -05:00
|
|
|
end
|
2014-06-17 14:53:26 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
|
|
|
Projects::DestroyService.prepend_if_ee('EE::Projects::DestroyService')
|