Allow unwinding nova DB manage cron jobs

Add enablement flags to classes managing Nova DB records purging and
archieving. With that change, cron jobs can be removed, if no longer
needed. For example, when the cron jobs were originally enabled but
later will be disabled so that users can always restore any deleted
instances.

Co-authored-by: Takashi Kajinami <tkajinam@redhat.com>
Signed-off-by: Bogdan Dobrelya <bdobreli@redhat.com>
Change-Id: I4b2c65b91b83c7ac64f909359585aa76c19d4705
This commit is contained in:
Bogdan Dobrelya
2023-03-09 15:40:34 +01:00
parent a50a1a95be
commit 64cc577ceb
6 changed files with 59 additions and 2 deletions

View File

@@ -88,6 +88,10 @@
# all cron jobs at the same time on all hosts this job is configured. # all cron jobs at the same time on all hosts this job is configured.
# Defaults to 0. # Defaults to 0.
# #
# [*ensure*]
# (optional) Ensure cron jobs present or absent
# Defaults to present.
#
class nova::cron::archive_deleted_rows ( class nova::cron::archive_deleted_rows (
$minute = 1, $minute = 1,
$hour = 0, $hour = 0,
@@ -105,6 +109,7 @@ class nova::cron::archive_deleted_rows (
$sleep = undef, $sleep = undef,
$verbose = false, $verbose = false,
$maxdelay = 0, $maxdelay = 0,
Enum['present', 'absent'] $ensure = 'present',
) inherits nova::params { ) inherits nova::params {
include nova::deps include nova::deps
@@ -165,6 +170,7 @@ class nova::cron::archive_deleted_rows (
$cron_cmd = 'nova-manage db archive_deleted_rows' $cron_cmd = 'nova-manage db archive_deleted_rows'
cron { 'nova-manage db archive_deleted_rows': cron { 'nova-manage db archive_deleted_rows':
ensure => $ensure,
# lint:ignore:140chars # lint:ignore:140chars
command => "${delay_cmd}${cron_cmd}${purge_real} --max_rows ${max_rows}${verbose_real}${age_real}${until_complete_real}${all_cells_real}${task_log_real}${sleep_real} >>${destination} 2>&1", command => "${delay_cmd}${cron_cmd}${purge_real} --max_rows ${max_rows}${verbose_real}${age_real}${until_complete_real}${all_cells_real}${task_log_real}${sleep_real} >>${destination} 2>&1",
# lint:endignore # lint:endignore

View File

@@ -60,7 +60,11 @@
# Induces a random delay before running the cronjob to avoid running # Induces a random delay before running the cronjob to avoid running
# all cron jobs at the same time on all hosts this job is configured. # all cron jobs at the same time on all hosts this job is configured.
# Defaults to 0. # Defaults to 0.
#
# [*ensure*]
# (optional) Ensure cron jobs present or absent
# Defaults to present.
#
class nova::cron::purge_shadow_tables ( class nova::cron::purge_shadow_tables (
$minute = 0, $minute = 0,
$hour = 5, $hour = 5,
@@ -73,6 +77,7 @@ class nova::cron::purge_shadow_tables (
$all_cells = false, $all_cells = false,
$verbose = false, $verbose = false,
$maxdelay = 0, $maxdelay = 0,
Enum['present', 'absent'] $ensure = 'present',
) inherits nova::params { ) inherits nova::params {
include nova::deps include nova::deps
@@ -101,6 +106,7 @@ class nova::cron::purge_shadow_tables (
$cron_cmd = 'nova-manage db purge' $cron_cmd = 'nova-manage db purge'
cron { 'nova-manage db purge': cron { 'nova-manage db purge':
ensure => $ensure,
# lint:ignore:140chars # lint:ignore:140chars
command => "${delay_cmd}${cron_cmd} --before `date --date='today - ${age} days' +\\%D`${verbose_real}${all_cells_real} >>${destination} 2>&1", command => "${delay_cmd}${cron_cmd} --before `date --date='today - ${age} days' +\\%D`${verbose_real}${all_cells_real} >>${destination} 2>&1",
# lint:endignore # lint:endignore

View File

@@ -0,0 +1,8 @@
---
fixes:
- |
The new parameters, ``nova::cron::archive_deleted_rows::ensure`` and
``nova::cron::purge_shadow_tables::ensure``, have been added.
These parameters control the corresponding cron jobs existence. When either
of two classes included, they can be given ``ensure => absent`` to unwind
existing cron jobs.

View File

@@ -39,7 +39,6 @@ describe 'nova::cache' do
:hashclient_retry_delay => '<SERVICE DEFAULT>', :hashclient_retry_delay => '<SERVICE DEFAULT>',
:dead_timeout => '<SERVICE DEFAULT>', :dead_timeout => '<SERVICE DEFAULT>',
:manage_backend_package => true, :manage_backend_package => true,
:manage_backend_package => true,
) )
end end
end end

View File

@@ -21,9 +21,22 @@ describe 'nova::cron::archive_deleted_rows' do
:destination => '/var/log/nova/nova-rowsflush.log' } :destination => '/var/log/nova/nova-rowsflush.log' }
end end
context 'ensure the cron job is absent' do
before :each do
params.merge!(
:ensure => :absent,
)
end
it 'removes the cron job' do
is_expected.to contain_cron('nova-manage db archive_deleted_rows').with_ensure(:absent)
end
end
context 'until_complete and all_cells is false' do context 'until_complete and all_cells is false' do
it 'configures a cron without until_complete and all_cells' do it 'configures a cron without until_complete and all_cells' do
is_expected.to contain_cron('nova-manage db archive_deleted_rows').with( is_expected.to contain_cron('nova-manage db archive_deleted_rows').with(
:ensure => :present,
:command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} >>#{params[:destination]} 2>&1", :command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],
@@ -46,6 +59,7 @@ describe 'nova::cron::archive_deleted_rows' do
it 'configures a cron with until_complete' do it 'configures a cron with until_complete' do
is_expected.to contain_cron('nova-manage db archive_deleted_rows').with( is_expected.to contain_cron('nova-manage db archive_deleted_rows').with(
:ensure => :present,
:command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} --verbose >>#{params[:destination]} 2>&1", :command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} --verbose >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],
@@ -68,6 +82,7 @@ describe 'nova::cron::archive_deleted_rows' do
it 'configures a cron with until_complete' do it 'configures a cron with until_complete' do
is_expected.to contain_cron('nova-manage db archive_deleted_rows').with( is_expected.to contain_cron('nova-manage db archive_deleted_rows').with(
:ensure => :present,
:command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} --until-complete >>#{params[:destination]} 2>&1", :command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} --until-complete >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],
@@ -90,6 +105,7 @@ describe 'nova::cron::archive_deleted_rows' do
it 'configures a cron with all_cells' do it 'configures a cron with all_cells' do
is_expected.to contain_cron('nova-manage db archive_deleted_rows').with( is_expected.to contain_cron('nova-manage db archive_deleted_rows').with(
:ensure => :present,
:command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} --all-cells >>#{params[:destination]} 2>&1", :command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} --all-cells >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],
@@ -112,6 +128,7 @@ describe 'nova::cron::archive_deleted_rows' do
it 'configures a cron with task_log' do it 'configures a cron with task_log' do
is_expected.to contain_cron('nova-manage db archive_deleted_rows').with( is_expected.to contain_cron('nova-manage db archive_deleted_rows').with(
:ensure => :present,
:command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} --task-log >>#{params[:destination]} 2>&1", :command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} --task-log >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],
@@ -134,6 +151,7 @@ describe 'nova::cron::archive_deleted_rows' do
it 'configures a cron with purge' do it 'configures a cron with purge' do
is_expected.to contain_cron('nova-manage db archive_deleted_rows').with( is_expected.to contain_cron('nova-manage db archive_deleted_rows').with(
:ensure => :present,
:command => "nova-manage db archive_deleted_rows --purge --max_rows #{params[:max_rows]} >>#{params[:destination]} 2>&1", :command => "nova-manage db archive_deleted_rows --purge --max_rows #{params[:max_rows]} >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],
@@ -157,6 +175,7 @@ describe 'nova::cron::archive_deleted_rows' do
it 'configures a cron with all purge params' do it 'configures a cron with all purge params' do
is_expected.to contain_cron('nova-manage db archive_deleted_rows').with( is_expected.to contain_cron('nova-manage db archive_deleted_rows').with(
:ensure => :present,
:command => "nova-manage db archive_deleted_rows --purge --max_rows #{params[:max_rows]} --until-complete >>#{params[:destination]} 2>&1", :command => "nova-manage db archive_deleted_rows --purge --max_rows #{params[:max_rows]} --until-complete >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],
@@ -179,6 +198,7 @@ describe 'nova::cron::archive_deleted_rows' do
it 'configures a cron with maxdelay' do it 'configures a cron with maxdelay' do
is_expected.to contain_cron('nova-manage db archive_deleted_rows').with( is_expected.to contain_cron('nova-manage db archive_deleted_rows').with(
:ensure => :present,
:command => "sleep `expr ${RANDOM} \\% #{params[:maxdelay]}`; nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} >>#{params[:destination]} 2>&1", :command => "sleep `expr ${RANDOM} \\% #{params[:maxdelay]}`; nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],
@@ -201,6 +221,7 @@ describe 'nova::cron::archive_deleted_rows' do
it 'configures a cron with --before' do it 'configures a cron with --before' do
is_expected.to contain_cron('nova-manage db archive_deleted_rows').with( is_expected.to contain_cron('nova-manage db archive_deleted_rows').with(
:ensure => :present,
:command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} --before `date --date='today - #{params[:age]} days' +\\%F` >>#{params[:destination]} 2>&1", :command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} --before `date --date='today - #{params[:age]} days' +\\%F` >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],
@@ -224,6 +245,7 @@ describe 'nova::cron::archive_deleted_rows' do
it 'configures a cron with --before' do it 'configures a cron with --before' do
is_expected.to contain_cron('nova-manage db archive_deleted_rows').with( is_expected.to contain_cron('nova-manage db archive_deleted_rows').with(
:ensure => :present,
:command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} --until-complete --sleep #{params[:sleep]} >>#{params[:destination]} 2>&1", :command => "nova-manage db archive_deleted_rows --max_rows #{params[:max_rows]} --until-complete --sleep #{params[:sleep]} >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],

View File

@@ -17,6 +17,18 @@ describe 'nova::cron::purge_shadow_tables' do
:age => 10 } :age => 10 }
end end
context 'ensure the cron job is absent' do
before :each do
params.merge!(
:ensure => :absent,
)
end
it 'removes the cron job' do
is_expected.to contain_cron('nova-manage db purge').with_ensure(:absent)
end
end
context 'verbose is true' do context 'verbose is true' do
before :each do before :each do
params.merge!( params.merge!(
@@ -26,6 +38,7 @@ describe 'nova::cron::purge_shadow_tables' do
it 'configures a nova purge cron with verbose output' do it 'configures a nova purge cron with verbose output' do
is_expected.to contain_cron('nova-manage db purge').with( is_expected.to contain_cron('nova-manage db purge').with(
:ensure => :present,
:command => "nova-manage db purge --before `date --date='today - #{params[:age]} days' +\\%D` --verbose >>#{params[:destination]} 2>&1", :command => "nova-manage db purge --before `date --date='today - #{params[:age]} days' +\\%D` --verbose >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],
@@ -48,6 +61,7 @@ describe 'nova::cron::purge_shadow_tables' do
it 'configures a nova purge cron without verbose output' do it 'configures a nova purge cron without verbose output' do
is_expected.to contain_cron('nova-manage db purge').with( is_expected.to contain_cron('nova-manage db purge').with(
:ensure => :present,
:command => "nova-manage db purge --before `date --date='today - #{params[:age]} days' +\\%D` >>#{params[:destination]} 2>&1", :command => "nova-manage db purge --before `date --date='today - #{params[:age]} days' +\\%D` >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],
@@ -70,6 +84,7 @@ describe 'nova::cron::purge_shadow_tables' do
it 'configures a nova purge cron with all cells enabled' do it 'configures a nova purge cron with all cells enabled' do
is_expected.to contain_cron('nova-manage db purge').with( is_expected.to contain_cron('nova-manage db purge').with(
:ensure => :present,
:command => "nova-manage db purge --before `date --date='today - #{params[:age]} days' +\\%D` --all-cells >>#{params[:destination]} 2>&1", :command => "nova-manage db purge --before `date --date='today - #{params[:age]} days' +\\%D` --all-cells >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],
@@ -92,6 +107,7 @@ describe 'nova::cron::purge_shadow_tables' do
it 'configures a nova purge cron with maxdelay' do it 'configures a nova purge cron with maxdelay' do
is_expected.to contain_cron('nova-manage db purge').with( is_expected.to contain_cron('nova-manage db purge').with(
:ensure => :present,
:command => "sleep `expr ${RANDOM} \\% #{params[:maxdelay]}`; nova-manage db purge --before `date --date='today - #{params[:age]} days' +\\%D` >>#{params[:destination]} 2>&1", :command => "sleep `expr ${RANDOM} \\% #{params[:maxdelay]}`; nova-manage db purge --before `date --date='today - #{params[:age]} days' +\\%D` >>#{params[:destination]} 2>&1",
:environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh',
:user => params[:user], :user => params[:user],