From 8c507c2bdfbcbe259d6496f06dfb2f55735dfb85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phanie=20Jaumotte?= Date: Sat, 7 Jan 2023 12:01:41 +0100 Subject: [PATCH] Add parameter plugindir (Directory containing kibana plugins) --- Guardfile | 17 ------ lib/puppet/functions/kibana/hash2yaml.rb | 30 ++++++++++ lib/puppet_x/elastic/hash.rb | 75 ------------------------ manifests/config.pp | 12 +++- manifests/init.pp | 4 ++ spec/classes/kibana_spec.rb | 14 +++-- spec/functions/kibana_hash2yaml.rb | 16 +++++ spec/templates/kibana.yml.erb_spec.rb | 65 -------------------- spec/unit/puppet_x/elastic/hash_spec.rb | 26 -------- templates/etc/kibana/kibana.yml.erb | 7 --- 10 files changed, 70 insertions(+), 196 deletions(-) delete mode 100644 Guardfile create mode 100644 lib/puppet/functions/kibana/hash2yaml.rb delete mode 100644 lib/puppet_x/elastic/hash.rb create mode 100644 spec/functions/kibana_hash2yaml.rb delete mode 100644 spec/templates/kibana.yml.erb_spec.rb delete mode 100644 spec/unit/puppet_x/elastic/hash_spec.rb delete mode 100644 templates/etc/kibana/kibana.yml.erb diff --git a/Guardfile b/Guardfile deleted file mode 100644 index 9f6b9d4..0000000 --- a/Guardfile +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -notification :tmux, display_message: true - -guard :bundler do - watch('Gemfile') -end - -guard 'rake', :task => 'test' do - watch(%r{^manifests\/(.+)\.pp$}) -end - -guard :rspec, :cmd => 'rspec' do - watch(%r{^spec\/(classes|templates|unit)\/(.+)\_spec.rb$}) - watch(%r{^templates\/.*\/(.*)$}) { |m| "spec/templates/#{m[1]}_spec.rb" } - watch(%r{^lib\/puppet\/(.*)[.]rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" } -end diff --git a/lib/puppet/functions/kibana/hash2yaml.rb b/lib/puppet/functions/kibana/hash2yaml.rb new file mode 100644 index 0000000..ef54472 --- /dev/null +++ b/lib/puppet/functions/kibana/hash2yaml.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +# @summary Converts a puppet hash to YAML string. +Puppet::Functions.create_function(:'kibana::hash2yaml') do + # @param input The hash to be converted to YAML + # @param options A hash of options to control YAML file format + # @return [String] A YAML formatted string + # @example Call the function with the $input hash + # kibana::hash2yaml($input) + dispatch :yaml do + param 'Hash', :input + optional_param 'Hash', :options + end + + require 'yaml' + + def yaml(input, options = {}) + settings = { + 'header' => '# File managed by Puppet.' + } + + settings.merge!(options) + + if settings['header'].to_s.empty? + input.to_yaml + else + "#{settings['header']}\n#{input.to_yaml}" + end + end +end diff --git a/lib/puppet_x/elastic/hash.rb b/lib/puppet_x/elastic/hash.rb deleted file mode 100644 index 1d3d5c9..0000000 --- a/lib/puppet_x/elastic/hash.rb +++ /dev/null @@ -1,75 +0,0 @@ -# frozen_string_literal: true - -# Custom extensions namespace -module Puppet_X # rubocop:disable Naming/ClassAndModuleCamelCase - # Elastic helpers - module Elastic - # Utility extension for consistent to_yaml behavior. - module SortedHash - # Upon extension, modify the hash appropriately to render - # sorted yaml dependent upon whichever way is supported for - # this version of Puppet/Ruby's yaml implementation. - def self.extended(base) - if RUBY_VERSION >= '1.9' - # We can sort the hash in Ruby >= 1.9 by recursively - # re-inserting key/values in sorted order. Native to_yaml will - # call .each and get sorted pairs back. - tmp = base.to_a.sort - base.clear - tmp.each do |key, val| - case val - when base.class - val.extend Puppet_X::Elastic::SortedHash - when Array - val.map do |elem| - if elem.is_a? base.class - elem.extend(Puppet_X::Elastic::SortedHash) - else - elem - end - end - end - base[key] = val - end - else - # Otherwise, recurse into the hash to extend all nested - # hashes with the sorted each_pair method. - # - # Ruby < 1.9 doesn't support any notion of sorted hashes, - # so we have to expressly monkey patch each_pair, which is - # called by ZAML (the yaml library used in Puppet < 4; Puppet - # >= 4 deprecates Ruby 1.8) - # - # Note that respond_to? is used here as there were weird - # problems with .class/.is_a? - base.merge! base do |_, ov, _| - if ov.respond_to? :each_pair - ov.extend Puppet_X::Elastic::SortedHash - elsif ov.is_a? Array - ov.map do |elem| - if elem.respond_to? :each_pair - elem.extend Puppet_X::Elastic::SortedHash - else - elem - end - end - else - ov - end - end - end - end - - # Override each_pair with a method that yields key/values in - # sorted order. - def each_pair - return to_enum(:each_pair) unless block_given? - - keys.sort.each do |key| - yield key, self[key] - end - self - end - end - end -end diff --git a/manifests/config.pp b/manifests/config.pp index 8fa5acb..06b2927 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -9,13 +9,21 @@ 'absent' => $kibana::ensure, default => 'file', } - $config = $kibana::config file { '/etc/kibana/kibana.yml': ensure => $_ensure, - content => template("${module_name}/etc/kibana/kibana.yml.erb"), + content => Sensitive(kibana::hash2yaml($kibana::config)), owner => $kibana::kibana_user, group => $kibana::kibana_group, mode => '0660', } + + if $kibana::plugindir { + file { $kibana::plugindir: + ensure => 'directory', + owner => $kibana::kibana_user, + group => $kibana::kibana_group, + mode => '0755', + } + } } diff --git a/manifests/init.pp b/manifests/init.pp index 28c2afa..881dbf5 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -24,6 +24,9 @@ # @param status Service status # @param service_name Service name # @param package_name Package name +# @param plugindir +# Directory containing kibana plugins. +# Use this setting if you want to manage the directory # @param kibana_user owner of kibana.yml # @param kibana_group group of kibana.yml # @@ -36,6 +39,7 @@ Boolean $oss, Optional[String] $package_source, Kibana::Status $status, + Optional[Stdlib::Absolutepath] $plugindir = undef, String[1] $service_name = 'kibana', String[1] $package_name = 'kibana', String[1] $kibana_user = 'kibana', diff --git a/spec/classes/kibana_spec.rb b/spec/classes/kibana_spec.rb index 56fe914..2abacf2 100644 --- a/spec/classes/kibana_spec.rb +++ b/spec/classes/kibana_spec.rb @@ -41,10 +41,10 @@ group: 'kibana', mode: '0660' ). - with_content(%r{ - # Managed by Puppet.. - ---. - }xm) + with_content(sensitive(%r{ + # Managed by Puppet.. + ---. + }xm)) end it 'enables and starts the service' do @@ -246,6 +246,12 @@ with_name('kibana-custom') } end + + describe 'kibana_plugindir' do + let(:params) { { plugindir: '/usr/local/kibana/plugins' } } + + it { is_expected.to contain_file('/usr/local/kibana/plugins').with(mode: '0755') } + end end end end diff --git a/spec/functions/kibana_hash2yaml.rb b/spec/functions/kibana_hash2yaml.rb new file mode 100644 index 0000000..550ce6f --- /dev/null +++ b/spec/functions/kibana_hash2yaml.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'spec_helper' +describe 'kibana::hash2yaml' do + context 'with header' do + it do + is_expected.to run.with_params({ cle: 1 }, { header: '# HEADER' }).and_return('# HEADER\ncle: 1') + end + end + + context 'without header' do + it do + is_expected.to run.with_params({ cle: 1 }).and_return('# File managed by Puppet.\ncle: 1') + end + end +end diff --git a/spec/templates/kibana.yml.erb_spec.rb b/spec/templates/kibana.yml.erb_spec.rb deleted file mode 100644 index f0f9e4d..0000000 --- a/spec/templates/kibana.yml.erb_spec.rb +++ /dev/null @@ -1,65 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' -require 'yaml' - -# A few helpers to make config handling easier -class String - def config - "# File managed by Puppet.\n---\n#{unindent}" - end - - def unindent - gsub(%r{^#{scan(%r{^\s*}).min_by(&:length)}}, '') - end -end - -describe 'kibana.yml.erb' do - let :harness do - h = TemplateHarness.new('templates/etc/kibana/kibana.yml.erb') - h.set('@config', config) - h.run - end - - describe 'normal, sorted hashes' do - let :config do - { - 'server.host' => 'localhost', - 'kibana.index' => '.kibana' - } - end - - let :yaml do - <<-CONFIG - kibana.index: ".kibana" - server.host: localhost - CONFIG - end - - it { expect(harness).to eq(yaml.config) } - end - - describe 'nested hashes' do - let :config do - { - 'tilemap' => { - 'options' => { - 'maxZoom' => 18 - }, - 'url' => 'https://test' - } - } - end - - let :yaml do - <<-CONFIG - tilemap: - options: - maxZoom: 18 - url: https://test - CONFIG - end - - it { expect(harness).to eq(yaml.config) } - end -end diff --git a/spec/unit/puppet_x/elastic/hash_spec.rb b/spec/unit/puppet_x/elastic/hash_spec.rb deleted file mode 100644 index a4fbbfd..0000000 --- a/spec/unit/puppet_x/elastic/hash_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -# frozen_string_literal: true - -$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib')) - -require 'puppet' -require 'puppet_x/elastic/hash' - -describe Puppet_X::Elastic::SortedHash do - subject { { 'foo' => 1, 'bar' => 2 } } - - describe 'each_pair' do - it { is_expected.to respond_to :each_pair } - - it 'yields values' do - expect { |b| subject.each_pair(&b) }.to yield_control.exactly(2).times - end - - it 'returns an Enumerator if not passed a block' do - expect(subject.each_pair).to be_an_instance_of(Enumerator) - end - - it 'returns values' do - subject.each_pair.map { |k, v| [k, v] }.should == subject.to_a - end - end -end diff --git a/templates/etc/kibana/kibana.yml.erb b/templates/etc/kibana/kibana.yml.erb deleted file mode 100644 index b238547..0000000 --- a/templates/etc/kibana/kibana.yml.erb +++ /dev/null @@ -1,7 +0,0 @@ -# File managed by Puppet. -<%= - $LOAD_PATH.unshift(File.join(File.dirname(__FILE__),'..','..','..','lib')) - require 'puppet_x/elastic/hash' - - @config.extend(Puppet_X::Elastic::SortedHash).to_yaml --%>