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)
|
|
|
|
Rails.logger.info("User #{current_user.id} scheduled destruction of project #{project.path_with_namespace} 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)
|
|
|
|
|
2015-06-03 05:50:08 -04:00
|
|
|
repo_path = project.path_with_namespace
|
|
|
|
wiki_path = repo_path + '.wiki'
|
|
|
|
|
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).
|
|
|
|
flush_caches(project, wiki_path)
|
|
|
|
|
2016-09-01 07:42:17 -04:00
|
|
|
Projects::UnlinkForkService.new(project, current_user).execute
|
|
|
|
|
2015-06-03 05:50:08 -04:00
|
|
|
Project.transaction do
|
2017-02-19 08:00:27 -05:00
|
|
|
project.team.truncate
|
2017-04-05 08:20:33 -04:00
|
|
|
project.destroy!
|
2017-04-04 06:57:38 -04:00
|
|
|
|
|
|
|
unless remove_legacy_registry_tags
|
|
|
|
raise_error('Failed to remove some tags in project container registry. Please try again or contact administrator.')
|
|
|
|
end
|
2014-06-17 14:53:26 -04:00
|
|
|
|
2015-06-03 05:50:08 -04:00
|
|
|
unless remove_repository(repo_path)
|
2017-04-04 06:57:38 -04:00
|
|
|
raise_error('Failed to remove project repository. Please try again or contact administrator.')
|
2015-06-03 05:50:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
unless remove_repository(wiki_path)
|
2017-04-04 06:57:38 -04:00
|
|
|
raise_error('Failed to remove wiki repository. Please try again or contact administrator.')
|
2015-06-03 05:50:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-11 01:07:13 -04:00
|
|
|
log_info("Project \"#{project.path_with_namespace}\" was removed")
|
2015-06-03 05:50:08 -04:00
|
|
|
system_hook_service.execute_hooks_for(project, :destroy)
|
|
|
|
true
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
# There is a possibility project does not have repository or wiki
|
2016-06-22 17:04:51 -04:00
|
|
|
return true unless gitlab_shell.exists?(project.repository_storage_path, path + '.git')
|
2015-06-03 05:50:08 -04:00
|
|
|
|
|
|
|
new_path = removal_path(path)
|
|
|
|
|
2016-06-22 17:04:51 -04:00
|
|
|
if gitlab_shell.mv_repository(project.repository_storage_path, 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
|
|
|
|
|
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
|
|
|
|
|
|
|
def flush_caches(project, wiki_path)
|
2016-02-23 06:02:59 -05:00
|
|
|
project.repository.before_delete
|
2016-02-16 17:11:56 -05:00
|
|
|
|
2016-02-23 06:02:59 -05:00
|
|
|
Repository.new(wiki_path, project).before_delete
|
2016-02-16 17:11:56 -05:00
|
|
|
end
|
2014-06-17 14:53:26 -04:00
|
|
|
end
|
|
|
|
end
|