Skip to content

Commit

Permalink
Fixes #37717 - Update evr extension ownership to foreman
Browse files Browse the repository at this point in the history
  • Loading branch information
sjha4 committed Aug 8, 2024
1 parent d7c9dec commit 4961182
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions hooks/boot/01-kafo-hook-extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def execute!(command, do_say = true, do_log = true)
log_and_say(:error, "#{command} failed! Check the output for error!", do_say, do_log)
exit 1
end
stdout_stderr
end

def execute_as!(user, command, do_say = true, do_log = true)
Expand Down
26 changes: 26 additions & 0 deletions hooks/pre/35-change-evr-extension-ownership.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# In Katello 4.14, the 'evr' extension is removed from PostgreSQL and integrated into the Katello database via a migration.
# This hook ensures the 'evr' extension's ownership is transferred to the 'foreman' user so migrations can act on it.

if local_postgresql? && execute("rpm -q postgresql-evr", false, false)
is_postgresql_active = execute_command("systemctl is-active postgresql", false, true)&.first&.strip == "active"

# Ensure the PostgreSQL service is running
unless is_postgresql_active
logger.debug("Starting postgresql service")
start_services(['postgresql']) unless app_value(:noop)
end

# Update the ownership of the evr extension
logger.debug("Updating ownership of the evr extension if it is enabled")
database = param_value('foreman', 'db_database') || 'foreman'
username = param_value('foreman', 'db_username') || 'foreman'
sql = "runuser -l postgres -c \"psql -d '#{database}' -c \\\"UPDATE pg_extension SET extowner = (SELECT oid FROM pg_authid WHERE rolname='#{username}') WHERE extname='evr';\\\"\""
logger.debug("Executing: #{sql}")
execute!(sql, false, true) unless app_value(:noop)

# Stop the PostgreSQL service if it was not running
unless is_postgresql_active
logger.debug("Stopping postgresql service")
stop_services(['postgresql']) unless app_value(:noop)
end
end
2 changes: 1 addition & 1 deletion spec/hook_context_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
end

it 'executes a command' do
expect(subject).to be_nil
expect(subject).to eq(command)
expect(context).to have_received(:execute_command).with(command, true, true)
end
end
Expand Down

0 comments on commit 4961182

Please sign in to comment.