-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #36310 - Migrate off ContentViews generated for repository export
Ensures no Hosts nor ActivationKeys remain on ContentViews generated for repository export. If doing so would leave the Host or ActivationKey without a valid ContentView, the Default Organization View is assigned to it. Also implements additional filtering in the candlepin proxies controller, and several additional tests.
- Loading branch information
Showing
5 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
db/migrate/20240815080259_migrate_off_generated_content_views.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
class MigrateOffGeneratedContentViews < ActiveRecord::Migration[6.1] | ||
def up | ||
say_with_time "Migrating hosts and activation keys off generated content views" do | ||
migrate_hosts | ||
migrate_activation_keys | ||
end | ||
end | ||
|
||
def down | ||
say "This migration cannot be reversed", true | ||
end | ||
|
||
private | ||
|
||
def migrate_hosts | ||
say_with_time "Migrating hosts..." do | ||
generated_content_views = Katello::ContentView.where(generated_for: 'repository_export') | ||
|
||
facets = Katello::Host::ContentFacet.joins(:content_view_environments) | ||
facets = facets.where(katello_content_view_environments: { content_view_id: generated_content_views }) | ||
facets.find_each do |content_facet| | ||
offending_cves = content_facet.content_view_environments.select { |cve| generated_content_views.include?(cve.content_view) } | ||
valid_cves = content_facet.content_view_environments - offending_cves | ||
|
||
if valid_cves.empty? | ||
default_view = Katello::ContentView.find_by(name: "Default Organization View", organization: content_facet.host.organization) | ||
if default_view | ||
default_cve = default_view.content_view_environments.find_by(lifecycle_environment: content_facet.lifecycle_environments.first) | ||
content_facet.update!(content_view_environments: [default_cve]) | ||
say "Replaced all content views with Default Organization View for host #{content_facet.host.name}", true | ||
else | ||
say "No Default Organization View found for host #{content_facet.host.name}. Skipping.", true | ||
end | ||
else | ||
content_facet.update!(content_view_environments: valid_cves) | ||
say "Removed offending content views for host #{content_facet.host.name}", true | ||
end | ||
end | ||
end | ||
end | ||
|
||
def migrate_activation_keys | ||
say_with_time "Migrating activation keys..." do | ||
generated_content_views = Katello::ContentView.where(generated_for: 'repository_export') | ||
|
||
Katello::ActivationKey.find_each do |ak| | ||
if generated_content_views.include?(ak.content_view) | ||
other_cves = ak.environment.content_view_environments.where.not(content_view: generated_content_views) | ||
|
||
if other_cves.empty? | ||
default_view = Katello::ContentView.find_by(name: "Default Organization View", organization: ak.organization) | ||
if default_view | ||
ak.update!(content_view: default_view) | ||
say "Replaced all content views with Default Organization View for activation key #{ak.name}", true | ||
else | ||
say "No Default Organization View found for activation key #{ak.name}. Skipping.", true | ||
end | ||
else | ||
ak.update!(content_view: other_cves.first.content_view) | ||
say "Removed offending content views for activation key #{ak.name}", true | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters