destroy_service.rb 3.4 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1
module Projects
2
  class DestroyService < BaseService
3 4
    include Gitlab::ShellAdapter

5
    DestroyError = Class.new(StandardError)
6

D
Douwe Maan 已提交
7
    DELETED_FLAG = '+deleted'.freeze
8

S
Stan Hu 已提交
9 10 11 12 13 14
    def async_execute
      project.transaction do
        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}")
      end
15 16
    end

17
    def execute
D
Dmitriy Zaporozhets 已提交
18 19
      return false unless can?(current_user, :remove_project, project)

20 21 22
      repo_path = project.path_with_namespace
      wiki_path = repo_path + '.wiki'

23 24 25 26 27
      # 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)

28 29
      Projects::UnlinkForkService.new(project, current_user).execute

30
      Project.transaction do
31
        project.team.truncate
32 33 34 35

        unless remove_legacy_registry_tags
          raise_error('Failed to remove some tags in project container registry. Please try again or contact administrator.')
        end
D
Dmitriy Zaporozhets 已提交
36

37
        unless remove_repository(repo_path)
38
          raise_error('Failed to remove project repository. Please try again or contact administrator.')
39 40 41
        end

        unless remove_repository(wiki_path)
42
          raise_error('Failed to remove wiki repository. Please try again or contact administrator.')
43
        end
44 45

        project.destroy!
46 47
      end

48
      log_info("Project \"#{project.path_with_namespace}\" was removed")
49 50 51
      system_hook_service.execute_hooks_for(project, :destroy)
      true
    end
D
Dmitriy Zaporozhets 已提交
52

53
    private
D
Dmitriy Zaporozhets 已提交
54

55
    def remove_repository(path)
56 57 58 59
      # 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
60
      return true unless gitlab_shell.exists?(project.repository_storage_path, path + '.git')
61 62 63

      new_path = removal_path(path)

64
      if gitlab_shell.mv_repository(project.repository_storage_path, path, new_path)
65
        log_info("Repository \"#{path}\" moved to \"#{new_path}\"")
66
        GitlabShellWorker.perform_in(5.minutes, :remove_repository, project.repository_storage_path, new_path)
67 68 69 70 71
      else
        false
      end
    end

72 73 74 75 76 77 78 79 80 81 82 83
    ##
    # 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|
        return repository.delete_tags! if repository.has_tags?
      end
    end

84 85 86 87 88 89 90 91 92 93 94 95
    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}"
D
Dmitriy Zaporozhets 已提交
96
    end
97 98

    def flush_caches(project, wiki_path)
99
      project.repository.before_delete
100

101
      Repository.new(wiki_path, project).before_delete
102
    end
D
Dmitriy Zaporozhets 已提交
103 104
  end
end