Skip to content

Commit

Permalink
Fixes #29798 - Move --lock-package-versions to hooks/
Browse files Browse the repository at this point in the history
The primary goal of this PR is to move the hook from Katello to a shared
one. An accidental move in 6e1a07b
resulted on this being executed on Debian which showed some flaws.
Additional improvements are made to make it robust.

This uses the package_lock_feature? helper to determine whether locking
is available. It then uses the newly introduced app_option? to determine
if the option is available. This means it doesn't have to execute
package_lock_feature? within the pre_commit hook again which saves an
exec() of an external process.

In case locking could not be performed, the preference is cleared to
allow the user to proceed. This saves them from having to edit the
scenario manually. Instructions are printed to restore functionality.

This also reduces the size of ForemanMaintainHookContextExtension to
just the minimal wrapper. Methods that are only used once are moved into
their particular hook file. This makes it easier to understand a single
file.

There helper itself is split into two methods. The one with bang exits
on failure while the one without only returns the status.
  • Loading branch information
ekohl authored and ehelms committed Sep 4, 2020
1 parent 214e96e commit 5d22a4c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
25 changes: 6 additions & 19 deletions hooks/boot/03-foreman-maintain-extensions.rb
@@ -1,25 +1,12 @@
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 foreman_maintain(command, exit_on_fail = false)
def foreman_maintain(command)
command = "foreman-maintain #{command}"
status = execute_command(command, false, true)
execute_command(command, false, true)
end

exit 1 if exit_on_fail && !status
def foreman_maintain!(command)
status = foreman_maintain(command)
exit 1 unless status
status
end
end
Expand Down
@@ -1,4 +1,8 @@
if system('command -v foreman-maintain > /dev/null')
def package_lock_feature?
foreman_maintain('packages -h')
end

if package_lock_feature?
app_option(
'--[no-]lock-package-versions',
:flag,
Expand Down
@@ -1,3 +1,7 @@
def lock_packages
foreman_maintain!('packages lock --assumeyes')
end

if get_custom_config(:lock_package_versions)
log_and_say :info, "Package versions are being locked."
lock_packages
Expand Down
35 changes: 35 additions & 0 deletions hooks/pre_commit/09-version_locking.rb
@@ -0,0 +1,35 @@
def packages_locked?
foreman_maintain('packages is-locked --assumeyes')
end

def unlock_packages
foreman_maintain!('packages unlock --assumeyes')
end

if app_option?(:lock_package_versions)
# evaluate version locking settings
cli_param = app_value(:lock_package_versions)
custom_config_value = get_custom_config(:lock_package_versions)
lock_versions = cli_param.nil? ? !!custom_config_value : cli_param # rubocop:disable Style/DoubleNegation

if lock_versions != custom_config_value
store_custom_config(:lock_package_versions, lock_versions)
kafo.config.configure_application
end

if packages_locked?
log_and_say :info, "Package versions are locked. Continuing with unlock."
unlock_packages
end
elsif get_custom_config(:lock_package_versions)
# This case can happen if previously packages were locked but this is no
# longer supported. For example, if foreman-maintain was removed. Clearing
# this ensures the post hook works properly
store_custom_config(:lock_package_versions, nil)
kafo.config.configure_application
fail_and_exit <<~MESSAGE
Locking of package versions was requested but foreman-maintain couldn't be used for locking.
The locking preference was cleared. Rerun the installer with the same arguments to proceed.
If locking is desired, ensure foreman-maintain can be used and rerun with --lock-package-versions.
MESSAGE
end
18 changes: 0 additions & 18 deletions katello/hooks/pre_commit/09-version_locking.rb

This file was deleted.

0 comments on commit 5d22a4c

Please sign in to comment.