2018-07-17 16:50:37 +00:00
# frozen_string_literal: true
2014-06-17 18:53:26 +00:00
module Projects
2014-06-17 20:49:17 +00:00
class DestroyService < BaseService
2015-06-03 09:50:08 +00:00
include Gitlab :: ShellAdapter
2017-03-01 11:00:37 +00:00
DestroyError = Class . new ( StandardError )
2015-06-03 09:50:08 +00:00
2019-08-31 19:22:19 +00:00
DELETED_FLAG = '+deleted'
2019-01-16 19:44:52 +00:00
REPO_REMOVAL_DELAY = 5 . minutes . to_i
2015-06-03 09:50:08 +00:00
2016-08-06 14:25:51 +00:00
def async_execute
2017-06-01 21:36:04 +00:00
project . update_attribute ( :pending_delete , true )
2019-01-16 19:44:52 +00:00
# Ensure no repository +deleted paths are kept,
# regardless of any issue with the ProjectDestroyWorker
# job process.
schedule_stale_repos_removal
2017-06-01 21:36:04 +00:00
job_id = ProjectDestroyWorker . perform_async ( project . id , current_user . id , params )
2019-07-10 19:26:47 +00:00
Rails . logger . info ( " User #{ current_user . id } scheduled destruction of project #{ project . full_path } with job ID #{ job_id } " ) # rubocop:disable Gitlab/RailsLogger
2016-01-22 19:13:37 +00:00
end
2014-06-17 20:49:17 +00:00
def execute
2014-06-17 18:53:26 +00:00
return false unless can? ( current_user , :remove_project , project )
2016-02-16 22:11:56 +00: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 15:09:14 +00:00
flush_caches ( project )
2016-02-16 22:11:56 +00:00
2016-09-01 11:42:17 +00:00
Projects :: UnlinkForkService . new ( project , current_user ) . execute
2017-07-18 15:09:14 +00:00
attempt_destroy_transaction ( project )
2015-06-03 09:50:08 +00:00
system_hook_service . execute_hooks_for ( project , :destroy )
2017-06-29 11:43:01 +00:00
log_info ( " Project \" #{ project . full_path } \" was removed " )
2017-07-18 15:09:14 +00:00
2018-04-04 15:14:19 +00:00
current_user . invalidate_personal_projects_count
2015-06-03 09:50:08 +00:00
true
2017-07-18 15:09:14 +00:00
rescue = > error
attempt_rollback ( project , error . message )
2017-07-06 13:43:07 +00:00
false
2017-07-18 15:09:14 +00:00
rescue Exception = > error # rubocop:disable Lint/RescueException
# Project.transaction can raise Exception
attempt_rollback ( project , error . message )
raise
2015-06-03 09:50:08 +00:00
end
2014-06-17 18:53:26 +00:00
2018-04-06 15:23:49 +00:00
def attempt_repositories_rollback
return unless @project
flush_caches ( @project )
2018-05-23 14:54:07 +00:00
unless rollback_repository ( removal_path ( repo_path ) , repo_path )
2019-04-15 12:25:48 +00:00
raise_error ( s_ ( 'DeleteProject|Failed to restore project repository. Please contact the administrator.' ) )
2018-04-06 15:23:49 +00:00
end
2018-05-23 14:54:07 +00:00
unless rollback_repository ( removal_path ( wiki_path ) , wiki_path )
2019-04-15 12:25:48 +00:00
raise_error ( s_ ( 'DeleteProject|Failed to restore wiki repository. Please contact the administrator.' ) )
2018-04-06 15:23:49 +00:00
end
end
2015-06-03 09:50:08 +00:00
private
2014-06-17 18:53:26 +00:00
2017-07-18 15:09:14 +00:00
def repo_path
2017-07-21 04:13:26 +00:00
project . disk_path
2017-07-18 15:09:14 +00:00
end
def wiki_path
2017-10-18 11:53:06 +00:00
project . wiki . disk_path
2017-07-18 15:09:14 +00:00
end
def trash_repositories!
unless remove_repository ( repo_path )
2019-04-15 12:25:48 +00:00
raise_error ( s_ ( 'DeleteProject|Failed to remove project repository. Please try again or contact administrator.' ) )
2017-07-18 15:09:14 +00:00
end
unless remove_repository ( wiki_path )
2019-04-15 12:25:48 +00:00
raise_error ( s_ ( 'DeleteProject|Failed to remove wiki repository. Please try again or contact administrator.' ) )
2017-07-18 15:09:14 +00:00
end
end
2015-06-03 09:50:08 +00:00
def remove_repository ( path )
2018-05-23 14:54:07 +00:00
# There is a possibility project does not have repository or wiki
return true unless repo_exists? ( path )
2015-06-03 09:50:08 +00:00
new_path = removal_path ( path )
2018-04-06 15:23:49 +00:00
if mv_repository ( path , new_path )
2018-05-10 06:24:57 +00:00
log_info ( %Q{ Repository " #{ path } " moved to " #{ new_path } " for project " #{ project . full_path } " } )
2017-06-01 21:36:04 +00:00
project . run_after_commit do
2019-01-16 19:44:52 +00:00
GitlabShellWorker . perform_in ( REPO_REMOVAL_DELAY , :remove_repository , self . repository_storage , new_path )
2017-06-01 21:36:04 +00:00
end
2015-06-03 09:50:08 +00:00
else
false
end
end
2019-01-16 19:44:52 +00:00
def schedule_stale_repos_removal
repo_paths = [ removal_path ( repo_path ) , removal_path ( wiki_path ) ]
# Ideally it should wait until the regular removal phase finishes,
# so let's delay it a bit further.
repo_paths . each do | path |
GitlabShellWorker . perform_in ( REPO_REMOVAL_DELAY * 2 , :remove_repository , project . repository_storage , path )
end
end
2018-05-23 14:54:07 +00:00
def rollback_repository ( old_path , new_path )
2018-04-06 15:23:49 +00:00
# There is a possibility project does not have repository or wiki
2018-05-23 14:54:07 +00:00
return true unless repo_exists? ( old_path )
mv_repository ( old_path , new_path )
end
def repo_exists? ( path )
2019-10-09 09:06:19 +00:00
gitlab_shell . repository_exists? ( project . repository_storage , path + '.git' )
2018-05-23 14:54:07 +00:00
end
def mv_repository ( from_path , to_path )
2019-01-16 19:44:52 +00:00
return true unless repo_exists? ( from_path )
2018-04-06 15:23:49 +00:00
2018-04-13 10:57:19 +00:00
gitlab_shell . mv_repository ( project . repository_storage , from_path , to_path )
2018-04-06 15:23:49 +00:00
end
2017-07-18 15:09:14 +00:00
def attempt_rollback ( project , message )
return unless project
2018-03-17 06:14:12 +00: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 10:43:06 +00:00
project . update ( delete_error : message , pending_delete : false ) unless project . destroyed?
2018-03-17 06:14:12 +00:00
2017-07-18 15:09:14 +00:00
log_error ( " Deletion failed on #{ project . full_path } with the following message: #{ message } " )
end
def attempt_destroy_transaction ( project )
2018-09-19 12:37:02 +00:00
unless remove_registry_tags
2019-04-15 12:25:48 +00:00
raise_error ( s_ ( 'DeleteProject|Failed to remove some tags in project container registry. Please try again or contact administrator.' ) )
2018-09-19 12:37:02 +00:00
end
2017-07-06 13:43:07 +00:00
2018-12-17 08:49:38 +00:00
project . leave_pool_repository
2018-09-19 12:37:02 +00:00
Project . transaction do
2018-06-08 20:33:51 +00:00
log_destroy_event
2017-07-18 15:09:14 +00:00
trash_repositories!
2017-07-06 13:43:07 +00:00
2018-04-27 02:45:22 +00:00
# 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 ] )
2017-07-06 13:43:07 +00:00
project . destroy!
end
end
2018-06-08 20:33:51 +00:00
def log_destroy_event
log_info ( " Attempting to destroy #{ project . full_path } ( #{ project . id } ) " )
end
2018-09-19 12:37:02 +00:00
def remove_registry_tags
2019-08-06 20:36:07 +00:00
return true unless Gitlab . config . registry . enabled
2018-09-19 12:37:02 +00: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 10:57:38 +00: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 05:36:19 +00:00
:: ContainerRepository . build_root_repository ( project ) . tap do | repository |
2018-04-18 09:19:40 +00:00
break repository . has_tags? ? repository . delete_tags! : true
2017-04-04 10:57:38 +00:00
end
end
2015-06-03 09:50:08 +00: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 18:53:26 +00:00
end
2016-02-16 22:11:56 +00:00
2017-07-18 15:09:14 +00:00
def flush_caches ( project )
2019-07-25 23:15:00 +00:00
ignore_git_errors ( repo_path ) { project . repository . before_delete }
2016-02-16 22:11:56 +00:00
2019-07-25 23:15:00 +00:00
ignore_git_errors ( wiki_path ) { Repository . new ( wiki_path , project , disk_path : repo_path ) . before_delete }
2017-08-14 13:22:09 +00:00
Projects :: ForksCountService . new ( project ) . delete_cache
2016-02-16 22:11:56 +00:00
end
2019-07-25 23:15:00 +00:00
# If we get a Gitaly error, the repository may be corrupted. We can
# ignore these errors since we're going to trash the repositories
# anyway.
def ignore_git_errors ( disk_path , & block )
yield
rescue Gitlab :: Git :: CommandError = > e
Gitlab :: GitLogger . warn ( class : self . class . name , project_id : project . id , disk_path : disk_path , message : e . to_s )
end
2014-06-17 18:53:26 +00:00
end
end
2019-09-13 13:26:31 +00:00
Projects :: DestroyService . prepend_if_ee ( 'EE::Projects::DestroyService' )