From 396f308e6ad4798461a6471ee8bb8e764d8b03cb Mon Sep 17 00:00:00 2001 From: Morgan Haskel Date: Wed, 3 Jun 2015 17:19:16 -0700 Subject: [PATCH] Compatibility between 1.8.x and 2.x for apt::source --- README.md | 20 +++- manifests/setting.pp | 5 +- manifests/source.pp | 128 +++++++++++++++++------ spec/defines/source_compat_spec.rb | 158 +++++++++++++++++++++++++++++ spec/defines/source_spec.rb | 94 +++++++++++++++++ templates/source.list.erb | 12 +-- 6 files changed, 377 insertions(+), 40 deletions(-) create mode 100644 spec/defines/source_compat_spec.rb diff --git a/README.md b/README.md index 0e2dfd6..4babf3c 100644 --- a/README.md +++ b/README.md @@ -418,11 +418,11 @@ Manages the Apt sources in `/etc/apt/sources.list.d/`. * `key`: Creates a declaration of the apt::key define Valid options: a string to be passed to the `id` parameter of the `apt::key` define, or a hash of `parameter => value` pairs to be passed to `apt::key`'s `id`, `server`, `content`, `source`, and/or `options` parameters. Default: undef. -* `include`: Configures include options. Valid options: a hash made up from the following keys: +* `include`: Configures include options. Valid options: a hash made up from the following keys: Default: {} -* 'deb' - Specifies whether to request the distribution's compiled binaries. Valid options: 'true' and 'false. Default: 'true'. +* 'deb' - Specifies whether to request the distribution's compiled binaries. Valid options: 'true' and 'false'. Default: 'true'. -* 'src' - Specifies whether to request the distribution's uncompiled source code. Valid options: 'true' and 'false'. Default: 'false'. Default: {}. +* 'src' - Specifies whether to request the distribution's uncompiled source code. Valid options: 'true' and 'false'. Default: 'false'. * `location`: *Required, unless `ensure` is set to 'absent'.* Specifies an Apt repository. Valid options: a string containing a repository URL. Default: undef. @@ -432,6 +432,20 @@ Manages the Apt sources in `/etc/apt/sources.list.d/`. * `repos`: Specifies a component of the Apt repository. Valid options: a string. Default: 'main'. +* `include_deb`: Specify whether to request the distrubution's compiled binaries. Valid options: 'true' and 'false'. Default: undef **Note** this parameter is deprecated and will be removed in future versions of the module. + +* `include_src`: Specifies whether to request the distribution's uncompiled source code. Valid options: 'true' and 'false'. Default: undef **Note** this parameter is deprecated andd will be removed in future versions of the module. + +* `required_packages`: install packages required for this Apt source via an exec. Default: 'false'. **Note** this parameter is deprecated and will be removed in future versions of the module. + +* `key_content`: Specify the content to be passed to `apt::key`. Default: undef. **Note** this parameter is deprecated and will be removed in future versions of the module. + +* `key_server`: Specify the server to be passed to `apt::key`. Default: undef. **Note** this parameter is deprecated and will be removed in future versions of the module. + +* `key_source`: Specify the source to be passed to `apt::key`. Default: undef. **Note** this parameter is deprecated and will be removed in future versions of the module. + +* `trusted_source`: Specifies whether to authenticate packages from this release, even if the Release file is not signed or the signature can't be checked. Valid options: 'true' and 'false'. Default: undef. **Note** this parameter is deprecated and will be removed in future versions of the module. + #### Type: `apt_key` Manages the GPG keys that Apt uses to authenticate packages. diff --git a/manifests/setting.pp b/manifests/setting.pp index ab84460..59d0dd4 100644 --- a/manifests/setting.pp +++ b/manifests/setting.pp @@ -6,6 +6,7 @@ define apt::setting ( $notify_update = true, ) { + include 'apt::params' if $content and $source { fail('apt::setting cannot have both content and source') } @@ -42,8 +43,8 @@ define apt::setting ( $_priority = $priority } - $_path = $::apt::config_files[$setting_type]['path'] - $_ext = $::apt::config_files[$setting_type]['ext'] + $_path = $::apt::params::config_files[$setting_type]['path'] + $_ext = $::apt::params::config_files[$setting_type]['ext'] if $notify_update { $_notify = Exec['apt_update'] diff --git a/manifests/source.pp b/manifests/source.pp index 958bf25..734f375 100644 --- a/manifests/source.pp +++ b/manifests/source.pp @@ -1,41 +1,114 @@ # source.pp # add an apt source define apt::source( - $location = undef, - $comment = $name, - $ensure = present, - $release = $::apt::xfacts['lsbdistcodename'], - $repos = 'main', - $include = {}, - $key = undef, - $pin = undef, - $architecture = undef, - $allow_unsigned = false, + $location = undef, + $comment = $name, + $ensure = present, + $release = undef, + $repos = 'main', + $include = {}, + $key = undef, + $pin = undef, + $architecture = undef, + $allow_unsigned = false, + $include_src = undef, + $include_deb = undef, + $required_packages = undef, + $key_server = undef, + $key_content = undef, + $key_source = undef, + $trusted_source = undef, ) { validate_string($architecture, $comment, $location, $repos) validate_bool($allow_unsigned) validate_hash($include) - unless $release { - fail('lsbdistcodename fact not available: release parameter required') + include 'apt::params' + + $_before = Apt::Setting["list-${title}"] + + if $include_src != undef { + warning("\$include_src is deprecated and will be removed in the next major release, please use \$include => { 'src' => ${include_src} } instead") + } + + if $include_deb != undef { + warning("\$include_deb is deprecated and will be removed in the next major release, please use \$include => { 'deb' => ${include_deb} } instead") + } + + if $required_packages != undef { + warning('$required_packages is deprecated and will be removed in the next major release, please use package resources instead.') + exec { "Required packages: '${required_packages}' for ${name}": + command => "${::apt::params::provider} -y install ${required_packages}", + logoutput => 'on_failure', + refreshonly => true, + tries => 3, + try_sleep => 1, + before => $_before, + } + } + + if $key_server != undef { + warning("\$key_server is deprecated and will be removed in the next major release, please use \$key => { 'server' => ${key_server} } instead.") + } + + if $key_content != undef { + warning("\$key_content is deprecated and will be removed in the next major release, please use \$key => { 'content' => ${key_content} } instead.") + } + + if $key_source != undef { + warning("\$key_source is deprecated and will be removed in the next major release, please use \$key => { 'source' => ${key_source} } instead.") + } + + if $trusted_source != undef { + warning('$trusted_source is deprecated and will be removed in the next major release, please use $allow_unsigned instead.') + $_allow_unsigned = $trusted_source + } else { + $_allow_unsigned = $allow_unsigned + } + + if ! $release { + $_release = $::apt::params::xfacts['lsbdistcodename'] + unless $_release { + fail('lsbdistcodename fact not available: release parameter required') + } + } else { + $_release = $release } if $ensure == 'present' and ! $location { fail('cannot create a source entry without specifying a location') } - $_before = Apt::Setting["list-${title}"] - $_include = merge($::apt::include_defaults, $include) + if $include_src != undef and $include_deb != undef { + $_deprecated_include = { + 'src' => $include_src, + 'deb' => $include_deb, + } + } elsif $include_src != undef { + $_deprecated_include = { 'src' => $include_src } + } elsif $include_deb != undef { + $_deprecated_include = { 'deb' => $include_deb } + } else { + $_deprecated_include = {} + } + + $_include = merge($::apt::params::include_defaults, $_deprecated_include, $include) + + $_deprecated_key = { + 'key_server' => $key_server, + 'key_content' => $key_content, + 'key_source' => $key_source, + } if $key { if is_hash($key) { unless $key['id'] { fail('key hash must contain at least an id entry') } - $_key = merge($::apt::source_key_defaults, $key) + $_key = merge($::apt::params::source_key_defaults, $_deprecated_key, $key) } else { validate_string($key) - $_key = $key + $_key = merge( { 'id' => $key }, $_deprecated_key) } } @@ -66,19 +139,16 @@ define apt::source( if $key and ($ensure == 'present') { if is_hash($_key) { apt::key { "Add key: ${$_key['id']} from Apt::Source ${title}": - ensure => present, - id => $_key['id'], - server => $_key['server'], - content => $_key['content'], - source => $_key['source'], - options => $_key['options'], - before => $_before, - } - } else { - apt::key { "Add key: ${_key} from Apt::Source ${title}": - ensure => present, - id => $_key, - before => $_before, + ensure => present, + id => $_key['id'], + server => $_key['server'], + content => $_key['content'], + source => $_key['source'], + options => $_key['options'], + key_server => $_key['key_server'], + key_content => $_key['key_content'], + key_source => $_key['key_source'], + before => $_before, } } } diff --git a/spec/defines/source_compat_spec.rb b/spec/defines/source_compat_spec.rb new file mode 100644 index 0000000..2e813f3 --- /dev/null +++ b/spec/defines/source_compat_spec.rb @@ -0,0 +1,158 @@ +require 'spec_helper' + +describe 'apt::source', :type => :define do + GPG_KEY_ID = '47B320EB4C7C375AA9DAE1A01054B7A24BD6EC30' + + let :title do + 'my_source' + end + + context 'mostly defaults' do + let :facts do + { + :lsbdistid => 'Debian', + :lsbdistcodename => 'wheezy', + :osfamily => 'Debian', + } + end + + let :params do + { + 'include_deb' => false, + 'include_src' => true, + 'location' => 'http://debian.mirror.iweb.ca/debian/', + } + end + + it { is_expected.to contain_apt__setting('list-my_source').with_content(/# my_source\ndeb-src http:\/\/debian\.mirror\.iweb\.ca\/debian\/ wheezy main\n/) + } + end + + context 'no defaults' do + let :facts do + { + :lsbdistid => 'Debian', + :lsbdistcodename => 'wheezy', + :osfamily => 'Debian' + } + end + let :params do + { + 'comment' => 'foo', + 'location' => 'http://debian.mirror.iweb.ca/debian/', + 'release' => 'sid', + 'repos' => 'testing', + 'include_src' => false, + 'required_packages' => 'vim', + 'key' => GPG_KEY_ID, + 'key_server' => 'pgp.mit.edu', + 'key_content' => 'GPG key content', + 'key_source' => 'http://apt.puppetlabs.com/pubkey.gpg', + 'pin' => '10', + 'architecture' => 'x86_64', + 'trusted_source' => true, + } + end + + it { is_expected.to contain_apt__setting('list-my_source').with_content(/# foo\ndeb \[arch=x86_64 trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ sid testing\n/).without_content(/deb-src/) + } + + it { is_expected.to contain_apt__pin('my_source').that_comes_before('Apt::Setting[list-my_source]').with({ + 'ensure' => 'present', + 'priority' => '10', + 'origin' => 'debian.mirror.iweb.ca', + }) + } + + it { is_expected.to contain_exec("Required packages: 'vim' for my_source").that_comes_before('Apt::Setting[list-my_source]').with({ + 'command' => '/usr/bin/apt-get -y install vim', + 'logoutput' => 'on_failure', + 'refreshonly' => true, + 'tries' => '3', + 'try_sleep' => '1', + }) + } + + it { is_expected.to contain_apt__key("Add key: #{GPG_KEY_ID} from Apt::Source my_source").that_comes_before('Apt::Setting[list-my_source]').with({ + 'ensure' => 'present', + 'id' => GPG_KEY_ID, + 'key_server' => 'pgp.mit.edu', + 'key_content' => 'GPG key content', + 'key_source' => 'http://apt.puppetlabs.com/pubkey.gpg', + }) + } + end + + context 'trusted_source true' do + let :facts do + { + :lsbdistid => 'Debian', + :lsbdistcodename => 'wheezy', + :osfamily => 'Debian' + } + end + let :params do + { + 'include_src' => false, + 'location' => 'http://debian.mirror.iweb.ca/debian/', + 'trusted_source' => true, + } + end + + it { is_expected.to contain_apt__setting('list-my_source').with_content(/# my_source\ndeb \[trusted=yes\] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ wheezy main\n/) } + end + + context 'architecture equals x86_64' do + let :facts do + { + :lsbdistid => 'Debian', + :lsbdistcodename => 'wheezy', + :osfamily => 'Debian' + } + end + let :params do + { + 'location' => 'http://debian.mirror.iweb.ca/debian/', + 'architecture' => 'x86_64', + } + end + + it { is_expected.to contain_apt__setting('list-my_source').with_content(/# my_source\ndeb \[arch=x86_64 \] http:\/\/debian\.mirror\.iweb\.ca\/debian\/ wheezy main\n/) + } + end + + context 'ensure => absent' do + let :facts do + { + :lsbdistid => 'Debian', + :lsbdistcodename => 'wheezy', + :osfamily => 'Debian' + } + end + let :params do + { + 'ensure' => 'absent', + } + end + + it { is_expected.to contain_apt__setting('list-my_source').with({ + 'ensure' => 'absent' + }) + } + end + + describe 'validation' do + context 'no release' do + let :facts do + { + :lsbdistid => 'Debian', + :osfamily => 'Debian' + } + end + + it do + expect { subject.call }.to raise_error(Puppet::Error, /lsbdistcodename fact not available: release parameter required/) + end + end + end +end diff --git a/spec/defines/source_spec.rb b/spec/defines/source_spec.rb index 26c8fa7..c9863dd 100644 --- a/spec/defines/source_spec.rb +++ b/spec/defines/source_spec.rb @@ -229,6 +229,100 @@ describe 'apt::source' do } end + context 'include_src => true' do + let :facts do + { + :lsbdistid => 'Debian', + :lsbdistcodename => 'wheezy', + :osfamily => 'Debian', + :puppetversion => Puppet.version, + } + end + let :params do + { + :location => 'hello.there', + :include_src => true, + } + end + + it { is_expected.to contain_apt__setting('list-my_source').with({ + :ensure => 'present', + }).with_content(/# my_source\ndeb hello.there wheezy main\ndeb-src hello.there wheezy main\n/) + } + end + + context 'include_deb => false' do + let :facts do + { + :lsbdistid => 'debian', + :lsbdistcodename => 'wheezy', + :osfamily => 'debian', + :puppetversion => Puppet.version, + } + end + let :params do + { + :location => 'hello.there', + :include_deb => false, + } + end + + it { is_expected.to contain_apt__setting('list-my_source').with({ + :ensure => 'present', + }).without_content(/deb-src hello.there wheezy main\n/) + } + it { is_expected.to contain_apt__setting('list-my_source').without_content(/deb hello.there wheezy main\n/) } + end + + context 'include_src => true and include_deb => false' do + let :facts do + { + :lsbdistid => 'debian', + :lsbdistcodename => 'wheezy', + :osfamily => 'debian', + :puppetversion => Puppet.version, + } + end + let :params do + { + :location => 'hello.there', + :include_deb => false, + :include_src => true, + } + end + + it { is_expected.to contain_apt__setting('list-my_source').with({ + :ensure => 'present', + }).with_content(/deb-src hello.there wheezy main\n/) + } + it { is_expected.to contain_apt__setting('list-my_source').without_content(/deb hello.there wheezy main\n/) } + end + + context 'include precedence' do + let :facts do + { + :lsbdistid => 'debian', + :lsbdistcodename => 'wheezy', + :osfamily => 'debian', + :puppetversion => Puppet.version, + } + end + let :params do + { + :location => 'hello.there', + :include_deb => true, + :include_src => false, + :include => { 'deb' => false, 'src' => true }, + } + end + + it { is_expected.to contain_apt__setting('list-my_source').with({ + :ensure => 'present', + }).with_content(/deb-src hello.there wheezy main\n/) + } + it { is_expected.to contain_apt__setting('list-my_source').without_content(/deb hello.there wheezy main\n/) } + end + context 'ensure => absent' do let :facts do { diff --git a/templates/source.list.erb b/templates/source.list.erb index 26838db..fe7f559 100644 --- a/templates/source.list.erb +++ b/templates/source.list.erb @@ -1,11 +1,11 @@ # <%= @comment %> <%- if @_include['deb'] then -%> -deb <%- if @architecture or @allow_unsigned -%> -[<%- if @architecture %>arch=<%= @architecture %> <% end %><% if @allow_unsigned %>trusted=yes<% end -%> -] <%- end %><%= @location %> <%= @release %> <%= @repos %> +deb <%- if @architecture or @_allow_unsigned -%> +[<%- if @architecture %>arch=<%= @architecture %> <% end %><% if @_allow_unsigned %>trusted=yes<% end -%> +] <%- end %><%= @location %> <%= @_release %> <%= @repos %> <%- end -%> <%- if @_include['src'] then -%> -deb-src <%- if @architecture or @allow_unsigned -%> -[<%- if @architecture %>arch=<%= @architecture %> <% end %><% if @allow_unsigned %>trusted=yes<% end -%> -] <%- end %><%= @location %> <%= @release %> <%= @repos %> +deb-src <%- if @architecture or @_allow_unsigned -%> +[<%- if @architecture %>arch=<%= @architecture %> <% end %><% if @_allow_unsigned %>trusted=yes<% end -%> +] <%- end %><%= @location %> <%= @_release %> <%= @repos %> <%- end -%> -- 2.32.3