diff options
author | Gabriel Mazetto <brodock@gmail.com> | 2017-09-12 06:57:22 +0200 |
---|---|---|
committer | Gabriel Mazetto <brodock@gmail.com> | 2017-09-12 06:57:22 +0200 |
commit | 2c489f8289e5e887d86be32e2bf59bad22a19af4 (patch) | |
tree | 10e812bc6f10040b537260ddb5cabe43cbb3c150 /lib/system_check | |
parent | cc28abeafdd7f32fa9e5d4e0f2b7c82271067796 (diff) | |
download | gitlab-ce-2c489f8289e5e887d86be32e2bf59bad22a19af4.tar.gz |
Refactor on namespace and repository checks and added specs
Diffstat (limited to 'lib/system_check')
-rw-r--r-- | lib/system_check/orphans/namespace_check.rb | 41 | ||||
-rw-r--r-- | lib/system_check/orphans/repository_check.rb | 58 |
2 files changed, 67 insertions, 32 deletions
diff --git a/lib/system_check/orphans/namespace_check.rb b/lib/system_check/orphans/namespace_check.rb index 1963cbfc985..b8446300f72 100644 --- a/lib/system_check/orphans/namespace_check.rb +++ b/lib/system_check/orphans/namespace_check.rb @@ -4,20 +4,13 @@ module SystemCheck set_name 'Orphaned namespaces:' def multi_check - Gitlab.config.repositories.storages.each do |name, repository_storage| + Gitlab.config.repositories.storages.each do |storage_name, repository_storage| $stdout.puts - $stdout.puts "* Storage: #{name} (#{repository_storage['path']})".color(:yellow) - toplevel_namespace_dirs = Dir.glob(File.join(repository_storage['path'], '*')).map{|p| File.basename(p)} + $stdout.puts "* Storage: #{storage_name} (#{repository_storage['path']})".color(:yellow) + toplevel_namespace_dirs = disk_namespaces(repository_storage['path']) orphans = (toplevel_namespace_dirs - existing_namespaces) - if orphans.empty? - $stdout.puts "* No orphaned namespaces for #{name} storage".color(:green) - next - end - - orphans.each do |orphan| - $stdout.puts " - #{orphan}".color(:red) - end + print_orphans(orphans, storage_name) end clear_namespaces! # releases memory when check finishes @@ -25,8 +18,32 @@ module SystemCheck private + def print_orphans(orphans, storage_name) + if orphans.empty? + $stdout.puts "* No orphaned namespaces for #{storage_name} storage".color(:green) + return + end + + orphans.each do |orphan| + $stdout.puts " - #{orphan}".color(:red) + end + end + + def disk_namespaces(storage_path) + fetch_disk_namespaces(storage_path).each_with_object([]) do |namespace_path, result| + namespace = File.basename(namespace_path) + next if namespace.eql?('@hashed') + + result << namespace + end + end + + def fetch_disk_namespaces(storage_path) + Dir.glob(File.join(storage_path, '*')) + end + def existing_namespaces - @namespaces ||= Namespace.all.pluck(:path) + @namespaces ||= Namespace.where(parent: nil).all.pluck(:path) end def clear_namespaces! diff --git a/lib/system_check/orphans/repository_check.rb b/lib/system_check/orphans/repository_check.rb index 1de6bec165b..9b6b2429783 100644 --- a/lib/system_check/orphans/repository_check.rb +++ b/lib/system_check/orphans/repository_check.rb @@ -2,49 +2,67 @@ module SystemCheck module Orphans class RepositoryCheck < SystemCheck::BaseCheck set_name 'Orphaned repositories:' + attr_accessor :orphans def multi_check - Gitlab.config.repositories.storages.each do |name, repository_storage| + Gitlab.config.repositories.storages.each do |storage_name, repository_storage| $stdout.puts - $stdout.puts "* Storage: #{name} (#{repository_storage['path']})".color(:yellow) + $stdout.puts "* Storage: #{storage_name} (#{repository_storage['path']})".color(:yellow) - repositories = toplevel_namespace_dirs(repository_storage['path']).map do |path| - namespace = File.basename(path) - Dir.glob(File.join(path, '*')).map {|repo| "#{namespace}/#{File.basename(repo)}"} - end.try(:flatten!) + repositories = disk_repositories(repository_storage['path']) + orphans = (repositories - fetch_repositories(storage_name)) - orphans = (repositories - list_repositories(name)) - if orphans.empty? - $stdout.puts "* No orphaned repositories for #{name} storage".color(:green) - next - end - - orphans.each do |orphan| - $stdout.puts " - #{orphan}".color(:red) - end + print_orphans(orphans, storage_name) end end private - def list_repositories(storage_name) + 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) + 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]) - ActiveRecord::Base.connection.select_all(query).rows.try(:flatten!) + 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 toplevel_namespace_dirs(storage_path) + 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 |