Unable to delete a host from Red Hat Satellite 6 with OpenSCAP reports
Environment
- Red Hat Satellite v 6.2
- Red Hat Satellite v 6.3
Issue
- Unable to delete a host from Red Hat Satellite 6 with OpenSCAP reports
- After upgrading Red Hat Satellite 6 server, deleting a host fails with the following error
Error: PG::Error: ERROR: update or delete on table "hosts" violates foreign key constraint "reports_host_id_fk" on table "reports" DETAIL: Key (id)=(8) is still referenced from table "reports". : DELETE FROM "hosts" WHERE "hosts"."type" IN ('Host::Managed') AND "hosts"."id" = $1
Resolution
- This is a known issue and a bugzilla BZ # 1402513 is opened for the same.
WORKAROUND
As a workaround, follow below steps until BZ # 1402513 is fixed.
[root@satellite ~]# cp -p /usr/share/foreman/app/models/host/{managed.rb,managed.rb.bak}
[root@satellite ~]# vi /usr/share/foreman/app/models/host/managed.rb
Replace below block of code in /usr/share/foreman/app/models/host/managed.rb file
def remove_reports
return if reports.empty?
Log.delete_all("report_id IN (#{reports.pluck(:id).join(',')})")
Report.delete_all("host_id = #{id}")
end
With the following lines
def remove_reports
host_reports = Report.where("host_id = #{id}")
return if host_reports.count == 0
Log.where("report_id IN (#{host_reports.pluck(:id).join(',')})").delete_all
host_reports.delete_all
end
Restart httpd service
[root@satellite ~]# systemctl restart httpd
Root Cause
- If Hosts with 0 config reports and at least 1 or more OpenSCAP ARF reports exists, then it will fail to delete that specific host because of a foreign key constraint set on the
reportstable inforemandatabase.
Diagnostic Steps
In /var/log/foreman/production.log following unsuccessful attempt to delete a problematic host would appear.
2018-07-19 12:36:08 a50fcf41 [app] [I] Started DELETE "/hosts/client.example.com" for 192.168.1.10 at 2018-07-16 07:36:08 +0200
2018-07-19 12:36:08 a50fcf41 [app] [I] Processing by HostsController#destroy as HTML
2018-07-19 12:36:08 a50fcf41 [app] [I] Parameters: {"authenticity_token"=+FILTERED+ "id"=>"client.example.com"}
2018-07-19 12:36:08 a50fcf41 [app] [I] Current user: admin (administrator)
2018-07-19 12:36:08 a50fcf41 [app] [I] Expire fragment views/tabs_and_title_records-6 (0.1ms)
2018-07-19 12:36:09 a50fcf41 [app] [I] Failed to save:
2018-07-19 12:36:09 a50fcf41 [app] [I] Redirected to https://satellite.example.com/hosts
2018-07-19 12:36:09 a50fcf41 [app] [I] Completed 302 Found in 614ms (ActiveRecord: 81.4ms)
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
