diff --git a/README.md b/README.md index 035d4af79..4f2910a88 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/manifests/plugin/python.pp b/manifests/plugin/python.pp index f2c318c94..fb1e4c593 100644 --- a/manifests/plugin/python.pp +++ b/manifests/plugin/python.pp @@ -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 @@ -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', @@ -60,7 +70,7 @@ 'purge' => $::collectd::purge_config, 'force' => true, 'group' => $collectd::config_group, - 'require' => Package[$collectd::package_name] + 'require' => $_module_dir_require, } ) diff --git a/spec/classes/collectd_plugin_python_spec.rb b/spec/classes/collectd_plugin_python_spec.rb index c8f7e464d..8f1e6620e 100644 --- a/spec/classes/collectd_plugin_python_spec.rb +++ b/spec/classes/collectd_plugin_python_spec.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index 3c20041c0..af144efd4 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -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]