From c204077a60f1571bca864c39c1485044dd9e91f4 Mon Sep 17 00:00:00 2001 From: Martin Magr Date: Fri, 25 Sep 2020 15:50:33 +0200 Subject: [PATCH] Add posibility to download scripts for sensubility This patch adds parameters for class collectd::sensubility enabling download of various scripts for usage within sensubility check definitions Change-Id: Ifb1046d710827a82dcecbfbb2a449b1f87bbe363 --- .../base/metrics/collectd/sensubility.pp | 63 +++++++++++++++--- .../metrics/collectd/sensubility_script.pp | 64 +++++++++++++++++++ .../sensubility-scripts-994014edfcc2da88.yaml | 6 ++ ..._base_metrics_collectd_sensubility_spec.rb | 33 ++++++++++ ...pleo_profile_base_metrics_collectd_spec.rb | 1 + ...etrics_collectd_sensubility_script_spec.rb | 45 +++++++++++++ 6 files changed, 204 insertions(+), 8 deletions(-) create mode 100644 manifests/profile/base/metrics/collectd/sensubility_script.pp create mode 100644 releasenotes/notes/sensubility-scripts-994014edfcc2da88.yaml create mode 100644 spec/defines/tripleo_profile_base_metrics_collectd_sensubility_script_spec.rb diff --git a/manifests/profile/base/metrics/collectd/sensubility.pp b/manifests/profile/base/metrics/collectd/sensubility.pp index 158c2c7b1..7d248ce74 100644 --- a/manifests/profile/base/metrics/collectd/sensubility.pp +++ b/manifests/profile/base/metrics/collectd/sensubility.pp @@ -55,11 +55,6 @@ # (Optional) Integer. Interval in seconds for sending keepalive messages to Sensu server side (By default 20). # Defaults to undef. # -# [*tmp_base_dir*] -# (Optional) String. Path to temporary directory which is used for creation of check scripts -# (by default /var/tmp/collectd-sensubility-checks). -# Defaults to undef. -# # [*shell_path*] # (Optional) String. Path to shell used for executing check scripts (by default /usr/bin/sh). # Defaults to undef. @@ -117,6 +112,26 @@ # [*transport*] # String. Bus type for message transport. Options are 'sensu' (rabbitmq) or 'amqp1' # Defaults to 'sensu' +# +# [*workdir*] +# (Optional) String. Working directory for sensubility. This directory will contain +# temporary check scripts (in checks subdirectory) and downloaded scripts (in scripts subdirectory). +# Defaults to '/var/lib/collectd-sensubility' +# +# [*scripts*] +# (Optional) Hash. Should contain information about what scripts should be downloaded. The item format is following: +# { "script-name" => +# "source" => "http://uri.from.where.to.download/script-name", +# "checksum" => "checksum-of-the-script", +# "create_bin_link" => true/false # whether to create link to /usr/bin +# } +# Defaults to {} +# +# DEPRECATED PARAMETERS +# +# [*tmp_base_dir*] +# (Optional) String. DEPRECATED, use "workdir" parameter instead. +# class tripleo::profile::base::metrics::collectd::sensubility ( $ensure = 'present', $config_path = '/etc/collectd-sensubility.conf', @@ -127,7 +142,6 @@ class tripleo::profile::base::metrics::collectd::sensubility ( $client_name = undef, $client_address = undef, $keepalive_interval = undef, - $tmp_base_dir = undef, $shell_path = undef, $worker_count = undef, $checks = undef, @@ -140,7 +154,11 @@ class tripleo::profile::base::metrics::collectd::sensubility ( $exec_sudo_rule = undef, $results_format = 'smartgateway', $results_channel = undef, - $transport = 'sensu' + $transport = 'sensu', + $workdir = '/var/lib/collectd-sensubility', + $scripts = {}, + # DEPRECATED + $tmp_base_dir = undef, ) { include collectd include collectd::plugin::exec @@ -149,6 +167,21 @@ class tripleo::profile::base::metrics::collectd::sensubility ( ensure => $ensure, } + if $tmp_base_dir { + warning('The "tmp_base_dir" parameter is deprecated and might be ignored in future releases. Use "workdir" instead.') + $checkdir = $tmp_base_dir + } else { + $checkdir = "${workdir}/checks" + } + $scriptsdir = "${workdir}/scripts" + + file { [$workdir, $checkdir, $scriptsdir]: + ensure => 'directory', + mode => '0700', + owner => $exec_user, + group => $exec_group + } + file { $config_path: ensure => $ensure, mode => '0644', @@ -160,7 +193,7 @@ class tripleo::profile::base::metrics::collectd::sensubility ( client_name => $client_name, client_address => $client_address, keepalive_interval => $keepalive_interval, - tmp_base_dir => $tmp_base_dir, + tmp_base_dir => $checkdir, shell_path => $shell_path, worker_count => $worker_count, checks => inline_template('<%= @checks.to_json %>'), @@ -196,4 +229,18 @@ class tripleo::profile::base::metrics::collectd::sensubility ( } } + $scripts.each |$name, $data| { + tripleo::profile::base::metrics::collectd::sensubility_script { $name: + checksum => $data['checksum'], + source => $data['source'], + user => $exec_user, + group => $exec_group, + scriptsdir => $scriptsdir, + create_bin_link => has_key($data, 'create_bin_link') ? { + true => $data['create_bin_link'], + default => true + } + } + } + } diff --git a/manifests/profile/base/metrics/collectd/sensubility_script.pp b/manifests/profile/base/metrics/collectd/sensubility_script.pp new file mode 100644 index 000000000..b9cd476c6 --- /dev/null +++ b/manifests/profile/base/metrics/collectd/sensubility_script.pp @@ -0,0 +1,64 @@ +# +# == Define: tripleo::profile::base::metrics::collectd::sensubility_script +# +# This is used to download third party script for sensubility check usage. The +# +# === Parameters +# [*source*] +# URI from where the file should be downloaded (only http:// is supported currently) +# +# [*scriptsdir*] +# Directory where all downloaded scripts reside. +# +# [*scriptname*] +# (optional) Name of script under which it will be saved. +# Defaults to $title +# +# [*checksum*] +# (optional) The checksum of the source contents. Only md5, sha256, sha224, +# sha384 and sha512 are supported when specifying this parameter. +# Defaults to undef +# +# [*user*] +# (optional) Owner of script directory and script files. +# Defaults to 'collectd' +# +# [*group*] +# (optional) Group of script directory and script files. +# Defaults to 'collectd' +# +# [*create_bin_link*] +# (optional) Whether the script should be linked to /usr/bin/sensubility_. +# Defaults to true +# +# [*bindir*] +# (optional) Which bin folder exactly should be used for links. +# Defaults to '/usr/bin' +# +define tripleo::profile::base::metrics::collectd::sensubility_script ( + $source, + $scriptsdir, + $scriptname = $title, + $checksum = undef, + $user = 'collectd', + $group = 'collectd', + $create_bin_link = true, + $bindir = '/usr/bin', +) { + file { "${scriptsdir}/${scriptname}": + ensure => 'present', + source => $source, + checksum_value => $checksum, + checksum => 'md5', + mode => '0700', + owner => $user, + group => $group + } + + if $create_bin_link { + file { "${bindir}/sensubility_${scriptname}": + ensure => 'link', + target => "${scriptsdir}/${scriptname}", + } + } +} diff --git a/releasenotes/notes/sensubility-scripts-994014edfcc2da88.yaml b/releasenotes/notes/sensubility-scripts-994014edfcc2da88.yaml new file mode 100644 index 000000000..8f573988b --- /dev/null +++ b/releasenotes/notes/sensubility-scripts-994014edfcc2da88.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Added "scripts" parameters for class tripleo::profile::base::metrics::collectd::sensubility + enabling download of various scripts for usage within sensubility check definitions. + Supported transfer method is HTTP only currently. diff --git a/spec/classes/tripleo_profile_base_metrics_collectd_sensubility_spec.rb b/spec/classes/tripleo_profile_base_metrics_collectd_sensubility_spec.rb index fcd6e43c2..d8acee00a 100644 --- a/spec/classes/tripleo_profile_base_metrics_collectd_sensubility_spec.rb +++ b/spec/classes/tripleo_profile_base_metrics_collectd_sensubility_spec.rb @@ -30,6 +30,39 @@ describe 'tripleo::profile::base::metrics::collectd::sensubility' do ) end end + + context 'with defaults and scripts for download defined' do + let(:params) do + { :workdir => '/some/path', + :scripts => { + 'scriptA' => { + 'source' => 'http://some.uri/scriptA', + 'checksum' => '227e8f542d95e416462a7f17652da655', + }, + 'scriptB' => { + 'source' => 'http://some.other.uri/scriptB', + 'checksum' => '48a404e59d4a43239ce64dee3af038b9', + 'create_bin_link' => false + } + } + } + end + + it 'requests the scripts download' do + is_expected.to compile.with_all_deps + is_expected.to contain_file('/some/path/scripts/scriptA').with( + :source => 'http://some.uri/scriptA', + :checksum_value => '227e8f542d95e416462a7f17652da655', + ) + is_expected.to contain_file('/usr/bin/sensubility_scriptA') + + is_expected.to contain_file('/some/path/scripts/scriptB').with( + :source => 'http://some.other.uri/scriptB', + :checksum_value => '48a404e59d4a43239ce64dee3af038b9', + ) + is_expected.not_to contain_file('/usr/bin/sensubility_scriptB') + end + end end on_supported_os.each do |os, facts| diff --git a/spec/classes/tripleo_profile_base_metrics_collectd_spec.rb b/spec/classes/tripleo_profile_base_metrics_collectd_spec.rb index bbc3eaaa9..8f465ce78 100644 --- a/spec/classes/tripleo_profile_base_metrics_collectd_spec.rb +++ b/spec/classes/tripleo_profile_base_metrics_collectd_spec.rb @@ -23,6 +23,7 @@ sensubility_conf = " [sensu] connection=ampq://sensu:sensu@localhost:5672//sensu subscriptions=default,test +tmp_base_dir=/var/lib/collectd-sensubility/checks checks={\"standalone_check\":{\"command\":\"echo 'foobar'\",\"interval\":5}} [amqp1] diff --git a/spec/defines/tripleo_profile_base_metrics_collectd_sensubility_script_spec.rb b/spec/defines/tripleo_profile_base_metrics_collectd_sensubility_script_spec.rb new file mode 100644 index 000000000..554b4f508 --- /dev/null +++ b/spec/defines/tripleo_profile_base_metrics_collectd_sensubility_script_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper' + +describe 'tripleo::profile::base::metrics::collectd::sensubility_script' do + let(:title) { 'test' } + + let :params do { + :scriptname => 'test', + :checksum => '227e8f542d95e416462a7f17652da655', + :user => 'collectd', + :group => 'collectd', + :source => 'http://some.uri', + :scriptsdir => '/some/path' + } + end + + shared_examples_for 'tripleo::profile::base::metrics::collectd::sensubility_script' do + context 'with basic parameters' do + it 'should download the script' do + is_expected.to contain_file('/some/path/test').with( + :ensure => 'present', + :owner => 'collectd', + :group => 'collectd', + :mode => '0700', + :source => 'http://some.uri', + :checksum_value => '227e8f542d95e416462a7f17652da655', + ) + + is_expected.to contain_file('/usr/bin/sensubility_test').with( + :ensure => 'link', + :target => '/some/path/test', + ) + end + end + end + + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts.merge({}) + end + + it_behaves_like 'tripleo::profile::base::metrics::collectd::sensubility_script' + end + end +end