summaryrefslogtreecommitdiff
path: root/lib/system_check
diff options
context:
space:
mode:
authorGabriel Mazetto <brodock@gmail.com>2017-09-05 07:05:23 +0200
committerGabriel Mazetto <brodock@gmail.com>2017-09-12 04:06:00 +0200
commitcc28abeafdd7f32fa9e5d4e0f2b7c82271067796 (patch)
treebb1069b701849a56dfcc5ee0d594c2a9f8ffc1cc /lib/system_check
parent021724eef20c9b7770119cd9ec367198f19cf442 (diff)
downloadgitlab-ce-cc28abeafdd7f32fa9e5d4e0f2b7c82271067796.tar.gz
Detect orphaned repositories and namespaces in any storage
Diffstat (limited to 'lib/system_check')
-rw-r--r--lib/system_check/orphans/namespace_check.rb37
-rw-r--r--lib/system_check/orphans/repository_check.rb50
2 files changed, 87 insertions, 0 deletions
diff --git a/lib/system_check/orphans/namespace_check.rb b/lib/system_check/orphans/namespace_check.rb
new file mode 100644
index 00000000000..1963cbfc985
--- /dev/null
+++ b/lib/system_check/orphans/namespace_check.rb
@@ -0,0 +1,37 @@
+module SystemCheck
+ module Orphans
+ class NamespaceCheck < SystemCheck::BaseCheck
+ set_name 'Orphaned namespaces:'
+
+ def multi_check
+ Gitlab.config.repositories.storages.each do |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)}
+
+ 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
+ end
+
+ clear_namespaces! # releases memory when check finishes
+ end
+
+ private
+
+ def existing_namespaces
+ @namespaces ||= Namespace.all.pluck(:path)
+ end
+
+ def clear_namespaces!
+ @namespaces = nil
+ end
+ end
+ end
+end
diff --git a/lib/system_check/orphans/repository_check.rb b/lib/system_check/orphans/repository_check.rb
new file mode 100644
index 00000000000..1de6bec165b
--- /dev/null
+++ b/lib/system_check/orphans/repository_check.rb
@@ -0,0 +1,50 @@
+module SystemCheck
+ module Orphans
+ class RepositoryCheck < SystemCheck::BaseCheck
+ set_name 'Orphaned repositories:'
+
+ def multi_check
+ Gitlab.config.repositories.storages.each do |name, repository_storage|
+ $stdout.puts
+ $stdout.puts "* 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!)
+
+ 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
+ end
+ end
+
+ private
+
+ def list_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)
+ WHERE (p.repository_storage LIKE ?)
+ "
+
+ query = ActiveRecord::Base.send(:sanitize_sql_array, [sql, storage_name])
+ ActiveRecord::Base.connection.select_all(query).rows.try(:flatten!)
+ end
+
+ def toplevel_namespace_dirs(storage_path)
+ Dir.glob(File.join(storage_path, '*'))
+ end
+ end
+ end
+end