Skip to content

Commit 0e8b14e

Browse files
committed
add flag to allow sensu to purge unmanaged config files
1 parent 1e7fba6 commit 0e8b14e

15 files changed

+143
-36
lines changed

manifests/check.pp

+19-13
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,27 @@
2020
$aggregate = false,
2121
$config = '',
2222
$config_key = $name,
23+
$purge_config = 'false',
2324
) {
2425

26+
# Handler config
27+
case $ensure {
28+
'present': {
29+
$config_present = $config ? {
30+
'' => 'absent',
31+
default => 'present'
32+
}
33+
}
34+
default: {
35+
$config_present = 'absent'
36+
}
37+
}
38+
39+
if $purge_config {
40+
file { "/etc/sensu/conf.d/check_${name}.json": ensure => $ensure, before => sensu_check[$name] }
41+
file { "/etc/sensu/conf.d/${config_key}.json": ensure => $config_present, before => sensu_check_config[$config_key] }
42+
}
43+
2544
sensu_check { $name:
2645
ensure => $ensure,
2746
realname => $name,
@@ -38,19 +57,6 @@
3857
aggregate => $aggregate,
3958
}
4059

41-
# Handler config
42-
case $ensure {
43-
'present': {
44-
$config_present = $config ? {
45-
'' => 'absent',
46-
default => 'present'
47-
}
48-
}
49-
default: {
50-
$config_present = 'absent'
51-
}
52-
}
53-
5460
sensu_check_config { $config_key:
5561
ensure => $config_present,
5662
config => $config,

manifests/client.pp

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@
88
$address = $::ipaddress,
99
$subscriptions = [],
1010
$client_name = $::fqdn,
11-
$enabled = 'true'
11+
$enabled = 'true',
12+
$purge_config = 'false',
1213
) {
1314

1415
$ensure = $enabled ? {
1516
'true' => 'present',
1617
true => 'present',
1718
default => 'absent'
1819
}
20+
21+
if $purge_config {
22+
file { '/etc/sensu/conf.d/client.json': ensure => $ensure }
23+
}
1924

2025
sensu_client_config { $::fqdn:
2126
ensure => $ensure,

manifests/handler.pp

+17-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# Handler specific config
2020
$config = '',
2121
$config_key = $name,
22+
$purge_config = $sensu::purge_config,
2223
) {
2324

2425
if defined(Class['sensu::service::server']) {
@@ -48,17 +49,6 @@
4849
$command_real = $command
4950
}
5051

51-
sensu_handler { $name:
52-
ensure => $ensure,
53-
type => $type,
54-
command => $command_real,
55-
handlers => $handlers,
56-
severities => $severities,
57-
exchange => $exchange,
58-
mutator => $mutator,
59-
notify => $notify_services,
60-
}
61-
6252
# Handler config
6353
case $ensure {
6454
'present': {
@@ -72,6 +62,22 @@
7262
}
7363
}
7464

65+
if $purge_config {
66+
file { "/etc/sensu/conf.d/handler_${name}.json": ensure => $ensure, before => sensu_handler[$name] }
67+
file { "/etc/sensu/conf.d/${config_key}.json": ensure => $config_present, before => sensu_handler_config[$config_key] }
68+
}
69+
70+
sensu_handler { $name:
71+
ensure => $ensure,
72+
type => $type,
73+
command => $command_real,
74+
handlers => $handlers,
75+
severities => $severities,
76+
exchange => $exchange,
77+
mutator => $mutator,
78+
notify => $notify_services,
79+
}
80+
7581
sensu_handler_config { $config_key:
7682
ensure => $config_present,
7783
config => $config,

manifests/init.pp

+10-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
$subscriptions = [],
3131
$client_address = $::ipaddress,
3232
$client_name = $::fqdn,
33-
$plugins = []
33+
$plugins = [],
34+
$purge_config = false,
3435
){
3536

3637
Class['sensu::package'] ->
@@ -61,7 +62,8 @@
6162
class { 'sensu::package':
6263
version => $version,
6364
install_repo => $install_repo,
64-
notify_services => $notify_services
65+
notify_services => $notify_services,
66+
purge_config => $purge_config,
6567
}
6668

6769
class { 'sensu::rabbitmq':
@@ -72,7 +74,8 @@
7274
user => $rabbitmq_user,
7375
password => $rabbitmq_password,
7476
vhost => $rabbitmq_vhost,
75-
notify_services => $notify_services
77+
notify_services => $notify_services,
78+
purge_config => $purge_config,
7679
}
7780

7881
class { 'sensu::server':
@@ -84,7 +87,8 @@
8487
dashboard_port => $dashboard_port,
8588
dashboard_user => $dashboard_user,
8689
dashboard_password => $dashboard_password,
87-
enabled => $server
90+
enabled => $server,
91+
purge_config => $purge_config,
8892
}
8993

9094
class { 'sensu::service::server': enabled => $server }
@@ -93,7 +97,8 @@
9397
address => $client_address,
9498
subscriptions => $subscriptions,
9599
client_name => $client_name,
96-
enabled => $client
100+
enabled => $client,
101+
purge_config => $purge_config,
97102
}
98103

99104
class { 'sensu::service::client': enabled => $client }

manifests/package.pp

+12-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
#
77

88
class sensu::package(
9-
$version = 'latest',
10-
$notify_services = [],
11-
$install_repo = 'true'
9+
$version = 'latest',
10+
$notify_services = [],
11+
$install_repo = 'true',
12+
$purge_config = 'false',
1213
) {
1314

1415
if $install_repo == 'true' or $install_repo == true {
@@ -20,6 +21,14 @@
2021
notify => $notify_services
2122
}
2223

24+
if $purge_config {
25+
file { '/etc/sensu/conf.d':
26+
purge => true,
27+
recurse => true,
28+
force => true,
29+
}
30+
}
31+
2332
file { ['/etc/sensu/plugins', '/etc/sensu/handlers']:
2433
ensure => directory,
2534
mode => '0555',

manifests/rabbitmq.pp

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
$user = 'sensu',
1414
$password = '',
1515
$vhost = '/sensu',
16-
$notify_services = ''
16+
$notify_services = '',
17+
$purge_config = 'false',
1718
) {
1819

1920
if !defined(Sensu_rabbitmq_config[$::fqdn]) {
@@ -66,6 +67,10 @@
6667
}
6768
}
6869

70+
if $purge_config {
71+
file { '/etc/sensu/conf.d/rabbitmq.json': before => sensu_rabbitmq_config[$::fqdn] }
72+
}
73+
6974
sensu_rabbitmq_config { $::fqdn:
7075
port => $port,
7176
host => $host,

manifests/server.pp

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@
1414
$dashboard_port = '8080',
1515
$dashboard_user = 'admin',
1616
$dashboard_password = 'secret',
17-
$enabled = 'false'
17+
$enabled = 'false',
18+
$purge_config = 'false',
1819
) {
19-
20+
2021
$ensure = $enabled ? {
2122
'true' => 'present',
2223
true => 'present',
2324
default => 'absent'
2425
}
2526

27+
if $purge_config {
28+
file { '/etc/sensu/conf.d/redis.json': ensure => $ensure }
29+
file { '/etc/sensu/conf.d/api.json': ensure => $ensure }
30+
file { '/etc/sensu/conf.d/dashboard.json': ensure => $ensure }
31+
}
32+
2633
sensu_redis_config { $::fqdn:
2734
ensure => $ensure,
2835
host => $redis_host,

manifests/subscription.pp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# = Define: sensu::subscription
2+
#
3+
# Defines Sensu subscriptions
4+
#
5+
# == Parameters
6+
#
7+
8+
define sensu::subscription (
9+
$ensure = 'present',
10+
$purge_config = $sensu::purge_config,
11+
) {
12+
13+
if $purge_config {
14+
file { "/etc/sensu/conf.d/subscription_${name}.json": ensure => $ensure, before => sensu_client_subscription[$name] }
15+
}
16+
17+
sensu_client_subscription { $name: ensure => $ensure }
18+
19+
}

spec/classes/sensu_client_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,11 @@
3737
let(:params) { { :enabled => false } }
3838
it { should contain_sensu_client_config('host.domain.com').with_ensure('absent') }
3939
end
40+
41+
context 'purge_configs' do
42+
let(:params) { { :purge_config => true } }
43+
44+
it { should contain_file('/etc/sensu/conf.d/client.json').with_ensure('present') }
45+
end
4046

4147
end

spec/classes/sensu_package_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,15 @@
2525
'notify' => 'Class[Sensu::Service::Server]'
2626
) }
2727
end
28+
29+
context 'purge_configs' do
30+
let(:params) { { :purge_config => true } }
31+
32+
it { should contain_file('/etc/sensu/conf.d/').with(
33+
'purge' => 'true',
34+
'recurse' => 'true',
35+
'force' => 'true'
36+
) }
37+
end
2838

2939
end

spec/classes/sensu_rabbitmq_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
'notify' => 'Class[sensu::service::server]'
5858
) }
5959
end
60+
61+
context 'purge_configs' do
62+
let(:params) { { :purge_config => true } }
63+
64+
it { should contain_file('/etc/sensu/conf.d/rabbitmq.json') }
65+
end
6066

6167
end
6268

spec/classes/sensu_server_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,12 @@
7171
) }
7272
end # setting params
7373

74+
context 'purge_configs' do
75+
let(:params) { { :purge_config => true, :enabled => true } }
76+
77+
it { should contain_file('/etc/sensu/conf.d/redis.json').with_ensure('present') }
78+
it { should contain_file('/etc/sensu/conf.d/api.json').with_ensure('present') }
79+
it { should contain_file('/etc/sensu/conf.d/dashboard.json').with_ensure('present') }
80+
end
81+
7482
end

spec/defines/sensu_check_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,11 @@
6161
it { should contain_sensu_check_config('mycheck').with_ensure('absent') }
6262
end
6363

64+
context 'purge_configs' do
65+
let(:params) { { :command => '/foo/bar', :purge_config => true, :config => { 'foo' => 'bar' } } }
66+
67+
it { should contain_file('/etc/sensu/conf.d/check_mycheck.json').with_ensure('present') }
68+
it { should contain_file('/etc/sensu/conf.d/mycheck.json').with_ensure('present') }
69+
end
70+
6471
end

spec/defines/sensu_handler_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@
6767
let(:params) { { :config => { 'foo' => 'bar' }, :config_key => 'configkey' } }
6868
it { should contain_sensu_handler_config('configkey').with_config({'foo' => 'bar'} ) }
6969
end
70+
71+
context 'purge_configs' do
72+
let(:params) { { :purge_config => true, :config => { 'foo' => 'bar' } } }
73+
74+
it { should contain_file('/etc/sensu/conf.d/handler_myhandler.json').with_ensure('present') }
75+
it { should contain_file('/etc/sensu/conf.d/myhandler.json').with_ensure('present') }
76+
end
77+
7078
end

spec/defines/sensu_subscription_spec.rb

Whitespace-only changes.

0 commit comments

Comments
 (0)