Skip to content

Commit

Permalink
Fixes #29512: Call foreman-maintain without SCL context
Browse files Browse the repository at this point in the history
  • Loading branch information
ehelms committed Apr 27, 2020
1 parent 30ea2c0 commit d687c2f
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 25 deletions.
43 changes: 43 additions & 0 deletions hooks/boot/03-foreman-maintain-extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'English'
require 'open3'

module ForemanMaintainHookContextExtension
def package_lock_feature?
foreman_maintain('packages -h')
end

def packages_locked?
foreman_maintain('packages is-locked --assumeyes')
end

def lock_packages
foreman_maintain('packages lock --assumeyes', true)
end

def unlock_packages
foreman_maintain('packages unlock --assumeyes', true)
end

def start_services(services)
foreman_maintain("service start --only=#{services.join(',')}", true)
end

def stop_services(services)
command = "service stop"
command = "service stop --only=#{services.join(',')}" if services

foreman_maintain(command, true)
end

def foreman_maintain(command, exit_on_fail = false)
command = "foreman-maintain #{command}"
log_and_say :debug, "Executing: #{command}"

_stdout, stderr, status = Open3.capture3(*Kafo::PuppetCommand.format_command(command))

fail_and_exit(stderr) if exit_on_fail && !status.success?
status.success?
end
end

Kafo::HookContext.send(:include, ForemanMaintainHookContextExtension)
4 changes: 2 additions & 2 deletions hooks/pre/30-el7_upgrade_postgresql.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def postgresql_12_upgrade
execute('foreman-maintain service start --only=postgresql')
start_services(['postgresql'])
(_name, _owner, _enconding, collate, ctype, _privileges) = `runuser postgres -c 'psql -lt | grep -E "^\s+postgres"'`.chomp.split('|').map(&:strip)
execute('foreman-maintain service stop')
stop_services

server_packages = ['rh-postgresql12-postgresql-server']
if execute_command("rpm -q postgresql-contrib", false, false)
Expand Down
2 changes: 1 addition & 1 deletion katello/hooks/post/99-version_locking.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if get_custom_config(:lock_package_versions)
log_and_say :info, "Package versions are being locked."
execute('foreman-maintain packages lock --assumeyes', false)
lock_packages
end
6 changes: 1 addition & 5 deletions katello/hooks/pre/10-reset_feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def reset
if foreman_installed?

stop_services
start_services(['postgresql'])
reset_database
reset_candlepin
reset_pulp
Expand All @@ -20,11 +21,6 @@ def foreman_installed?
$CHILD_STATUS.success?
end

def stop_services
Kafo::KafoConfigure.logger.info 'Stopping services'
execute('foreman-maintain service stop --exclude postgresql')
end

def load_foreman_config
db_config = {}
db_config[:host] = param_value('foreman', 'db_host') || 'localhost'
Expand Down
12 changes: 7 additions & 5 deletions katello/hooks/pre/31-mongo_storage_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,39 @@ def migration

# Create export directory and dump MongoDB
logger.info 'Stopping Pulp services except MongoDB'
execute('foreman-maintain service stop --exclude "rh-mongodb34-mongod","postgresql","tomcat","dynflowd","foreman-proxy","puppetserver"')
stop_services
start_services(['rh-mongodb34-mongod'])
FileUtils.mkdir(export_dir) unless File.directory?(export_dir)
File.chmod(0o700, export_dir)
logger.info "Starting mongodump to #{export_dir}"
execute("mongodump --host localhost --out #{export_dir}")

# Remove datafiles out of MongoDB directory so it will start
logger.info 'Export done, stopping MongoDB to remove old datafiles'
execute('foreman-maintain service stop --only rh-mongodb34-mongod')
stop_services(['rh-mongodb34-mongod'])
logger.info "Removing contents from #{mongo_dir}"
execute("rm -rf #{mongo_dir}/*")

# Import the dump, fail and notify user of backup if restore does not work.
logger.info 'Changing config to WiredTiger and starting restore.'
execute("sed -i.bak -e 's/mmapv1/wiredTiger/g' #{mongo_conf}")
execute("mv #{mongo_conf}.bak #{export_dir}")
execute('foreman-maintain service start --only rh-mongodb34-mongod')
start_services(['rh-mongodb34-mongod'])
pulp_db = katello ? param('katello', 'pulp_db_name').value : 'pulp_database'
execute("mongorestore --host localhost --db=#{pulp_db} --drop --dir=#{export_dir}/#{pulp_db}")
unless $CHILD_STATUS.success?
logger.error 'The restore could not be completed correctly, reverting actions.'
logger.info 'Stopping MongoDB'
execute('foreman-maintain service stop --only rh-mongodb34-mongod')
stop_services(['rh-mongodb34-mongod'])

logger.info 'Restoring old config'
execute("mv -f #{export_dir}/mongod.conf.bak #{mongo_conf}")
logger.info "Removing contents in #{mongo_dir}"
execute("rm -rf #{mongo_dir}/*")
logger.info "Restoring database under MMAPV1 storage engine"
execute("mongorestore --host localhost --db=#{pulp_db} --drop --dir=#{export_dir}/#{pulp_db}")
logger.info 'Starting MongoDB with old config and database files'
execute('foreman-maintain service start --only rh-mongodb34-mongod')
start_services(['rh-mongodb34-mongod'])
logger.error "Mongo started up in MMAPV1 mode, backup at #{export_dir}"
kafo.class.exit 1
end
Expand Down
15 changes: 4 additions & 11 deletions katello/hooks/pre_commit/09-version_locking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,11 @@
kafo.config.configure_application
end

if lock_versions
unless system('command -v foreman-maintain > /dev/null')
fail_and_exit('Locking of package versions was requested but foreman-maintain is not installed')
end
unless system('foreman-maintain packages -h > /dev/null 2>&1')
fail_and_exit('Locking of package versions was requested but foreman-maintain version installed does not support it')
end
if lock_versions && !package_lock_feature?
fail_and_exit('Locking of package versions was requested but foreman-maintain version installed does not support it')
end

# unlock packages if locked
if system('foreman-maintain packages is-locked --assumeyes > /dev/null 2>&1')
if packages_locked?
log_and_say :info, "Package versions are locked. Continuing with unlock."
execute('foreman-maintain packages unlock --assumeyes', false)
unlock_packages
end
nil
2 changes: 1 addition & 1 deletion katello/hooks/pre_validations/30-mongo_storage_engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def disk_space

# Make sure MongoDB is running before start of engine upgrade
log_and_say :info, 'Ensuring MongoDB is running before upgrade.'
execute('foreman-maintain service start --only rh-mongodb34-mongod')
start_services(['rh-mongodb34-mongod'])

log_and_say :info, "Starting disk space check for upgrade"
disk_space
Expand Down

0 comments on commit d687c2f

Please sign in to comment.