Skip to content

Instantly share code, notes, and snippets.

@parthaa
Created August 14, 2025 20:01
Show Gist options
  • Save parthaa/8070570a5ee95019c8460b5c87421cc4 to your computer and use it in GitHub Desktop.
Save parthaa/8070570a5ee95019c8460b5c87421cc4 to your computer and use it in GitHub Desktop.
diff --git a/app/services/katello/registration_manager.rb b/app/services/katello/registration_manager.rb
index 0ec8f0545a..7bee010a15 100644
--- a/app/services/katello/registration_manager.rb
+++ b/app/services/katello/registration_manager.rb
@@ -325,9 +325,18 @@ module Katello
host.content_facet.bound_repositories = []
host.content_facet.applicable_errata = []
host.content_facet.uuid = nil
- host.content_facet.content_view_environments = []
+
+ # Clear or retain provisioning information based on setting
+ if Setting[:retain_build_profile_upon_unregistration]
+ # Retain current kickstart_repository_id if setting is enabled
+ host.content_facet.kickstart_repository_id = kickstart_repository_id if kickstart_repository_id
+ else
+ # Clear provisioning information if setting is disabled
+ host.content_facet.content_view_environments = []
+ host.content_facet.kickstart_repository_id = nil
+ end
+
host.content_facet.content_source = ::SmartProxy.pulp_primary
- host.content_facet.kickstart_repository_id = kickstart_repository_id
host.content_facet.save!
Rails.logger.debug "remove_host_artifacts: marking CVEs unchanged to prevent backend update"
host.content_facet.mark_cves_unchanged
diff --git a/lib/katello/plugin.rb b/lib/katello/plugin.rb
index 23432fd870..53543e2434 100644
--- a/lib/katello/plugin.rb
+++ b/lib/katello/plugin.rb
@@ -596,6 +596,12 @@ Foreman::Plugin.register :katello do
full_name: N_('Delete Host upon unregister'),
description: N_("When unregistering a host via subscription-manager, also delete the host record. Managed resources linked to host such as virtual machines and DNS records may also be deleted.")
+ setting 'retain_build_profile_upon_unregistration',
+ type: :boolean,
+ default: false,
+ full_name: N_('Retain build profile upon unregistration'),
+ description: N_("When enabled, provisioning information like Content View, Lifecycle Environment, Kickstart repository, and media will be retained when a host is unregistered. When disabled, this information will be cleared during unregistration.")
+
setting 'register_hostname_fact',
type: :string,
default: '',
diff --git a/test/models/setting_test.rb b/test/models/setting_test.rb
index 69871700a3..a074049e1c 100644
--- a/test/models/setting_test.rb
+++ b/test/models/setting_test.rb
@@ -21,5 +21,15 @@ module Katello
ForemanTasks.expects(:async_task).with(::Actions::Katello::Host::RecalculateErrataStatus)
Setting['errata_status_installable'] = !Setting['errata_status_installable']
end
+
+ def test_retain_build_profile_upon_unregistration_setting
+ setting = Foreman.settings.set_user_value('retain_build_profile_upon_unregistration', true)
+ assert setting.valid?
+ assert_equal true, setting.value
+
+ setting.value = false
+ assert setting.valid?
+ assert_equal false, setting.value
+ end
end
end
diff --git a/test/services/katello/registration_manager_test.rb b/test/services/katello/registration_manager_test.rb
index a89ba793da..05d0b4ab78 100644
--- a/test/services/katello/registration_manager_test.rb
+++ b/test/services/katello/registration_manager_test.rb
@@ -333,6 +333,74 @@ module Katello
assert_equal ::SmartProxy.pulp_primary, @host.content_facet.content_source
end
+ def test_unregister_host_clears_build_profile_when_setting_disabled
+ ::Host::Managed.any_instance.stubs(:update_candlepin_associations)
+ @host = FactoryBot.create(:host, :with_content, :with_subscription, :content_view => @content_view,
+ :lifecycle_environment => @library, :organization => @content_view.organization)
+ kickstart_repo = katello_repositories(:fedora_17_x86_64)
+ @host.content_facet.update(:kickstart_repository_id => kickstart_repo.id)
+
+ original_cves = @host.content_facet.content_view_environments.dup
+ original_ks_repo_id = @host.content_facet.kickstart_repository_id
+
+ Setting[:retain_build_profile_upon_unregistration] = false
+ ::Katello::Resources::Candlepin::Consumer.expects(:destroy)
+
+ ::Katello::RegistrationManager.unregister_host(@host, unregistering: true)
+
+ # Verify provisioning information is cleared
+ assert_empty @host.content_facet.content_view_environments
+ assert_nil @host.content_facet.kickstart_repository_id
+ # Verify other data is still cleared as expected
+ assert_empty @host.content_facet.bound_repositories
+ assert_empty @host.content_facet.applicable_errata
+ assert_nil @host.content_facet.uuid
+ end
+
+ def test_unregister_host_retains_build_profile_when_setting_enabled
+ ::Host::Managed.any_instance.stubs(:update_candlepin_associations)
+ @host = FactoryBot.create(:host, :with_content, :with_subscription, :content_view => @content_view,
+ :lifecycle_environment => @library, :organization => @content_view.organization)
+ kickstart_repo = katello_repositories(:fedora_17_x86_64)
+ @host.content_facet.update(:kickstart_repository_id => kickstart_repo.id)
+
+ original_cves = @host.content_facet.content_view_environments.dup
+ original_ks_repo_id = @host.content_facet.kickstart_repository_id
+
+ Setting[:retain_build_profile_upon_unregistration] = true
+ ::Katello::Resources::Candlepin::Consumer.expects(:destroy)
+
+ ::Katello::RegistrationManager.unregister_host(@host, unregistering: true)
+
+ # Verify provisioning information is retained
+ assert_equal original_cves, @host.content_facet.content_view_environments
+ assert_equal original_ks_repo_id, @host.content_facet.kickstart_repository_id
+ # Verify other data is still cleared as expected
+ assert_empty @host.content_facet.bound_repositories
+ assert_empty @host.content_facet.applicable_errata
+ assert_nil @host.content_facet.uuid
+ end
+
+ def test_unregister_host_retains_kickstart_repository_when_keep_kickstart_repository_and_setting_enabled
+ ::Host::Managed.any_instance.stubs(:update_candlepin_associations)
+ @host = FactoryBot.create(:host, :with_content, :with_subscription, :content_view => @content_view,
+ :lifecycle_environment => @library, :organization => @content_view.organization)
+ kickstart_repo = katello_repositories(:fedora_17_x86_64)
+ new_kickstart_repo = katello_repositories(:fedora_17_x86_64_dev)
+ @host.content_facet.update(:kickstart_repository_id => kickstart_repo.id)
+
+ original_cves = @host.content_facet.content_view_environments.dup
+
+ Setting[:retain_build_profile_upon_unregistration] = true
+ ::Katello::Resources::Candlepin::Consumer.expects(:destroy)
+
+ ::Katello::RegistrationManager.unregister_host(@host, unregistering: true, keep_kickstart_repository: true)
+
+ # Verify provisioning information is retained
+ assert_equal original_cves, @host.content_facet.content_view_environments
+ assert_equal kickstart_repo.id, @host.content_facet.kickstart_repository_id
+ end
+
def test_destroy_host_not_found
@host = FactoryBot.create(:host, :with_content, :with_subscription, :content_view => @content_view,
:lifecycle_environment => @library, :organization => @content_view.organization)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment