Skip to content

Commit

Permalink
New option python_manage to collectd::plugin::python
Browse files Browse the repository at this point in the history
Particularly on Ubuntu it nescesary to install python explicitly
otherwise the directory `/usr/local/lib/python2.7` does
not exist. Collectd itself only requires python-lib.

The option defaults to `false` and a python package is not
maintained.
  • Loading branch information
traylenator committed Jun 6, 2018
1 parent 5486501 commit 7bcbcea
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,8 @@ class { 'collectd::plugin::protocols':
```

#### Class: `collectd::plugin::python`
The plugin uses a fact `python_dir` to find the python load path for modules.
python must be installed for the this fact to give a non-default value.

* `modulepaths` is an array of paths where will be Collectd looking for Python
modules, Puppet will ensure that each of specified directories exists and it
Expand All @@ -1425,6 +1427,10 @@ class { 'collectd::plugin::protocols':
collectd with the name of the exception and the message (default: `false`)
* `conf_name` name of the file that will contain the python module configuration
(default: `python-config.conf`)
* `python_manage` Should a python package be installed. (default `false`) Python
is needed for the fact `python_dir` to function.
* `python_package` Name of python package to install. (default `python`)
* `python_package_ensure` Ensure state of python package to install. (default `present`)

See [collectd-python documentation](https://collectd.org/documentation/manpages/collectd-python.5.shtml)
for more details.
Expand Down
34 changes: 22 additions & 12 deletions manifests/plugin/python.pp
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# See http://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_python
class collectd::plugin::python (
# Python 2 defaults to 'ascii' and Python 3 to 'utf-8'
$encoding = undef,
$ensure = 'present',
$encoding = undef,
$ensure = 'present',
# Unlike most other plugins, this one should set "Globals true". This will cause collectd
# to export the name of all objects in the Python interpreter for all plugins to see.
Boolean $globals = true,
Boolean $interactive = false,
$interval = undef,
Boolean $logtraces = false,
$manage_package = undef,
Array $modulepaths = [],
Hash $modules = {},
$order = '10',
$conf_name = 'python-config.conf',
Boolean $globals = true,
Boolean $interactive = false,
$interval = undef,
Boolean $logtraces = false,
$manage_package = undef,
Array $modulepaths = [],
Hash $modules = {},
$order = '10',
$conf_name = 'python-config.conf',
Boolean $manage_python = false,
String $python_package = 'python',
String $python_package_ensure = 'present',
) {

include ::collectd
Expand Down Expand Up @@ -47,6 +50,13 @@
globals => $globals,
}

if $manage_python {
ensure_packages([$python_package], { ensure => $python_package_ensure })
$_module_dir_require = [Package[$collectd::package_name],Package[$python_package]]
} else {
$_module_dir_require = Package[$collectd::package_name]
}

$ensure_modulepath = $ensure ? {
'absent' => $ensure,
default => 'directory',
Expand All @@ -60,7 +70,7 @@
'purge' => $::collectd::purge_config,
'force' => true,
'group' => $collectd::config_group,
'require' => Package[$collectd::package_name]
'require' => $_module_dir_require,
}
)

Expand Down
38 changes: 37 additions & 1 deletion spec/classes/collectd_plugin_python_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

context ':ensure => present' do
context ':ensure => present and default parameters' do
it 'install python package' do
is_expected.not_to contain_package('python')
end
it 'ensures that $modulepaths exits' do
is_expected.to contain_file('/usr/local/lib/python2.7/dist-packages').with(ensure: 'directory')
end
Expand Down Expand Up @@ -48,6 +51,37 @@
)
end
end
context ':ensure => present and python package specified' do
let :params do
{
python_package: 'mypython',
python_package_ensure: '1.2.3',
manage_python: true
}
end

it 'install specified python package' do
is_expected.to contain_package('mypython').with(ensure: '1.2.3')
end
end

context ':ensure => present, moduledirs and manage_python false' do
let :params do
{
modulepaths: ['/tmp/', '/data/'],
manage_python: false
}
end

it 'install specified python package' do
is_expected.not_to contain_package('python')
end
it 'will ensure one directory is here here' do
is_expected.to contain_file('/tmp/').with(
require: 'Package[collectd]'
)
end
end

context ':ensure => present and multiple $modulepaths' do
let :params do
Expand All @@ -57,7 +91,9 @@
end

it 'will ensure the two directories are here' do
is_expected.to contain_file('/tmp/')
is_expected.to contain_file('/tmp/').with(
require: 'Package[collectd]'
)
is_expected.to contain_file('/data/')
end
it 'will set two modulepath in the module conf' do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# Install module and dependencies
puppet_module_install(source: proj_root, module_name: 'collectd')
hosts.each do |host|
on host, puppet('resource', 'package', 'python', 'ensure=installed')
on host, puppet('module', 'install', 'puppetlabs-apt'), acceptable_exit_codes: [0]
on host, puppet('module', 'install', 'puppetlabs-stdlib'), acceptable_exit_codes: [0]
on host, puppet('module', 'install', 'puppetlabs-concat'), acceptable_exit_codes: [0]
Expand Down

0 comments on commit 7bcbcea

Please sign in to comment.