summaryrefslogtreecommitdiff
path: root/lib/system_check/orphans/repository_check.rb
blob: 2695c658874e014a8605fb0f4cb7f5b23464bbaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module SystemCheck
  module Orphans
    class RepositoryCheck < SystemCheck::BaseCheck
      set_name 'Orphaned repositories:'
      attr_accessor :orphans

      def multi_check
        Gitlab::GitalyClient::StorageSettings.allow_disk_access do
          Gitlab.config.repositories.storages.each do |storage_name, repository_storage|
            storage_path = repository_storage.legacy_disk_path

            $stdout.puts
            $stdout.puts "* Storage: #{storage_name} (#{storage_path})".color(:yellow)

            repositories = disk_repositories(storage_path)
            orphans = (repositories - fetch_repositories(storage_name))

            print_orphans(orphans, storage_name)
          end
        end
      end

      private

      def print_orphans(orphans, storage_name)
        if orphans.empty?
          $stdout.puts "* No orphaned repositories for #{storage_name} storage".color(:green)
          return
        end

        orphans.each do |orphan|
          $stdout.puts " - #{orphan}".color(:red)
        end
      end

      def disk_repositories(storage_path)
        fetch_disk_namespaces(storage_path).each_with_object([]) do |namespace_path, result|
          namespace = File.basename(namespace_path)
          next if namespace.eql?('@hashed')

          fetch_disk_repositories(namespace_path).each do |repo|
            result << "#{namespace}/#{File.basename(repo)}"
          end
        end
      end

      def fetch_repositories(storage_name)
        sql = "
          SELECT
            CONCAT(n.path, '/', p.path, '.git') repo,
            CONCAT(n.path, '/', p.path, '.wiki.git') wiki
          FROM projects p
            JOIN namespaces n
              ON (p.namespace_id = n.id AND
                  n.parent_id IS NULL)
          WHERE (p.repository_storage LIKE ?)
        "

        query = ActiveRecord::Base.send(:sanitize_sql_array, [sql, storage_name]) # rubocop:disable GitlabSecurity/PublicSend
        ActiveRecord::Base.connection.select_all(query).rows.try(:flatten!) || []
      end

      def fetch_disk_namespaces(storage_path)
        Dir.glob(File.join(storage_path, '*'))
      end

      def fetch_disk_repositories(namespace_path)
        Dir.glob(File.join(namespace_path, '*'))
      end
    end
  end
end