Skip to content

Commit

Permalink
Fix purge of unwanted kernels on DNF based machines
Browse files Browse the repository at this point in the history
Previously when `installonly_limit` of a node is adjusted then
`package-cleanup` was called to remove unwanted old kernels.
This is invalid for dnf based systems and a new command must be used.

This DNF command will remove kernel packages except in two cases:
* It is the running kernel as determined from `$facts['kernelrelease']`
* It is one of last N kernel versions where N is the `installonly_limit`

Fixes #295
  • Loading branch information
traylenator committed May 17, 2023
1 parent 7e7b8c7 commit 413fbc6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
23 changes: 13 additions & 10 deletions manifests/init.pp
Expand Up @@ -205,19 +205,22 @@
default => '3',
}

$_pc_cmd = [
'/usr/bin/package-cleanup',
'--oldkernels',
"--count=${_real_installonly_limit}",
'-y',
$keep_kernel_devel ? {
true => '--keepdevel',
default => undef,
$_keep_kernel_devel = $keep_kernel_devel ? {
true => $facts['package_provider'] ? {
'yum' => '--keepdevel ',
'dnf' => '--exclude kernel-release',
default => fail("Fact package_provider is not set to \'yum\' or \'dnf\' - giving up"),
},
].filter |$val| { $val =~ NotUndef }
default => '',
}

$_pc_cmd = $facts['package_provider'] ? {
'yum' => "/usr/bin/package-cleanup --oldkernels --count=${_real_installonly_limit} -y${$_keep_kernel_devel}",
default => "/usr/bin/dnf -y remove $(/usr/bin/dnf repoquery --installonly --latest-limit=-${_real_installonly_limit}${_keep_kernel_devel} | /usr/bin/grep -v ${facts['kernelrelease']})"
}

exec { 'package-cleanup_oldkernels':
command => shellquote($_pc_cmd),
command => $_pc_cmd,
refreshonly => true,
require => Package[$utils_package_name],
subscribe => $_clean_old_kernels_subscribe,
Expand Down
20 changes: 20 additions & 0 deletions spec/acceptance/yum_config_install_limit_2_spec.rb
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require 'spec_helper_acceptance'

describe 'yum::config installonly_limit 1' do
context 'simple parameters' do
# Using puppet_apply as a helper
it 'must work idempotently with no errors' do
pp = <<-EOS
yum::config{'installonly_limit':
ensure => 2, # yum (and not dnf) does not like the value 1
}
include yum
EOS
# Run it twice and test for idempotency
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end
end
end
16 changes: 15 additions & 1 deletion spec/classes/init_spec.rb
Expand Up @@ -10,7 +10,7 @@

it 'contains Exec[package-cleanup_oldkernels' do
is_expected.to contain_exec('package-cleanup_oldkernels').with(
command: "/usr/bin/package-cleanup --oldkernels --count=#{value} -y",
command: %r{/usr/bin/dnf -y remove \$\(/usr/bin/dnf repoquery --installonly --latest-limit=-\$\{value\} | /usr/bin/grep -v \S+\)},
refreshonly: true
).that_subscribes_to('Yum::Config[installonly_limit]')
end
Expand Down Expand Up @@ -45,6 +45,20 @@
it { is_expected.to have_yumrepo_resource_count(0) }
end

context 'with package_provider yum' do
let(:facts) do
facts.merge({ package_provider: 'yum' })
end

it { is_expected.to compile.with_all_deps }

it {
is_expected.to contain_exec('package-cleanup_oldkernels').with(
command: '/usr/bin/package-cleanup --oldkernels --count=3 -y'
)
}
end

context 'when `manage_os_default_repos` is enabled' do
let(:params) { { 'manage_os_default_repos' => true } }

Expand Down

0 comments on commit 413fbc6

Please sign in to comment.