Skip to content

Commit

Permalink
Fixes #36563 - Run orphan content unit deletion during orphan cleanup…
Browse files Browse the repository at this point in the history
… only
  • Loading branch information
sjha4 committed Jul 11, 2023
1 parent 442b1da commit 87f89d9
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@ module Actions
module Katello
module OrphanCleanup
class RemoveOrphanedContentUnits < Actions::Base
def plan(options = {})
plan_self(id: options[:repo_id], destroy_all: options[:destroy_all])
end

def run
content_types_to_index = []
if input[:destroy_all]
::Katello::RepositoryTypeManager.enabled_repository_types.each_value do |repo_type|
content_types_to_index << repo_type.content_types_to_index
end
elsif input[:id]
repo = ::Katello::Repository.find(input[:id])
content_types_to_index = repo.repository_type.content_types_to_index
else
fail "Pass either a repository to determine content type or destroy_all to destroy all orphaned content units"
models = []

::Katello::RepositoryTypeManager.enabled_repository_types.each_value do |repo_type|
models << repo_type.content_types_to_index
end
content_types_to_index.flatten.each do |type|
type.model_class.orphaned.destroy_all
models.flatten.each do |content_type|
content_type.model_class.orphaned.destroy_all
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/lib/actions/katello/orphan_cleanup/remove_orphans.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def plan(proxy)
sequence do
if proxy.pulp_primary?
::Katello::RootRepository.orphaned.destroy_all
plan_action(RemoveOrphanedContentUnits, destroy_all: true)
plan_action(RemoveOrphanedContentUnits)
end
if proxy.pulp3_enabled?
plan_action(
Expand Down
4 changes: 0 additions & 4 deletions app/lib/actions/katello/repository/index_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ def run
output[:new_content][content_type.label] = new_count - initial_counts[content_type.label]
end
end

def finalize
ForemanTasks.async_task(OrphanCleanup::RemoveOrphanedContentUnits, {repo_id: input[:id]})
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/katello/pulp3/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def add_content(content_unit_href, remove_all_units = false)
rescue api.client_module::ApiError => e
if e.message.include? 'Could not find the following content units'
raise ::Katello::Errors::Pulp3Error, "Content units that do not exist in Pulp were requested to be copied."\
" Please run a complete sync on the following repository: #{repository_reference.root_repository.name}. Original error: #{e.message}"
" Please run `foreman-rake katello:delete_orphaned_content`. Original error: #{e.message}"
else
raise e
end
Expand All @@ -509,7 +509,7 @@ def add_content_for_repo(repository_href, content_unit_href)
rescue api.client_module::ApiError => e
if e.message.include? 'Could not find the following content units'
raise ::Katello::Errors::Pulp3Error, "Content units that do not exist in Pulp were requested to be copied."\
" Please run a complete sync on the following repository:"\
" Please run `foreman-rake katello:delete_orphaned_content` to fix the following repository:"\
" #{::Katello::Pulp3::RepositoryReference.find_by(repository_href: repository_href).root_repository.name}. Original error: #{e.message}"
else
raise e
Expand Down
2 changes: 1 addition & 1 deletion app/services/katello/pulp3/repository/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def add_content(content_unit_href)
rescue api.client_module::ApiError => e
if e.message.include? 'Could not find the following content units'
raise ::Katello::Errors::Pulp3Error, "Content units that do not exist in Pulp were requested to be copied."\
" Please run a complete sync on the following repository: #{repository_reference.root_repository.name}. Original error: #{e.message}"
" Please run `foreman-rake katello:delete_orphaned_content` to fix the following repository: #{repository_reference.root_repository.name}. Original error: #{e.message}"
else
raise e
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ def setup
def test_orphaned_content_task_destroys_orphans
rpm = katello_rpms(:one)
rpm.repository_rpms.destroy_all
ForemanTasks.sync_task(Actions::Katello::OrphanCleanup::RemoveOrphanedContentUnits, {repo_id: @rhel6.id})
assert_raises(ActiveRecord::RecordNotFound) { rpm.reload }
end

def test_orphaned_content_task_destroy_all
rpm = katello_rpms(:one)
rpm.repository_rpms.destroy_all
ForemanTasks.sync_task(Actions::Katello::OrphanCleanup::RemoveOrphanedContentUnits, {destroy_all: true})
ForemanTasks.sync_task(Actions::Katello::OrphanCleanup::RemoveOrphanedContentUnits)
assert_raises(ActiveRecord::RecordNotFound) { rpm.reload }
end
end
Expand Down
9 changes: 0 additions & 9 deletions test/models/repository_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -710,15 +710,6 @@ def test_with_errata
assert_includes Repository.with_errata([errata]), @rhel6
end

def test_orphaned_content_task_destroys_orphans
rpm = katello_rpms(:one)
::Katello::ContentUnitIndexer.any_instance.stubs(:import_all).returns(true)
::Katello::Repository.any_instance.stubs(:import_distribution_data).returns(true)
rpm.repository_rpms.destroy_all
ForemanTasks.sync_task(Actions::Katello::OrphanCleanup::RemoveOrphanedContentUnits, {repo_id: @rhel6.id})
assert_raises(ActiveRecord::RecordNotFound) { rpm.reload }
end

def test_index_content_ordering
repo_type = @rhel6.repository_type
SmartProxy.stubs(:pulp_primary).returns(SmartProxy.pulp_primary)
Expand Down

0 comments on commit 87f89d9

Please sign in to comment.