From 283b2dcf18b11774290240085e5b2021475641be Mon Sep 17 00:00:00 2001 From: Adrian Date: Tue, 1 Jun 2021 11:04:17 -0700 Subject: [PATCH] (SUP-2404) Migrate from cron to systemd units Prior to this commit, maintenance and backups were done with cron entries. This commit replaces all but the backup feature with systemd services and timers to provide better logging. --- .sync.yml | 15 +--- files/vacuum_full_tables.sh | 43 --------- lib/facter/pe_databases.rb | 11 +++ manifests/collect.pp | 41 +++++++++ manifests/init.pp | 50 +++++++---- manifests/maintenance.pp | 31 ------- manifests/maintenance/pg_repack.pp | 94 -------------------- manifests/maintenance/vacuum_full.pp | 68 -------------- manifests/pg_repack.pp | 72 +++++++++++++++ metadata.json | 21 +---- spec/classes/init_spec.rb | 16 ++++ spec/classes/maintenance/pg_repack_spec.rb | 87 ------------------ spec/classes/maintenance/vacuum_full_spec.rb | 12 --- spec/classes/maintenance_spec.rb | 45 ---------- spec/classes/pg_repack_spec.rb | 74 +++++++++++++++ spec/default_facts.yml | 6 +- templates/service.epp | 13 +++ templates/timer.epp | 11 +++ 18 files changed, 281 insertions(+), 429 deletions(-) delete mode 100644 files/vacuum_full_tables.sh create mode 100644 lib/facter/pe_databases.rb create mode 100644 manifests/collect.pp delete mode 100644 manifests/maintenance.pp delete mode 100644 manifests/maintenance/pg_repack.pp delete mode 100644 manifests/maintenance/vacuum_full.pp create mode 100644 manifests/pg_repack.pp delete mode 100644 spec/classes/maintenance/pg_repack_spec.rb delete mode 100644 spec/classes/maintenance/vacuum_full_spec.rb delete mode 100644 spec/classes/maintenance_spec.rb create mode 100644 spec/classes/pg_repack_spec.rb create mode 100644 templates/service.epp create mode 100644 templates/timer.epp diff --git a/.sync.yml b/.sync.yml index 30df5c4..935a1ca 100644 --- a/.sync.yml +++ b/.sync.yml @@ -34,18 +34,11 @@ spec/spec_helper.rb: coverage_report: true spec/default_facts.yml: extra_facts: - pe_server_version: 2018.1.0 - memory: - "system": - "total": "16.00 GiB" - "total_bytes": 17179869184 - processors: - "count": 8 -spec/default_facts.yml: - extra_facts: + pe_databases: + have_systemd: true pe_postgresql_info: - installed_server_version: 9.6 - pe_server_version: 2018.1.0 + installed_server_version: 11 + pe_server_version: 2019.8.6 memory: system: total: 16.00 GiB diff --git a/files/vacuum_full_tables.sh b/files/vacuum_full_tables.sh deleted file mode 100644 index fc79a3b..0000000 --- a/files/vacuum_full_tables.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash - -usage() { - cat < [SLEEP_DURATION]" -where is one of: facts catalogs other -and [SLEEP_DURATION] is an optional integer of seconds to sleep. Defaults to 300 -EOF - exit 1 -} -# Print usage if given 0 arguments -(( $# == 0 )) && usage - -sleep_duration="${2:-300}" - -case "$1" in - 'facts') - vacuum_tables=("'facts'" "'factsets'" "'fact_paths'" "'fact_values'") - ;; - 'catalogs') - vacuum_tables=("'catalogs'" "'catalog_resources'" "'edges'" "'certnames'") - ;; - 'other') - vacuum_tables=("'producers'" "'resource_params'" "'resource_params_cache'") - ;; - *) - usage -esac - -SQL="SELECT t.relname::varchar AS table_name - FROM pg_class t - JOIN pg_namespace n - ON n.oid = t.relnamespace - WHERE t.relkind = 'r' - AND t.relname IN ( $(IFS=,; echo "${vacuum_tables[*]}") )" - -# shellcheck disable=SC2207 -tables=($(su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/bin/psql -d pe-puppetdb -c \"$SQL\" --tuples-only")) - -for table in "${tables[@]}"; do - su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/bin/vacuumdb -d pe-puppetdb -t $table --full --analyze" - sleep "$sleep_duration" -done diff --git a/lib/facter/pe_databases.rb b/lib/facter/pe_databases.rb new file mode 100644 index 0000000..4e1dd86 --- /dev/null +++ b/lib/facter/pe_databases.rb @@ -0,0 +1,11 @@ +Facter.add(:pe_databases, type: :aggregate) do + confine kernel: 'Linux' + + chunk(:have_systemd) do + if Puppet::FileSystem.exist?('/proc/1/comm') && Puppet::FileSystem.read('/proc/1/comm').include?('systemd') + { have_systemd: true } + else + { have_systemd: false } + end + end +end diff --git a/manifests/collect.pp b/manifests/collect.pp new file mode 100644 index 0000000..fa0df3e --- /dev/null +++ b/manifests/collect.pp @@ -0,0 +1,41 @@ +# Create systemd units for repacking a given database type +define pe_databases::collect ( + String $database_type = $title, + String $command = undef, + Boolean $disable_maintenance = false, + String $on_cal = undef, +) { + + Service { + notify => Exec['pe_databases_daemon_reload'], + } + File { + notify => Exec['pe_databases_daemon_reload'], + } + + $ensure_service = $disable_maintenance ? { + true => stopped, + default => running, + } + + $ensure_file = $disable_maintenance ? { + true => absent, + default => present + } + + file {"/etc/systemd/system/pe_databases-${database_type}.service": + ensure => $ensure_file, + content => epp('pe_databases/service.epp', { 'tables' => $database_type, 'command' => $command }), + } + file {"/etc/systemd/system/pe_databases-${database_type}.timer": + ensure => $ensure_file, + content => epp('pe_databases/timer.epp', { 'tables' => $database_type, 'on_cal' => $on_cal }), + } + + service { "pe_databases-${database_type}.service": } + service { "pe_databases-${database_type}.timer": + ensure => $ensure_service, + enable => ! $disable_maintenance, + subscribe => File["/etc/systemd/system/pe_databases-${database_type}.timer"], + } +} diff --git a/manifests/init.pp b/manifests/init.pp index 2a9e0d3..946a544 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -3,7 +3,7 @@ # @summary Tuning, maintenance, and backups for PE PostgreSQL. class pe_databases ( - Boolean $manage_database_backups = true, + Boolean $manage_database_backups = false, Boolean $manage_database_maintenance = true, Boolean $manage_postgresql_settings = true, Boolean $manage_table_settings = true, @@ -21,27 +21,39 @@ mode => '0755', } - if $manage_database_maintenance { - include pe_databases::maintenance + exec { 'pe_databases_daemon_reload': + command => 'systemctl daemon-reload', + path => ['/bin', '/usr/bin'], + refreshonly => true, } - # Do not manage postgresql_settings in 2018.1.0 or newer. - if $manage_postgresql_settings and (versioncmp('2018.1.0', $facts['pe_server_version']) > 0) { - include pe_databases::postgresql_settings - class { 'pe_databases::postgresql_settings::table_settings' : - manage_reports_autovacuum_cost_delay => $pe_databases::postgresql_settings::manage_reports_autovacuum_cost_delay, - factsets_autovacuum_vacuum_scale_factor => $pe_databases::postgresql_settings::factsets_autovacuum_vacuum_scale_factor, - reports_autovacuum_vacuum_scale_factor => $pe_databases::postgresql_settings::reports_autovacuum_vacuum_scale_factor, - require => Class['pe_databases::postgresql_settings'], + if $facts.dig('pe_databases', 'have_systemd') { + if $manage_database_maintenance and (versioncmp('2019.0.2', $facts['pe_server_version']) <= 0) { + class {'pe_databases::pg_repack': + disable_maintenance => lookup('pe_databases::maintenance::disable_maintenance', {'default_value' => false}), + } + if $manage_table_settings { + # This is to provide for situations, like PE XL, + # where the pe-puppetdb database does not exist on the PostgreSQL system being tuned. + # In PE XL, the Master and Replica run PostgreSQL for all databases *except* for pe-puppetdb. + include pe_databases::postgresql_settings::table_settings + } + + if $manage_database_backups { + include pe_databases::backup + } + } + else { + notify { 'pe_databases_version_warn': + message => 'This module only supports PE 2019.0.2 and later', + loglevel => warning, + } } - } elsif $manage_table_settings { - # This is to provide for situations, like PE XL, - # where the pe-puppetdb database does not exist on the PostgreSQL system being tuned. - # In PE XL, the Master and Replica run PostgreSQL for all databases *except* for pe-puppetdb. - include pe_databases::postgresql_settings::table_settings } - - if $manage_database_backups { - include pe_databases::backup + else { + notify { 'pe_databases_systemd_warn': + message => 'This module only works with systemd as the provider', + loglevel => warning, + } } } diff --git a/manifests/maintenance.pp b/manifests/maintenance.pp deleted file mode 100644 index bf1cf03..0000000 --- a/manifests/maintenance.pp +++ /dev/null @@ -1,31 +0,0 @@ -# Maintenance for PostgreSQL -# -# @summary Maintenance for PostgreSQL - -class pe_databases::maintenance ( - Boolean $disable_maintenance = false, - String $logging_directory = '/var/log/puppetlabs/pe_databases_cron', - String $script_directory = $pe_databases::scripts_dir, -){ - - # If this version of PE includes pg_repack (2018.1.7 and 2019.0.2 and newer), - # then use pg_repack and remove the old script and cron jobs. - - if (versioncmp( '2018.1.7', $facts['pe_server_version']) <= 0 and versioncmp($facts['pe_server_version'], '2019.0.0') < 0 ) { - include pe_databases::maintenance::pg_repack - class { 'pe_databases::maintenance::vacuum_full': - disable_maintenance => true, - } - } elsif ( versioncmp( '2019.0.2', $facts['pe_server_version']) <= 0 ) { - include pe_databases::maintenance::pg_repack - class { 'pe_databases::maintenance::vacuum_full': - disable_maintenance => true, - } - } else { - include pe_databases::maintenance::vacuum_full - } - - file { $logging_directory : - ensure => directory, - } -} diff --git a/manifests/maintenance/pg_repack.pp b/manifests/maintenance/pg_repack.pp deleted file mode 100644 index 8b79df6..0000000 --- a/manifests/maintenance/pg_repack.pp +++ /dev/null @@ -1,94 +0,0 @@ -# Maintenance pg_repack -# -# @summary Maintenance pg_repack - -class pe_databases::maintenance::pg_repack ( - Boolean $disable_maintenance = $pe_databases::maintenance::disable_maintenance, - String $logging_directory = $pe_databases::maintenance::logging_directory, - Integer $jobs = $facts['processors']['count'] / 4 -) { - - $ensure_cron = $disable_maintenance ? { - true => absent, - default => present - } - - # PE 2019.1 starting shipping versioned pe-postgres packages where all paths are versioned. - # So, prior to 2019.1 use a non-versioned path, and after use a versioned path. - # TODO: Use $pe_databases::psql_version after identifying why it is cast to ${psql_version}00000 in spec tests. - $postgresql_version = $facts['pe_postgresql_info']['installed_server_version'] - $repack_executable = versioncmp('2019.1.0', $facts['pe_server_version']) ? { - 1 => '/opt/puppetlabs/server/apps/postgresql/bin/pg_repack', - default => "/opt/puppetlabs/server/apps/postgresql/${postgresql_version}/bin/pg_repack" - } - - $repack = "su - pe-postgres -s /bin/bash -c \"${repack_executable} -d pe-puppetdb" - $repack_jobs = "--jobs ${jobs}" - - $facts_tables = '-t factsets -t fact_paths"' - $catalogs_tables = versioncmp($facts['pe_server_version'], '2019.8.1') ? { - 1 => '-t catalogs -t catalog_resources -t catalog_inputs -t edges -t certnames"', - default => '-t catalogs -t catalog_resources -t edges -t certnames"' } - $other_tables = '-t producers -t resource_params -t resource_params_cache"' - $reports_table = '-t reports"' - $resource_events_table = '-t resource_events"' - - Cron { - ensure => $ensure_cron, - user => 'root', - require => File[$logging_directory], - } - - cron { 'pg_repack facts tables' : - weekday => [2,6], - hour => 4, - minute => 30, - command => "${repack} ${repack_jobs} ${facts_tables} > ${logging_directory}/facts_repack.log 2>&1", - } - - cron { 'pg_repack catalogs tables' : - weekday => [0,4], - hour => 4, - minute => 30, - command => "${repack} ${repack_jobs} ${catalogs_tables} > ${logging_directory}/catalogs_repack.log 2>&1", - } - - cron { 'pg_repack other tables' : - monthday => 20, - hour => 5, - minute => 30, - command => "${repack} ${repack_jobs} ${other_tables} > ${logging_directory}/other_repack.log 2>&1", - } - - if versioncmp($facts['pe_server_version'], '2019.7.0') < 0 { - cron { 'pg_repack reports tables' : - monthday => 10, - hour => 5, - minute => 30, - command => "${repack} ${repack_jobs} ${reports_table} > ${logging_directory}/reports_repack.log 2>&1", - } - } - else { - cron { 'pg_repack reports tables' : - ensure => 'absent', - } - } - - if versioncmp($facts['pe_server_version'], '2019.3.0') < 0 { - cron { 'pg_repack resource_events tables' : - monthday => 15, - hour => 5, - minute => 30, - command => "${repack} ${repack_jobs} ${resource_events_table} > ${logging_directory}/resource_events_repack.log 2>&1", - } - } - else { - cron { 'pg_repack resource_events tables' : - ensure => 'absent', - } - } - - file { "${logging_directory}/output.log" : - ensure => absent, - } -} diff --git a/manifests/maintenance/vacuum_full.pp b/manifests/maintenance/vacuum_full.pp deleted file mode 100644 index 5630bd0..0000000 --- a/manifests/maintenance/vacuum_full.pp +++ /dev/null @@ -1,68 +0,0 @@ -# Maintenance VACUUM FULL -# -# @summary Maintenance VACUUM FULL - -class pe_databases::maintenance::vacuum_full ( - Boolean $disable_maintenance = $pe_databases::maintenance::disable_maintenance, - String $logging_directory = $pe_databases::maintenance::logging_directory, - String $script_directory = $pe_databases::maintenance::script_directory, -){ - - $ensure_cron = $disable_maintenance ? { - true => absent, - default => present - } - - $ensure_vacuum_script = $disable_maintenance ? { - true => absent, - default => file - } - - $vacuum_script_path = "${script_directory}/vacuum_full_tables.sh" - - file { $vacuum_script_path: - ensure => $ensure_vacuum_script, - source => 'puppet:///modules/pe_databases/vacuum_full_tables.sh', - owner => 'pe-postgres', - group => 'pe-postgres', - mode => '0744', - } - - Cron { - ensure => $ensure_cron, - user => 'root', - require => File[$logging_directory, $script_directory], - } - - cron { 'VACUUM FULL facts tables' : - weekday => [2,6], - hour => 4, - minute => 30, - command => "${vacuum_script_path} facts", - } - - cron { 'VACUUM FULL catalogs tables' : - weekday => [0,4], - hour => 4, - minute => 30, - command => "${vacuum_script_path} catalogs", - } - - cron { 'VACUUM FULL other tables' : - monthday => 20, - hour => 5, - minute => 30, - command => "${vacuum_script_path} other", - } - - # LEGACY CLEANUP - - # lint:ignore:140chars - cron { 'Maintain PE databases' : - ensure => absent, - user => 'root', - command => "su - pe-postgres -s /bin/bash -c '/opt/puppetlabs/server/bin/reindexdb --all; /opt/puppetlabs/server/bin/vacuumdb --analyze --verbose --all' > ${logging_directory}/output.log 2> ${logging_directory}/output_error.log", - require => File[$logging_directory, $script_directory], - } - # lint:endignore -} diff --git a/manifests/pg_repack.pp b/manifests/pg_repack.pp new file mode 100644 index 0000000..37e0eb8 --- /dev/null +++ b/manifests/pg_repack.pp @@ -0,0 +1,72 @@ +# Maintenance pg_repack +# +# @summary Maintenance pg_repack + +class pe_databases::pg_repack ( + Boolean $disable_maintenance = false, + Integer $jobs = $facts['processors']['count'] / 4 +) { + + # PE 2019.1 starting shipping versioned pe-postgres packages where all paths are versioned. + # So, prior to 2019.1 use a non-versioned path, and after use a versioned path. + # TODO: Use $pe_databases::psql_version after identifying why it is cast to ${psql_version}00000 in spec tests. + $postgresql_version = $facts['pe_postgresql_info']['installed_server_version'] + $repack_executable = versioncmp('2019.1.0', $facts['pe_server_version']) ? { + 1 => '/opt/puppetlabs/server/apps/postgresql/bin/pg_repack', + default => "/opt/puppetlabs/server/apps/postgresql/${postgresql_version}/bin/pg_repack" + } + + $repack = "${repack_executable} -d pe-puppetdb" + $repack_jobs = "--jobs ${jobs}" + + $facts_tables = '-t factsets -t fact_paths' + $catalogs_tables = versioncmp($facts['pe_server_version'], '2019.8.1') ? { + 1 => '-t catalogs -t catalog_resources -t catalog_inputs -t edges -t certnames', + default => '-t catalogs -t catalog_resources -t edges -t certnames' } + $other_tables = '-t producers -t resource_params -t resource_params_cache' + $reports_table = '-t reports' + $resource_events_table = '-t resource_events' + + pe_databases::collect {'facts': + disable_maintenance => $disable_maintenance, + command => "${repack} ${repack_jobs} ${facts_tables}", + on_cal => 'Tue,Sat *-*-* 04:30:00', + } + + pe_databases::collect {'catalogs': + disable_maintenance => $disable_maintenance, + command => "${repack} ${repack_jobs} ${catalogs_tables}", + on_cal => 'Sun,Thu *-*-* 04:30:00', + } + + pe_databases::collect {'other': + disable_maintenance => $disable_maintenance, + command => "${repack} ${repack_jobs} ${other_tables}", + on_cal => '*-*-20 05:30:00', + } + + if versioncmp($facts['pe_server_version'], '2019.7.0') < 0 { + pe_databases::collect {'reports': + disable_maintenance => $disable_maintenance, + command => "${repack} ${repack_jobs} ${reports_table}", + on_cal => '*-*-10 05:30:00', + } + } + + if versioncmp($facts['pe_server_version'], '2019.3.0') < 0 { + pe_databases::collect {'resource_events': + disable_maintenance => $disable_maintenance, + command => "${repack} ${repack_jobs} ${resource_events_table}", + on_cal => '*-*-15 05:30:00', + } + } + + # Legacy cleanup + $legacy_crons = [ + 'pg_repack facts tables', 'pg_repack catalogs tables', 'pg_repack other tables', + 'pg_repack reports tables', 'pg_repack resource_events tables' + ] + cron { $legacy_crons: + ensure => absent, + } + } diff --git a/metadata.json b/metadata.json index c3d4837..3f392ac 100644 --- a/metadata.json +++ b/metadata.json @@ -14,7 +14,6 @@ { "operatingsystem": "RedHat", "operatingsystemrelease": [ - "6", "7", "8" ] @@ -22,7 +21,6 @@ { "operatingsystem": "CentOS", "operatingsystemrelease": [ - "6", "7", "8" ] @@ -30,15 +28,6 @@ { "operatingsystem": "OracleLinux", "operatingsystemrelease": [ - "6", - "7", - "8" - ] - }, - { - "operatingsystem": "Scientific", - "operatingsystemrelease": [ - "6", "7", "8" ] @@ -46,17 +35,15 @@ { "operatingsystem": "Debian", "operatingsystemrelease": [ - "6", - "7", - "8" + "9", + "10" ] }, { "operatingsystem": "Ubuntu", "operatingsystemrelease": [ - "14.04", - "16.04", - "18.04" + "18.04", + "20.04" ] } ], diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index d2054e8..f00799f 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -16,4 +16,20 @@ it { is_expected.to compile } end end + + context 'on latest PE release' do + it { is_expected.to contain_class('pe_databases::pg_repack') } + end + + context 'on unsupported PE release' do + let(:facts) { { pe_server_version: '2019.0.1' } } + + it { is_expected.to contain_notify('pe_databases_version_warn') } + end + + context 'when systemd is not the init provider' do + let(:facts) { { pe_databases: { have_systemd: false } } } + + it { is_expected.to contain_notify('pe_databases_systemd_warn') } + end end diff --git a/spec/classes/maintenance/pg_repack_spec.rb b/spec/classes/maintenance/pg_repack_spec.rb deleted file mode 100644 index fc02821..0000000 --- a/spec/classes/maintenance/pg_repack_spec.rb +++ /dev/null @@ -1,87 +0,0 @@ -require 'spec_helper' - -describe 'pe_databases::maintenance::pg_repack' do - on_supported_os.each do |os, os_facts| - context "on #{os}" do - let(:pre_condition) { "class { 'pe_databases': manage_database_backups => false, manage_postgresql_settings => false, manage_table_settings => false, }" } - let(:facts) { os_facts } - - it { is_expected.to compile } - context 'on < PE 2019.3.0' do - before :each do - facts['pe_server_version'] = '2019.1.0' - facts['pe_postgresql_info']['installed_server_version'] = 9.6 - end - it { - is_expected.to contain_cron('pg_repack resource_events tables') - .with_command('su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/apps/postgresql/9.6/bin/pg_repack'\ - ' -d pe-puppetdb --jobs 2 -t resource_events" > /var/log/puppetlabs/pe_databases_cron/resource_events_repack.log 2>&1') - } - it { - is_expected.to contain_cron('pg_repack reports tables') - .with_command('su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/apps/postgresql/9.6/bin/pg_repack'\ - ' -d pe-puppetdb --jobs 2 -t reports" > /var/log/puppetlabs/pe_databases_cron/reports_repack.log 2>&1') - } - end - context 'on < PE 2019.3.0 with postgresql 11' do - before :each do - facts['pe_server_version'] = '2019.2.0' - facts['pe_postgresql_info']['installed_server_version'] = 11 - end - it { - is_expected.to contain_cron('pg_repack resource_events tables') - .with_command('su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/apps/postgresql/11/bin/pg_repack'\ - ' -d pe-puppetdb --jobs 2 -t resource_events" > /var/log/puppetlabs/pe_databases_cron/resource_events_repack.log 2>&1') - } - it { - is_expected.to contain_cron('pg_repack reports tables') - .with_command('su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/apps/postgresql/11/bin/pg_repack'\ - ' -d pe-puppetdb --jobs 2 -t reports" > /var/log/puppetlabs/pe_databases_cron/reports_repack.log 2>&1') - } - end - context 'on < PE 2019.7.0' do - before :each do - facts['pe_server_version'] = '2019.5.0' - facts['pe_postgresql_info']['installed_server_version'] = 11 - end - it { - is_expected.to contain_cron('pg_repack reports tables') - .with_command('su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/apps/postgresql/11/bin/pg_repack'\ - ' -d pe-puppetdb --jobs 2 -t reports" > /var/log/puppetlabs/pe_databases_cron/reports_repack.log 2>&1') - } - it { - is_expected.not_to contain_cron('pg_repack resource_events tables') - .with_command('su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/apps/postgresql/11/bin/pg_repack'\ - ' -d pe-puppetdb --jobs 2 -t resource_events" > /var/log/puppetlabs/pe_databases_cron/resource_events_repack.log 2>&1') - } - end - context 'on >= PE 2019.7.0' do - before :each do - facts['pe_server_version'] = '2019.7.0' - facts['pe_postgresql_info']['installed_server_version'] = 11 - end - it { - is_expected.not_to contain_cron('pg_repack reports tables') - .with_command('su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/apps/postgresql/11/bin/pg_repack'\ - ' -d pe-puppetdb --jobs 2 -t reports" > /var/log/puppetlabs/pe_databases_cron/reports_repack.log 2>&1') - } - it { - is_expected.not_to contain_cron('pg_repack resource_events tables') - .with_command('su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/apps/postgresql/11/bin/pg_repack'\ - ' -d pe-puppetdb --jobs 2 -t resource_events" > /var/log/puppetlabs/pe_databases_cron/resource_events_repack.log 2>&1') - } - end - context 'on >= PE 2019.8.2' do - before :each do - facts['pe_server_version'] = '2019.8.2' - facts['pe_postgresql_info']['installed_server_version'] = 11 - end - it { - is_expected.to contain_cron('pg_repack catalogs tables') - .with_command('su - pe-postgres -s /bin/bash -c "/opt/puppetlabs/server/apps/postgresql/11/bin/pg_repack'\ - ' -d pe-puppetdb --jobs 2 -t catalogs -t catalog_resources -t catalog_inputs -t edges -t certnames" > /var/log/puppetlabs/pe_databases_cron/catalogs_repack.log 2>&1') - } - end - end - end -end diff --git a/spec/classes/maintenance/vacuum_full_spec.rb b/spec/classes/maintenance/vacuum_full_spec.rb deleted file mode 100644 index 660e951..0000000 --- a/spec/classes/maintenance/vacuum_full_spec.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'spec_helper' - -describe 'pe_databases::maintenance::vacuum_full' do - on_supported_os.each do |os, os_facts| - context "on #{os}" do - let(:pre_condition) { "class { 'pe_databases': manage_database_backups => false, manage_postgresql_settings => false, manage_table_settings => false,}" } - let(:facts) { os_facts } - - it { is_expected.to compile } - end - end -end diff --git a/spec/classes/maintenance_spec.rb b/spec/classes/maintenance_spec.rb deleted file mode 100644 index 55fa4de..0000000 --- a/spec/classes/maintenance_spec.rb +++ /dev/null @@ -1,45 +0,0 @@ -require 'spec_helper' - -describe 'pe_databases::maintenance' do - on_supported_os.each do |os, os_facts| - context "on #{os}" do - let(:pre_condition) { "class { 'pe_databases': manage_database_backups => false, manage_postgresql_settings => false, manage_table_settings => false,}" } - let(:facts) { os_facts } - - it { is_expected.to compile } - - context 'on PE 2019.0.0' do - before(:each) do - facts['pe_server_version'] = '2019.0.0' - end - - it { is_expected.to contain_class('pe_databases::maintenance::vacuum_full') } - it { is_expected.not_to contain_class('pe_databases::maintenance::pg_repack') } - end - context 'on PE 2018.1.4' do - before(:each) do - facts['pe_server_version'] = '2018.1.4' - end - - it { is_expected.to contain_class('pe_databases::maintenance::vacuum_full') } - it { is_expected.not_to contain_class('pe_databases::maintenance::pg_repack') } - end - context 'on PE 2018.1.8' do - before(:each) do - facts['pe_server_version'] = '2018.1.9' - end - - it { is_expected.to contain_class('pe_databases::maintenance::vacuum_full').with('disable_maintenance' => true) } - it { is_expected.to contain_class('pe_databases::maintenance::pg_repack') } - end - context 'on PE 2019.0.3' do - before(:each) do - facts['pe_server_version'] = '2019.0.3' - end - - it { is_expected.to contain_class('pe_databases::maintenance::vacuum_full').with('disable_maintenance' => true) } - it { is_expected.to contain_class('pe_databases::maintenance::pg_repack') } - end - end - end -end diff --git a/spec/classes/pg_repack_spec.rb b/spec/classes/pg_repack_spec.rb new file mode 100644 index 0000000..e5454f9 --- /dev/null +++ b/spec/classes/pg_repack_spec.rb @@ -0,0 +1,74 @@ +require 'spec_helper' + +describe 'pe_databases::pg_repack' do + on_supported_os.each do |os, os_facts| + context "on #{os}" do + let(:pre_condition) do + "class { 'pe_databases': + manage_database_backups => false, + manage_postgresql_settings => false, + manage_table_settings => false, + }" + end + let(:facts) { os_facts } + + it { is_expected.to compile } + context 'on < PE 2019.3.0' do + before :each do + facts['pe_server_version'] = '2019.1.0' + facts['pe_postgresql_info']['installed_server_version'] = 9.6 + end + it { + # TODO: postgres versions + is_expected.to contain_service('pe_databases-resource_events.timer').with_ensure('running') + is_expected.to contain_service('pe_databases-reports.timer').with_ensure('running') + } + end + + context 'on < PE 2019.3.0 with postgresql 11' do + before :each do + facts['pe_server_version'] = '2019.2.0' + facts['pe_postgresql_info']['installed_server_version'] = 11 + end + it { + # TODO: postgres versions + is_expected.to contain_service('pe_databases-resource_events.timer').with_ensure('running') + is_expected.to contain_service('pe_databases-reports.timer').with_ensure('running') + } + end + + context 'on < PE 2019.7.0' do + before :each do + facts['pe_server_version'] = '2019.5.0' + facts['pe_postgresql_info']['installed_server_version'] = 11 + end + it { + # TODO: postgres versions + is_expected.to contain_service('pe_databases-reports.timer').with_ensure('running') + is_expected.not_to contain_service('pe_databases-resource_events.timer').with_ensure('running') + } + end + + context 'on >= PE 2019.7.0' do + before :each do + facts['pe_server_version'] = '2019.7.0' + facts['pe_postgresql_info']['installed_server_version'] = 11 + end + it { + is_expected.not_to contain_service('pe_databases-reports.timer').with_ensure('running') + is_expected.not_to contain_service('pe_databases-resource_events.timer').with_ensure('running') + } + end + + context 'on >= PE 2019.8.2' do + before :each do + facts['pe_server_version'] = '2019.8.2' + facts['pe_postgresql_info']['installed_server_version'] = 11 + end + it { + is_expected.to contain_service('pe_databases-catalogs.timer').with_ensure('running') + } + end + end + end +end diff --git a/spec/default_facts.yml b/spec/default_facts.yml index 05a1b85..aaa0499 100644 --- a/spec/default_facts.yml +++ b/spec/default_facts.yml @@ -6,9 +6,11 @@ ipaddress: "172.16.254.254" ipaddress6: "FE80:0000:0000:0000:AAAA:AAAA:AAAA" is_pe: false macaddress: "AA:AA:AA:AA:AA:AA" +pe_databases: + have_systemd: true pe_postgresql_info: - installed_server_version: 9.6 -pe_server_version: 2018.1.0 + installed_server_version: 11 +pe_server_version: 2019.8.6 memory: system: total: 16.00 GiB diff --git a/templates/service.epp b/templates/service.epp new file mode 100644 index 0000000..3d140e0 --- /dev/null +++ b/templates/service.epp @@ -0,0 +1,13 @@ +<%- | String $tables, String $command | -%> +[Unit] +Description=Service to repack PE database tables +Wants=pe_databases-<%= $tables%>.timer + +[Service] +User=pe-postgres +Group=pe-postgres +Type=oneshot +ExecStart=<%= $command %> + +[Install] +WantedBy=multi-user.target diff --git a/templates/timer.epp b/templates/timer.epp new file mode 100644 index 0000000..348bb62 --- /dev/null +++ b/templates/timer.epp @@ -0,0 +1,11 @@ +<%- | String $tables, String $on_cal | -%> +[Unit] +Description=Timer to trigger repacking PE database tables +Requires=pe_databases-<%= $tables %>.service + +[Timer] +OnCalendar=<%= $on_cal %> +Persistent=true + +[Install] +WantedBy=timers.target