Commit 97345bc6 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'jc-warn-add-namespace' into 'master'

Add deprecation guard to prevent further usage of NamespaceService

Closes #34047

See merge request gitlab-org/gitlab!20202
parents a90f77fc 04eefcda
......@@ -57,18 +57,20 @@ module Storage
# Move the namespace directory in all storages used by member projects
repository_storages(legacy_only: true).each do |repository_storage|
# Ensure old directory exists before moving it
gitlab_shell.add_namespace(repository_storage, full_path_before_last_save)
Gitlab::GitalyClient::NamespaceService.allow do
gitlab_shell.add_namespace(repository_storage, full_path_before_last_save)
# Ensure new directory exists before moving it (if there's a parent)
gitlab_shell.add_namespace(repository_storage, parent.full_path) if parent
# Ensure new directory exists before moving it (if there's a parent)
gitlab_shell.add_namespace(repository_storage, parent.full_path) if parent
unless gitlab_shell.mv_namespace(repository_storage, full_path_before_last_save, full_path)
unless gitlab_shell.mv_namespace(repository_storage, full_path_before_last_save, full_path)
Rails.logger.error "Exception moving path #{repository_storage} from #{full_path_before_last_save} to #{full_path}" # rubocop:disable Gitlab/RailsLogger
Rails.logger.error "Exception moving path #{repository_storage} from #{full_path_before_last_save} to #{full_path}" # rubocop:disable Gitlab/RailsLogger
# if we cannot move namespace directory we should rollback
# db changes in order to prevent out of sync between db and fs
raise Gitlab::UpdatePathError.new('namespace directory cannot be moved')
# if we cannot move namespace directory we should rollback
# db changes in order to prevent out of sync between db and fs
raise Gitlab::UpdatePathError.new('namespace directory cannot be moved')
end
end
end
end
......@@ -95,13 +97,15 @@ module Storage
# We will remove it later async
new_path = "#{full_path}+#{id}+deleted"
if gitlab_shell.mv_namespace(repository_storage, full_path, new_path)
Gitlab::AppLogger.info %Q(Namespace directory "#{full_path}" moved to "#{new_path}")
Gitlab::GitalyClient::NamespaceService.allow do
if gitlab_shell.mv_namespace(repository_storage, full_path, new_path)
Gitlab::AppLogger.info %Q(Namespace directory "#{full_path}" moved to "#{new_path}")
# Remove namespace directory async with delay so
# GitLab has time to remove all projects first
run_after_commit do
GitlabShellWorker.perform_in(5.minutes, :rm_namespace, repository_storage, new_path)
# Remove namespace directory async with delay so
# GitLab has time to remove all projects first
run_after_commit do
GitlabShellWorker.perform_in(5.minutes, :rm_namespace, repository_storage, new_path)
end
end
end
end
......
......@@ -8,6 +8,8 @@ class GitlabShellWorker
latency_sensitive_worker!
def perform(action, *arg)
gitlab_shell.__send__(action, *arg) # rubocop:disable GitlabSecurity/PublicSend
Gitlab::GitalyClient::NamespaceService.allow do
gitlab_shell.__send__(action, *arg) # rubocop:disable GitlabSecurity/PublicSend
end
end
end
......@@ -66,11 +66,13 @@ module Gitlab
def move_repositories(namespace, old_full_path, new_full_path)
repo_shards_for_namespace(namespace).each do |repository_storage|
# Ensure old directory exists before moving it
gitlab_shell.add_namespace(repository_storage, old_full_path)
Gitlab::GitalyClient::NamespaceService.allow do
gitlab_shell.add_namespace(repository_storage, old_full_path)
unless gitlab_shell.mv_namespace(repository_storage, old_full_path, new_full_path)
message = "Exception moving on shard #{repository_storage} from #{old_full_path} to #{new_full_path}"
Rails.logger.error message # rubocop:disable Gitlab/RailsLogger
unless gitlab_shell.mv_namespace(repository_storage, old_full_path, new_full_path)
message = "Exception moving on shard #{repository_storage} from #{old_full_path} to #{new_full_path}"
Rails.logger.error message # rubocop:disable Gitlab/RailsLogger
end
end
end
end
......
......@@ -3,7 +3,22 @@
module Gitlab
module GitalyClient
class NamespaceService
extend Gitlab::TemporarilyAllow
NamespaceServiceAccessError = Class.new(StandardError)
ALLOW_KEY = :allow_namespace
def self.allow
temporarily_allow(ALLOW_KEY) { yield }
end
def self.denied?
!temporarily_allowed?(ALLOW_KEY)
end
def initialize(storage)
raise NamespaceServiceAccessError if self.class.denied?
@storage = storage
end
......
......@@ -401,7 +401,7 @@ describe Gitlab::Shell do
describe '#add_namespace' do
it 'creates a namespace' do
subject.add_namespace(storage, "mepmep")
Gitlab::GitalyClient::NamespaceService.allow { subject.add_namespace(storage, "mepmep") }
expect(TestEnv.storage_dir_exists?(storage, "mepmep")).to be(true)
end
......@@ -425,8 +425,10 @@ describe Gitlab::Shell do
describe '#remove' do
it 'removes the namespace' do
subject.add_namespace(storage, "mepmep")
subject.rm_namespace(storage, "mepmep")
Gitlab::GitalyClient::NamespaceService.allow do
subject.add_namespace(storage, "mepmep")
subject.rm_namespace(storage, "mepmep")
end
expect(TestEnv.storage_dir_exists?(storage, "mepmep")).to be(false)
end
......@@ -434,8 +436,10 @@ describe Gitlab::Shell do
describe '#mv_namespace' do
it 'renames the namespace' do
subject.add_namespace(storage, "mepmep")
subject.mv_namespace(storage, "mepmep", "2mep")
Gitlab::GitalyClient::NamespaceService.allow do
subject.add_namespace(storage, "mepmep")
subject.mv_namespace(storage, "mepmep", "2mep")
end
expect(TestEnv.storage_dir_exists?(storage, "mepmep")).to be(false)
expect(TestEnv.storage_dir_exists?(storage, "2mep")).to be(true)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment