2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module SystemCheck
|
|
|
|
module Orphans
|
|
|
|
class RepositoryCheck < SystemCheck::BaseCheck
|
|
|
|
set_name 'Orphaned repositories:'
|
|
|
|
|
|
|
|
def multi_check
|
2018-11-08 19:23:39 +05:30
|
|
|
Gitlab::GitalyClient::StorageSettings.allow_disk_access do
|
|
|
|
Gitlab.config.repositories.storages.each do |storage_name, repository_storage|
|
|
|
|
storage_path = repository_storage.legacy_disk_path
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
$stdout.puts
|
|
|
|
$stdout.puts "* Storage: #{storage_name} (#{storage_path})".color(:yellow)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
repositories = disk_repositories(storage_path)
|
|
|
|
orphans = (repositories - fetch_repositories(storage_name))
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
print_orphans(orphans, storage_name)
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
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 ?)
|
|
|
|
"
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
query = ::Project.sanitize_sql_array([sql, storage_name])
|
|
|
|
::Project.connection.select_all(query).rows.try(:flatten!) || []
|
2018-03-17 18:26:18 +05:30
|
|
|
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
|