Skip to content

Commit

Permalink
Merge 0c8153c into ee5802c
Browse files Browse the repository at this point in the history
  • Loading branch information
tmuntaner committed Nov 6, 2018
2 parents ee5802c + 0c8153c commit 64f5094
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 65 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Expand Up @@ -9,6 +9,7 @@ inherit_mode:
- Exclude

AllCops:
TargetRubyVersion: 2.5
Include:
- Gemfile
- Rakefile
Expand Down
36 changes: 19 additions & 17 deletions .rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-06-13 14:58:43 +0200 using RuboCop version 0.53.0.
# on 2018-11-06 09:34:58 +0100 using RuboCop version 0.53.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
Expand All @@ -26,6 +26,12 @@ Lint/MissingCopEnableDirective:
- 'spec/models/migration_engine_spec.rb'
- 'spec/support/messy_stdout_guard.rb'

# Offense count: 2
# Cop supports --auto-correct.
Performance/RegexpMatch:
Exclude:
- 'lib/rmt/cli/base.rb'

# Offense count: 3
# Configuration parameters: Max.
RSpec/ExampleLength:
Expand All @@ -34,7 +40,7 @@ RSpec/ExampleLength:
- 'spec/lib/rmt/cli/import_spec.rb'
- 'spec/lib/rmt/rpm/repomd_xml_parser_spec.rb'

# Offense count: 41
# Offense count: 47
RSpec/ExpectInHook:
Exclude:
- 'spec/lib/rmt/cli/export_spec.rb'
Expand All @@ -52,7 +58,7 @@ RSpec/HookArgument:
Exclude:
- 'spec/support/messy_stdout_guard.rb'

# Offense count: 22
# Offense count: 23
# Configuration parameters: AssignmentOnly.
RSpec/InstanceVariable:
Exclude:
Expand All @@ -70,13 +76,13 @@ RSpec/LetBeforeExamples:
- 'spec/lib/rmt/cli/import_spec.rb'
- 'spec/models/activation_spec.rb'

# Offense count: 66
# Offense count: 79
# Configuration parameters: .
# SupportedStyles: have_received, receive
RSpec/MessageSpies:
EnforcedStyle: receive

# Offense count: 55
# Offense count: 63
RSpec/MultipleExpectations:
Max: 6

Expand Down Expand Up @@ -157,18 +163,6 @@ Style/FormatStringToken:
- 'app/controllers/api/connect/v3/systems/systems_controller.rb'
- 'app/controllers/api/connect/v4/systems/products_controller.rb'

# Offense count: 8
# Cop supports --auto-correct.
Style/IfUnlessModifier:
Exclude:
- 'app/controllers/api/connect/v3/systems/products_controller.rb'
- 'app/controllers/api/connect/v3/systems/systems_controller.rb'
- 'lib/rmt/checksum_verifier.rb'
- 'lib/rmt/cli/repos_custom.rb'
- 'lib/rmt/downloader.rb'
- 'lib/rmt/fiber_request.rb'
- 'spec/factories/products.rb'

# Offense count: 2
Style/MixinUsage:
Exclude:
Expand All @@ -182,6 +176,14 @@ Style/RandomWithOffset:
- 'spec/factories/subscription_product_classes.rb'
- 'spec/factories/subscriptions.rb'

# Offense count: 3
# Cop supports --auto-correct.
Style/RedundantBegin:
Exclude:
- 'lib/rmt/cli/export.rb'
- 'lib/rmt/cli/main.rb'
- 'lib/rmt/downloader.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
Expand Down
10 changes: 9 additions & 1 deletion app/models/product.rb
Expand Up @@ -70,12 +70,20 @@ def self.clean_up_version(version)
[version, version.tr('-', '.').chomp('.0')].uniq
end

def friendly_name
"#{name} #{version} #{arch}"
end

def product_string
[identifier, version, arch].join('/')
end

def change_repositories_mirroring!(conditions, mirroring_enabled)
repositories.where(conditions).update_all(mirroring_enabled: mirroring_enabled)
repos = repositories.where(conditions)
repo_names = repos.pluck(:name)
repos.update_all(mirroring_enabled: mirroring_enabled)

repo_names.sort
end

def recommended_for?(root_product)
Expand Down
6 changes: 3 additions & 3 deletions app/services/repository_service.rb
Expand Up @@ -40,14 +40,14 @@ def detach_product!(product, repository)
end

def change_mirroring_by_product!(mirroring_enabled, products)
repo_count = 0
repos = []
products.each do |product|
conditions = { mirroring_enabled: !mirroring_enabled } # to only update the repos which need change
conditions[:enabled] = true if mirroring_enabled
repo_count += product.change_repositories_mirroring!(conditions, mirroring_enabled)
repos += product.change_repositories_mirroring!(conditions, mirroring_enabled)
end

repo_count
repos
end

private
Expand Down
77 changes: 57 additions & 20 deletions lib/rmt/cli/products.rb
Expand Up @@ -4,6 +4,9 @@ class RMT::CLI::Products < RMT::CLI::Base

include ::RMT::CLI::ArrayPrintable

class ProductNotFoundException < StandardError
end

desc 'list', 'List products which are marked to be mirrored.'
option :all, aliases: '-a', type: :boolean, desc: 'List all products, including ones which are not marked to be mirrored'
option :release_stage, aliases: '-r', type: :string, desc: 'beta, released'
Expand Down Expand Up @@ -35,21 +38,63 @@ def list
end
map ls: :list

desc 'enable', 'Enable mirroring of product repositories by product ID or product string.'
desc 'enable TARGETS', 'Enable mirroring of product repositories by a list of product IDs or product strings.'
option :all_modules, type: :boolean, desc: 'Enables all free modules for a product'
def enable(target)
change_product(target, true, options[:all_modules])
def enable(*targets)
change_products(targets, true, options[:all_modules])
end

desc 'disable', 'Disable mirroring of product repositories by product ID or product string.'
def disable(target)
change_product(target, false, false)
desc 'disable TARGETS', 'Disable mirroring of product repositories by a list of product IDs or product strings.'
def disable(*targets)
change_products(targets, false, false)
end

protected

def change_products(targets, set_enabled, all_modules)
success = true
targets = clean_target_input(targets)
raise RMT::CLI::Error.new('No product ids supplied') if targets.empty?

targets.each do |target|
change_product(target, set_enabled, all_modules)
rescue ProductNotFoundException => e
puts e.message
success = false
end

raise RMT::CLI::Error.new("Not all products were #{set_enabled ? 'enabled' : 'disabled'}.") unless success
end

def change_product(target, set_enabled, all_modules)
products = find_products(target)
raise ProductNotFoundException.new("Product by target '#{target}' not found.") if products.empty?
puts "Found product(s) by target #{target}: #{products.map(&:friendly_name).join(', ')}."

if set_enabled
products.each do |product|
extensions = all_modules ? Product.free_and_recommended_modules(product.id).to_a : Product.recommended_extensions(product.id).to_a
next if extensions.empty?
puts " The following required extensions for #{product.product_string} have been enabled: #{extensions.pluck(:name).join(', ')}."
products.push(*extensions)
end
end

repo_names = repository_service.change_mirroring_by_product!(set_enabled, products.uniq)
if repo_names.empty?
puts " All repositories have already been #{set_enabled ? 'enabled' : 'disabled'}."
else
repo_names.each do |repo_name|
puts " Repository #{repo_name} has been successfully #{set_enabled ? 'enabled' : 'disabled'}."
end
end
end

private

def find_products(target)
product_id = Integer(target, 10) rescue nil

products = []
if product_id
product = Product.find(product_id)
Expand All @@ -61,25 +106,17 @@ def change_product(target, set_enabled, all_modules)
products = Product.where(conditions).to_a
end

if set_enabled
products.each do |product|
extensions = all_modules ? Product.free_and_recommended_modules(product.id).to_a : Product.recommended_extensions(product.id).to_a
next if extensions.empty?
puts "The following required extensions for #{product.product_string} have been enabled: #{extensions.pluck(:name).join(', ')}."
products.push(*extensions)
end
end

repo_count = repository_service.change_mirroring_by_product!(set_enabled, products.uniq)
puts "#{repo_count} repo(s) successfully #{set_enabled ? 'enabled' : 'disabled'}."
products
rescue ActiveRecord::RecordNotFound
raise RMT::CLI::Error.new("Product by id \"#{product_id}\" not found.")
raise ProductNotFoundException.new("Product by id \"#{product_id}\" not found.")
end

private

def repository_service
@repository_service ||= RepositoryService.new
end

def clean_target_input(input)
input.inject([]) { |targets, object| targets + object.split(',') }.reject(&:empty?)
end

end

0 comments on commit 64f5094

Please sign in to comment.