From f1ebb8e63484cf002ee30da6fdf08ad0780b783e Mon Sep 17 00:00:00 2001 From: Markus Bucher Date: Wed, 6 Sep 2023 22:13:32 +0200 Subject: [PATCH] Fixes #36716 - Add task to fix candlepin DB --- .../tasks/fix_candlepin_product_content.rake | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lib/katello/tasks/fix_candlepin_product_content.rake diff --git a/lib/katello/tasks/fix_candlepin_product_content.rake b/lib/katello/tasks/fix_candlepin_product_content.rake new file mode 100644 index 00000000000..d5f3dcf1e4f --- /dev/null +++ b/lib/katello/tasks/fix_candlepin_product_content.rake @@ -0,0 +1,43 @@ +namespace :katello do + desc "Fix missing repository content in candlepin" + task :fix_candlepin_content => ["environment", "check_ping"] do + logger = Logger.new(STDOUT) + User.current = User.anonymous_api_admin + + Katello::Resources::Candlepin::Owner.all.each do |owner| + logger.debug("Checking Products in Organization #{owner['name']}(#{owner['key']})") + org = Organization.find_by(label: owner['key']) + + # find_missing associations + Katello::Resources::Candlepin::Product.all(owner['key']).each do |cp_product| + katello_product = Katello::Product.find_by_cp_id(cp_product['id'], org) + logger.debug("Checking '#{cp_product['name']}'") + content_count_in_cp = cp_product['productContent'].length + content_count_in_katello = katello_product.contents.count + logger.debug("Product has #{content_count_in_cp} content entries in Candlepin and #{content_count_in_katello} in Katello") + if content_count_in_cp != content_count_in_katello + logger.info("Product '#{cp_product['name']}' has #{content_count_in_cp} content entries in Candlepin but #{content_count_in_katello} in Katello") + + existing_content_ids = cp_product['productContent']. + map { |e| e['content']['id'] }.uniq + missing_content = katello_product.contents.where.not(cp_content_id: existing_content_ids) + + missing_content.each do |katello_content| + logger.warn("Product #{katello_product.name.inspect} is missing Content #{katello_content.name.inspect}") + Katello::Resources::Candlepin::Product.remove_content( + owner['key'], + cp_product['id'], + katello_content.cp_content_id + ) + Katello::Resources::Candlepin::Product.add_content( + owner['key'], + cp_product['id'], + katello_content.cp_content_id, + true # enabled=false only seems to occur for RHEL content + ) + end + end + end + end + end +end