Skip to content

Commit

Permalink
Move lockfile logic to a method
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergeykot committed May 4, 2018
1 parent bbdba77 commit 0a7cd66
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 51 deletions.
11 changes: 11 additions & 0 deletions lib/rmt/cli/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ def subcommand_prefix

private

def locked_method(&_block)
RMT::Lockfile.create_file
yield
RMT::Lockfile.remove_file
rescue RMT::Lockfile::ExecutionLockedError
puts 'Process is locked'
rescue StandardError
RMT::Lockfile.remove_file
raise
end

def needs_path(path)
File.directory?(path) ? yield : warn("#{path} is not a directory.")
end
Expand Down
52 changes: 21 additions & 31 deletions lib/rmt/cli/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,36 @@ class RMT::CLI::Import < RMT::CLI::Base

desc 'data PATH', 'Read SCC data from given path'
def data(path)
RMT::Lockfile.create_file
needs_path(path) do
RMT::SCC.new(options).import(path)
locked_method do
needs_path(path) do
RMT::SCC.new(options).import(path)
end
end
RMT::Lockfile.remove_file
rescue RMT::Lockfile::ExecutionLockedError
puts 'Process is locked'
rescue StandardError
RMT::Lockfile.remove_file
raise
end

desc 'repos PATH', 'Mirror repos from given path'
def repos(path)
RMT::Lockfile.create_file
needs_path(path) do
repos_file = File.join(path, 'repos.json')
unless File.exist?(repos_file)
warn "#{repos_file} does not exist."
return
end

repos = JSON.parse(File.read(repos_file))
repos.each do |repo_json|
repo = Repository.find_by(external_url: repo_json['url'])
if repo.nil?
warn "repository by url #{repo_json['url']} does not exist in database"
next
locked_method do
needs_path(path) do
repos_file = File.join(path, 'repos.json')
unless File.exist?(repos_file)
warn "#{repos_file} does not exist."
return
end

repo.external_url = 'file://' + path + Repository.make_local_path(repo_json['url'])
mirror!(repo, repository_url: repo_json['url'], to_offline: true)
repos = JSON.parse(File.read(repos_file))
repos.each do |repo_json|
repo = Repository.find_by(external_url: repo_json['url'])
if repo.nil?
warn "repository by url #{repo_json['url']} does not exist in database"
next
end

repo.external_url = 'file://' + path + Repository.make_local_path(repo_json['url'])
mirror!(repo, repository_url: repo_json['url'], to_offline: true)
end
end
end
RMT::Lockfile.remove_file
rescue RMT::Lockfile::ExecutionLockedError
puts 'Process is locked'
rescue StandardError
RMT::Lockfile.remove_file
raise
end

end
30 changes: 10 additions & 20 deletions lib/rmt/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ class RMT::CLI::Main < RMT::CLI::Base

desc 'sync', 'Sync database with SUSE Customer Center'
def sync
RMT::Lockfile.create_file
RMT::SCC.new(options).sync
RMT::Lockfile.remove_file
rescue RMT::Lockfile::ExecutionLockedError
puts 'Process is locked'
rescue StandardError
RMT::Lockfile.remove_file
raise
locked_method do
RMT::SCC.new(options).sync
end
end

desc 'products', 'List and modify products'
Expand All @@ -25,19 +20,14 @@ def sync

desc 'mirror', 'Mirror repositories'
def mirror
RMT::Lockfile.create_file
repos = Repository.where(mirroring_enabled: true)
if repos.empty?
warn 'There are no repositories marked for mirroring.'
return
locked_method do
repos = Repository.where(mirroring_enabled: true)
if repos.empty?
warn 'There are no repositories marked for mirroring.'
return
end
repos.each { |repo| mirror!(repo) }
end
repos.each { |repo| mirror!(repo) }
RMT::Lockfile.remove_file
rescue RMT::Lockfile::ExecutionLockedError
puts 'Process is locked'
rescue StandardError
RMT::Lockfile.remove_file
raise
end

desc 'import', 'Import commands for Offline Sync'
Expand Down

0 comments on commit 0a7cd66

Please sign in to comment.