Commit 2bcc324f authored by DJ Mountney's avatar DJ Mountney Committed by Ahmad Hassan

Add back some of the non-gitaly restore functionality behind gates

parent 0734da16
...@@ -68,40 +68,70 @@ module Backup ...@@ -68,40 +68,70 @@ module Backup
def prepare_directories def prepare_directories
Gitlab.config.repositories.storages.each do |name, repository_storage| Gitlab.config.repositories.storages.each do |name, repository_storage|
path = repository_storage.legacy_disk_path gitaly_migrate(:remove_repositories) do |is_enabled|
next unless File.exist?(path) # TODO: Need to find a way to do this for gitaly
unless is_enabled
# Move all files in the existing repos directory except . and .. to path = repository_storage.legacy_disk_path
# repositories.old.<timestamp> directory next unless File.exist?(path)
bk_repos_path = File.join(Gitlab.config.backup.path, "tmp", "#{name}-repositories.old." + Time.now.to_i.to_s)
FileUtils.mkdir_p(bk_repos_path, mode: 0700) # Move all files in the existing repos directory except . and .. to
files = Dir.glob(File.join(path, "*"), File::FNM_DOTMATCH) - [File.join(path, "."), File.join(path, "..")] # repositories.old.<timestamp> directory
bk_repos_path = File.join(Gitlab.config.backup.path, "tmp", "#{name}-repositories.old." + Time.now.to_i.to_s)
begin FileUtils.mkdir_p(bk_repos_path, mode: 0700)
FileUtils.mv(files, bk_repos_path) files = Dir.glob(File.join(path, "*"), File::FNM_DOTMATCH) - [File.join(path, "."), File.join(path, "..")]
rescue Errno::EACCES
access_denied_error(path) begin
rescue Errno::EBUSY FileUtils.mv(files, bk_repos_path)
resource_busy_error(path) rescue Errno::EACCES
access_denied_error(path)
rescue Errno::EBUSY
resource_busy_error(path)
end
end
end end
end end
end end
def restore def restore
prepare_directories prepare_directories
gitlab_shell = Gitlab::Shell.new
Project.find_each(batch_size: 1000) do |project| Project.find_each(batch_size: 1000) do |project|
path_to_project_bundle = path_to_bundle(project) path_to_project_bundle = path_to_bundle(project)
path_to_project_repo = path_to_repo(project)
project.ensure_storage_path_exists
restore_repo_status = nil
if File.exist?(path_to_project_bundle) if File.exist?(path_to_project_bundle)
begin begin
project.repository.create_from_bundle path_to_project_bundle unless project.repository_exists? gitlab_shell.remove_repository(project.repository_storage, project.disk_path) if project.repository_exists?
progress.puts "[DONE] restoring #{project.name} repository".color(:green) project.repository.create_from_bundle path_to_project_bundle
rescue StandardError => e restore_repo_status = true
progress.puts "[Failed] restoring #{project.name} repository".color(:red) rescue => e
restore_repo_status = false
progress.puts "Error: #{e}".color(:red) progress.puts "Error: #{e}".color(:red)
end end
else else
progress.puts "[Failed] bundle file #{path_to_project_bundle} does not exist" restore_repo_status = gitlab_shell.create_repository(project.repository_storage, project.disk_path)
end
if restore_repo_status
progress.puts "[DONE] restoring #{project.name} repository".color(:green)
else
progress.puts "[Failed] restoring #{project.name} repository".color(:red)
end
gitaly_migrate(:restore_custom_hooks) do |is_enabled|
# TODO: Need to find a way to do this for gitaly
unless is_enabled
in_path(path_to_tars(project)) do |dir|
cmd = %W(tar -xf #{path_to_tars(project, dir)} -C #{path_to_project_repo} #{dir})
output, status = Gitlab::Popen.popen(cmd)
unless status.zero?
progress_warn(project, cmd.join(' '), output)
end
end
end
end end
wiki = ProjectWiki.new(project) wiki = ProjectWiki.new(project)
...@@ -109,6 +139,7 @@ module Backup ...@@ -109,6 +139,7 @@ module Backup
if File.exist?(path_to_wiki_bundle) if File.exist?(path_to_wiki_bundle)
begin begin
gitlab_shell.remove_repository(project.wiki.repository_storage, project.wiki.disk_path) if project.wiki_repository_exists?
project.repository.create_from_bundle(path_to_wiki_bundle) project.repository.create_from_bundle(path_to_wiki_bundle)
progress.puts "[DONE] restoring #{project.name} wiki".color(:green) progress.puts "[DONE] restoring #{project.name} wiki".color(:green)
rescue StandardError => e rescue StandardError => e
...@@ -116,6 +147,13 @@ module Backup ...@@ -116,6 +147,13 @@ module Backup
progress.puts "Error #{e}".color(:red) progress.puts "Error #{e}".color(:red)
end end
end end
gitaly_migrate(:create_hooks) do |is_enabled|
# TODO: Need to find a way to do this for gitaly
unless is_enabled
Gitlab::Git::Repository.create_hooks(path_to_project_repo, Gitlab.config.gitlab_shell.hooks_path)
end
end
end end
end end
# rubocop:enable Metrics/AbcSize # rubocop:enable Metrics/AbcSize
...@@ -190,5 +228,13 @@ module Backup ...@@ -190,5 +228,13 @@ module Backup
def display_repo_path(project) def display_repo_path(project)
project.hashed_storage?(:repository) ? "#{project.full_path} (#{project.disk_path})" : project.full_path project.hashed_storage?(:repository) ? "#{project.full_path} (#{project.disk_path})" : project.full_path
end end
def gitaly_migrate(method, status: Gitlab::GitalyClient::MigrationStatus::OPT_IN, &block)
Gitlab::GitalyClient.migrate(method, status: status, &block)
rescue GRPC::NotFound, GRPC::BadStatus => e
# Old Popen code returns [Error, output] to the caller, so we
# need to do the same here...
raise Error, e
end
end end
end end
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