gitlab-org--gitlab-foss/lib/system_check/orphans/namespace_check.rb
gfyoung c8755543f0 Enable even more frozen string in lib/**/*.rb
Enables frozen string for the following files:

* lib/generators/**/*.rb
* lib/gitaly/**/*.rb
* lib/google_api/**/*.rb
* lib/haml_lint/**/*.rb
* lib/json_web_token/**/*.rb
* lib/mattermost/**/*.rb
* lib/microsoft_teams/**/*.rb
* lib/object_storage/**/*.rb
* lib/omni_auth/**/*.rb
* lib/peek/**/*.rb
* lib/rouge/**/*.rb
* lib/rspec_flaky/**/*.rb
* lib/system_check/**/*.rb

Partially addresses #47424.
2018-10-08 11:16:49 -07:00

58 lines
1.6 KiB
Ruby

# frozen_string_literal: true
module SystemCheck
module Orphans
class NamespaceCheck < SystemCheck::BaseCheck
set_name 'Orphaned namespaces:'
def multi_check
Gitlab::GitalyClient::StorageSettings.allow_disk_access do
Gitlab.config.repositories.storages.each do |storage_name, repository_storage|
$stdout.puts
$stdout.puts "* Storage: #{storage_name} (#{repository_storage.legacy_disk_path})".color(:yellow)
toplevel_namespace_dirs = disk_namespaces(repository_storage.legacy_disk_path)
orphans = (toplevel_namespace_dirs - existing_namespaces)
print_orphans(orphans, storage_name)
end
end
clear_namespaces! # releases memory when check finishes
end
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.where(parent: nil).all.pluck(:path)
end
def clear_namespaces!
@namespaces = nil
end
end
end
end