Bug 1380664

Summary: destroying of hosts with foreman-rake console Host.find_by_name("host.example.test").destroy fails
Product: Red Hat Satellite Reporter: Stefan Nemeth <snemeth>
Component: Hosts - ContentAssignee: satellite6-bugs <satellite6-bugs>
Status: CLOSED WONTFIX QA Contact: Katello QA List <katello-qa-list>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 6.2.2CC: bbuckingham, jsherril, ohadlevy, pcfe, phess, snemeth
Target Milestone: UnspecifiedFlags: bbuckingham: needinfo? (snemeth)
Target Release: Unused   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2016-12-19 20:56:01 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:

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