Skip to content

Commit

Permalink
support definition of multiple exec commands redhat-openstack#286
Browse files Browse the repository at this point in the history
  • Loading branch information
deric committed Jul 22, 2015
1 parent 975e1d5 commit 6aec1f3
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 37 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class { 'collectd::plugin::entropy':
####Class: `collectd::plugin::exec`

```puppet
collectd::plugin::exec {
collectd::plugin::exec::cmd {
'dummy':
user => nobody,
group => nogroup,
Expand Down
59 changes: 34 additions & 25 deletions manifests/plugin/exec.pp
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
# See http://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_exec
define collectd::plugin::exec (
$user,
$group,
$exec = [],
$notification_exec = [],
$ensure = present,
$order = '10',
class collectd::plugin::exec (
$commands = {},
$interval = undef,
$ensure = present,
$globals = false,
) {
include collectd::params

validate_array($exec)
validate_array($notification_exec)
validate_hash($commands)
validate_bool($globals)

$conf_dir = $collectd::params::plugin_conf_dir
collectd::plugin {'exec':
ensure => $ensure,
globals => $globals,
interval => $interval,
}

# should be loaded after global plugin configuration
$exec_conf = "${collectd::params::plugin_conf_dir}/exec-config.conf"

concat{ $exec_conf:
ensure => $ensure,
mode => '0640',
owner => 'root',
group => $collectd::params::root_group,
notify => Service['collectd'],
ensure_newline => true,
}

# This is deprecated file naming ensuring old style file removed, and should be removed in next major relese
file { "${name}.load-deprecated":
ensure => absent,
path => "${conf_dir}/${name}.conf",
concat::fragment{'collectd_plugin_exec_conf_header':
order => '00',
content => '<Plugin exec>',
target => $exec_conf,
}
# End deprecation

file {
"${name}.load":
ensure => $ensure,
path => "${conf_dir}/${order}-${name}.conf",
owner => 'root',
group => $collectd::params::root_group,
mode => '0644',
content => template('collectd/exec.conf.erb'),
notify => Service['collectd'],
concat::fragment{'collectd_plugin_exec_conf_footer':
order => '99',
content => '</Plugin>',
target => $exec_conf,
}

}
create_resources(collectd::plugin::exec::cmd, $commands)
}
29 changes: 29 additions & 0 deletions manifests/plugin/exec/cmd.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
define collectd::plugin::exec::cmd (
$user,
$group,
$exec = [],
$notification_exec = [],
$ensure = present,
) {
include collectd::params
include collectd::plugin::exec

validate_array($exec)
validate_array($notification_exec)

$conf_dir = $collectd::params::plugin_conf_dir

# This is deprecated file naming ensuring old style file removed, and should be removed in next major relese
file { "${name}.load-deprecated":
ensure => absent,
path => "${conf_dir}/${name}.conf",
}
# End deprecation

concat::fragment{"collectd_plugin_exec_conf_${title}":
ensure => $ensure,
order => '50', # somewhere between header and footer
target => $collectd::plugin::exec::exec_conf,
content => template('collectd/plugin/exec/cmd.conf.erb'),
}
}
78 changes: 78 additions & 0 deletions spec/classes/collectd_plugin_exec_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
require 'spec_helper'

describe 'collectd::plugin::exec', :type => :class do

let :facts do
{
:osfamily => 'Debian',
:concat_basedir => tmpfilename('collectd-exec'),
:id => 'root',
:kernel => 'Linux',
:path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
:collectd_version => '5.0'
}
end

context 'single command' do
let :params do
{
:commands => { 'hello' =>
{'user' => 'nobody', 'group' => 'users', 'exec' => ['/bin/echo', 'hello world']}
},
}
end

it 'Will create /etc/collectd.d/conf.d/exec-config.conf' do
should contain_concat__fragment('collectd_plugin_exec_conf_header').with({
:content => /<Plugin exec>/,
:target => '/etc/collectd/conf.d/exec-config.conf',
:order => '00'
})
end

it 'Will create /etc/collectd.d/conf.d/exec-config' do
should contain_concat__fragment('collectd_plugin_exec_conf_footer').with({
:content => /<\/Plugin>/,
:target => '/etc/collectd/conf.d/exec-config.conf',
:order => '99'
})
end

it 'includes exec statement' do
should contain_concat__fragment('collectd_plugin_exec_conf_hello').with({
:content => /Exec \"nobody:users\" \"\/bin\/echo\" \"hello world\"/,
:target => '/etc/collectd/conf.d/exec-config.conf',
})
end
end

context 'multiple commands' do
let :params do
{
:commands => {
'hello' => { 'user' => 'nobody', 'group' => 'users',
'exec' => ['/bin/echo', 'hello world']
},
'my_date' => { 'user' => 'nobody', 'group' => 'users',
'exec' => ['/bin/date']
}
},
}
end

it 'includes echo statement' do
should contain_concat__fragment('collectd_plugin_exec_conf_hello').with({
:content => /Exec \"nobody:users\" \"\/bin\/echo\" \"hello world\"/,
:target => '/etc/collectd/conf.d/exec-config.conf',
})
end

it 'includes date statement' do
should contain_concat__fragment('collectd_plugin_exec_conf_my_date').with({
:content => /Exec \"nobody:users\" \"\/bin\/date\"/,
:target => '/etc/collectd/conf.d/exec-config.conf',
})
end
end

end
31 changes: 31 additions & 0 deletions spec/defines/collectd_plugin_exec_cmd_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'

describe 'collectd::plugin::exec::cmd', :type => :define do
let :facts do
{
:osfamily => 'Debian',
:id => 'root',
:concat_basedir => tmpfilename('collectd-exec'),
:path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
}
end

context 'define a command' do
let(:title) { 'whoami' }
let :params do
{
:user => 'www-data',
:group => 'users',
:exec => ['whoami', '--help']
}
end

it 'executes whoami command' do
should contain_concat__fragment('collectd_plugin_exec_conf_whoami').with({
:content => /Exec/,
:target => '/etc/collectd/conf.d/exec-config.conf',
})
end
end

end
11 changes: 0 additions & 11 deletions templates/exec.conf.erb

This file was deleted.

7 changes: 7 additions & 0 deletions templates/plugin/exec/cmd.conf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% if @exec %>
Exec "<%= @user %>:<%= @group %>" "<%= @exec.join('" "') %>"
<% end %>
<% if !@notification_exec.empty? %>
NotificationExec "<%= @user %>:<%= @group %>" <% @notification_exec.each do |exec| -%>"<%= exec %>"<% end -%>
<% end %>

0 comments on commit 6aec1f3

Please sign in to comment.