Skip to content

Commit

Permalink
Make testing and installing more reliable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
smortex committed Aug 11, 2023
1 parent a8153d6 commit 2bed001
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 66 deletions.
11 changes: 3 additions & 8 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
56 changes: 20 additions & 36 deletions manifests/parser.pp
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,38 @@
$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'],
logoutput => true,
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}"],
}
}
37 changes: 15 additions & 22 deletions spec/defines/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2bed001

Please sign in to comment.