From 9dd462d2e892b2d5f16b30207775795c504f4902 Mon Sep 17 00:00:00 2001 From: Sascha Doering Date: Fri, 30 Dec 2022 16:07:33 +0000 Subject: [PATCH] Added ability to delete old releases Move the URL releases to their own directory under /opt to create the possibility for Puppet to remove old releases that are no longer used. --- manifests/alertmanager.pp | 2 +- manifests/daemon.pp | 10 +-- manifests/dellhw_exporter.pp | 6 +- manifests/init.pp | 20 ++++++ manifests/install.pp | 14 ++-- manifests/ipsec_exporter.pp | 2 +- manifests/mongodb_exporter.pp | 2 +- manifests/nginx_prometheus_exporter.pp | 2 +- manifests/openldap_exporter.pp | 2 +- manifests/php_fpm_exporter.pp | 2 +- manifests/postgres_exporter.pp | 2 +- manifests/puppetdb_exporter.pp | 2 +- manifests/pushprox_client.pp | 2 +- manifests/pushprox_proxy.pp | 2 +- manifests/redis_exporter.pp | 2 +- manifests/snmp_exporter.pp | 4 +- manifests/ssl_exporter.pp | 2 +- spec/acceptance/prometheus_spec.rb | 99 ++++++++++++++++++++++++++ spec/classes/init_spec.rb | 48 +++++++++++++ 19 files changed, 196 insertions(+), 29 deletions(-) create mode 100644 spec/acceptance/prometheus_spec.rb create mode 100644 spec/classes/init_spec.rb diff --git a/manifests/alertmanager.pp b/manifests/alertmanager.pp index 2c9193ce9..da08faeed 100644 --- a/manifests/alertmanager.pp +++ b/manifests/alertmanager.pp @@ -187,7 +187,7 @@ # If version >= 0.10.0 then install amtool - Alertmanager validation tool file { "${bin_dir}/amtool": ensure => link, - target => "/opt/${package_name}-${version}.${os}-${arch}/amtool", + target => "${prometheus::basepath}/${package_name}-${version}.${os}-${arch}/amtool", } if $manage_config { diff --git a/manifests/daemon.pp b/manifests/daemon.pp index 2329d9238..ce6e379c0 100644 --- a/manifests/daemon.pp +++ b/manifests/daemon.pp @@ -75,8 +75,8 @@ Hash[String[1], Scalar] $env_vars = {}, Stdlib::Absolutepath $env_file_path = $prometheus::env_file_path, Optional[String[1]] $extract_command = $prometheus::extract_command, - Stdlib::Absolutepath $extract_path = '/opt', - Stdlib::Absolutepath $archive_bin_path = "/opt/${name}-${version}.${os}-${arch}/${name}", + Stdlib::Absolutepath $extract_path = $prometheus::basepath, + Stdlib::Absolutepath $archive_bin_path = "${prometheus::basepath}/${name}-${version}.${os}-${arch}/${name}", Boolean $export_scrape_job = false, Stdlib::Host $scrape_host = $facts['networking']['fqdn'], Optional[Stdlib::Port] $scrape_port = undef, @@ -89,17 +89,17 @@ case $install_method { 'url': { if $download_extension == '' { - file { "/opt/${name}-${version}.${os}-${arch}": + file { "${prometheus::basepath}/${name}-${version}.${os}-${arch}": ensure => directory, owner => 'root', group => 0, # 0 instead of root because OS X uses "wheel". mode => '0755', } - -> archive { "/opt/${name}-${version}.${os}-${arch}/${name}": + -> archive { "${prometheus::basepath}/${name}-${version}.${os}-${arch}/${name}": ensure => present, source => $real_download_url, checksum_verify => false, - before => File["/opt/${name}-${version}.${os}-${arch}/${name}"], + before => File["${prometheus::basepath}/${name}-${version}.${os}-${arch}/${name}"], proxy_server => $proxy_server, proxy_type => $proxy_type, } diff --git a/manifests/dellhw_exporter.pp b/manifests/dellhw_exporter.pp index 1d9561a15..3eaef03e4 100644 --- a/manifests/dellhw_exporter.pp +++ b/manifests/dellhw_exporter.pp @@ -46,7 +46,7 @@ # @param version # The binary release version # @param omreport_path -# The file path to the omReport executable (default "/opt/dell/srvadmin/bin/omreport") +# The file path to the omReport executable (default "/opt/prometheus/dell/srvadmin/bin/omreport") # @param scrape_ipadress # The ip address that the exporter will to listen to (default '') # @param proxy_server @@ -84,7 +84,7 @@ String[1] $scrape_job_name = 'dellhw', Optional[Hash] $scrape_job_labels = undef, Optional[String[1]] $bin_name = undef, - Stdlib::Unixpath $omreport_path = '/opt/dell/srvadmin/bin/omreport', + Stdlib::Unixpath $omreport_path = "${prometheus::basepath}/dell/srvadmin/bin/omreport", Optional[String[1]] $proxy_server = undef, Optional[Enum['none', 'http', 'https', 'ftp']] $proxy_type = undef, ) inherits prometheus { @@ -127,7 +127,7 @@ scrape_job_name => $scrape_job_name, scrape_job_labels => $scrape_job_labels, bin_name => $bin_name, - archive_bin_path => "/opt/dellhw_exporter-${version}.${os}-${arch}/dellhw_exporter", + archive_bin_path => "${prometheus::basepath}/dellhw_exporter-${version}.${os}-${arch}/dellhw_exporter", proxy_server => $proxy_server, proxy_type => $proxy_type, } diff --git a/manifests/init.pp b/manifests/init.pp index 54a0a798c..b05d5af07 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -220,6 +220,8 @@ # Optional proxy server, with port number if needed. ie: https://example.com:8080 # @param proxy_type # Optional proxy server type (none|http|https|ftp) +# @param clean_url_releases +# If this variable is activated, the releases are no longer installed under /opt but under /opt/prometheus. In addition, all releases that are no longer used are automatically deleted. class prometheus ( String $user, String $group, @@ -307,6 +309,7 @@ Boolean $include_default_scrape_configs = true, Optional[String[1]] $proxy_server = undef, Optional[Enum['none', 'http', 'https', 'ftp']] $proxy_type = undef, + Boolean $clean_url_releases = false, ) { case $arch { 'x86_64', 'amd64': { $real_arch = 'amd64' } @@ -321,6 +324,23 @@ } } + if $clean_url_releases { + $basepath = '/opt/prometheus' + + file { $basepath: + ensure => directory, + owner => 'root', + group => 'root', + mode => '0755', + backup => false, + force => true, + purge => true, + recurse => true, + } + } else { + $basepath = '/opt' + } + if $manage_prometheus_server { include prometheus::server } diff --git a/manifests/install.pp b/manifests/install.pp index 739cb43ef..32a72cee4 100644 --- a/manifests/install.pp +++ b/manifests/install.pp @@ -19,25 +19,25 @@ archive { "/tmp/prometheus-${prometheus::server::version}.${prometheus::server::download_extension}": ensure => present, extract => true, - extract_path => '/opt', + extract_path => $prometheus::basepath, source => $prometheus::server::real_download_url, checksum_verify => false, - creates => "/opt/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/prometheus", + creates => "${prometheus::basepath}/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/prometheus", cleanup => true, extract_command => $prometheus::extract_command, } -> file { - "/opt/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/prometheus": + "${prometheus::basepath}/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/prometheus": owner => 'root', group => 0, # 0 instead of root because OS X uses "wheel". mode => '0555'; "${prometheus::server::bin_dir}/prometheus": ensure => link, notify => $prometheus::server::notify_service, - target => "/opt/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/prometheus"; + target => "${prometheus::basepath}/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/prometheus"; "${prometheus::server::bin_dir}/promtool": ensure => link, - target => "/opt/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/promtool"; + target => "${prometheus::basepath}/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/promtool"; $prometheus::server::shared_dir: ensure => directory, owner => $prometheus::server::user, @@ -46,11 +46,11 @@ "${prometheus::server::shared_dir}/consoles": ensure => link, notify => $prometheus::server::notify_service, - target => "/opt/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/consoles"; + target => "${prometheus::basepath}/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/consoles"; "${prometheus::server::shared_dir}/console_libraries": ensure => link, notify => $prometheus::server::notify_service, - target => "/opt/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/console_libraries"; + target => "${prometheus::basepath}/prometheus-${prometheus::server::version}.${prometheus::server::os}-${prometheus::server::real_arch}/console_libraries"; } } 'package': { diff --git a/manifests/ipsec_exporter.pp b/manifests/ipsec_exporter.pp index 50af9fb3a..f418f5044 100644 --- a/manifests/ipsec_exporter.pp +++ b/manifests/ipsec_exporter.pp @@ -88,7 +88,7 @@ } else { $release = "v${version}" - $archive_bin_path = "/opt/ipsec_exporter-v${version}.${os}-${arch}" + $archive_bin_path = "${prometheus::basepath}/ipsec_exporter-v${version}.${os}-${arch}" } $real_download_url = pick($download_url,"${download_url_base}/download/v${version}/${package_name}-${release}.${os}-${arch}.${download_extension}") diff --git a/manifests/mongodb_exporter.pp b/manifests/mongodb_exporter.pp index 1c90b553f..6b0839adf 100644 --- a/manifests/mongodb_exporter.pp +++ b/manifests/mongodb_exporter.pp @@ -95,7 +95,7 @@ if versioncmp($version, '0.7.0') < 0 or versioncmp($version, '0.20.4') >= 0 { $archive_bin_path = undef # use default } else { - $archive_bin_path = '/opt/mongodb_exporter' + $archive_bin_path = "${prometheus::basepath}/mongodb_exporter" } $notify_service = $restart_on_change ? { diff --git a/manifests/nginx_prometheus_exporter.pp b/manifests/nginx_prometheus_exporter.pp index 9d993c02d..bfeca7981 100644 --- a/manifests/nginx_prometheus_exporter.pp +++ b/manifests/nginx_prometheus_exporter.pp @@ -104,7 +104,7 @@ # nginx_prometheus_exporter lacks currently as of version 0.9.0 # TODO: patch prometheus::daemon to support custom extract directories $real_install_method = 'none' - $install_dir = "/opt/${package_name}-${version}.${os}-${arch}" + $install_dir = "${prometheus::basepath}/${package_name}-${version}.${os}-${arch}" file { $install_dir: ensure => 'directory', owner => 'root', diff --git a/manifests/openldap_exporter.pp b/manifests/openldap_exporter.pp index 22bb422ee..e28acf29b 100644 --- a/manifests/openldap_exporter.pp +++ b/manifests/openldap_exporter.pp @@ -86,7 +86,7 @@ if versioncmp($version, '2.2.1') >= 0 { $real_download_extension = 'gz' $real_download_url = pick($download_url,"${download_url_base}/download/${release}/${package_name}-${os}-${prometheus::real_arch}.gz") - $extract_path = "/opt/openldap_exporter-${version}.${os}-${prometheus::real_arch}" + $extract_path = "${prometheus::basepath}/openldap_exporter-${version}.${os}-${prometheus::real_arch}" $archive_bin_path = "${extract_path}/openldap_exporter-${os}-${prometheus::real_arch}" $extract_command = "gzip -cd %s > ${archive_bin_path}" file { $extract_path: diff --git a/manifests/php_fpm_exporter.pp b/manifests/php_fpm_exporter.pp index f395aeada..a85b1f6ed 100644 --- a/manifests/php_fpm_exporter.pp +++ b/manifests/php_fpm_exporter.pp @@ -105,7 +105,7 @@ # php-fpm_exporter lacks currently as of version 2.0.4 # TODO: patch prometheus::daemon to support custom extract directories $real_install_method = 'none' - $install_dir = "/opt/${package_name}-${version}.${os}-${arch}" + $install_dir = "${prometheus::basepath}/${package_name}-${version}.${os}-${arch}" file { $install_dir: ensure => 'directory', owner => 'root', diff --git a/manifests/postgres_exporter.pp b/manifests/postgres_exporter.pp index 11b4491c1..b2dce30d9 100644 --- a/manifests/postgres_exporter.pp +++ b/manifests/postgres_exporter.pp @@ -138,7 +138,7 @@ # postgres_exporter lacks. # TODO: patch prometheus::daemon to support custom extract directories $exporter_install_method = 'none' - $install_dir = "/opt/${service_name}-${version}.${os}-${arch}" + $install_dir = "${prometheus::basepath}/${service_name}-${version}.${os}-${arch}" file { $install_dir: ensure => 'directory', owner => 'root', diff --git a/manifests/puppetdb_exporter.pp b/manifests/puppetdb_exporter.pp index 4198bb189..ff9f704e5 100644 --- a/manifests/puppetdb_exporter.pp +++ b/manifests/puppetdb_exporter.pp @@ -123,7 +123,7 @@ scrape_job_name => $scrape_job_name, scrape_job_labels => $scrape_job_labels, bin_name => $bin_name, - archive_bin_path => "/opt/prometheus-puppetdb-exporter-${version}.${os}-${arch}/prometheus-puppetdb-exporter", + archive_bin_path => "${prometheus::basepath}/prometheus-puppetdb-exporter-${version}.${os}-${arch}/prometheus-puppetdb-exporter", proxy_server => $proxy_server, proxy_type => $proxy_type, } diff --git a/manifests/pushprox_client.pp b/manifests/pushprox_client.pp index 695671d8b..936446285 100644 --- a/manifests/pushprox_client.pp +++ b/manifests/pushprox_client.pp @@ -96,7 +96,7 @@ install_method => $install_method, version => $version, download_extension => $download_extension, - archive_bin_path => "/opt/PushProx-${version}.${os}-${arch}/pushprox-client", + archive_bin_path => "${prometheus::basepath}/PushProx-${version}.${os}-${arch}/pushprox-client", os => $os, arch => $arch, real_download_url => $real_download_url, diff --git a/manifests/pushprox_proxy.pp b/manifests/pushprox_proxy.pp index 49429d445..34e980f46 100644 --- a/manifests/pushprox_proxy.pp +++ b/manifests/pushprox_proxy.pp @@ -90,7 +90,7 @@ install_method => $install_method, version => $version, download_extension => $download_extension, - archive_bin_path => "/opt/PushProx-${version}.${os}-${arch}/pushprox-proxy", + archive_bin_path => "${prometheus::basepath}/PushProx-${version}.${os}-${arch}/pushprox-proxy", os => $os, arch => $arch, real_download_url => $real_download_url, diff --git a/manifests/redis_exporter.pp b/manifests/redis_exporter.pp index 0904952e0..bb71f2c95 100644 --- a/manifests/redis_exporter.pp +++ b/manifests/redis_exporter.pp @@ -110,7 +110,7 @@ # redis_exporter lacks before version 1.0.0 # TODO: patch prometheus::daemon to support custom extract directories $real_install_method = 'none' - $install_dir = "/opt/${service_name}-${version}.${os}-${arch}" + $install_dir = "${prometheus::basepath}/${service_name}-${version}.${os}-${arch}" file { $install_dir: ensure => 'directory', owner => 'root', diff --git a/manifests/snmp_exporter.pp b/manifests/snmp_exporter.pp index edf797299..f712bdd7a 100644 --- a/manifests/snmp_exporter.pp +++ b/manifests/snmp_exporter.pp @@ -105,12 +105,12 @@ } $_source = $config_template ? { - '' => "file:/opt/snmp_exporter-${version}.${os}-${arch}/snmp.yml", + '' => "file:${prometheus::basepath}/snmp_exporter-${version}.${os}-${arch}/snmp.yml", default => undef, } $_require = $config_template ? { - '' => File["/opt/snmp_exporter-${version}.${os}-${arch}/snmp_exporter"], + '' => File["${prometheus::basepath}/snmp_exporter-${version}.${os}-${arch}/snmp_exporter"], default => undef, } diff --git a/manifests/ssl_exporter.pp b/manifests/ssl_exporter.pp index 3aabb0fe0..37c5abc6e 100644 --- a/manifests/ssl_exporter.pp +++ b/manifests/ssl_exporter.pp @@ -113,7 +113,7 @@ ], ' ') # SSL exporter is not packaged into a directory - $extract_path = "/opt/${service_name}-${version}.${os}-${arch}" + $extract_path = "${prometheus::basepath}/${service_name}-${version}.${os}-${arch}" file { $extract_path: ensure => 'directory', owner => 'root', diff --git a/spec/acceptance/prometheus_spec.rb b/spec/acceptance/prometheus_spec.rb new file mode 100644 index 000000000..c8447c642 --- /dev/null +++ b/spec/acceptance/prometheus_spec.rb @@ -0,0 +1,99 @@ +# frozen_string_literal: true + +require 'spec_helper_acceptance' + +describe 'prometheus' do + it 'prometheus do not clean URL releases' do + pp_dirty_v0152 = <<-EOS + class { 'prometheus': + clean_url_releases => false, + } + class { 'prometheus::node_exporter': + version => '0.15.2', + } + EOS + + pp_dirty_v0160 = <<-EOS + class { 'prometheus': + clean_url_releases => false, + } + class { 'prometheus::node_exporter': + version => '0.16.0', + } + EOS + + # Run it twice and test for idempotency + apply_manifest(pp_dirty_v0152, catch_failures: true) + apply_manifest(pp_dirty_v0152, catch_changes: true) + + # Run it twice and test for idempotency + apply_manifest(pp_dirty_v0160, catch_failures: true) + apply_manifest(pp_dirty_v0160, catch_changes: true) + end + + # rubocop:disable RSpec/RepeatedExampleGroupBody,RSpec/RepeatedExampleGroupDescription + describe service('node_exporter') do + it { is_expected.to be_running } + it { is_expected.to be_enabled } + end + + describe port(9100) do + it { is_expected.to be_listening.with('tcp6') } + end + + describe file('/opt/node_exporter-0.15.2.linux-amd64') do + it { is_expected.to exist } + end + + describe file('/opt/node_exporter-0.16.0.linux-amd64') do + it { is_expected.to exist } + end + # rubocop:enable RSpec/RepeatedExampleGroupBody,RSpec/RepeatedExampleGroupDescription + + it 'prometheus do clean URL releases' do + pp_clean_v0152 = <<-EOS + class { 'prometheus': + clean_url_releases => false, + } + class { 'prometheus::node_exporter': + version => '0.15.2', + } + EOS + + pp_clean_v0160 = <<-EOS + class { 'prometheus': + clean_url_releases => false, + } + class { 'prometheus::node_exporter': + version => '0.16.0', + } + EOS + + # Run it twice and test for idempotency + apply_manifest(pp_clean_v0152, catch_failures: true) + apply_manifest(pp_clean_v0152, catch_changes: true) + + # Run it twice and test for idempotency + apply_manifest(pp_clean_v0160, catch_failures: true) + apply_manifest(pp_clean_v0160, catch_changes: true) + end + + # rubocop:disable RSpec/RepeatedExampleGroupBody,RSpec/RepeatedExampleGroupDescription + describe service('node_exporter') do + it { is_expected.to be_running } + it { is_expected.to be_enabled } + end + + describe port(9100) do + it { is_expected.to be_listening.with('tcp6') } + end + + describe file('/opt/prometheus/node_exporter-0.15.2.linux-amd64') do + it { is_expected.not_to exist } + end + + describe file('/opt/prometheus/node_exporter-0.16.0.linux-amd64') do + it { is_expected.to exist } + end + # rubocop:enable RSpec/RepeatedExampleGroupBody,RSpec/RepeatedExampleGroupDescription +end diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb new file mode 100644 index 000000000..5d7f0f06a --- /dev/null +++ b/spec/classes/init_spec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe 'prometheus' do + on_supported_os.each do |os, facts| + context "on #{os}" do + let(:facts) do + facts.merge(os_specific_facts(facts)) + end + + context 'without parameters' do + it { + is_expected.to compile.with_all_deps + } + + it { + is_expected.not_to contain_file('/opt/prometheus') + } + end + + context 'with activated url release cleaning' do + let(:params) do + { + clean_url_releases: true, + } + end + + it { + is_expected.to compile.with_all_deps + } + + it { + is_expected.to contain_file('/opt/prometheus').with( + ensure: 'directory', + owner: 'root', + group: 'root', + mode: '0755', + backup: false, + force: true, + purge: true, + recurse: true + ) + } + end + end + end +end