diff --git a/manifests/alertmanager.pp b/manifests/alertmanager.pp index 63c88b02d..6fac59bc0 100644 --- a/manifests/alertmanager.pp +++ b/manifests/alertmanager.pp @@ -111,6 +111,7 @@ Stdlib::Ensure::Service $service_ensure = 'running', String[1] $service_name = 'alertmanager', Boolean $restart_on_change = true, + Boolean $reload_on_change = false, Boolean $purge_config_dir = true, Boolean $manage_config = true, Prometheus::Initstyle $init_style = $facts['service_provider'], @@ -138,6 +139,25 @@ default => undef, } + $alertmanager_reload = $prometheus::init_style ? { + 'systemd' => "systemctl reload-or-restart ${service_name}", + /^(upstart|none)$/ => "service ${service_name} reload", + /^(sysv|redhat|sles|debian)$/ => "/etc/init.d/${service_name} reload", + 'launchd' => "launchctl stop ${service_name} && launchctl start ${service_name}", + } + + exec { 'alertmanager-reload': + command => $alertmanager_reload, + path => [ '/usr/bin', '/bin', '/usr/sbin', '/sbin' ], + refreshonly => true, + } + + if $reload_on_change { + $_notify_service = Exec['alertmanager-reload'] + } else { + $_notify_service = $notify_service + } + file { $config_dir: ensure => 'directory', owner => 'root', @@ -160,7 +180,7 @@ group => $group, mode => $config_mode, content => template('prometheus/alertmanager.yaml.erb'), - notify => $notify_service, + notify => $_notify_service, require => File["${bin_dir}/amtool", $config_dir], validate_cmd => "${bin_dir}/amtool check-config --alertmanager.url='' %", } @@ -174,7 +194,7 @@ group => $group, mode => $config_mode, content => template('prometheus/alertmanager.yaml.erb'), - notify => $notify_service, + notify => $_notify_service, require => File[$config_dir], } } diff --git a/spec/classes/alertmanager_spec.rb b/spec/classes/alertmanager_spec.rb index 9a7c6561c..22416e989 100644 --- a/spec/classes/alertmanager_spec.rb +++ b/spec/classes/alertmanager_spec.rb @@ -32,10 +32,27 @@ end describe 'config file contents' do it { - is_expected.to contain_file('/etc/alertmanager/alertmanager.yaml') + is_expected.to contain_file('/etc/alertmanager/alertmanager.yaml').with_notify('Service[alertmanager]') verify_contents(catalogue, '/etc/alertmanager/alertmanager.yaml', ['---', 'global:', ' smtp_smarthost: localhost:25', ' smtp_from: alertmanager@localhost']) } end + describe 'service reload' do + it { + is_expected.to contain_exec('alertmanager-reload').with( + # 'command' => 'systemctl reload alertmanager', + 'path' => ['/usr/bin', '/bin', '/usr/sbin', '/sbin'], + 'refreshonly' => true + ) + } + end + end + + context 'when reload_on_change => true' do + let(:params) { { reload_on_change: true } } + + it { + is_expected.to contain_file('/etc/alertmanager/alertmanager.yaml').with_notify('Exec[alertmanager-reload]') + } end context 'with manage_config => false' do