From: sheena Date: Thu, 10 Sep 2020 09:07:41 +0000 (+0100) Subject: (MODULES-10804) option to force purge source.lists file X-Git-Tag: v7.6.0~2^2~1 X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=6c706c65873540ed7ac896c184343d5634c8add7;p=puppet-modules%2Fpuppetlabs-apt.git (MODULES-10804) option to force purge source.lists file --- diff --git a/manifests/init.pp b/manifests/init.pp index d2aead9..fcfb09c 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -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, @@ -151,6 +154,7 @@ class apt ( 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' { @@ -185,6 +189,9 @@ class apt ( 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']) } @@ -204,10 +211,27 @@ class apt ( } } - $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, @@ -226,11 +250,12 @@ class apt ( } 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': diff --git a/manifests/params.pp b/manifests/params.pp index 149e4c9..c648c4e 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -8,28 +8,29 @@ class apt::params { fail(translate('This module only works on Debian or derivatives like Ubuntu')) } - $root = '/etc/apt' - $provider = '/usr/bin/apt-get' - $sources_list = "${root}/sources.list" - $sources_list_d = "${root}/sources.list.d" - $trusted_gpg_d = "${root}/trusted.gpg.d" - $conf_d = "${root}/apt.conf.d" - $preferences = "${root}/preferences" - $preferences_d = "${root}/preferences.d" - $apt_conf_d = "${root}/apt.conf.d" - $keyserver = 'keyserver.ubuntu.com' - $key_options = undef - $confs = {} - $update = {} - $purge = {} - $proxy = {} - $sources = {} - $keys = {} - $ppas = {} - $pins = {} - $settings = {} - $manage_auth_conf = true - $auth_conf_entries = [] + $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" + $preferences = "${root}/preferences" + $preferences_d = "${root}/preferences.d" + $apt_conf_d = "${root}/apt.conf.d" + $keyserver = 'keyserver.ubuntu.com' + $key_options = undef + $confs = {} + $update = {} + $purge = {} + $proxy = {} + $sources = {} + $keys = {} + $ppas = {} + $pins = {} + $settings = {} + $manage_auth_conf = true + $auth_conf_entries = [] $config_files = { 'conf' => { diff --git a/spec/classes/apt_spec.rb b/spec/classes/apt_spec.rb index ca11d4a..84c6574 100644 --- a/spec/classes/apt_spec.rb +++ b/spec/classes/apt_spec.rb @@ -200,6 +200,74 @@ describe 'apt' do } 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' => {