Commit eadde611 authored by James Fargher's avatar James Fargher

Extract repository logic from repository backups

Renamed Backup::Repository to Backup::Repositories
parent 0dae421b
......@@ -3,7 +3,7 @@
require 'yaml'
module Backup
class Repository
class Repositories
attr_reader :progress
def initialize(progress)
......@@ -53,44 +53,11 @@ module Backup
private
def restore_repository(container, type)
repository = type.repository_for(container)
repository_bundle_path = path_to_bundle(repository)
progress.puts " * #{display_repo_path(repository)} ... "
repository.remove rescue nil
if File.exist?(repository_bundle_path)
repository.create_from_bundle(repository_bundle_path)
restore_custom_hooks(repository)
elsif type.project?
repository.create_repository
end
progress.puts " * #{display_repo_path(repository)} ... " + "[DONE]".color(:green)
rescue => e
progress_warn(container, e, 'Failed to restore repo')
end
def restore_custom_hooks(repository)
return unless Dir.exist?(repository_backup_path(repository))
return if Dir.glob("#{repository_backup_path(repository)}/custom_hooks*").none?
custom_hooks_path = custom_hooks_tar(repository)
repository.gitaly_repository_client.restore_custom_hooks(custom_hooks_path)
end
def path_to_bundle(repository)
File.join(backup_repos_path, repository.disk_path + '.bundle')
end
def repository_backup_path(repository)
File.join(backup_repos_path, repository.disk_path)
end
def custom_hooks_tar(repository)
File.join(repository_backup_path(repository), "custom_hooks.tar")
BackupRestorer.new(
progress,
type.repository_for(container),
backup_repos_path
).restore(always_create: type.project?)
end
def backup_repos_path
......@@ -154,52 +121,99 @@ module Backup
end
def backup_repository(container, type)
repository = type.repository_for(container)
BackupRestorer.new(
progress,
type.repository_for(container),
backup_repos_path
).backup
end
progress.puts " * #{display_repo_path(repository)} ... "
def restore_object_pools
PoolRepository.includes(:source_project).find_each do |pool|
progress.puts " - Object pool #{pool.disk_path}..."
if empty_repository?(repository)
progress.puts " * #{display_repo_path(repository)} ... " + "[SKIPPED]".color(:cyan)
return
pool.source_project ||= pool.member_projects.first.root_of_fork_network
pool.state = 'none'
pool.save
pool.schedule
end
end
class BackupRestorer
attr_accessor :progress, :repository, :backup_repos_path
def initialize(progress, repository, backup_repos_path)
@progress = progress
@repository = repository
@backup_repos_path = backup_repos_path
end
FileUtils.mkdir_p(repository_backup_path(repository))
def backup
progress.puts " * #{display_repo_path} ... "
if repository.empty?
progress.puts " * #{display_repo_path} ... " + "[SKIPPED]".color(:cyan)
return
end
repository_bundle_path = path_to_bundle(repository)
repository.bundle_to_disk(repository_bundle_path)
FileUtils.mkdir_p(repository_backup_path)
custom_hooks_path = custom_hooks_tar(repository)
repository.gitaly_repository_client.backup_custom_hooks(custom_hooks_path)
repository.bundle_to_disk(path_to_bundle)
repository.gitaly_repository_client.backup_custom_hooks(custom_hooks_tar)
progress.puts " * #{display_repo_path(repository)} ... " + "[DONE]".color(:green)
progress.puts " * #{display_repo_path} ... " + "[DONE]".color(:green)
rescue => e
progress_warn(container, e, 'Failed to backup repo')
progress_warn(e, 'Failed to backup repo')
end
def progress_warn(project, cmd, output)
progress.puts "[WARNING] Executing #{cmd}".color(:orange)
progress.puts "Ignoring error on #{display_repo_path(project)} - #{output}".color(:orange)
def restore(always_create: false)
progress.puts " * #{display_repo_path} ... "
repository.remove rescue nil
if File.exist?(path_to_bundle)
repository.create_from_bundle(path_to_bundle)
restore_custom_hooks
elsif always_create
repository.create_repository
end
def empty_repository?(repository)
repository.expire_emptiness_caches
repository.empty?
progress.puts " * #{display_repo_path} ... " + "[DONE]".color(:green)
rescue => e
progress_warn(e, 'Failed to restore repo')
end
def display_repo_path(repository)
private
def display_repo_path
"#{repository.full_path} (#{repository.disk_path})"
end
def restore_object_pools
PoolRepository.includes(:source_project).find_each do |pool|
progress.puts " - Object pool #{pool.disk_path}..."
def repository_backup_path
@repository_backup_path ||= File.join(backup_repos_path, repository.disk_path)
end
pool.source_project ||= pool.member_projects.first.root_of_fork_network
pool.state = 'none'
pool.save
def path_to_bundle
@path_to_bundle ||= File.join(backup_repos_path, repository.disk_path + '.bundle')
end
pool.schedule
def restore_custom_hooks
return unless Dir.exist?(repository_backup_path)
return if Dir.glob("#{repository_backup_path}/custom_hooks*").none?
repository.gitaly_repository_client.restore_custom_hooks(custom_hooks_tar)
end
def custom_hooks_tar
File.join(repository_backup_path, "custom_hooks.tar")
end
def progress_warn(cmd, output)
progress.puts "[WARNING] Executing #{cmd}".color(:orange)
progress.puts "Ignoring error on #{display_repo_path} - #{output}".color(:orange)
end
end
......
......@@ -107,7 +107,7 @@ namespace :gitlab do
puts "GITLAB_BACKUP_MAX_CONCURRENCY and GITLAB_BACKUP_MAX_STORAGE_CONCURRENCY must have a value of at least 1".color(:red)
exit 1
else
Backup::Repository.new(progress).dump(
Backup::Repositories.new(progress).dump(
max_concurrency: max_concurrency,
max_storage_concurrency: max_storage_concurrency
)
......@@ -117,7 +117,7 @@ namespace :gitlab do
task restore: :gitlab_environment do
puts_time "Restoring repositories ...".color(:blue)
Backup::Repository.new(progress).restore
Backup::Repositories.new(progress).restore
puts_time "done".color(:green)
end
end
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe Backup::Repository do
RSpec.describe Backup::Repositories do
let(:progress) { StringIO.new }
subject { described_class.new(progress) }
......@@ -190,46 +190,15 @@ RSpec.describe Backup::Repository do
end
it 'cleans existing repositories' do
wiki_repository_spy = spy(:wiki)
expect(Repository).to receive(:new).twice.and_wrap_original do |method, *original_args|
repository = method.call(*original_args)
allow_next_instance_of(ProjectWiki) do |project_wiki|
allow(project_wiki).to receive(:repository).and_return(wiki_repository_spy)
end
expect(repository).to receive(:remove)
expect_next_instance_of(Repository) do |repo|
expect(repo).to receive(:remove)
repository
end
subject.restore
expect(wiki_repository_spy).to have_received(:remove)
end
end
describe '#empty_repository?' do
context 'for a wiki' do
let(:wiki) { create(:project_wiki) }
let(:repository) { wiki.repository }
it 'invalidates the emptiness cache' do
expect(repository).to receive(:expire_emptiness_caches).once
subject.send(:empty_repository?, repository)
end
context 'wiki repo has content' do
let!(:wiki_page) { create(:wiki_page, wiki: wiki) }
it 'returns false, regardless of bad cache value' do
expect(subject.send(:empty_repository?, repository)).to be_falsy
end
end
context 'wiki repo does not have content' do
it 'returns true, regardless of bad cache value' do
expect(subject.send(:empty_repository?, repository)).to be_truthy
end
end
end
end
end
......@@ -370,7 +370,7 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
end
it 'has defaults' do
expect_next_instance_of(::Backup::Repository) do |instance|
expect_next_instance_of(::Backup::Repositories) do |instance|
expect(instance).to receive(:dump)
.with(max_concurrency: 1, max_storage_concurrency: 1)
.and_call_original
......@@ -383,7 +383,7 @@ RSpec.describe 'gitlab:app namespace rake task', :delete do
stub_env('GITLAB_BACKUP_MAX_CONCURRENCY', 5)
stub_env('GITLAB_BACKUP_MAX_STORAGE_CONCURRENCY', 2)
expect_next_instance_of(::Backup::Repository) do |instance|
expect_next_instance_of(::Backup::Repositories) do |instance|
expect(instance).to receive(:dump)
.with(max_concurrency: 5, max_storage_concurrency: 2)
.and_call_original
......
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