From e465a60592a91206fe9e90ed2f12161b3a161c27 Mon Sep 17 00:00:00 2001 From: Leo Antunes Date: Wed, 24 Jan 2018 18:18:09 +0100 Subject: [PATCH] add envvars support to daemon --- manifests/daemon.pp | 45 ++++++++++++++++++++++-------------- manifests/params.pp | 9 ++++++++ spec/defines/daemon_spec.rb | 25 +++++++++++++++++++- templates/daemon.env.erb | 3 +++ templates/daemon.launchd.erb | 7 ++++++ templates/daemon.systemd.erb | 1 + 6 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 templates/daemon.env.erb diff --git a/manifests/daemon.pp b/manifests/daemon.pp index a9d01b85c..8e2f6364d 100644 --- a/manifests/daemon.pp +++ b/manifests/daemon.pp @@ -60,22 +60,24 @@ $user, $group, - $install_method = $::prometheus::params::install_method, - $download_extension = $::prometheus::params::download_extension, - $os = $::prometheus::params::os, - $arch = $::prometheus::params::arch, - $bin_dir = $::prometheus::params::bin_dir, - $package_name = undef, - $package_ensure = 'installed', - $manage_user = true, - $extra_groups = [], - $manage_group = true, - $purge = true, - $options = '', - $init_style = $::prometheus::params::init_style, - $service_ensure = 'running', - $service_enable = true, - $manage_service = true, + $install_method = $::prometheus::params::install_method, + $download_extension = $::prometheus::params::download_extension, + $os = $::prometheus::params::os, + $arch = $::prometheus::params::arch, + $bin_dir = $::prometheus::params::bin_dir, + $package_name = undef, + $package_ensure = 'installed', + $manage_user = true, + $extra_groups = [], + $manage_group = true, + $purge = true, + $options = '', + $init_style = $::prometheus::params::init_style, + $service_ensure = 'running', + $service_enable = true, + $manage_service = true, + Hash[String, Scalar] $env_vars = {}, + Optional[String] $env_file_path = $::prometheus::params::env_file_path, ) { case $install_method { @@ -149,7 +151,6 @@ if $init_style { - case $init_style { 'upstart' : { file { "/etc/init/${name}.conf": @@ -216,6 +217,16 @@ } } + if $env_file_path != undef { + file { "${env_file_path}/${name}": + mode => '0644', + owner => 'root', + group => 'root', + content => template('prometheus/daemon.env.erb'), + notify => $notify_service, + } + } + $init_selector = $init_style ? { 'launchd' => "io.${name}.daemon", default => $name, diff --git a/manifests/params.pp b/manifests/params.pp index a8f400e16..420cc4832 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -192,6 +192,7 @@ $os = downcase($::kernel) if $::operatingsystem == 'Ubuntu' { + $env_file_path = '/etc/default' if versioncmp($::operatingsystemrelease, '8.04') < 1 { $init_style = 'debian' } elsif versioncmp($::operatingsystemrelease, '15.04') < 0 { @@ -200,36 +201,44 @@ $init_style = 'systemd' } } elsif $::operatingsystem =~ /Scientific|CentOS|RedHat|OracleLinux/ { + $env_file_path = '/etc/sysconfig' if versioncmp($::operatingsystemrelease, '7.0') < 0 { $init_style = 'sysv' } else { $init_style = 'systemd' } } elsif $::operatingsystem == 'Fedora' { + $env_file_path = '/etc/sysconfig' if versioncmp($::operatingsystemrelease, '12') < 0 { $init_style = 'sysv' } else { $init_style = 'systemd' } } elsif $::operatingsystem == 'Debian' { + $env_file_path = '/etc/default' if versioncmp($::operatingsystemrelease, '8.0') < 0 { $init_style = 'debian' } else { $init_style = 'systemd' } } elsif $::operatingsystem == 'Archlinux' { + $env_file_path = '/etc/default' $init_style = 'systemd' } elsif $::operatingsystem == 'OpenSuSE' { + $env_file_path = '/etc/sysconfig' $init_style = 'systemd' } elsif $::operatingsystem =~ /SLE[SD]/ { + $env_file_path = '/etc/sysconfig' if versioncmp($::operatingsystemrelease, '12.0') < 0 { $init_style = 'sles' } else { $init_style = 'systemd' } } elsif $::operatingsystem == 'Darwin' { + $env_file_path = undef $init_style = 'launchd' } elsif $::operatingsystem == 'Amazon' { + $env_file_path = '/etc/sysconfig' $init_style = 'sysv' } else { $init_style = undef diff --git a/spec/defines/daemon_spec.rb b/spec/defines/daemon_spec.rb index 462fea2cd..a4c306a1a 100644 --- a/spec/defines/daemon_spec.rb +++ b/spec/defines/daemon_spec.rb @@ -21,7 +21,8 @@ real_download_url: 'https://github.com/prometheus/smurf_exporter/releases/v1.2.3/smurf_exporter-1.2.3.any.tar.gz', notify_service: 'Service[smurf_exporter]', user: 'smurf_user', - group: 'smurf_group' + group: 'smurf_group', + env_vars: { SOMEVAR: 42 } } ].each do |parameters| context "with parameters #{parameters}" do @@ -145,6 +146,28 @@ } end + if ['debian-7-x86_64', 'ubuntu-14.04-x86_64', 'debian-8-x86_64', 'ubuntu-16.04-x86_64'].include?(os) + it { + is_expected.to contain_file('/etc/default/smurf_exporter').with( + 'mode' => '0644', + 'owner' => 'root', + 'group' => 'root' + ).with_content( + %r{SOMEVAR=42\n} + ) + } + elsif ['centos-6-x86_64', 'redhat-6-x86_64', 'centos-7-x86_64', 'redhat-7-x86_64'].include?(os) + it { + is_expected.to contain_file('/etc/sysconfig/smurf_exporter').with( + 'mode' => '0644', + 'owner' => 'root', + 'group' => 'root' + ).with_content( + %r{SOMEVAR=42\n} + ) + } + end + it { is_expected.to contain_service('smurf_exporter').with( 'ensure' => 'running', diff --git a/templates/daemon.env.erb b/templates/daemon.env.erb new file mode 100644 index 000000000..765629503 --- /dev/null +++ b/templates/daemon.env.erb @@ -0,0 +1,3 @@ +<% @env_vars.each do |key, value| -%> +<%= key %>=<%= value %> +<% end -%> \ No newline at end of file diff --git a/templates/daemon.launchd.erb b/templates/daemon.launchd.erb index bfae936cc..2956486f5 100644 --- a/templates/daemon.launchd.erb +++ b/templates/daemon.launchd.erb @@ -12,6 +12,13 @@ <% end %> RunAtLoad KeepAlive + EnvironmentVariables + +<% @env_vars.each do |key, value| -%> + <%= key %> + <%= value %> +<% end -%> + ProgramArguments <%= @bin_dir %>/<%= @name %> diff --git a/templates/daemon.systemd.erb b/templates/daemon.systemd.erb index 8a628503e..4e7ffc8f2 100644 --- a/templates/daemon.systemd.erb +++ b/templates/daemon.systemd.erb @@ -6,6 +6,7 @@ After=basic.target network.target [Service] User=<%= @user %> Group=<%= @group %> +EnvironmentFile=<%= @env_file_path %>/<%= @name%> <%- require 'shellwords' -%> ExecStart=<%= @bin_dir %>/<%= @name %><% for option in Shellwords.split(@options) %> \ <%= option -%>