Merge "Add posibility to download scripts for sensubility"

This commit is contained in:
Zuul 2021-06-07 20:34:40 +00:00 committed by Gerrit Code Review
commit ba1145c2e5
6 changed files with 204 additions and 8 deletions

View File

@ -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
}
}
}
}

View File

@ -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_<script-name>.
# 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}",
}
}
}

View File

@ -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.

View File

@ -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|

View File

@ -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]

View File

@ -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