Skip to content

Commit

Permalink
Use logger in RMT::Mirror
Browse files Browse the repository at this point in the history
  • Loading branch information
ikapelyukhin committed Jun 6, 2018
1 parent fb24fd7 commit dc62391
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 38 deletions.
8 changes: 4 additions & 4 deletions lib/rmt/cli/base.rb
Expand Up @@ -94,14 +94,14 @@ def needs_path(path)
File.directory?(path) ? yield : warn("#{path} is not a directory.")
end

def mirror!(repo, repository_url: nil, to: RMT::DEFAULT_MIRROR_DIR, to_offline: false)
def mirror!(repo, repository_url: nil, to: RMT::DEFAULT_MIRROR_DIR, to_offline: false, logger:)
repository_url ||= repo.external_url

puts "Mirroring repository #{repo.name} to #{to}"
RMT::Mirror.from_url(repo.external_url, repo.auth_token, repository_url: repository_url, base_dir: to, to_offline: to_offline).mirror
logger.info "Mirroring repository #{repo.name} to #{to}"
RMT::Mirror.from_url(repo.external_url, repo.auth_token, repository_url: repository_url, base_dir: to, to_offline: to_offline, logger: logger).mirror
repo.refresh_timestamp!
rescue RMT::Mirror::Exception => e
warn e.to_s
logger.warn e.to_s
end

end
3 changes: 2 additions & 1 deletion lib/rmt/cli/export.rb
Expand Up @@ -27,6 +27,7 @@ def settings(path)
REPOS
def repos(path)
needs_path(path) do
logger = RMT::Logger.new(STDOUT)
repos_file = File.join(path, 'repos.json')
unless File.exist?(repos_file)
warn "#{repos_file} does not exist."
Expand All @@ -36,7 +37,7 @@ def repos(path)
repos.each do |repo|
puts "Mirroring repository at #{repo['url']}"
begin
RMT::Mirror.from_url(repo['url'], repo['auth_token'], base_dir: path, to_offline: true).mirror
RMT::Mirror.from_url(repo['url'], repo['auth_token'], base_dir: path, to_offline: true, logger: logger).mirror
rescue RMT::Mirror::Exception => e
warn e.to_s
end
Expand Down
4 changes: 3 additions & 1 deletion lib/rmt/cli/import.rb
Expand Up @@ -13,6 +13,8 @@ def data(path)
def repos(path)
RMT::Lockfile.lock do
needs_path(path) do
logger = RMT::Logger.new(STDOUT)

repos_file = File.join(path, 'repos.json')
unless File.exist?(repos_file)
warn "#{repos_file} does not exist."
Expand All @@ -28,7 +30,7 @@ def repos(path)
end

repo.external_url = 'file://' + path + Repository.make_local_path(repo_json['url'])
mirror!(repo, repository_url: repo_json['url'], to_offline: true)
mirror!(repo, repository_url: repo_json['url'], to_offline: true, logger: logger)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/rmt/cli/main.rb
Expand Up @@ -20,12 +20,13 @@ def sync
desc 'mirror', 'Mirror repositories'
def mirror
RMT::Lockfile.lock do
logger = RMT::Logger.new(STDOUT)
repos = Repository.where(mirroring_enabled: true)
if repos.empty?
warn 'There are no repositories marked for mirroring.'
return
end
repos.each { |repo| mirror!(repo) }
repos.each { |repo| mirror!(repo, logger: logger) }
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rmt/mirror.rb
Expand Up @@ -40,7 +40,7 @@ def mirror
remove_tmp_directories
end

def self.from_url(uri, auth_token, repository_url: nil, base_dir: nil, to_offline: false)
def self.from_url(uri, auth_token, repository_url: nil, base_dir: nil, to_offline: false, logger:)
repository_url ||= uri

new(
Expand All @@ -49,7 +49,7 @@ def self.from_url(uri, auth_token, repository_url: nil, base_dir: nil, to_offlin
auth_token: auth_token,
local_path: Repository.make_local_path(repository_url),
mirror_src: Settings.try(:mirroring).try(:mirror_src),
logger: RMT::Logger.new(STDOUT),
logger: logger,
to_offline: to_offline
)
end
Expand Down
11 changes: 9 additions & 2 deletions spec/lib/rmt/cli/export_spec.rb
Expand Up @@ -58,8 +58,15 @@

context 'with valid repo ids' do
before do
expect(RMT::Mirror).to receive(:from_url).with('http://foo.bar/repo1', 'foobar', base_dir: path, to_offline: true).once.and_return(mirror_double)
expect(RMT::Mirror).to receive(:from_url).with('http://foo.bar/repo2', '', base_dir: path, to_offline: true).once.and_return(mirror_double)
expect(RMT::Mirror).to receive(:from_url).with(
'http://foo.bar/repo1', 'foobar', base_dir: path, to_offline: true,
logger: instance_of(RMT::Logger)
).once.and_return(mirror_double)

expect(RMT::Mirror).to receive(:from_url).with(
'http://foo.bar/repo2', '', base_dir: path, to_offline: true,
logger: instance_of(RMT::Logger)
).once.and_return(mirror_double)
end

it 'reads repo ids from file at path and mirrors these repos' do
Expand Down
51 changes: 27 additions & 24 deletions spec/lib/rmt/cli/import_spec.rb
Expand Up @@ -71,24 +71,24 @@
context 'without exception' do
before do
expect(mirror_double).to receive(:mirror).twice
expect(RMT::Mirror).to receive(:from_url).with(repo1_local_path, repo1.auth_token, base_dir: RMT::DEFAULT_MIRROR_DIR,
repository_url: repo1.external_url, to_offline: true).and_return(mirror_double)
expect(RMT::Mirror).to receive(:from_url).with(repo2_local_path, repo2.auth_token, base_dir: RMT::DEFAULT_MIRROR_DIR,
repository_url: repo2.external_url, to_offline: true).and_return(mirror_double)
expect(RMT::Mirror).to receive(:from_url).with(
repo1_local_path, repo1.auth_token, base_dir: RMT::DEFAULT_MIRROR_DIR,
repository_url: repo1.external_url, to_offline: true, logger: instance_of(RMT::Logger)
).and_return(mirror_double)

expect(RMT::Mirror).to receive(:from_url).with(
repo2_local_path, repo2.auth_token, base_dir: RMT::DEFAULT_MIRROR_DIR,
repository_url: repo2.external_url, to_offline: true, logger: instance_of(RMT::Logger)
).and_return(mirror_double)
end

it 'mirrors repo1' do
it 'mirrors repo1 and repo2' do
FileUtils.mkdir_p path
File.write("#{path}/repos.json", repo_settings.to_json)

expect { command }.to output(/Mirroring repository #{repo1.name}/).to_stdout.and output('').to_stderr
end

it 'mirrors repo2' do
FileUtils.mkdir_p path
File.write("#{path}/repos.json", repo_settings.to_json)

expect { command }.to output(/Mirroring repository #{repo2.name}/).to_stdout.and output('').to_stderr
expect_any_instance_of(RMT::Logger).to receive(:info).with(/Mirroring repository #{repo1.name}/)
expect_any_instance_of(RMT::Logger).to receive(:info).with(/Mirroring repository #{repo2.name}/)
command
end
end

Expand All @@ -104,23 +104,26 @@
before do
expect(mirror_error_double).to receive(:mirror).once.and_raise(RMT::Mirror::Exception, 'black mirror')
expect(mirror_double).to receive(:mirror).once
expect(RMT::Mirror).to receive(:from_url).with(repo1_local_path, repo1.auth_token, base_dir: RMT::DEFAULT_MIRROR_DIR,
repository_url: repo1.external_url, to_offline: true).and_return(mirror_error_double)
expect(RMT::Mirror).to receive(:from_url).with(repo2_local_path, repo2.auth_token, base_dir: RMT::DEFAULT_MIRROR_DIR,
repository_url: repo2.external_url, to_offline: true).and_return(mirror_double)
expect(RMT::Mirror).to receive(:from_url).with(
repo1_local_path, repo1.auth_token, base_dir: RMT::DEFAULT_MIRROR_DIR,
repository_url: repo1.external_url, to_offline: true, logger: instance_of(RMT::Logger)
).and_return(mirror_error_double)

expect(RMT::Mirror).to receive(:from_url).with(
repo2_local_path, repo2.auth_token, base_dir: RMT::DEFAULT_MIRROR_DIR,
repository_url: repo2.external_url, to_offline: true, logger: instance_of(RMT::Logger)
).and_return(mirror_double)
end

it 'tries to mirror repo1' do
it 'mirrors repo2 when repo1 raised an exception' do
FileUtils.mkdir_p path
File.write("#{path}/repos.json", repo_settings.to_json)

expect { command }.to output("black mirror\n").to_stderr.and output(/Mirroring repository #{repo1.name}/).to_stdout
end
it 'tries to mirror repo2' do
FileUtils.mkdir_p path
File.write("#{path}/repos.json", repo_settings.to_json)
expect_any_instance_of(RMT::Logger).to receive(:info).with(/Mirroring repository #{repo1.name}/)
expect_any_instance_of(RMT::Logger).to receive(:info).with(/Mirroring repository #{repo2.name}/)
expect_any_instance_of(RMT::Logger).to receive(:warn).with('black mirror')

expect { command }.to output("black mirror\n").to_stderr.and output(/Mirroring repository #{repo2.name}/).to_stdout
command
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions spec/lib/rmt/cli/main_spec.rb
Expand Up @@ -41,21 +41,24 @@
before { expect_any_instance_of(RMT::Mirror).to receive(:mirror) }

it 'outputs mirroring progress' do
expect { command }.to output(/Mirroring repository #{repository.name}/).to_stdout.and output('').to_stderr
expect_any_instance_of(RMT::Logger).to receive(:info).with(/Mirroring repository #{repository.name}/)
command
end

it 'updates repository mirroring timestamp' do
Timecop.freeze(Time.utc(2018)) do
expect_any_instance_of(RMT::Logger).to receive(:info).with(/Mirroring repository #{repository.name}/)
expect { command }.to change { repository.reload.last_mirrored_at }.to(DateTime.now.utc)
.and output(/Mirroring repository #{repository.name}/).to_stdout
end
end

context 'with exceptions during mirroring' do
before { allow_any_instance_of(RMT::Mirror).to receive(:mirror).and_raise(RMT::Mirror::Exception, 'black mirror') }

it 'outputs exception message' do
expect { command }.to output("black mirror\n").to_stderr.and output(/Mirroring repository #{repository.name}/).to_stdout
expect_any_instance_of(RMT::Logger).to receive(:info).with(/Mirroring repository #{repository.name}/)
expect_any_instance_of(RMT::Logger).to receive(:warn).with('black mirror')
command
end
end
end
Expand Down

0 comments on commit dc62391

Please sign in to comment.