Skip to content

Commit

Permalink
Fixes #31135: Introduce execute! and execute for fatal and non-fatal …
Browse files Browse the repository at this point in the history
…commands

Introduce two separate functions that run a command. execute! will
run a command and exit if the status indicates failure. execute will
run a command and return the status of the command to let the hook
take further action. execute is intended for actions that require
information to decide what further action to take or if the action
is non-fatal.

This present previously seen errors where detecting foreman-maintain
exists on the system or foreman-maintain checks for a feature
from presenting as an error incorrectly to the user.
  • Loading branch information
ehelms committed Nov 13, 2020
1 parent 8b9d392 commit d5e079d
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 31 deletions.
29 changes: 19 additions & 10 deletions hooks/boot/01-kafo-hook-extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,23 @@ def log_and_say(level, message, do_say = true, do_log = true)
Kafo::KafoConfigure.logger.send(level, message) if do_log
end

def execute(command, do_say = true, do_log = true)
exit 1 unless execute_command(command, do_say, do_log)
def execute!(command, do_say = true, do_log = true)
stdout_stderr, status = execute_command(command, do_say, do_log)

if stdout_stderr.nil?
log_and_say(:error, "Command #{command} not found", do_say, do_log)
exit 1
end

unless status
log_and_say("#{command} failed! Check the output for error!", do_say, do_log)
exit 1
end
end

def execute(command, do_say, do_log)
_stdout_stderr, status = execute_command(command, do_say, do_log)
status
end

def execute_command(command, do_say, do_log)
Expand All @@ -123,20 +138,14 @@ def execute_command(command, do_say, do_log)
begin
stdout_stderr, status = Open3.capture2e(*Kafo::PuppetCommand.format_command(command))
rescue Errno::ENOENT
log_and_say(:error, "Command #{command} not found", do_say, do_log)
return false
return [nil, false]
end

stdout_stderr.lines.map(&:chomp).each do |line|
log_and_say(:debug, line, do_say, do_log)
end

if status.success?
log_and_say(:debug, "#{command} finished successfully!", do_say, do_log)
else
log_and_say(:error, "#{command} failed! Check the output for error!", do_say, do_log)
end
status.success?
[stdout_stderr, status.success?]
end

def remote_host?(hostname)
Expand Down
7 changes: 3 additions & 4 deletions hooks/boot/03-foreman-maintain-extensions.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module ForemanMaintainHookContextExtension
def foreman_maintain(command)
command = "foreman-maintain #{command}"
execute_command(command, false, true)
execute(command, false, true)
end

def foreman_maintain!(command)
status = foreman_maintain(command)
exit 1 unless status
status
command = "foreman-maintain #{command}"
execute!(command, false, true)
end
end

Expand Down
2 changes: 1 addition & 1 deletion hooks/post/30-upgrade.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
if !app_value(:noop) && [0, 2].include?(@kafo.exit_code) && foreman_server?
execute('foreman-rake upgrade:run')
execute!('foreman-rake upgrade:run')
end
2 changes: 1 addition & 1 deletion hooks/post/31-cdn_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
param = get_custom_config('cdn_ssl_version')
if param
logger.info 'cdn_ssl_version param found, migrating to a Katello setting'
execute("foreman-rake -- config -k cdn_ssl_version -v '#{param}'")
execute!("foreman-rake -- config -k cdn_ssl_version -v '#{param}'")
store_custom_config('cdn_ssl_version', nil)
else
logger.debug 'cdn_ssl_version already migrated, skipping'
Expand Down
12 changes: 6 additions & 6 deletions hooks/pre/10-reset_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ def empty_db_in_postgresql(db)
if remote_host?(config[:host])
empty_database!(config)
else
execute("sudo -u postgres dropdb #{config[:database]}")
execute!("sudo -u postgres dropdb #{config[:database]}")
end
end

def reset_candlepin
execute('rm -f /var/lib/candlepin/.puppet-candlepin-cpdb*')
execute!('rm -f /var/lib/candlepin/.puppet-candlepin-cpdb*')
empty_db_in_postgresql('candlepin')
end

Expand All @@ -69,19 +69,19 @@ def empty_mongo(config)
password = "-p #{config[:password]}" if config[:password]
host = "--host #{config[:host]} --port #{config[:port]}"
cmd = "mongo #{config[:database]} #{username} #{password} #{host} #{ssl} #{ca_cert} #{client_cert} --eval 'db.dropDatabase();'"
execute(cmd)
execute!(cmd)
end

def reset_pulp
execute('rm -f /var/lib/pulp/init.flag')
execute!('rm -f /var/lib/pulp/init.flag')

mongo_config = load_mongo_config
start_services(['rh-mongodb34-mongod']) unless remote_host?(mongo_config[:host])
logger.info 'Dropping Pulp database!'
empty_mongo(mongo_config)

logger.info 'Clearing Pulp content from disk.'
execute('rm -rf /var/lib/pulp/{distributions,published,repos,content}/*')
execute!('rm -rf /var/lib/pulp/{distributions,published,repos,content}/*')
end

def pg_command_base(config, command, args)
Expand All @@ -100,7 +100,7 @@ def empty_database!(config)
where schemaname = 'public';
))
delete_statements = `#{generate_delete_statements}`
execute(pg_sql_statement(config, delete_statements)) if delete_statements
execute!(pg_sql_statement(config, delete_statements)) if delete_statements
end

def clear_pulpcore_content(content_dir)
Expand Down
2 changes: 1 addition & 1 deletion hooks/pre/12-clear_pulp_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def reset_repo_importers(config)
host = "--host #{config[:host]} --port #{config[:port]}"
cmd_base = '--eval \'db.repo_importers.update({"scratchpad": {$ne: null}}, {$set: {"scratchpad.repomd_revision": null}}, {"multi":true})\''
cmd = "mongo #{config[:database]} #{username} #{password} #{host} #{ssl} #{ca_cert} #{client_cert} #{cmd_base}"
execute(cmd)
execute!(cmd)
end

if app_value(:clear_pulp_content)
Expand Down
2 changes: 1 addition & 1 deletion hooks/pre/25-remove_apache_from_foreman_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def remove_user_from_group(user, group)
is_member = false
end
if is_member
execute("gpasswd -d '#{user}' '#{group}'")
execute!("gpasswd -d '#{user}' '#{group}'")
else
true
end
Expand Down
6 changes: 3 additions & 3 deletions hooks/pre/30-el7_upgrade_postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ def postgresql_12_upgrade
stop_services

server_packages = ['rh-postgresql12-postgresql-server']
if execute_command("rpm -q postgresql-contrib", false, false)
if execute("rpm -q postgresql-contrib", false, false)
server_packages << 'rh-postgresql12-postgresql-contrib'
end
ensure_packages(server_packages, 'installed')

execute(%(scl enable rh-postgresql12 "PGSETUP_INITDB_OPTIONS='--lc-collate=#{collate} --lc-ctype=#{ctype} --locale=#{collate}' postgresql-setup --upgrade"))
execute!(%(scl enable rh-postgresql12 "PGSETUP_INITDB_OPTIONS='--lc-collate=#{collate} --lc-ctype=#{ctype} --locale=#{collate}' postgresql-setup --upgrade"))
ensure_packages(['postgresql-server'], 'absent')
ensure_packages(['postgresql'], 'absent')
execute('rm -f /etc/systemd/system/postgresql.service')
execute!('rm -f /etc/systemd/system/postgresql.service')
ensure_packages(['rh-postgresql12-syspaths'], 'installed')
end

Expand Down
8 changes: 5 additions & 3 deletions hooks/pre/31-puppet_agent_oauth.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
unless app_value(:noop)
if File.exist?('/opt/puppetlabs/puppet/bin/ruby') && execute_command("rpm -q puppet-agent-oauth", false, false)
unless execute_command("/opt/puppetlabs/puppet/bin/ruby -e \"require 'oauth'\"", false, false)
execute_command("yum -y reinstall puppet-agent-oauth", false, true)
if File.exist?('/opt/puppetlabs/puppet/bin/ruby') && execute("rpm -q puppet-agent-oauth", false, false)
unless execute("/opt/puppetlabs/puppet/bin/ruby -e \"require 'oauth'\"", false, false)
success = execute("yum -y reinstall puppet-agent-oauth", false, true)

logger.error("Failed to reinstall puppet-agent-oauth. Please check that the package is available from a repository.") unless success
end
end
end
2 changes: 1 addition & 1 deletion hooks/pre_commit/20-certs_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
key_file = param('certs', 'server_key').value

unless app_value(:certs_skip_check) || [cert_file, ca_file, key_file].all? { |v| v.to_s.empty? }
execute(%(katello-certs-check -c "#{cert_file}" -k "#{key_file}" -b "#{ca_file}"))
execute!(%(katello-certs-check -c "#{cert_file}" -k "#{key_file}" -b "#{ca_file}"))
end
end

0 comments on commit d5e079d

Please sign in to comment.