From ddf1468a15142e905d43eb38285d3e8b120b6d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Thu, 10 Aug 2023 13:27:28 -1000 Subject: [PATCH] Make testing and installing more reliable If a pattern database is failing, it is not installed, but on subsequent run of Puppet the pattern database is not tested again and the configuration converge to a state where the expected pattern database is not installed. Rework the pattern database testing and installation so that a failling pattern database fail on each subsequent run of Puppet, so that errors can be spotted more easily. Fixes #42 --- manifests/init.pp | 11 ++------ manifests/parser.pp | 56 +++++++++++++------------------------ spec/defines/parser_spec.rb | 37 ++++++++++-------------- 3 files changed, 38 insertions(+), 66 deletions(-) diff --git a/manifests/init.pp b/manifests/init.pp index 7848c90..8786642 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -36,14 +36,9 @@ 'file', "${base_dir}/var/lib/syslog-ng", { ensure => 'directory' } ) - ensure_resource ( - 'file', "${base_dir}/var/lib/syslog-ng/patterndb", - { - ensure => 'directory', - purge => true, - recurse => true - } - ) + file { "${base_dir}/var/lib/syslog-ng/patterndb": + ensure => 'directory', + } $pdb_dir = "${base_dir}/etc/syslog-ng/patterndb.d" file { $pdb_dir: ensure => directory, diff --git a/manifests/parser.pp b/manifests/parser.pp index 811ddf3..4cd52c0 100644 --- a/manifests/parser.pp +++ b/manifests/parser.pp @@ -15,19 +15,20 @@ $tmp = join($_modules,' --module=') $modules = "--module=${tmp}" } - ensure_resource('file', "${patterndb::pdb_dir}/${name}", { - 'ensure' => 'directory', - 'purge' => true, - 'force' => true, - 'recurse' => true, - }) + + file { "${patterndb::pdb_dir}/${name}": + ensure => 'directory', + force => true, + recurse => true, + purge => true, + } + ensure_resource ('file', "${patterndb::temp_dir}/patterndb", { 'ensure' => 'directory', }) - ensure_resource ('file', "patterndb::file::${name}", { - 'ensure' => 'present', - 'path' => "${patterndb::base_dir}/var/lib/syslog-ng/patterndb/${name}.xml" - }) + + $_test_before_deploy = $test_before_deploy.lest || { $patterndb::test_before_deploy } + exec { "patterndb::merge::${name}": command => "pdbtool merge -r --glob \\*.pdb -D ${patterndb::pdb_dir}/${name} -p ${patterndb::temp_dir}/patterndb/${name}.xml", path => $facts['path'], @@ -35,34 +36,17 @@ refreshonly => true, } - exec { "patterndb::test::${name}": - #command => "/usr/bin/pdbtool --validate test ${::patterndb::temp_dir}/patterndb/${name}.xml $modules", - command => "pdbtool test ${patterndb::temp_dir}/patterndb/${name}.xml ${modules}", - path => $facts['path'], - logoutput => true, - refreshonly => true, + $deploy_command = if $_test_before_deploy { + "pdbtool test ${patterndb::temp_dir}/patterndb/${name}.xml ${modules} && cp ${patterndb::temp_dir}/patterndb/${name}.xml ${patterndb::base_dir}/var/lib/syslog-ng/patterndb/${name}.xml" + } else { + "cp ${patterndb::temp_dir}/patterndb/${name}.xml ${patterndb::base_dir}/var/lib/syslog-ng/patterndb/${name}.xml" } exec { "patterndb::deploy::${name}": - command => "cp ${patterndb::temp_dir}/patterndb/${name}.xml ${patterndb::base_dir}/var/lib/syslog-ng/patterndb/", - logoutput => true, - path => $facts['path'], - refreshonly => true, - } - if $test_before_deploy =~ Undef { - $_test_before_deploy = $patterndb::test_before_deploy - } else { - $_test_before_deploy = $test_before_deploy - } - if $_test_before_deploy { - File["patterndb::file::${name}"] - ~> Exec["patterndb::merge::${name}"] - ~> Exec["patterndb::test::${name}"] - ~> Exec["patterndb::deploy::${name}"] - } else { - File["patterndb::file::${name}"] - ~> Exec["patterndb::merge::${name}"] - # we won't 'pdbtool test' the merged file before deploying - ~> Exec["patterndb::deploy::${name}"] + command => $deploy_command, + path => $facts['path'], + logoutput => true, + onlyif => "[ ! -f ${patterndb::base_dir}/var/lib/syslog-ng/patterndb/${name}.xml -o ${patterndb::temp_dir}/patterndb/${name}.xml -nt ${patterndb::base_dir}/var/lib/syslog-ng/patterndb/${name}.xml ]", + require => Exec["patterndb::merge::${name}"], } } diff --git a/spec/defines/parser_spec.rb b/spec/defines/parser_spec.rb index f847e2d..0c6f6a4 100644 --- a/spec/defines/parser_spec.rb +++ b/spec/defines/parser_spec.rb @@ -17,12 +17,6 @@ context 'Catchall' do it { is_expected.to contain_class('Patterndb') } it { is_expected.to contain_exec('patterndb::merge::default') } - - it { - is_expected.to contain_file('patterndb::file::default').with( - 'ensure' => 'present' - ).that_notifies('Exec[patterndb::merge::default]') - } end context 'Default values (no parameters)' do @@ -33,8 +27,8 @@ end it { - is_expected.to contain_exec('patterndb::test::default').with( - 'command' => %r{patterndb/default\.xml $}m + is_expected.to contain_exec('patterndb::deploy::default').with( + 'command' => 'pdbtool test /tmp/syslog-ng/patterndb/default.xml && cp /tmp/syslog-ng/patterndb/default.xml //var/lib/syslog-ng/patterndb/default.xml' ) } end @@ -47,8 +41,8 @@ end it { - is_expected.to contain_exec('patterndb::test::default').with( - 'command' => %r{patterndb/default\.xml --module=foo --module=bar$}m + is_expected.to contain_exec('patterndb::deploy::default').with( + 'command' => 'pdbtool test /tmp/syslog-ng/patterndb/default.xml --module=foo --module=bar && cp /tmp/syslog-ng/patterndb/default.xml //var/lib/syslog-ng/patterndb/default.xml' ) } end @@ -59,14 +53,14 @@ end it { - is_expected.to contain_exec('patterndb::test::default').with( - 'command' => %r{patterndb/default\.xml $}m + is_expected.to contain_exec('patterndb::deploy::default').with( + 'command' => 'pdbtool test /tmp/syslog-ng/patterndb/default.xml && cp /tmp/syslog-ng/patterndb/default.xml //var/lib/syslog-ng/patterndb/default.xml' ) } it { - is_expected.to contain_exec('patterndb::test::stage1').with( - 'command' => %r{patterndb/stage1\.xml $}m + is_expected.to contain_exec('patterndb::deploy::stage1').with( + 'command' => 'pdbtool test /tmp/syslog-ng/patterndb/stage1.xml && cp /tmp/syslog-ng/patterndb/stage1.xml //var/lib/syslog-ng/patterndb/stage1.xml' ) } end @@ -78,8 +72,8 @@ end it { - is_expected.to contain_exec('patterndb::test::default').with( - 'command' => %r{patterndb/default\.xml --module=foo --module=bar$}m + is_expected.to contain_exec('patterndb::deploy::default').with( + 'command' => 'pdbtool test /tmp/syslog-ng/patterndb/default.xml --module=foo --module=bar && cp /tmp/syslog-ng/patterndb/default.xml //var/lib/syslog-ng/patterndb/default.xml' ) } end @@ -90,20 +84,20 @@ end it { - is_expected.to contain_exec('patterndb::test::default').with( - 'command' => %r{patterndb/default\.xml $}m + is_expected.to contain_exec('patterndb::deploy::default').with( + 'command' => 'pdbtool test /tmp/syslog-ng/patterndb/default.xml && cp /tmp/syslog-ng/patterndb/default.xml //var/lib/syslog-ng/patterndb/default.xml' ) } end - context 'Without test_before_deploy' do + context 'Without deploy' do let :params do { test_before_deploy: false, } end - it { is_expected.to contain_exec('patterndb::merge::default').that_notifies('Exec[patterndb::deploy::default]') } + it { is_expected.to contain_exec('patterndb::deploy::default').with(command: 'cp /tmp/syslog-ng/patterndb/default.xml //var/lib/syslog-ng/patterndb/default.xml') } end context 'With test_before_deploy' do @@ -113,8 +107,7 @@ } end - it { is_expected.to contain_exec('patterndb::merge::default').that_notifies('Exec[patterndb::test::default]') } - it { is_expected.to contain_exec('patterndb::test::default').that_notifies('Exec[patterndb::deploy::default]') } + it { is_expected.to contain_exec('patterndb::deploy::default').with(command: 'pdbtool test /tmp/syslog-ng/patterndb/default.xml && cp /tmp/syslog-ng/patterndb/default.xml //var/lib/syslog-ng/patterndb/default.xml') } end end end