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