Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daemon: create env files only if required #493

Merged
merged 1 commit into from
Sep 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8398,7 +8398,7 @@ Default value: `{}`

##### `env_file_path`

Data type: `Optional[String]`
Data type: `Stdlib::Absolutepath`



Expand Down
4 changes: 4 additions & 0 deletions manifests/bird_exporter.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down Expand Up @@ -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}")

Expand Down Expand Up @@ -118,5 +121,6 @@
scrape_job_name => $scrape_job_name,
scrape_job_labels => $scrape_job_labels,
bin_name => $bin_name,
env_vars => $env_vars,
}
}
6 changes: 3 additions & 3 deletions manifests/daemon.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down Expand Up @@ -215,7 +215,7 @@
'none': {}
}

if $env_file_path != undef {
unless $env_vars.empty {
file { "${env_file_path}/${name}":
mode => '0644',
owner => 'root',
Expand Down
20 changes: 20 additions & 0 deletions spec/classes/apache_exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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') }
Expand Down Expand Up @@ -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') }
Expand Down
19 changes: 19 additions & 0 deletions spec/classes/bird_exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/classes/node_exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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') }
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions templates/daemon.env.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# THIS FILE IS MANAGED BY PUPPET
<% @env_vars.each do |key, value| -%>
<%= key %>="<%= value %>"
<% end -%>
2 changes: 2 additions & 0 deletions templates/daemon.systemd.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%>
Expand Down