From 0bc7c981235995d03a831e98dea64f3588fae5a1 Mon Sep 17 00:00:00 2001 From: Tim Meusel Date: Sat, 26 Sep 2020 15:42:17 +0200 Subject: [PATCH] Daemon: create env files only if required --- REFERENCE.md | 2 +- manifests/bird_exporter.pp | 4 ++++ manifests/daemon.pp | 6 +++--- spec/classes/apache_exporter_spec.rb | 20 ++++++++++++++++++++ spec/classes/bird_exporter_spec.rb | 19 +++++++++++++++++++ spec/classes/node_exporter_spec.rb | 8 ++++---- templates/daemon.env.erb | 1 + templates/daemon.systemd.erb | 2 ++ 8 files changed, 54 insertions(+), 8 deletions(-) diff --git a/REFERENCE.md b/REFERENCE.md index f7f62da12..7dab8a224 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -8398,7 +8398,7 @@ Default value: `{}` ##### `env_file_path` -Data type: `Optional[String]` +Data type: `Stdlib::Absolutepath` diff --git a/manifests/bird_exporter.pp b/manifests/bird_exporter.pp index f6384e4e8..244a92d44 100644 --- a/manifests/bird_exporter.pp +++ b/manifests/bird_exporter.pp @@ -47,6 +47,8 @@ # User which runs the service # @param version # The binary release version +# @param env_vars +# hash with custom environment variables thats passed to the exporter via init script / unit file # # @see https://github.com/czerwonk/bird_exporter # @@ -82,6 +84,7 @@ String[1] $scrape_job_name = 'bird', Optional[Hash] $scrape_job_labels = undef, Optional[String[1]] $bin_name = undef, + Hash[String[1], Scalar] $env_vars = {}, ) inherits prometheus { $real_download_url = pick($download_url,"${download_url_base}/download/${version}/${package_name}-${version}_${os}_${arch}") @@ -118,5 +121,6 @@ scrape_job_name => $scrape_job_name, scrape_job_labels => $scrape_job_labels, bin_name => $bin_name, + env_vars => $env_vars, } } diff --git a/manifests/daemon.pp b/manifests/daemon.pp index e29261914..4ca67f9b1 100644 --- a/manifests/daemon.pp +++ b/manifests/daemon.pp @@ -68,8 +68,8 @@ Stdlib::Ensure::Service $service_ensure = 'running', Boolean $service_enable = true, Boolean $manage_service = true, - Hash[String, Scalar] $env_vars = {}, - Optional[String] $env_file_path = $prometheus::env_file_path, + Hash[String[1], Scalar] $env_vars = {}, + Stdlib::Absolutepath $env_file_path = $prometheus::env_file_path, Optional[String[1]] $extract_command = $prometheus::extract_command, Stdlib::Absolutepath $extract_path = '/opt', Stdlib::Absolutepath $archive_bin_path = "/opt/${name}-${version}.${os}-${arch}/${name}", @@ -215,7 +215,7 @@ 'none': {} } - if $env_file_path != undef { + unless $env_vars.empty { file { "${env_file_path}/${name}": mode => '0644', owner => 'root', diff --git a/spec/classes/apache_exporter_spec.rb b/spec/classes/apache_exporter_spec.rb index 8fd081c79..c09b757a4 100644 --- a/spec/classes/apache_exporter_spec.rb +++ b/spec/classes/apache_exporter_spec.rb @@ -8,6 +8,24 @@ end context 'with all defaults' do + it { is_expected.to compile.with_all_deps } + if facts[:os]['release']['major'].to_i == 6 + it { is_expected.to contain_file('/etc/init.d/apache_exporter') } + else + it { is_expected.to contain_systemd__unit_file('apache_exporter.service') } + end + + if facts[:os]['name'] == 'Archlinux' + it { is_expected.to contain_package('apache_exporter') } + it { is_expected.not_to contain_archive('/tmp/apache_exporter-0.8.0.tar.gz') } + it { is_expected.not_to contain_file('/opt/apache_exporter-0.8.0.linux-amd64/apache_exporter') } + else + it { is_expected.not_to contain_package('apache-exporter') } + it { is_expected.to contain_archive('/tmp/apache_exporter-0.8.0.tar.gz') } + it { is_expected.to contain_file('/opt/apache_exporter-0.8.0.linux-amd64/apache_exporter') } + end + end + context 'with some params' do let(:params) do { version: '0.5.0', @@ -21,6 +39,7 @@ describe 'with specific params' do it { is_expected.to compile.with_all_deps } it { is_expected.to contain_archive('/tmp/apache_exporter-0.5.0.tar.gz') } + it { is_expected.to contain_file('/opt/apache_exporter-0.5.0.linux-amd64/apache_exporter') } it { is_expected.to contain_class('prometheus') } it { is_expected.to contain_group('apache-exporter') } it { is_expected.to contain_user('apache-exporter') } @@ -48,6 +67,7 @@ describe 'with specific params' do it { is_expected.to compile.with_all_deps } it { is_expected.to contain_archive('/tmp/apache_exporter-0.4.0.tar.gz') } + it { is_expected.to contain_file('/opt/apache_exporter-0.4.0.linux-amd64/apache_exporter') } it { is_expected.to contain_class('prometheus') } it { is_expected.to contain_group('apache-exporter') } it { is_expected.to contain_user('apache-exporter') } diff --git a/spec/classes/bird_exporter_spec.rb b/spec/classes/bird_exporter_spec.rb index 54e151d24..bd6d8da42 100644 --- a/spec/classes/bird_exporter_spec.rb +++ b/spec/classes/bird_exporter_spec.rb @@ -25,10 +25,29 @@ it { is_expected.to contain_systemd__unit_file('bird_exporter.service') } end + if facts[:os]['family'] == 'RedHat' + it { is_expected.not_to contain_file('/etc/sysconfig/bird_exporter') } + else + it { is_expected.not_to contain_file('/etc/default/bird_exporter') } + end + end + + context 'with env vars' do + let :params do + { + env_vars: { + blub: 'foobar' + } + } + end + + it { is_expected.to compile.with_all_deps } if facts[:os]['family'] == 'RedHat' it { is_expected.to contain_file('/etc/sysconfig/bird_exporter') } + it { is_expected.not_to contain_file('/etc/default/bird_exporter') } else it { is_expected.to contain_file('/etc/default/bird_exporter') } + it { is_expected.not_to contain_file('/etc/sysconfig/bird_exporter') } end end end diff --git a/spec/classes/node_exporter_spec.rb b/spec/classes/node_exporter_spec.rb index 11099a0a2..6d142b481 100644 --- a/spec/classes/node_exporter_spec.rb +++ b/spec/classes/node_exporter_spec.rb @@ -19,7 +19,6 @@ it { is_expected.not_to contain_file('/opt/node_exporter-1.0.1.linux-amd64/node_exporter') } it { is_expected.not_to contain_file('/usr/local/bin/node_exporter') } it { is_expected.to contain_package('prometheus-node-exporter') } - it { is_expected.to contain_file('/etc/default/node_exporter') } it { is_expected.to contain_systemd__unit_file('node_exporter.service') } else it { is_expected.to contain_user('node-exporter') } @@ -29,12 +28,13 @@ end if facts[:os]['family'] == 'RedHat' && facts[:os]['release']['major'].to_i < 7 - it { is_expected.to contain_file('/etc/sysconfig/node_exporter') } it { is_expected.to contain_file('/etc/init.d/node_exporter') } end - if facts[:os]['release']['major'].to_i == 14 - it { is_expected.to contain_file('/etc/init/node_exporter.conf') } + if facts[:os]['family'] == 'RedHat' + it { is_expected.not_to contain_file('/etc/sysconfig/bird_exporter') } + else + it { is_expected.not_to contain_file('/etc/default/bird_exporter') } end end diff --git a/templates/daemon.env.erb b/templates/daemon.env.erb index 64e97a9e0..aecf6055f 100644 --- a/templates/daemon.env.erb +++ b/templates/daemon.env.erb @@ -1,3 +1,4 @@ +# THIS FILE IS MANAGED BY PUPPET <% @env_vars.each do |key, value| -%> <%= key %>="<%= value %>" <% end -%> diff --git a/templates/daemon.systemd.erb b/templates/daemon.systemd.erb index 4b726fff7..0bbb235cf 100644 --- a/templates/daemon.systemd.erb +++ b/templates/daemon.systemd.erb @@ -7,7 +7,9 @@ After=basic.target network.target [Service] User=<%= @user %> Group=<%= @group %> +<%- unless @env_vars.empty? -%> EnvironmentFile=<%= @env_file_path %>/<%= @name%> +<% end %> <%- require 'shellwords' -%> ExecStart=<%= @bin_dir %>/<%= @bin_name %><% for option in Shellwords.split(@options) %> \ <%= option -%>