2017-09-05 01:05:23 -04:00
|
|
|
module SystemCheck
|
|
|
|
module Orphans
|
|
|
|
class RepositoryCheck < SystemCheck::BaseCheck
|
|
|
|
set_name 'Orphaned repositories:'
|
2017-09-12 00:57:22 -04:00
|
|
|
attr_accessor :orphans
|
2017-09-05 01:05:23 -04:00
|
|
|
|
|
|
|
def multi_check
|
2017-09-12 00:57:22 -04:00
|
|
|
Gitlab.config.repositories.storages.each do |storage_name, repository_storage|
|
2017-09-05 01:05:23 -04:00
|
|
|
$stdout.puts
|
2017-09-12 00:57:22 -04:00
|
|
|
$stdout.puts "* Storage: #{storage_name} (#{repository_storage['path']})".color(:yellow)
|
2017-09-05 01:05:23 -04:00
|
|
|
|
2017-09-12 00:57:22 -04:00
|
|
|
repositories = disk_repositories(repository_storage['path'])
|
|
|
|
orphans = (repositories - fetch_repositories(storage_name))
|
2017-09-05 01:05:23 -04:00
|
|
|
|
2017-09-12 00:57:22 -04:00
|
|
|
print_orphans(orphans, storage_name)
|
2017-09-05 01:05:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-09-12 00:57:22 -04:00
|
|
|
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)
|
2017-09-05 01:05:23 -04:00
|
|
|
sql = "
|
|
|
|
SELECT
|
|
|
|
CONCAT(n.path, '/', p.path, '.git') repo,
|
|
|
|
CONCAT(n.path, '/', p.path, '.wiki.git') wiki
|
|
|
|
FROM projects p
|
|
|
|
JOIN namespaces n
|
2017-09-12 00:57:22 -04:00
|
|
|
ON (p.namespace_id = n.id AND
|
|
|
|
n.parent_id IS NULL)
|
2017-09-05 01:05:23 -04:00
|
|
|
WHERE (p.repository_storage LIKE ?)
|
|
|
|
"
|
|
|
|
|
2017-09-12 00:57:22 -04:00
|
|
|
query = ActiveRecord::Base.send(:sanitize_sql_array, [sql, storage_name]) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
ActiveRecord::Base.connection.select_all(query).rows.try(:flatten!) || []
|
2017-09-05 01:05:23 -04:00
|
|
|
end
|
|
|
|
|
2017-09-12 00:57:22 -04:00
|
|
|
def fetch_disk_namespaces(storage_path)
|
2017-09-05 01:05:23 -04:00
|
|
|
Dir.glob(File.join(storage_path, '*'))
|
|
|
|
end
|
2017-09-12 00:57:22 -04:00
|
|
|
|
|
|
|
def fetch_disk_repositories(namespace_path)
|
|
|
|
Dir.glob(File.join(namespace_path, '*'))
|
|
|
|
end
|
2017-09-05 01:05:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|