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
def prepare_directories
Gitlab.config.repositories.storages.each do |name, repository_storage|
path = repository_storage.legacy_disk_path
next unless File.exist?(path)
# Move all files in the existing repos directory except . and .. to
# repositories.old.<timestamp> directory
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)
files = Dir.glob(File.join(path, "*"), File::FNM_DOTMATCH) - [File.join(path, "."), File.join(path, "..")]
begin
FileUtils.mv(files, bk_repos_path)
rescue Errno::EACCES
access_denied_error(path)
rescue Errno::EBUSY
resource_busy_error(path)
gitaly_migrate(:remove_repositories) do |is_enabled|
# TODO: Need to find a way to do this for gitaly
unless is_enabled
path = repository_storage.legacy_disk_path
next unless File.exist?(path)
# Move all files in the existing repos directory except . and .. to
# repositories.old.<timestamp> directory
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)
files = Dir.glob(File.join(path, "*"), File::FNM_DOTMATCH) - [File.join(path, "."), File.join(path, "..")]
begin
FileUtils.mv(files, bk_repos_path)
rescue Errno::EACCES
access_denied_error(path)
rescue Errno::EBUSY
resource_busy_error(path)
end
end
end
end
end
def restore
prepare_directories
gitlab_shell = Gitlab::Shell.new
Project.find_each(batch_size: 1000) do |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)
begin
project.repository.create_from_bundle path_to_project_bundle unless project.repository_exists?
progress.puts "[DONE] restoring #{project.name} repository".color(:green)
rescue StandardError => e
progress.puts "[Failed] restoring #{project.name} repository".color(:red)
gitlab_shell.remove_repository(project.repository_storage, project.disk_path) if project.repository_exists?
project.repository.create_from_bundle path_to_project_bundle
restore_repo_status = true
rescue => e
restore_repo_status = false
progress.puts "Error: #{e}".color(:red)
end
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
wiki = ProjectWiki.new(project)
......@@ -109,6 +139,7 @@ module Backup
if File.exist?(path_to_wiki_bundle)
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)
progress.puts "[DONE] restoring #{project.name} wiki".color(:green)
rescue StandardError => e
......@@ -116,6 +147,13 @@ module Backup
progress.puts "Error #{e}".color(:red)
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
# rubocop:enable Metrics/AbcSize
......@@ -190,5 +228,13 @@ module Backup
def display_repo_path(project)
project.hashed_storage?(:repository) ? "#{project.full_path} (#{project.disk_path})" : project.full_path
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
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