-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Halt upgrades if evr is not owned by foreman
- Loading branch information
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
definitions/checks/foreman/check_external_db_evr_permissions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Checks | ||
module Foreman | ||
class CheckExternalDbEvrPermissions < ForemanMaintain::Check | ||
metadata do | ||
label :external_db_evr_permissions | ||
for_feature :foreman_database | ||
description 'Check that external DBs have proper EVR extension permissions' | ||
tags :pre_upgrade | ||
confine do | ||
feature(:foreman_database) && !feature(:foreman_database).local? && feature(:katello) | ||
end | ||
end | ||
|
||
def run | ||
return unless evr_exists? | ||
|
||
error_msg = 'The evr extension is not owned by the foreman DB owner. Please run the ' \ | ||
'following command to fix it: ' \ | ||
'UPDATE pg_extension SET extowner = (SELECT oid FROM pg_authid WHERE ' \ | ||
"rolname='foreman') WHERE extname='evr';" | ||
fail!(error_msg) unless foreman_owns_evr? | ||
end | ||
|
||
private | ||
|
||
def evr_exists? | ||
evr_exists = feature(:foreman_database).query(query_for_evr_existence) | ||
if !evr_exists.empty? && evr_exists.first['evr_exists'] == '1' | ||
return evr_exists.first['evr_exists'] == '1' | ||
end | ||
return false | ||
end | ||
|
||
def foreman_owns_evr? | ||
evr_owned_by_postgres = feature(:foreman_database).query(query_if_postgres_owns_evr) | ||
unless evr_owned_by_postgres.empty? | ||
return evr_owned_by_postgres.first['evr_owned_by_postgres'] == '0' | ||
end | ||
fail!('Could not determine if the evr extension is owned by the foreman DB owner') | ||
end | ||
|
||
def query_for_evr_existence | ||
<<-SQL | ||
SELECT 1 AS evr_exists FROM pg_extension WHERE extname = 'evr' | ||
SQL | ||
end | ||
|
||
def query_if_postgres_owns_evr | ||
<<-SQL | ||
SELECT CASE WHEN r.rolname = 'foreman' THEN 0 ELSE 1 END AS evr_owned_by_postgres | ||
FROM pg_extension e JOIN pg_roles r ON e.extowner = r.oid WHERE e.extname = 'evr' | ||
SQL | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters