From c830a68d66d54ff7cc94c11f044a65a0d630063c Mon Sep 17 00:00:00 2001 From: cswart Date: Wed, 20 Nov 2024 11:04:13 +0100 Subject: [PATCH 1/7] Issue_383: Add ensure and enable parameter for forwarder service On windows we need to set delayed for the service --- REFERENCE.md | 18 ++++++++++++++++ manifests/forwarder.pp | 8 ++++++++ manifests/forwarder/service.pp | 4 ++-- manifests/params.pp | 8 ++++++++ spec/acceptance/splunk_forwarder_spec.rb | 26 ++++++++++++++++++++++++ spec/classes/forwarder_spec.rb | 24 ++++++++++++++++++++++ 6 files changed, 86 insertions(+), 2 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index 61f3db43..cdbf1a11 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -864,6 +864,8 @@ The following parameters are available in the `splunk::forwarder` class: * [`forwarder_homedir`](#-splunk--forwarder--forwarder_homedir) * [`forwarder_confdir`](#-splunk--forwarder--forwarder_confdir) * [`service_name`](#-splunk--forwarder--service_name) +* [`service_ensure`](#-splunk--forwarder--service_ensure) +* [`service_enable`](#-splunk--forwarder--service_enable) * [`service_file`](#-splunk--forwarder--service_file) * [`boot_start`](#-splunk--forwarder--boot_start) * [`use_default_config`](#-splunk--forwarder--use_default_config) @@ -1026,6 +1028,22 @@ The name of the Splunk Forwarder service. Default value: `$splunk::params::forwarder_service` +##### `service_ensure` + +Data type: `String[1]` + +Ensure passed to the splunk service resource. + +Default value: `$splunk::params::forwarder_service_ensure` + +##### `service_enable` + +Data type: `String[1]` + +Enable passed to the splunk service resource. + +Default value: `$splunk::params::forwarder_service_enable` + ##### `service_file` Data type: `Stdlib::Absolutepath` diff --git a/manifests/forwarder.pp b/manifests/forwarder.pp index d9ca0876..29a28d3d 100644 --- a/manifests/forwarder.pp +++ b/manifests/forwarder.pp @@ -68,6 +68,12 @@ # @param service_name # The name of the Splunk Forwarder service. # +# @param service_ensure +# Ensure passed to the splunk service resource. +# +# @param service_enable +# Enable passed to the splunk service resource. +# # @param service_file # The path to the Splunk Forwarder service file. # @@ -172,6 +178,8 @@ Stdlib::Absolutepath $forwarder_homedir = $splunk::params::forwarder_homedir, Stdlib::Absolutepath $forwarder_confdir = $splunk::params::forwarder_confdir, String[1] $service_name = $splunk::params::forwarder_service, + String[1] $service_ensure = $splunk::params::forwarder_service_ensure, + String[1] $service_enable = $splunk::params::forwarder_service_enable, Stdlib::Absolutepath $service_file = $splunk::params::forwarder_service_file, Boolean $boot_start = $splunk::params::boot_start, Boolean $use_default_config = true, diff --git a/manifests/forwarder/service.pp b/manifests/forwarder/service.pp index 8d616aeb..68a03205 100644 --- a/manifests/forwarder/service.pp +++ b/manifests/forwarder/service.pp @@ -5,8 +5,8 @@ # class splunk::forwarder::service { service { $splunk::forwarder::service_name: - ensure => running, - enable => true, + ensure => $splunk::forwarder::service_ensure, + enable => $splunk::forwarder::service_enable, hasstatus => true, hasrestart => true, } diff --git a/manifests/params.pp b/manifests/params.pp index 31cdb4d3..3d0cf5a5 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -166,6 +166,8 @@ $enterprise_confdir = "${enterprise_homedir}/etc" $forwarder_install_options = [] $enterprise_install_options = [] + $forwarder_service_ensure = 'running' + $forwarder_service_enable = 'true' # Systemd not supported until Splunk 7.2.2 if $facts['service_provider'] == 'systemd' and versioncmp($version, '7.2.2') >= 0 { $enterprise_service = 'Splunkd' @@ -198,6 +200,8 @@ $enterprise_confdir = "${enterprise_homedir}/etc" $forwarder_install_options = [] $enterprise_install_options = [] + $forwarder_service_ensure = 'running' + $forwarder_service_enable = 'true' # Systemd not supported until Splunk 7.2.2 if $facts['service_provider'] == 'systemd' and versioncmp($version, '7.2.2') >= 0 { $enterprise_service = 'Splunkd' @@ -232,6 +236,8 @@ $enterprise_install_options = [] $enterprise_service = 'splunk' $forwarder_service = 'splunk' + $forwarder_service_ensure = 'running' + $forwarder_service_enable = 'true' $enterprise_service_file = '/etc/rc.d/splunk' $forwarder_service_file = '/etc/rc.d/splunk' $boot_start_args = '' @@ -247,6 +253,8 @@ $forwarder_secret_file = "${forwarder_homedir}\\etc\\auth\\splunk.secret" $enterprise_secret_file = "${enterprise_homedir}\\etc\\auth\\splunk.secret" $forwarder_service = 'SplunkForwarder' + $forwarder_service_ensure = 'running' + $forwarder_service_enable = 'delayed' $forwarder_service_file = "${forwarder_homedir}\\dummy" # Not used in Windows, but attribute must be defined with a valid path $forwarder_confdir = "${forwarder_homedir}\\etc" $enterprise_src_subdir = 'windows' diff --git a/spec/acceptance/splunk_forwarder_spec.rb b/spec/acceptance/splunk_forwarder_spec.rb index 6a539b46..570851f4 100644 --- a/spec/acceptance/splunk_forwarder_spec.rb +++ b/spec/acceptance/splunk_forwarder_spec.rb @@ -82,6 +82,32 @@ class { 'splunk::forwarder': end end + context 'stopped' do + # Using puppet_apply as a helper + it 'works idempotently with no errors' do + pp = <<-EOS + class { 'splunk::params': + } + class { 'splunk::forwarder': + splunkd_port => 8090, + service_ensure => 'stopped', + } + EOS + + # Run it twice and test for idempotency + apply_manifest(pp, catch_failures: true) + apply_manifest(pp, catch_changes: true) + end + + describe package('splunkforwarder') do + it { is_expected.to be_installed } + end + + describe service(service_name) do + it { is_expected.to be_stopped } + end + end + context 'purging' do context 'purge_outputs => false' do it 'works idempotently with no errors' do diff --git a/spec/classes/forwarder_spec.rb b/spec/classes/forwarder_spec.rb index f1f9cd0a..e7c5a416 100644 --- a/spec/classes/forwarder_spec.rb +++ b/spec/classes/forwarder_spec.rb @@ -33,6 +33,7 @@ it { is_expected.to contain_file('C:\\Program Files\\SplunkUniversalForwarder/etc/system/local/props.conf') } it { is_expected.to contain_file('C:\\Program Files\\SplunkUniversalForwarder/etc/system/local/transforms.conf') } it { is_expected.to contain_file('C:\\Program Files\\SplunkUniversalForwarder/etc/system/local/web.conf') } + it { is_expected.to contain_service('SplunkForwarder').with(ensure: 'running', enable: 'delayed', status: nil, restart: nil, start: nil, stop: nil) } it { is_expected.to contain_file('C:\\Program Files\\SplunkUniversalForwarder/etc/system/local/server.conf') } it { is_expected.not_to contain_file('C:\\Program Files\\SplunkUniversalForwarder/etc/auth/splunk.secret') } it { is_expected.not_to contain_file('C:\\Program Files\\SplunkUniversalForwarder/etc/passwd') } @@ -77,6 +78,29 @@ end end + context 'when service_ensure = stopped' do + let(:params) { { 'service_ensure' => 'stopped' } } + + case facts[:kernel] + when 'windows' + it { is_expected.to contain_service('SplunkForwarder').with(ensure: 'stopped', enable: 'delayed', status: nil, restart: nil, start: nil, stop: nil) } + when 'FreeBSD' + it { is_expected.to contain_service('splunk').with(ensure: 'stopped', enable: 'true', status: nil, restart: nil, start: nil, stop: nil) } + else + it { is_expected.to contain_service('SplunkForwarder').with(ensure: 'stopped', enable: 'true', status: nil, restart: nil, start: nil, stop: nil) } + end + end + + context 'when service_enable = false' do + let(:params) { { 'service_enable' => 'false' } } + + if facts[:kernel] == 'FreeBSD' + it { is_expected.to contain_service('splunk').with(ensure: 'running', enable: false, status: nil, restart: nil, start: nil, stop: nil) } + else + it { is_expected.to contain_service('SplunkForwarder').with(ensure: 'running', enable: false, status: nil, restart: nil, start: nil, stop: nil) } + end + end + context 'with $boot_start = true (defaults)' do if facts[:kernel] == 'Linux' || facts[:kernel] == 'SunOS' From 1f8dee55c6fc57d84cb6148e17f707078aff28b2 Mon Sep 17 00:00:00 2001 From: Cees-Jan Swart Date: Wed, 11 Dec 2024 16:27:25 +0100 Subject: [PATCH 2/7] Fix rspec error Provider redhat must have features 'delayed_startable' --- manifests/forwarder/service.pp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/manifests/forwarder/service.pp b/manifests/forwarder/service.pp index 68a03205..86829c77 100644 --- a/manifests/forwarder/service.pp +++ b/manifests/forwarder/service.pp @@ -4,10 +4,15 @@ # sub-classes # class splunk::forwarder::service { + $provider = $facts['os']['family'] ? { + 'windows' => 'windows', + default => 'redhat', + } service { $splunk::forwarder::service_name: ensure => $splunk::forwarder::service_ensure, enable => $splunk::forwarder::service_enable, hasstatus => true, hasrestart => true, + provider => $provider, } } From d5a84329f299b88534d1dd7870d96d0aca3a3a46 Mon Sep 17 00:00:00 2001 From: Cees-Jan Swart Date: Wed, 11 Dec 2024 16:43:45 +0100 Subject: [PATCH 3/7] fix beaker service provider --- manifests/forwarder/service.pp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifests/forwarder/service.pp b/manifests/forwarder/service.pp index 86829c77..14a41ace 100644 --- a/manifests/forwarder/service.pp +++ b/manifests/forwarder/service.pp @@ -6,7 +6,7 @@ class splunk::forwarder::service { $provider = $facts['os']['family'] ? { 'windows' => 'windows', - default => 'redhat', + default => undef, } service { $splunk::forwarder::service_name: ensure => $splunk::forwarder::service_ensure, From 69f60ed846dfbdb4759182cc1b9a29b192c9cfee Mon Sep 17 00:00:00 2001 From: Cees-Jan Swart Date: Fri, 13 Dec 2024 09:09:04 +0100 Subject: [PATCH 4/7] fix review comments removed the provider selector update type validation service_ensure removed service_ensure from params.pp --- manifests/forwarder.pp | 2 +- manifests/forwarder/service.pp | 5 ----- manifests/params.pp | 4 ---- 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/manifests/forwarder.pp b/manifests/forwarder.pp index 29a28d3d..120eef51 100644 --- a/manifests/forwarder.pp +++ b/manifests/forwarder.pp @@ -178,7 +178,7 @@ Stdlib::Absolutepath $forwarder_homedir = $splunk::params::forwarder_homedir, Stdlib::Absolutepath $forwarder_confdir = $splunk::params::forwarder_confdir, String[1] $service_name = $splunk::params::forwarder_service, - String[1] $service_ensure = $splunk::params::forwarder_service_ensure, + Stdlib::Ensure::Service $service_ensure = running, String[1] $service_enable = $splunk::params::forwarder_service_enable, Stdlib::Absolutepath $service_file = $splunk::params::forwarder_service_file, Boolean $boot_start = $splunk::params::boot_start, diff --git a/manifests/forwarder/service.pp b/manifests/forwarder/service.pp index 14a41ace..68a03205 100644 --- a/manifests/forwarder/service.pp +++ b/manifests/forwarder/service.pp @@ -4,15 +4,10 @@ # sub-classes # class splunk::forwarder::service { - $provider = $facts['os']['family'] ? { - 'windows' => 'windows', - default => undef, - } service { $splunk::forwarder::service_name: ensure => $splunk::forwarder::service_ensure, enable => $splunk::forwarder::service_enable, hasstatus => true, hasrestart => true, - provider => $provider, } } diff --git a/manifests/params.pp b/manifests/params.pp index 3d0cf5a5..a10d7819 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -166,7 +166,6 @@ $enterprise_confdir = "${enterprise_homedir}/etc" $forwarder_install_options = [] $enterprise_install_options = [] - $forwarder_service_ensure = 'running' $forwarder_service_enable = 'true' # Systemd not supported until Splunk 7.2.2 if $facts['service_provider'] == 'systemd' and versioncmp($version, '7.2.2') >= 0 { @@ -200,7 +199,6 @@ $enterprise_confdir = "${enterprise_homedir}/etc" $forwarder_install_options = [] $enterprise_install_options = [] - $forwarder_service_ensure = 'running' $forwarder_service_enable = 'true' # Systemd not supported until Splunk 7.2.2 if $facts['service_provider'] == 'systemd' and versioncmp($version, '7.2.2') >= 0 { @@ -236,7 +234,6 @@ $enterprise_install_options = [] $enterprise_service = 'splunk' $forwarder_service = 'splunk' - $forwarder_service_ensure = 'running' $forwarder_service_enable = 'true' $enterprise_service_file = '/etc/rc.d/splunk' $forwarder_service_file = '/etc/rc.d/splunk' @@ -253,7 +250,6 @@ $forwarder_secret_file = "${forwarder_homedir}\\etc\\auth\\splunk.secret" $enterprise_secret_file = "${enterprise_homedir}\\etc\\auth\\splunk.secret" $forwarder_service = 'SplunkForwarder' - $forwarder_service_ensure = 'running' $forwarder_service_enable = 'delayed' $forwarder_service_file = "${forwarder_homedir}\\dummy" # Not used in Windows, but attribute must be defined with a valid path $forwarder_confdir = "${forwarder_homedir}\\etc" From 7fd428194ce4ada5d109647cb943f6eb927a6747 Mon Sep 17 00:00:00 2001 From: Cees-Jan Swart Date: Fri, 13 Dec 2024 09:15:49 +0100 Subject: [PATCH 5/7] regenerate REFERENCE.md --- REFERENCE.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index cdbf1a11..2b9484fa 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -1030,11 +1030,11 @@ Default value: `$splunk::params::forwarder_service` ##### `service_ensure` -Data type: `String[1]` +Data type: `Stdlib::Ensure::Service` Ensure passed to the splunk service resource. -Default value: `$splunk::params::forwarder_service_ensure` +Default value: `running` ##### `service_enable` From 62875457213c9e3648b46c3cd9e7cd04fc704c26 Mon Sep 17 00:00:00 2001 From: Cees-Jan Swart Date: Fri, 13 Dec 2024 14:35:11 +0100 Subject: [PATCH 6/7] Fix the rspec provider issue for windows --- manifests/forwarder/service.pp | 1 + 1 file changed, 1 insertion(+) diff --git a/manifests/forwarder/service.pp b/manifests/forwarder/service.pp index 68a03205..db5bbe5f 100644 --- a/manifests/forwarder/service.pp +++ b/manifests/forwarder/service.pp @@ -9,5 +9,6 @@ enable => $splunk::forwarder::service_enable, hasstatus => true, hasrestart => true, + provider => $facts['service_provider'], } } From 2886a05797ac0216c02f370b3a6f28194972e34a Mon Sep 17 00:00:00 2001 From: Cees-Jan Swart Date: Sun, 15 Dec 2024 16:47:45 +0100 Subject: [PATCH 7/7] back to original acceptance test from master --- spec/acceptance/splunk_forwarder_spec.rb | 26 ------------------------ 1 file changed, 26 deletions(-) diff --git a/spec/acceptance/splunk_forwarder_spec.rb b/spec/acceptance/splunk_forwarder_spec.rb index 570851f4..6a539b46 100644 --- a/spec/acceptance/splunk_forwarder_spec.rb +++ b/spec/acceptance/splunk_forwarder_spec.rb @@ -82,32 +82,6 @@ class { 'splunk::forwarder': end end - context 'stopped' do - # Using puppet_apply as a helper - it 'works idempotently with no errors' do - pp = <<-EOS - class { 'splunk::params': - } - class { 'splunk::forwarder': - splunkd_port => 8090, - service_ensure => 'stopped', - } - EOS - - # Run it twice and test for idempotency - apply_manifest(pp, catch_failures: true) - apply_manifest(pp, catch_changes: true) - end - - describe package('splunkforwarder') do - it { is_expected.to be_installed } - end - - describe service(service_name) do - it { is_expected.to be_stopped } - end - end - context 'purging' do context 'purge_outputs => false' do it 'works idempotently with no errors' do