From 246023218f2f9e01da2d83683173d5ee874deb50 Mon Sep 17 00:00:00 2001 From: Hunter Haugen Date: Mon, 22 Feb 2016 12:15:35 -0800 Subject: [PATCH] Merge the package management code into one define. --- README.md | 29 +++++++++---- manifests/deep_merge.pp | 73 +++++-------------------------- manifests/eyaml.pp | 82 +++++------------------------------ manifests/init.pp | 13 ++++-- manifests/install.pp | 82 +++++++++++++++++++++++++++++++++++ spec/acceptance/hiera_spec.rb | 1 + spec/classes/hiera_spec.rb | 32 ++++++++++---- 7 files changed, 157 insertions(+), 155 deletions(-) create mode 100644 manifests/install.pp diff --git a/README.md b/README.md index 080a658..3588fdc 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ The resulting output in /etc/puppet/hiera.yaml: ## Usage ## Reference -This module will also allow you to configure different options for logger and merge_behaviour. The default behaviour is to set logger to console and merge behaviour to native. +This module will also allow you to configure different options for logger and merge_behavior. The default behavior is to set logger to console and merge behavior to native. For details and valid options see [Configuring Hiera](https://docs.puppetlabs.com/hiera/1/configuring.html#global-settings). @@ -151,15 +151,30 @@ The following parameters are available for the hiera class: * `eyaml_name` The name of the eyaml gem. Default: 'hiera-eyaml' +* `eyaml_version` + The version of hiera-eyaml to install. Accepts 'installed', 'latest', '2.0.7', etc + Default: `undef` +* `eyaml_source` + An alternate gem source for installing hiera-eyaml. + Default: `undef`, uses gem backend default * `eyaml_datadir` The path to the directory where hiera will look for databases with the eyaml backend. Default: same as `datadir` * `eyaml_extension` The file extension for the eyaml backend. Default: `undef`, backend defaults to `'.eyaml'` -* `eyaml_version` - The version of hiera-eyaml to install. Accepts 'installed', 'latest', '2.0.7', etc +* `deep_merge_name` + The name of the deep_merge gem. + Default: 'deep\_merge' +* `deep_merge_version` + The version of deep\_merge to install. Accepts 'installed', 'latest', '2.0.7', etc. Default: `undef` +* `deep_merge_source` + An alternate gem source for installing deep_merge. + Default: `undef`, uses gem backend default +* `deep_merge_options` + A hash of options to set in hiera.yaml for the deep merge behavior. + Default: `{}` * `confdir` The path to Puppet's confdir. Default: `$::settings::confdir` which should be the following: @@ -177,12 +192,8 @@ The following parameters are available for the hiera class: Whether to create pkcs7 keys and manage key files for hiera-eyaml. This is useful if you need to distribute a pkcs7 key pair. Default: `true` -* `gem_source` - An alternate gem source for installing hiera-eyaml. - Default: `undef`, uses gem backend default -* `merge_behavior` - Which hiera merge behavior to use. - **Note**: You need to manage any package/gem dependencies yourself. +* `merge_behavior` + Which hiera merge behavior to use. Valid values are 'native', 'deep', and 'deeper'. Deep and deeper values will install the deep\_merge gem into the puppet runtime. Default: `undef`, hiera defaults to `'native'` * `extra_config` Arbitrary YAML content to append to the end of the hiera.yaml config file. diff --git a/manifests/deep_merge.pp b/manifests/deep_merge.pp index 8658d3d..026c949 100644 --- a/manifests/deep_merge.pp +++ b/manifests/deep_merge.pp @@ -10,69 +10,16 @@ # # Copyright (C) 2016 Joseph Yaworski, unless otherwise noted. # -class hiera::deep_merge ( - $provider = $hiera::provider, - $deep_merge_version = $hiera::deep_merge_version, - $gem_source = $hiera::gem_source, - $deep_merge_name = $hiera::deep_merge_name, -) inherits hiera::params { +class hiera::deep_merge { + $provider = $hiera::provider + $deep_merge_version = $hiera::deep_merge_version + $deep_merge_source = $hiera::deep_merge_source + $deep_merge_name = $hiera::deep_merge_name - $package_ensure = $deep_merge_version ? { - undef => 'installed', - default => $deep_merge_version, - } - if $provider == 'pe_puppetserver_gem' { - Exec { - path => [ - '/opt/puppetlabs/server/bin', - '/opt/puppetlabs/puppet/bin', - '/opt/puppet/bin', - '/usr/bin', - '/bin', - ], - } - - if $::pe_server_version { - # PE 2015 - $vendored_gem_creates = '/opt/puppetlabs/puppet/bin/deep_merge' - $puppetserver_gem_creates = '/opt/puppetlabs/server/data/puppetserver/jruby-gems/bin/deep_merge' - } else { - $vendored_gem_creates = '/opt/puppet/bin/deep_merge' - $puppetserver_gem_creates = '/var/opt/lib/pe-puppet-server/jruby-gems/bin/deep_merge' - } - - # The puppetserver gem wouldn't install the commandline util, so we do - # that here - #XXX Pre-puppet 4.0.0 version (PUP-1073) - #BUG This can't actually update the gem version if already installed. - if $deep_merge_version and $deep_merge_version =~ /^\d+\.\d+\.\d+$/ { - $gem_flag = "--version ${deep_merge_version}" - } else { - $gem_flag = undef - } - #XXX Post-puppet 4.0.0 - #package { 'deep_merge command line': - # ensure => installed, - # name => 'deep_merge', - # provider => 'pe_gem', - # source => $gem_source, - #} - - exec { 'install ruby gem deep_merge': - command => "gem install deep_merge ${gem_flag}", - creates => $vendored_gem_creates, - } - - exec { 'install puppetserver gem deep_merge': - command => "puppetserver gem install deep_merge ${gem_flag}", - creates => $puppetserver_gem_creates, - } - } else { - package { 'deep_merge': - ensure => $package_ensure, - name => $deep_merge_name, - provider => $provider, - source => $gem_source, - } + hiera::install { 'deep_merge': + gem_name => $deep_merge_name, + provider => $provider, + gem_version => $deep_merge_version, + gem_source => $deep_merge_source, } } diff --git a/manifests/eyaml.pp b/manifests/eyaml.pp index ef8ed96..ef6a62e 100644 --- a/manifests/eyaml.pp +++ b/manifests/eyaml.pp @@ -11,85 +11,23 @@ # Copyright (C) 2014 Terri Haber, unless otherwise noted. # class hiera::eyaml { + $eyaml_name = $hiera::eyaml_name $provider = $hiera::provider + $eyaml_version = $hiera::eyaml_version + $eyaml_source = $hiera::_eyaml_source + $owner = $hiera::owner $group = $hiera::group $cmdpath = $hiera::cmdpath $confdir = $hiera::confdir $create_keys = $hiera::create_keys $_keysdir = $hiera::_keysdir - $eyaml_version = $hiera::eyaml_version - $gem_source = $hiera::gem_source - $eyaml_name = $hiera::eyaml_name - - $package_ensure = $eyaml_version ? { - undef => 'installed', - default => $eyaml_version, - } - if $provider == 'pe_puppetserver_gem' { - Exec { - path => [ - '/opt/puppet/bin', - '/usr/bin', - '/bin', - ], - } - - $hiera_package_depedencies = [ - Exec["install ruby gem ${eyaml_name}"], - Exec["install puppetserver gem ${eyaml_name}"], - ] - # The puppetserver gem wouldn't install the commandline util, so we do - # that here (PUP-1073) - #BUG This can't actually update the gem version if already installed. - if $eyaml_version and $eyaml_version =~ /^\d+\.\d+\.\d+$/ { - $gem_flag = "--version ${eyaml_version}" - } else { - $gem_flag = undef - } - - exec { "install ruby gem ${eyaml_name}": - command => "gem install ${eyaml_name} ${gem_flag}", - creates => '/opt/puppet/bin/eyaml', - } - - exec { "install puppetserver gem ${eyaml_name}": - command => "puppetserver gem install ${eyaml_name} ${gem_flag}", - creates => '/var/opt/lib/pe-puppet-server/jruby-gems/bin/eyaml', - } - $master_subscribe = Exec["install puppetserver gem ${eyaml_name}"] - } elsif $provider == 'puppetserver_gem' { - $hiera_package_depedencies = [ - Package[$eyaml_name], - Package["puppetserver ${eyaml_name}"], - ] - package { "puppetserver ${eyaml_name}": - ensure => $package_ensure, - name => $eyaml_name, - provider => $provider, - source => $gem_source, - } - package { $eyaml_name: - ensure => $package_ensure, - provider => 'puppet_gem', - source => $gem_source, - } - $master_subscribe = [ - Package[$eyaml_name], - Package["puppetserver ${eyaml_name}"], - ] - } else { - $hiera_package_depedencies = Package[$eyaml_name] - package { $eyaml_name: - ensure => $package_ensure, - provider => $provider, - source => $gem_source, - } - $master_subscribe = Package[$eyaml_name] - } - Service <| title == $hiera::master_service |> { - subscribe +> $master_subscribe, + hiera::install { 'eyaml': + gem_name => $eyaml_name, + provider => $provider, + gem_version => $eyaml_version, + gem_source => $eyaml_source, } File { @@ -110,7 +48,7 @@ command => 'eyaml createkeys', path => $cmdpath, creates => "${_keysdir}/private_key.pkcs7.pem", - require => [ $hiera_package_depedencies, File[$_keysdir] ], + require => [ Hiera::Install['eyaml'], File[$_keysdir] ], } file { "${_keysdir}/private_key.pkcs7.pem": diff --git a/manifests/init.pp b/manifests/init.pp index e519037..9c15a68 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -50,6 +50,8 @@ $provider = $hiera::params::provider, $eyaml = false, $eyaml_name = 'hiera-eyaml', + $eyaml_version = undef, + $eyaml_source = undef, $eyaml_datadir = undef, $eyaml_extension = undef, $confdir = $hiera::params::confdir, @@ -58,20 +60,24 @@ $cmdpath = $hiera::params::cmdpath, $create_keys = true, $keysdir = undef, - $gem_source = undef, - $eyaml_version = undef, - $deep_merge_version = undef, $deep_merge_name = 'deep_merge', + $deep_merge_version = undef, + $deep_merge_source = undef, $deep_merge_options = {}, $merge_behavior = undef, $extra_config = '', $master_service = $hiera::params::master_service, + + #Deprecated + $gem_source = undef, ) inherits hiera::params { if $keysdir { $_keysdir = $keysdir } else { $_keysdir = "${confdir}/keys" } + + $_eyaml_source = pick_default($eyaml_source, $gem_source, undef) File { owner => $owner, group => $group, @@ -109,6 +115,7 @@ # - $_keysdir # - $confdir # - $merge_behavior + # - $deep_merge_options # - $extra_config file { $hiera_yaml: ensure => present, diff --git a/manifests/install.pp b/manifests/install.pp new file mode 100644 index 0000000..88a2a17 --- /dev/null +++ b/manifests/install.pp @@ -0,0 +1,82 @@ +# Private define +define hiera::install ( + $gem_name, + $provider, + $creates_bin = $name, + $gem_version = undef, + $gem_source = undef, +) { + $gem_ensure = pick($gem_version, 'installed') + if $provider == 'pe_puppetserver_gem' { + Exec { + path => [ + '/opt/puppetlabs/server/bin', + '/opt/puppetlabs/puppet/bin', + '/opt/puppet/bin', + '/usr/bin', + '/bin', + ], + } + if $::pe_server_version { + $vendored_gem_creates = "/opt/puppetlabs/puppet/bin/${creates_bin}" + $puppetserver_gem_creates = "/opt/puppetlabs/server/data/puppetserver/jruby-gems/bin/${creates_bin}" + } else { + $vendored_gem_creates = "/opt/puppet/bin/${creates_bin}" + $puppetserver_gem_creates = "/var/opt/lib/pe-puppet-server/jruby-gems/bin/${creates_bin}" + } + + # The puppetserver gem wouldn't install the commandline util, so we do + # that here (PUP-1073) + #BUG This can't actually update the gem version if already installed. + if $gem_version and $gem_version =~ /^\d+\.\d+\.\d+$/ { + $gem_flag = "--version ${gem_version}" + } else { + $gem_flag = undef + } + if $gem_source { + # Use a local source, like the package providers would + validate_absolute_path($gem_source) + $source_flag = '--local' + } else { + $source_flag = undef + } + + exec { "install ruby gem ${gem_name}": + command => "gem install ${source_flag} ${gem_name} ${gem_flag}", + creates => $vendored_gem_creates, + } + + exec { "install puppetserver gem ${gem_name}": + command => "puppetserver gem install ${source_flag} ${gem_name} ${gem_flag}", + creates => $puppetserver_gem_creates, + } + $master_subscribe = Exec["install puppetserver gem ${gem_name}"] + } elsif $provider == 'puppetserver_gem' { + package { "puppetserver ${gem_name}": + ensure => $gem_ensure, + name => $gem_name, + provider => $provider, + source => $gem_source, + } + package { $gem_name: + ensure => $gem_ensure, + provider => 'puppet_gem', + source => $gem_source, + } + $master_subscribe = [ + Package[$gem_name], + Package["puppetserver ${gem_name}"], + ] + } else { + $hiera_package_depedencies = Package[$gem_name] + package { $gem_name: + ensure => $gem_ensure, + provider => $provider, + source => $gem_source, + } + $master_subscribe = Package[$gem_name] + } + Service <| title == $hiera::master_service |> { + subscribe +> $master_subscribe, + } +} diff --git a/spec/acceptance/hiera_spec.rb b/spec/acceptance/hiera_spec.rb index 9077d70..828403b 100644 --- a/spec/acceptance/hiera_spec.rb +++ b/spec/acceptance/hiera_spec.rb @@ -29,6 +29,7 @@ class { 'hiera': eyaml => true, eyaml_name => 'custom-eyaml', + merge_behavior => 'deep', puppet_conf_manage => true, hierarchy => [ 'virtual/%{::virtual}', diff --git a/spec/classes/hiera_spec.rb b/spec/classes/hiera_spec.rb index e57ed48..2b47988 100644 --- a/spec/classes/hiera_spec.rb +++ b/spec/classes/hiera_spec.rb @@ -9,9 +9,13 @@ describe 'default params' do it { should compile.with_all_deps } end - describe 'eyaml param' do - let(:params) { { :eyaml => true }} + describe 'other params' do + let(:params) { { + :eyaml => true, + :merge_behavior => 'deeper', + } } it { should contain_class("hiera::eyaml") } + it { should contain_class("hiera::deep_merge") } end end context "pe puppet 3" do @@ -25,9 +29,13 @@ describe 'default params' do it { should compile.with_all_deps } end - describe 'eyaml param' do - let(:params) { { :eyaml => true }} + describe 'other params' do + let(:params) { { + :eyaml => true, + :merge_behavior => 'deeper', + } } it { should contain_class("hiera::eyaml") } + it { should contain_class("hiera::deep_merge") } end end elsif Puppet.version =~ /^4/ @@ -38,9 +46,13 @@ describe 'default params' do it { should compile.with_all_deps } end - describe 'eyaml param' do - let(:params) { { :eyaml => true }} + describe 'other params' do + let(:params) { { + :eyaml => true, + :merge_behavior => 'deeper', + } } it { should contain_class("hiera::eyaml") } + it { should contain_class("hiera::deep_merge") } end end context "pe puppet 2015.2" do @@ -53,9 +65,13 @@ describe 'default params' do it { should compile.with_all_deps } end - describe 'eyaml param' do - let(:params) { { :eyaml => true }} + describe 'other params' do + let(:params) { { + :eyaml => true, + :merge_behavior => 'deeper', + } } it { should contain_class("hiera::eyaml") } + it { should contain_class("hiera::deep_merge") } end end end