Skip to content

Commit

Permalink
771957 - let repositories deal with content deletion
Browse files Browse the repository at this point in the history
Only marketing products must deal with deleting product - content assignment in
CP. For engineering products there are repositories created, which delete the
content when they are removed. This change prevents trying to delete the
content twice while deleting product.

The other posibility would be to prevent deleting of conent in repository in
case product is being deleted and let product handle it all, but there would be
necessary a mechanism for finding out if the repo orchestration happens during
product orchestration or not - when not repo should handle content deletion
itself.
  • Loading branch information
iNecas committed Feb 6, 2012
1 parent 443a3bf commit e26c2d8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 31 deletions.
1 change: 1 addition & 0 deletions scripts/system-test/cli_tests/provider_import.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# this also tests ability to intall imported products

header "provider import"

if [ -e /etc/redhat-release ]; then
RELEASEVER=$(rpm -qf /etc/redhat-release --queryformat '%{VERSION}' | sed 's/6Server/6.2/g' | sed 's/5Server/5.7/g')
fi
Expand Down
20 changes: 5 additions & 15 deletions src/app/models/glue/candlepin/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ def add_content content
end

def remove_content_by_id content_id
self.productContent_will_change!
self.productContent.delete_if {|pc| pc.content.id == content_id}
self.save!
Candlepin::Product.remove_content cp_id, content_id
end

def set_product
Expand Down Expand Up @@ -195,6 +193,10 @@ def del_content
end

def remove_all_content
# engineering products handle content deletion when destroying
# repositories
return true unless self.is_a? MarketingProduct

self.productContent.each do |pc|
Rails.logger.debug "Removing content from product '#{self.cp_id}' in candlepin: #{pc.content.name}"
self.remove_content_by_id pc.content.id
Expand Down Expand Up @@ -244,17 +246,6 @@ def del_unlimited_subscription
end
end

def del_unused_content
self.productContent.each do |pc|
content_repos = Pulp::Repository.all [Glue::Pulp::Repos.content_groupid(pc)]
if content_repos.empty?
self.remove_content_by_id pc.content.id
pc.destroy
end
end
true
end

def del_subscriptions
Rails.logger.debug "Deleting subscriptions for product #{name} in candlepin"
Candlepin::Product.delete_subscriptions self.organization.cp_key, self.cp_id
Expand Down Expand Up @@ -282,7 +273,6 @@ def save_product_orchestration
def destroy_product_orchestration
queue.create(:name => "delete subscriptions for product in candlepin: #{self.name}", :priority => 7, :action => [self, :del_subscriptions])
queue.create(:name => "remove candlepin content from a product: #{self.name}", :priority => 8, :action => [self, :remove_all_content])
queue.create(:name => "delete unused content in candlein: #{self.name}", :priority => 9, :action => [self, :del_unused_content])
queue.create(:name => "candlepin product: #{self.name}", :priority => 10, :action => [self, :del_product])
end

Expand Down
8 changes: 5 additions & 3 deletions src/app/models/glue/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ def url_to_host_and_path(url = "")

def del_products
Rails.logger.debug "Deleting all products for provider: #{name}"
self.products.each do |p|
p.destroy
end
# we first delete marketing products, because there are no repos for them
# and they take care of deleting product <-> content association in CP
# for themselves.
self.products.where("type = 'MarketingProduct'").uniq.each(&:destroy)
self.products.where("type <> 'MarketingProduct'").uniq.each(&:destroy)
true
rescue => e
Rails.logger.error "Failed to delete all products for provider #{name}: #{e}, #{e.backtrace.join("\n")}"
Expand Down
23 changes: 17 additions & 6 deletions src/app/models/glue/pulp/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,29 @@ def destroy_repo
end

def del_content
return true if self.content.nil?
content_group_id = Glue::Pulp::Repos.content_groupid(self.content)
return true unless self.content_id

content_repo_ids = Pulp::Repository.all([content_group_id]).map{|r| r['id']}
other_content_repo_ids = (content_repo_ids - [self.pulp_id])

if other_content_repo_ids.empty?
if other_repos_with_same_product_and_content.empty?
self.product.remove_content_by_id self.content_id
if other_repos_with_same_content.empty?
Candlepin::Content.destroy(self.content_id)
end
end

true
end

def other_repos_with_same_product_and_content
product_group_id = Glue::Pulp::Repos.product_groupid(self.product_id)
content_group_id = Glue::Pulp::Repos.content_groupid(self.content_id)
Pulp::Repository.all([content_group_id, product_group_id]).map{|r| r['id']} - [self.pulp_id]
end

def other_repos_with_same_content
content_group_id = Glue::Pulp::Repos.content_groupid(self.content_id)
Pulp::Repository.all([content_group_id]).map{|r| r['id']} - [self.pulp_id]
end

def destroy_repo_orchestration
queue.create(:name => "remove product content : #{self.name}", :priority => 1, :action => [self, :del_content])
queue.create(:name => "delete pulp repo : #{self.name}", :priority => 2, :action => [self, :destroy_repo])
Expand Down
20 changes: 13 additions & 7 deletions src/app/models/glue/pulp/repos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,21 @@ def self.env_groupid(environment)
end

def self.product_groupid(product)
"product:#{product.cp_id}"
if product.is_a? String
product_id = product
else
product_id = product.cp_id
end
"product:#{product_id}"
end

def self.content_groupid(content)
"content:#{content.id}"
if content.is_a? String
content_id = content
else
content_id = content.id
end
"content:#{content_id}"
end

def self.prepopulate! products, environment, repos=[]
Expand Down Expand Up @@ -453,11 +463,7 @@ def update_repos
def del_repos
#destroy all repos in all environments
Rails.logger.debug "deleting all repositories in product #{name}"
self.environments.each do |env|
self.repos(env).each do |repo|
repo.destroy
end
end
self.environment_products.destroy_all
true
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class UpdateProductTypeAddDefault < ActiveRecord::Migration
def self.up
change_column :products, :type, :string, :default => "Product", :null => false
end

def self.down
change_column :products, :type, :string, :default => nil, :null => true
end
end

0 comments on commit e26c2d8

Please sign in to comment.