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
|
|
|
|
2017-02-21 18:32:18 -05:00
|
|
|
DELETED_FLAG = '+deleted'.freeze
|
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)
|
|
|
|
job_id = ProjectDestroyWorker.perform_async(project.id, current_user.id, params)
|
2017-07-20 05:34:09 -04:00
|
|
|
Rails.logger.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
|
|
|
|
|
2017-10-03 12:27:51 -04:00
|
|
|
# The project is not necessarily a fork, so update the fork network originating
|
|
|
|
# from this project
|
|
|
|
if fork_network = project.root_of_fork_network
|
|
|
|
fork_network.update(root_project: nil,
|
|
|
|
deleted_root_project_name: project.full_name)
|
|
|
|
end
|
|
|
|
|
2017-07-18 11:09:14 -04:00
|
|
|
attempt_destroy_transaction(project)
|
2015-06-03 05:50:08 -04:00
|
|
|
|
|
|
|
system_hook_service.execute_hooks_for(project, :destroy)
|
2017-06-29 07:43:01 -04:00
|
|
|
log_info("Project \"#{project.full_path}\" was removed")
|
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
|
|
|
|
2018-04-06 11:23:49 -04:00
|
|
|
def attempt_repositories_rollback
|
|
|
|
return unless @project
|
|
|
|
|
|
|
|
flush_caches(@project)
|
|
|
|
|
|
|
|
unless mv_repository(removal_path(repo_path), repo_path)
|
|
|
|
raise_error('Failed to restore project repository. Please contact the administrator.')
|
|
|
|
end
|
|
|
|
|
|
|
|
unless mv_repository(removal_path(wiki_path), wiki_path)
|
|
|
|
raise_error('Failed to restore wiki repository. Please contact the administrator.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-03 05:50:08 -04:00
|
|
|
private
|
2014-06-17 14:53:26 -04:00
|
|
|
|
2017-07-18 11:09:14 -04:00
|
|
|
def repo_path
|
2017-07-21 00:13:26 -04:00
|
|
|
project.disk_path
|
2017-07-18 11:09:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def wiki_path
|
2017-10-18 07:53:06 -04:00
|
|
|
project.wiki.disk_path
|
2017-07-18 11:09:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def trash_repositories!
|
|
|
|
unless remove_repository(repo_path)
|
|
|
|
raise_error('Failed to remove project repository. Please try again or contact administrator.')
|
|
|
|
end
|
|
|
|
|
|
|
|
unless remove_repository(wiki_path)
|
|
|
|
raise_error('Failed to remove wiki repository. Please try again or contact administrator.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-03 05:50:08 -04:00
|
|
|
def remove_repository(path)
|
2015-06-03 10:16:27 -04:00
|
|
|
# Skip repository removal. We use this flag when remove user or group
|
|
|
|
return true if params[:skip_repo] == true
|
|
|
|
|
2015-06-03 05:50:08 -04:00
|
|
|
new_path = removal_path(path)
|
|
|
|
|
2018-04-06 11:23:49 -04:00
|
|
|
if mv_repository(path, new_path)
|
2015-06-03 05:50:08 -04:00
|
|
|
log_info("Repository \"#{path}\" moved to \"#{new_path}\"")
|
2017-06-01 17:36:04 -04:00
|
|
|
|
|
|
|
project.run_after_commit do
|
|
|
|
# self is now project
|
|
|
|
GitlabShellWorker.perform_in(5.minutes, :remove_repository, self.repository_storage_path, new_path)
|
|
|
|
end
|
2015-06-03 05:50:08 -04:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-06 11:23:49 -04:00
|
|
|
def mv_repository(from_path, to_path)
|
|
|
|
# There is a possibility project does not have repository or wiki
|
|
|
|
return true unless gitlab_shell.exists?(project.repository_storage_path, from_path + '.git')
|
|
|
|
|
|
|
|
gitlab_shell.mv_repository(project.repository_storage_path, from_path, to_path)
|
|
|
|
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.
|
|
|
|
project.update_attributes(delete_error: message, pending_delete: false) unless project.destroyed?
|
|
|
|
|
2017-07-18 11:09:14 -04:00
|
|
|
log_error("Deletion failed on #{project.full_path} with the following message: #{message}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def attempt_destroy_transaction(project)
|
2017-07-06 09:43:07 -04:00
|
|
|
Project.transaction do
|
|
|
|
unless remove_legacy_registry_tags
|
|
|
|
raise_error('Failed to remove some tags in project container registry. Please try again or contact administrator.')
|
|
|
|
end
|
|
|
|
|
2017-07-18 11:09:14 -04:00
|
|
|
trash_repositories!
|
2017-07-06 09:43:07 -04:00
|
|
|
|
|
|
|
project.team.truncate
|
|
|
|
project.destroy!
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
|
|
|
ContainerRepository.build_root_repository(project).tap do |repository|
|
2017-04-05 08:32:45 -04:00
|
|
|
return 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
|
|
|
|
|
|
|
|
# Build a path for removing repositories
|
|
|
|
# We use `+` because its not allowed by GitLab so user can not create
|
|
|
|
# project with name cookies+119+deleted and capture someone stalled repository
|
|
|
|
#
|
|
|
|
# gitlab/cookies.git -> gitlab/cookies+119+deleted.git
|
|
|
|
#
|
|
|
|
def removal_path(path)
|
|
|
|
"#{path}+#{project.id}#{DELETED_FLAG}"
|
2014-06-17 14:53:26 -04:00
|
|
|
end
|
2016-02-16 17:11:56 -05:00
|
|
|
|
2017-07-18 11:09:14 -04:00
|
|
|
def flush_caches(project)
|
2016-02-23 06:02:59 -05:00
|
|
|
project.repository.before_delete
|
2016-02-16 17:11:56 -05:00
|
|
|
|
2017-07-23 03:05:34 -04:00
|
|
|
Repository.new(wiki_path, project, disk_path: repo_path).before_delete
|
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
|