Skip to content

Commit

Permalink
add envvars support to daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
costela committed Jan 24, 2018
1 parent c1da72f commit 146e01f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 18 deletions.
45 changes: 28 additions & 17 deletions manifests/daemon.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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, String] $env_vars = {},
Optional[String] $env_file_path = $::prometheus::params::env_file_path,
) {

case $install_method {
Expand Down Expand Up @@ -149,7 +151,6 @@


if $init_style {

case $init_style {
'upstart' : {
file { "/etc/init/${name}.conf":
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
25 changes: 24 additions & 1 deletion spec/defines/daemon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down
7 changes: 7 additions & 0 deletions templates/daemon.launchd.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
<% end %>
<key>RunAtLoad</key> <true/>
<key>KeepAlive</key> <true/>
<key>EnvironmentVariables</key>
<dict>
<% @env_vars.each do |key, value| -%>
<key><%= key %></key>
<string><%= value %></string>
<% end -%>
</dict>
<key>ProgramArguments</key>
<array>
<string><%= @bin_dir %>/<%= @name %></string>
Expand Down
1 change: 1 addition & 0 deletions templates/daemon.systemd.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%>
Expand Down

0 comments on commit 146e01f

Please sign in to comment.