From 019883ba1d0eaf3d5c2d34c86ced924f0f4a4123 Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Fri, 15 Jan 2016 12:15:20 -0500 Subject: [PATCH] Implement crontab to purge deleted data heat-manage provides a tool to purge Heat deleted data [1]. This patch implements a crontab using the same interface as other modules. By default, it will purge that are more than 1 day old. [1] http://docs.openstack.org/developer/heat/man/heat-manage.html Change-Id: Ia2b80e5003450cd794ebb0c9ca72200ec8616e81 --- manifests/cron/purge_deleted.pp | 86 +++++++++++++++++ spec/acceptance/basic_heat_spec.rb | 4 + spec/classes/heat_cron_purge_deleted_spec.rb | 99 ++++++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 manifests/cron/purge_deleted.pp create mode 100644 spec/classes/heat_cron_purge_deleted_spec.rb diff --git a/manifests/cron/purge_deleted.pp b/manifests/cron/purge_deleted.pp new file mode 100644 index 00000000..a0d148b2 --- /dev/null +++ b/manifests/cron/purge_deleted.pp @@ -0,0 +1,86 @@ +# == Class: heat::cron::purge_deleted +# +# Installs a cron job to purge db entries marked as deleted and older than $age. +# Default will be 1 day. +# +# === Parameters +# +# [*ensure*] +# (optional) Defaults to present. +# Valid values are present, absent. +# +# [*minute*] +# (optional) Defaults to '1'. +# +# [*hour*] +# (optional) Defaults to '0'. +# +# [*monthday*] +# (optional) Defaults to '*'. +# +# [*month*] +# (optional) Defaults to '*'. +# +# [*weekday*] +# (optional) Defaults to '*'. +# +# [*maxdelay*] +# (optional) Seconds. Defaults to 0. Should be a positive integer. +# 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. +# +# [*user*] +# (optional) User with access to heat files. +# Defaults to 'heat'. +# +# [*age*] +# (optional) Age value for $age_type. +# Defaults to '1'. +# +# [*age_type*] +# (optional) Age type. +# Can be days, hours, minutes, seconds +# Defaults to 'days'. +# +# [*destination*] +# (optional) Path to file to which rows should be archived +# Defaults to '/var/log/heat/heat-purge_deleted.log'. +# +class heat::cron::purge_deleted ( + $ensure = present, + $minute = 1, + $hour = 0, + $monthday = '*', + $month = '*', + $weekday = '*', + $maxdelay = 0, + $user = 'heat', + $age = 1, + $age_type = 'days', + $destination = '/var/log/heat/heat-purge_deleted.log' +) { + + if ! member(['days', 'hours', 'minutes', 'seconds'], $age_type) { + fail('age_type possible values are only days, hours, minutes, or seconds.') + } + + if $maxdelay == 0 { + $sleep = '' + } else { + $sleep = "sleep `expr \${RANDOM} \\% ${maxdelay}`; " + } + + cron { 'heat-manage purge_deleted': + ensure => $ensure, + command => "${sleep}heat-manage purge_deleted -g ${age_type} ${age} >>${destination} 2>&1", + environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', + user => $user, + minute => $minute, + hour => $hour, + monthday => $monthday, + month => $month, + weekday => $weekday + } + + Package['heat-common'] -> Cron['heat-manage purge_deleted'] +} diff --git a/spec/acceptance/basic_heat_spec.rb b/spec/acceptance/basic_heat_spec.rb index 9021f294..04a9cd30 100644 --- a/spec/acceptance/basic_heat_spec.rb +++ b/spec/acceptance/basic_heat_spec.rb @@ -55,6 +55,7 @@ describe 'basic heat' do } class { '::heat::api_cloudwatch': } class { '::heat::api_cfn': } + class { '::heat::cron::purge_deleted': } EOS @@ -75,5 +76,8 @@ describe 'basic heat' do it { is_expected.to be_listening.with('tcp') } end + describe cron do + it { is_expected.to have_entry('1 0 * * * heat-manage purge_deleted -g days 1 >>/var/log/heat/heat-purge_deleted.log 2>&1').with_user('heat') } + end end end diff --git a/spec/classes/heat_cron_purge_deleted_spec.rb b/spec/classes/heat_cron_purge_deleted_spec.rb new file mode 100644 index 00000000..f157ed73 --- /dev/null +++ b/spec/classes/heat_cron_purge_deleted_spec.rb @@ -0,0 +1,99 @@ +require 'spec_helper' + +describe 'heat::cron::purge_deleted' do + + let :facts do + @default_facts.merge({ :osfamily => 'Debian' }) + end + + let :params do + { :ensure => 'present', + :minute => 1, + :hour => 0, + :monthday => '*', + :month => '*', + :weekday => '*', + :maxdelay => 0, + :user => 'heat', + :age => 1, + :age_type => 'days', + :destination => '/var/log/heat/heat-purge_deleted.log' } + end + + let :pre_condition do + 'include ::heat' + end + + describe 'with default parameters' do + it 'configures a cron' do + is_expected.to contain_cron('heat-manage purge_deleted').with( + :ensure => params[:ensure], + :command => "heat-manage purge_deleted -g days 1 >>#{params[:destination]} 2>&1", + :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', + :user => 'heat', + :minute => params[:minute], + :hour => params[:hour], + :monthday => params[:monthday], + :month => params[:month], + :weekday => params[:weekday] + ) + is_expected.to contain_package('heat-common').that_comes_before('Cron[heat-manage purge_deleted]') + end + end + + describe 'when specifying a maxdelay param' do + before :each do + params.merge!( + :maxdelay => 600 + ) + end + + it 'configures a cron with delay' do + is_expected.to contain_cron('heat-manage purge_deleted').with( + :ensure => params[:ensure], + :command => "sleep `expr ${RANDOM} \\% #{params[:maxdelay]}`; heat-manage purge_deleted -g days 1 >>#{params[:destination]} 2>&1", + :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', + :user => 'heat', + :minute => params[:minute], + :hour => params[:hour], + :monthday => params[:monthday], + :month => params[:month], + :weekday => params[:weekday] + ) + is_expected.to contain_package('heat-common').that_comes_before('Cron[heat-manage purge_deleted]') + end + end + + describe 'when disabling cron job' do + before :each do + params.merge!( + :ensure => 'absent' + ) + end + + it 'configures a cron with delay' do + is_expected.to contain_cron('heat-manage purge_deleted').with( + :ensure => params[:ensure], + :command => "heat-manage purge_deleted -g days 1 >>#{params[:destination]} 2>&1", + :environment => 'PATH=/bin:/usr/bin:/usr/sbin SHELL=/bin/sh', + :user => 'heat', + :minute => params[:minute], + :hour => params[:hour], + :monthday => params[:monthday], + :month => params[:month], + :weekday => params[:weekday] + ) + is_expected.to contain_package('heat-common').that_comes_before('Cron[heat-manage purge_deleted]') + end + end + + describe 'when setting a wrong age_type' do + before :each do + params.merge!( + :age_type => 'foobar' + ) + end + + it_raises 'a Puppet::Error', /age_type possible values are only days, hours, minutes, or seconds./ + end +end