diff --git a/manifests/blackbox_exporter.pp b/manifests/blackbox_exporter.pp index 2a5cc41d8..c85fa434d 100644 --- a/manifests/blackbox_exporter.pp +++ b/manifests/blackbox_exporter.pp @@ -83,6 +83,7 @@ Boolean $manage_user = true, String[1] $os = downcase($facts['kernel']), String $extra_options = '', + Hash[String, Scalar] $env_vars = {}, Optional[String] $download_url = undef, String[1] $config_mode = $prometheus::config_mode, String[1] $arch = $prometheus::real_arch, @@ -136,6 +137,7 @@ manage_group => $manage_group, options => $options, init_style => $init_style, + env_vars => $env_vars, service_ensure => $service_ensure, service_enable => $service_enable, manage_service => $manage_service, diff --git a/manifests/config.pp b/manifests/config.pp index c95fd8a8b..f20088edc 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -158,6 +158,15 @@ default => undef, } + if $prometheus::env_file_path { + file { "${prometheus::env_file_path}/prometheus": + mode => '0644', + owner => 'root', + group => '0', # Darwin uses wheel + content => "ARGS='${join(sort($daemon_flags), ' ')}'\n", + } + } + case $prometheus::server::init_style { # lint:ignore:case_without_default 'upstart': { file { '/etc/init/prometheus.conf': @@ -182,9 +191,10 @@ content => epp("${module_name}/prometheus.systemd.epp", { 'user' => $prometheus::server::user, 'group' => $prometheus::server::group, - 'daemon_flags' => $daemon_flags, + 'daemon_flags' => $daemon_flags.sort, 'max_open_files' => $max_open_files, 'bin_dir' => $prometheus::server::bin_dir, + 'env_file_path' => $prometheus::server::env_file_path, }), notify => $notify, } diff --git a/manifests/postfix_exporter.pp b/manifests/postfix_exporter.pp index 7ec06412a..ebf819b52 100644 --- a/manifests/postfix_exporter.pp +++ b/manifests/postfix_exporter.pp @@ -79,6 +79,7 @@ # exporter configuration String $extra_options = '--systemd.enable --systemd.unit=\'postfix.service\' --postfix.logfile_path=\'\'', Boolean $restart_on_change = true, + Hash[String, Scalar] $env_vars = {}, # scrape job configuration Boolean $export_scrape_job = false, @@ -113,6 +114,7 @@ manage_group => $manage_group, options => $extra_options, init_style => $init_style, + env_vars => $env_vars, service_ensure => $service_ensure, service_enable => $service_enable, manage_service => $manage_service, diff --git a/spec/classes/prometheus_spec.rb b/spec/classes/prometheus_spec.rb index fbdae06da..ea167f1d4 100644 --- a/spec/classes/prometheus_spec.rb +++ b/spec/classes/prometheus_spec.rb @@ -115,9 +115,21 @@ } it { - is_expected.to contain_systemd__unit_file('prometheus.service').with( - 'content' => File.read(fixtures('files', "prometheus#{prom_major}.systemd")) - ) + if ['centos-7-x86_64', 'redhat-7-x86_64'].include?(os) + is_expected.to contain_file('/etc/sysconfig/prometheus').with( + 'content' => File.read(fixtures('files', 'prometheus.default')) + ) + is_expected.to contain_systemd__unit_file('prometheus.service').with( + 'content' => File.read(fixtures('files', 'prometheus-redhat.systemd')) + ) + elsif ['debian-8-x86_64', 'debian-9-x86_64', 'ubuntu-16.04-x86_64', 'ubuntu-18.04-x86_64', 'archlinux-4-x86_64'].include?(os) + is_expected.to contain_file('/etc/default/prometheus').with( + 'content' => File.read(fixtures('files', 'prometheus.default')) + ) + is_expected.to contain_systemd__unit_file('prometheus.service').with( + 'content' => File.read(fixtures('files', 'prometheus-debian.systemd')) + ) + end } describe 'max_open_files' do context 'by default' do diff --git a/spec/fixtures/files/prometheus2.systemd b/spec/fixtures/files/prometheus-debian.systemd similarity index 65% rename from spec/fixtures/files/prometheus2.systemd rename to spec/fixtures/files/prometheus-debian.systemd index 796db72d9..5089ce8d0 100644 --- a/spec/fixtures/files/prometheus2.systemd +++ b/spec/fixtures/files/prometheus-debian.systemd @@ -7,12 +7,8 @@ After=basic.target network.target [Service] User=prometheus Group=prometheus -ExecStart=/usr/local/bin/prometheus \ - --config.file=/etc/prometheus/prometheus.yaml \ - --web.console.templates=/usr/local/share/prometheus/consoles \ - --web.console.libraries=/usr/local/share/prometheus/console_libraries \ - --storage.tsdb.path=/var/lib/prometheus \ - --storage.tsdb.retention=360h +EnvironmentFile=/etc/default/prometheus +ExecStart=/usr/local/bin/prometheus $ARGS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=always diff --git a/spec/fixtures/files/prometheus1.systemd b/spec/fixtures/files/prometheus-redhat.systemd similarity index 64% rename from spec/fixtures/files/prometheus1.systemd rename to spec/fixtures/files/prometheus-redhat.systemd index aa1a75653..0424b2a0e 100644 --- a/spec/fixtures/files/prometheus1.systemd +++ b/spec/fixtures/files/prometheus-redhat.systemd @@ -1,3 +1,4 @@ +# THIS FILE IS MANAGED BY PUPPET [Unit] Description=Prometheus Monitoring framework Wants=basic.target @@ -6,13 +7,8 @@ After=basic.target network.target [Service] User=prometheus Group=prometheus -ExecStart=/usr/local/bin/prometheus \ - -config.file=/etc/prometheus/prometheus.yaml \ - -web.console.templates=/usr/local/share/prometheus/consoles \ - -web.console.libraries=/usr/local/share/prometheus/console_libraries \ - -storage.local.path=/var/lib/prometheus \ - -storage.local.retention=360h \ - +EnvironmentFile=/etc/sysconfig/prometheus +ExecStart=/usr/local/bin/prometheus $ARGS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=always diff --git a/spec/fixtures/files/prometheus.default b/spec/fixtures/files/prometheus.default new file mode 100644 index 000000000..e68d2f6f7 --- /dev/null +++ b/spec/fixtures/files/prometheus.default @@ -0,0 +1 @@ +ARGS='--config.file=/etc/prometheus/prometheus.yaml --storage.tsdb.path=/var/lib/prometheus --storage.tsdb.retention=360h --web.console.libraries=/usr/local/share/prometheus/console_libraries --web.console.templates=/usr/local/share/prometheus/consoles' diff --git a/templates/prometheus.systemd.epp b/templates/prometheus.systemd.epp index 75e08998c..b6b09147f 100644 --- a/templates/prometheus.systemd.epp +++ b/templates/prometheus.systemd.epp @@ -3,6 +3,7 @@ Array[String] $daemon_flags, Optional[Integer] $max_open_files, Stdlib::Absolutepath $bin_dir, + Optional[String] $env_file_path, | -%> # THIS FILE IS MANAGED BY PUPPET [Unit] @@ -13,8 +14,13 @@ After=basic.target network.target [Service] User=<%= $user %> Group=<%= $group %> +<% if $env_file_path { -%> +EnvironmentFile=<%= $env_file_path %>/prometheus +ExecStart=<%= $bin_dir %>/prometheus $ARGS +<% } else { -%> ExecStart=<%= $bin_dir %>/prometheus \ <%= $daemon_flags.join(" \\\n ") %> +<% } -%> ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=always