Bug 1380664 - destroying of hosts with foreman-rake console Host.find_by_name("host.example.test").destroy fails
Summary: destroying of hosts with foreman-rake console Host.find_by_name("host.example...
Keywords:
Status: CLOSED WONTFIX
Alias: None
Product: Red Hat Satellite
Classification: Red Hat
Component: Hosts - Content
Version: 6.2.2
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: Unspecified
Assignee: satellite6-bugs
QA Contact: Katello QA List
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2016-09-30 09:44 UTC by Stefan Nemeth
Modified: 2023-09-15 00:00 UTC (History)
6 users (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2016-12-19 20:56:01 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
Red Hat Knowledge Base (Solution) 2677461 0 None None None 2018-01-08 00:58:09 UTC

Description Stefan Nemeth 2016-09-30 09:44:53 UTC
Description of problem:
Deleting host from foreman-rake console fails on foreign key constraint violation. 



Version-Release number of selected component (if applicable):

How reproducible:
100%

Steps to Reproduce:
0.choose host to destroy
1.foreman-rake console
2.Host.find_by_name("host.example.test").destroy


Actual results:

Host.find_by_name("host.example.test").destroy
ActiveRecord::InvalidForeignKey: PG::Error: ERROR:  update or delete on table "hosts" violates foreign key constraint "katello_subscription_facets_host_id" on table "katello_subscription_facets"
DETAIL:  Key (id)=(67) is still referenced from table "katello_subscription_facets".
: DELETE FROM "hosts" WHERE "hosts"."type" IN ('Host::Managed') AND "hosts"."id" = $1
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:834:in `get_last_result'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:834:in `block in exec_cache'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activesupport-4.1.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:831:in `exec_cache'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `exec_delete'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:106:in `delete'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `delete'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/relation.rb:449:in `delete_all'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/persistence.rb:465:in `destroy_row'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/locking/optimistic.rb:108:in `destroy_row'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/persistence.rb:154:in `destroy'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:292:in `block in destroy'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activesupport-4.1.5/lib/active_support/callbacks.rb:113:in `call'
	from /opt/rh/rh-ror41/root/usr/share/gems/gems/activesupport-4.1.5/lib/active_support/callbacks.rb:113:in `call'
... 79 levels...
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
	from /opt/rh/rh-ruby22/root/usr/share/ruby/monitor.rb:211:in `mon_synchronize'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
	from /opt/rh/rh-ruby22/root/usr/share/gems/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'



Expected results:
host destroyed/deleted 

Additional info:

as a workaround you can delete from psql tables

#su postgres
#psql foreman

delete from katello_subscription_facets where host_id=(select id from hosts where name='host.example.test');
delete from katello_content_facets where host_id=(select id from hosts where name='host.example.test');

than 
Host.find_by_name("host.example.test").destroy

will pass successfully.

Comment 1 Brad Buckingham 2016-10-05 17:38:33 UTC
In Satellite 6.2, hosts should not be deleted using the procedure above.  One of the features introduced in the release is referred to as "Host Unification".  With that feature, a host can include subscription (candlepin) and content (pulp) facets, which means additional objects are stored in the backend services.  For this reason, errors like the one above are expected as a means to protect the user.

Is there a reason the host is being destroyed with "Host.find_by_name("host.example.test").destroy" as opposed to being destroyed through the UI/CLI/API?  If it is failing there, is there anything in foreman-tasks or logs indicating why it is failing?

Comment 2 Patrick C. F. Ernzer 2016-10-29 10:12:10 UTC
for me, the reason to want manual deletion is:
Katello::Resources::Candlepin::CandlepinResource: 404 Resource Not Found {"displayMessage":"Runtime Error RESTEASY001185: Could not find resource for relative : /consumers//entitlements of full path: https://localhost:8443/candlepin/consumers//entitlements at org.jboss.resteasy.core.registry.PathParamSegment.matchPattern:209","requestUuid":"eccbdfe4-f385-4724-9b5f-1f2a787e5a80"} (GET /candlepin/consumers//entitlements)

Comment 3 Justin Sherrill 2016-12-19 20:56:01 UTC
The proper way to do this would be:

ForemanTasks.sync_task(::Actions::Katello::Host::Destroy, Host.find_by(:name => "host.example.com"))

If this fails for some reason you could use:

host = Host.find_by(:name => 'host.example.com')
host.subscription_facet.try(:destroy! ) if host
host.content_facet.try(:destroy!) if host
host.destroy if host

however this could leave bad data in the backend.  This should be able to be fixed with the clean_backend_objects script as part of https://bugzilla.redhat.com/show_bug.cgi?id=1405500 

Closing as WONT_FIX

Comment 4 Red Hat Bugzilla 2023-09-15 00:00:11 UTC
The needinfo request[s] on this closed bug have been removed as they have been unresolved for 500 days


Note You need to log in before you can comment on or make changes to this bug.