Unable to delete a host from Red Hat Satellite 6 with OpenSCAP reports

Solution Verified - Updated -

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 issue has been fixed in Satellite version 6.4 under errata RHSA-2018:2927.

WORKAROUND

  • As a workaround for Satellite 6.2 and 6.3, follow below steps:

    • Take a backup of the file usr/share/foreman/app/models/host/managed.rb and edit it
    [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 reports table in foreman database.

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.X.X 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.

3 Comments

Still happens on Satellite 6.2.10.

Error: PG::Error: ERROR: update or delete on table "hosts" violates foreign key constraint "reports_host_id_fk" on table "reports" DETAIL: Key (id)=(383) is still referenced from table "reports". : DELETE FROM "hosts" WHERE "hosts"."type" IN ('Host::Managed') AND "hosts"."id" = $1

Issue resolved. Found four OpenSCAP reports for the same host which were preventing removal from Satellite.

Login to Satellite UI

Go to Hosts -> COMPLIANCE Reports: In filter option type: Host = hostname (which is not deletable)

Try to delete reports for the host

Try to delete the host

Thanks to Anand Agrawal (Red Hat Support) for the workaround.

Issue resolved. The solution worked perfectly as written.