debian-mirror-gitlab/app/services/projects/destroy_service.rb

143 lines
4.4 KiB
Ruby
Raw Normal View History

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
2017-08-17 22:00:37 +05:30
DELETED_FLAG = '+deleted'.freeze
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)
job_id = ProjectDestroyWorker.perform_async(project.id, current_user.id, params)
Rails.logger.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
2018-03-17 18:26:18 +05:30
# 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-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")
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
2017-09-10 17:25:29 +05:30
def repo_path
project.disk_path
end
def wiki_path
2018-03-17 18:26:18 +05:30
project.wiki.disk_path
2017-09-10 17:25:29 +05:30
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-09-11 14:41:01 +05:30
def remove_repository(path)
# Skip repository removal. We use this flag when remove user or group
return true if params[:skip_repo] == true
# There is a possibility project does not have repository or wiki
2016-08-24 12:49:21 +05:30
return true unless gitlab_shell.exists?(project.repository_storage_path, path + '.git')
2015-09-11 14:41:01 +05:30
new_path = removal_path(path)
2016-08-24 12:49:21 +05:30
if gitlab_shell.mv_repository(project.repository_storage_path, path, new_path)
2015-09-11 14:41:01 +05:30
log_info("Repository \"#{path}\" moved to \"#{new_path}\"")
2017-09-10 17:25:29 +05:30
project.run_after_commit do
# self is now project
GitlabShellWorker.perform_in(5.minutes, :remove_repository, self.repository_storage_path, new_path)
end
2015-09-11 14:41:01 +05:30
else
false
end
end
2017-09-10 17:25:29 +05:30
def attempt_rollback(project, message)
return unless project
project.update_attributes(delete_error: message, pending_delete: false)
log_error("Deletion failed on #{project.full_path} with the following message: #{message}")
end
def attempt_destroy_transaction(project)
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
trash_repositories!
project.team.truncate
project.destroy!
end
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
2017-08-17 22:00:37 +05:30
ContainerRepository.build_root_repository(project).tap do |repository|
return repository.has_tags? ? repository.delete_tags! : true
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
# 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-09-02 18:07:02 +05:30
end
2016-04-02 18:10:28 +05:30
2017-09-10 17:25:29 +05:30
def flush_caches(project)
2016-06-02 11:05:42 +05:30
project.repository.before_delete
2016-04-02 18:10:28 +05:30
2017-09-10 17:25:29 +05:30
Repository.new(wiki_path, project, disk_path: repo_path).before_delete
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