Skip to content

Commit

Permalink
(MODULES-10804) option to force purge source.lists file
Browse files Browse the repository at this point in the history
  • Loading branch information
sheenaajay committed Sep 10, 2020
1 parent 2e794c9 commit f51cc8d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 8 deletions.
41 changes: 33 additions & 8 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
# @param config_files
# A hash made up of the various configuration files used by Apt.
#
# @param sources_list_force
# Specifies whether to perform force purge or delete. Default false.
#
class apt (
Hash $update_defaults = $apt::params::update_defaults,
Hash $purge_defaults = $apt::params::purge_defaults,
Expand Down Expand Up @@ -151,6 +154,7 @@
String $apt_conf_d = $apt::params::apt_conf_d,
Hash $config_files = $apt::params::config_files,
Hash $source_key_defaults = $apt::params::source_key_defaults,
Boolean $sources_list_force = $apt::params::sources_list_force,
) inherits apt::params {

if $facts['osfamily'] != 'Debian' {
Expand Down Expand Up @@ -185,6 +189,9 @@
if $purge['preferences.d'] {
assert_type(Boolean, $purge['preferences.d'])
}
if $sources_list_force {
assert_type(Boolean, $sources_list_force)
}
if $purge['apt.conf.d'] {
assert_type(Boolean, $purge['apt.conf.d'])
}
Expand All @@ -204,10 +211,27 @@
}
}

$sources_list_ensure = $_purge['sources.list'] ? {
true => absent,
default => file,
if $sources_list_force {
$sources_list_ensure = $_purge['sources.list'] ? {
true => absent,
default => file,
}
$sources_list_content = $_purge['sources.list'] ? {
true => nil,
default => undef,
}
}
else
{
$sources_list_ensure = $_purge['sources.list'] ? {
true => file,
default => file,
}
$sources_list_content = $_purge['sources.list'] ? {
true => "# Repos managed by puppet.\n",
default => undef,
}
}

$preferences_ensure = $_purge['preferences'] ? {
true => absent,
Expand All @@ -226,11 +250,12 @@
}

file { 'sources.list':
ensure => $sources_list_ensure,
path => $::apt::sources_list,
owner => root,
group => root,
notify => Class['apt::update'],
ensure => $sources_list_ensure,
path => $::apt::sources_list,
owner => root,
group => root,
content => $sources_list_content,
notify => Class['apt::update'],
}

file { 'sources.list.d':
Expand Down
1 change: 1 addition & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
$root = '/etc/apt'
$provider = '/usr/bin/apt-get'
$sources_list = "${root}/sources.list"
$sources_list_force = false
$sources_list_d = "${root}/sources.list.d"
$trusted_gpg_d = "${root}/trusted.gpg.d"
$conf_d = "${root}/apt.conf.d"
Expand Down
68 changes: 68 additions & 0 deletions spec/classes/apt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,74 @@
}
end

context 'with lots of non-defaults' do
let :params do
{
update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
purge: { 'sources.list' => true, 'sources.list.d' => true,
'preferences' => true, 'preferences.d' => true,
'apt.conf.d' => true },
}
end

it {
is_expected.to contain_file('sources.list').with(content: "# Repos managed by puppet.\n")
}

it {
is_expected.to contain_file('sources.list.d').with(purge: true,
recurse: true)
}

it {
is_expected.to contain_file('preferences').with(ensure: 'absent')
}

it {
is_expected.to contain_file('preferences.d').with(purge: true,
recurse: true)
}

it {
is_expected.to contain_file('apt.conf.d').with(purge: true,
recurse: true)
}

it {
is_expected.to contain_exec('apt_update').with(refreshonly: false,
timeout: 1,
tries: 3)
}
end

context 'with defaults for sources_list_force' do
let :params do
{
update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
purge: { 'sources.list' => true },
sources_list_force: false,
}
end

it {
is_expected.to contain_file('sources.list').with(content: "# Repos managed by puppet.\n")
}
end

context 'with non defaults for sources_list_force' do
let :params do
{
update: { 'frequency' => 'always', 'timeout' => 1, 'tries' => 3 },
purge: { 'sources.list' => true },
sources_list_force: true,
}
end

it {
is_expected.to contain_file('sources.list').with(ensure: 'absent')
}
end

context 'with entries for /etc/apt/auth.conf' do
facts_hash = {
'Ubuntu 14.04' => {
Expand Down

0 comments on commit f51cc8d

Please sign in to comment.