|
| 1 | +module Procedures::Candlepin |
| 2 | + class ProductContentAssociation < ForemanMaintain::Procedure |
| 3 | + metadata do |
| 4 | + for_feature :candlepin_database |
| 5 | + description 'Reassociate Content to Product in CandlepinDB' |
| 6 | + end |
| 7 | + |
| 8 | + # returns a Hash of candlepin product cp_id keys with a Hash of queried values |
| 9 | + # consisting of the product's name and the number of associated content |
| 10 | + # e.g. { '<cp_id>' => { 'name' => '...', count => X, ..}, ... } |
| 11 | + def foreman_content_num_by_product |
| 12 | + feature(:foreman_database).query(<<~SQL).map { |e| [e['cp_id'], e] }.to_h |
| 13 | + SELECT p.cp_id as cp_id, p.name as name, COUNT(c.id) as count |
| 14 | + FROM katello_products p |
| 15 | + JOIN katello_product_contents pc ON p.id = pc.product_id |
| 16 | + JOIN katello_contents c ON pc.content_id = c.id |
| 17 | + GROUP BY p.cp_id, p.name |
| 18 | + SQL |
| 19 | + end |
| 20 | + |
| 21 | + # return Hash of query-result Hashes with the respective candlepin product_id as key |
| 22 | + # similar to foreman_content_num_by_product() |
| 23 | + def cp_content_count_by_product |
| 24 | + feature(:candlepin_database).query(<<~SQL).map { |e| [e['product_id'], e] }.to_h |
| 25 | + SELECT product.product_id, product.uuid, product.name, COUNT(content.content_id) |
| 26 | + FROM cp_pool pool |
| 27 | + JOIN cp2_products product ON pool.product_uuid = product.uuid |
| 28 | + LEFT JOIN cp2_product_content pc ON product.uuid = pc.product_uuid |
| 29 | + LEFT JOIN cp2_content content ON content.uuid = pc.content_uuid |
| 30 | + GROUP BY product.uuid |
| 31 | + SQL |
| 32 | + end |
| 33 | + |
| 34 | + # returns a set of cp2_content ids for given product_id |
| 35 | + def cp_product_content_ids(product_id) |
| 36 | + feature(:candlepin_database).query(<<~SQL).map { |e| e['content_id'] }.to_set |
| 37 | + SELECT content.content_id |
| 38 | + FROM cp_pool pool |
| 39 | + JOIN cp2_products product ON pool.product_uuid = product.uuid |
| 40 | + JOIN cp2_product_content pc ON product.uuid = pc.product_uuid |
| 41 | + JOIN cp2_content content ON content.uuid = pc.content_uuid |
| 42 | + WHERE product.product_id = '#{product_id}' |
| 43 | + SQL |
| 44 | + end |
| 45 | + |
| 46 | + # return Set of candlepin content ids from katello_content table |
| 47 | + # for candlepin product with cp_id |
| 48 | + def katello_content_ids(cp_id) |
| 49 | + feature(:foreman_database).query(<<~SQL).map { |e| e['cp_content_id'] }.to_set |
| 50 | + SELECT c.cp_content_id |
| 51 | + FROM katello_products p |
| 52 | + JOIN katello_product_contents pc ON p.id = pc.product_id |
| 53 | + JOIN katello_contents c ON pc.content_id = c.id |
| 54 | + WHERE p.cp_id = '#{cp_id}' |
| 55 | + SQL |
| 56 | + end |
| 57 | + |
| 58 | + def assemble_restore_commands(look_closer_products) |
| 59 | + commands = [] |
| 60 | + look_closer_products.each do |cp_id, product| |
| 61 | + puts "Process Product #{product['name'].inspect}" |
| 62 | + # get content_ids from candlepin and katello |
| 63 | + missing_ids = katello_content_ids(cp_id) - cp_product_content_ids(cp_id) |
| 64 | + |
| 65 | + missing_ids.each do |content_id| |
| 66 | + commands << create_new_association_sql_inserts(product['uuid'], content_id) |
| 67 | + |
| 68 | + # clear entity version of affected product to avoid versioning and convergence issues |
| 69 | + commands << 'UPDATE cp2_products SET entity_version = NULL ' \ |
| 70 | + "WHERE uuid = '#{product['uuid']}'" |
| 71 | + end |
| 72 | + end |
| 73 | + commands |
| 74 | + end |
| 75 | + |
| 76 | + # returns SQL-INSERT String to recreate missing associations |
| 77 | + def create_new_association_sql_inserts(product_uuid, content_id) |
| 78 | + missing = feature(:candlepin_database).query( |
| 79 | + "SELECT name, uuid FROM cp2_content WHERE content_id = '#{content_id}'" |
| 80 | + ) |
| 81 | + insert_sql = [] |
| 82 | + missing.each do |content| |
| 83 | + puts " - repair missing: #{content['name'].inspect}" |
| 84 | + insert_sql << "(REPLACE(uuid_in((md5((random())::text))::cstring)::text, '-', '' )," \ |
| 85 | + ' true,' \ |
| 86 | + " '#{product_uuid}'," \ |
| 87 | + " '#{content['uuid']}'," \ |
| 88 | + ' NOW(), NOW())' |
| 89 | + end |
| 90 | + |
| 91 | + <<~SQL |
| 92 | + INSERT INTO cp2_product_content |
| 93 | + (id, enabled, product_uuid, content_uuid, created, updated) |
| 94 | + VALUES #{insert_sql.join(', ')} |
| 95 | + SQL |
| 96 | + end |
| 97 | + |
| 98 | + def run |
| 99 | + candlepin_content_num_by_product = cp_content_count_by_product |
| 100 | + look_closer_products = {} |
| 101 | + |
| 102 | + foreman_content_num_by_product.each do |product_id, foreman_product| |
| 103 | + next unless candlepin_content_num_by_product.key?(product_id) |
| 104 | + |
| 105 | + candlepin_product = candlepin_content_num_by_product[product_id] |
| 106 | + next unless foreman_product['count'] != candlepin_product['count'] |
| 107 | + |
| 108 | + look_closer_products[product_id] = candlepin_product |
| 109 | + end |
| 110 | + |
| 111 | + res = feature(:candlepin_database).psql(<<~SQL) |
| 112 | + BEGIN; |
| 113 | + #{assemble_restore_commands(look_closer_products).join(";\n")}; |
| 114 | + COMMIT; |
| 115 | + SQL |
| 116 | + |
| 117 | + if res.include? 'ERROR' |
| 118 | + warn! "Repairing Product-Content association in CandlepinDB failed. Please check the logs." |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments