Skip to content

Commit

Permalink
Merge pull request #1159 from webcompas/main
Browse files Browse the repository at this point in the history
Support "hourly" and Integer as value for update frequency (fixes #1157)
  • Loading branch information
bastelfreak authored Feb 15, 2024
2 parents 278a343 + 86826d2 commit 3e364df
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ If you raise the priority through the `pin` parameter to 500, normal policy goes

### Update the list of packages

By default, Puppet runs `apt-get update` on the first Puppet run after you include the `apt` class, and anytime `notify => Exec['apt_update']` occurs; i.e., whenever config files get updated or other relevant changes occur. If you set `update['frequency']` to 'always', the update runs on every Puppet run. You can also set `update['frequency']` to 'daily' or 'weekly':
By default, Puppet runs `apt-get update` on the first Puppet run after you include the `apt` class, and anytime `notify => Exec['apt_update']` occurs; i.e., whenever config files get updated or other relevant changes occur. If you set `update['frequency']` to 'always', the update runs on every Puppet run. You can also set `update['frequency']` to 'hourly', 'daily', 'weekly' or any integer value >= 60:

```puppet
class { 'apt':
Expand Down
4 changes: 3 additions & 1 deletion REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,11 @@ Options:
`apt-get update` runs regardless of this value.
Valid options:
'always' (at every Puppet run);
daily' (if the value of `apt_update_last_success` is less than current epoch time minus 86400);
'hourly' (if the value of `apt_update_last_success` is less than current epoch time minus 3600);
'daily' (if the value of `apt_update_last_success` is less than current epoch time minus 86400);
'weekly' (if the value of `apt_update_last_success` is less than current epoch time minus 604800);
'reluctantly' (only if the exec resource `apt_update` is notified).
Integer (if the value of `apt_update_last_success` is less than current epoch time minus provided Integer value);
Default: 'reluctantly'.
* **:loglevel** `Integer`: Specifies the log level of logs outputted to the console. Default: undef.
* **:timeout** `Integer`: Specifies how long to wait for the update to complete before canceling it. Valid options: an integer, in seconds. Default: undef.
Expand Down
6 changes: 4 additions & 2 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
# `apt-get update` runs regardless of this value.
# Valid options:
# 'always' (at every Puppet run);
# daily' (if the value of `apt_update_last_success` is less than current epoch time minus 86400);
# 'hourly' (if the value of `apt_update_last_success` is less than current epoch time minus 3600);
# 'daily' (if the value of `apt_update_last_success` is less than current epoch time minus 86400);
# 'weekly' (if the value of `apt_update_last_success` is less than current epoch time minus 604800);
# Integer (if the value of `apt_update_last_success` is less than current epoch time minus provided Integer value);
# 'reluctantly' (only if the exec resource `apt_update` is notified).
# Default: 'reluctantly'.
#
Expand Down Expand Up @@ -193,7 +195,7 @@

if $update['frequency'] {
assert_type(
Enum['always','daily','weekly','reluctantly'],
Variant[Enum['always','hourly','daily','weekly','reluctantly'],Integer[60]],
$update['frequency'],
)
}
Expand Down
30 changes: 30 additions & 0 deletions manifests/update.pp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@
'always': {
$_kick_apt = true
}
Integer[60]:{
#compare current date with the apt_update_last_success fact to determine
#if we should kick apt_update.
$int_threshold = (Integer(Timestamp().strftime('%s')) - Integer($apt::_update['frequency']))
if $facts['apt_update_last_success'] {
if $facts['apt_update_last_success'] + 0 < $int_threshold {
$_kick_apt = true
} else {
$_kick_apt = false
}
} else {
#if apt-get update has not successfully run, we should kick apt_update
$_kick_apt = true
}
}
'hourly':{
#compare current date with the apt_update_last_success fact to determine
#if we should kick apt_update.
$hourly_threshold = (Integer(Timestamp().strftime('%s')) - 3600)
if $facts['apt_update_last_success'] {
if $facts['apt_update_last_success'] + 0 < $hourly_threshold {
$_kick_apt = true
} else {
$_kick_apt = false
}
} else {
#if apt-get update has not successfully run, we should kick apt_update
$_kick_apt = true
}
}
'daily': {
#compare current date with the apt_update_last_success fact to determine
#if we should kick apt_update.
Expand Down
8 changes: 4 additions & 4 deletions spec/classes/apt_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
end
end

['daily', 'weekly'].each do |update_frequency|
['hourly', 'daily', 'weekly', 60].each do |update_frequency|
context "when apt::update['frequency'] has the value of #{update_frequency}" do
pair = { 'we are due for a run' => 1_406_660_561, 'the update-success-stamp file does not exist' => -1 }
pair.each_pair do |desc, factval|
Expand All @@ -174,7 +174,7 @@
apt_update_last_success: factval
}
end
let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
let(:pre_condition) { "class{ '::apt': update => {'frequency' => #{update_frequency.inspect},} }" }

it 'triggers an apt-get update run' do
# set the apt_update exec\'s refreshonly attribute to false
Expand All @@ -200,7 +200,7 @@
apt_update_last_success: Time.now.to_i
}
end
let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
let(:pre_condition) { "class{ '::apt': update => {'frequency' => #{update_frequency.inspect},} }" }

it 'does not trigger an apt-get update run' do
# don't change the apt_update exec\'s refreshonly attribute. (it should be true)
Expand All @@ -226,7 +226,7 @@
apt_update_last_success: nil
}
end
let(:pre_condition) { "class{ '::apt': update => {'frequency' => '#{update_frequency}',} }" }
let(:pre_condition) { "class{ '::apt': update => {'frequency' => #{update_frequency.inspect},} }" }

it 'triggers an apt-get update run' do
# set the apt_update exec\'s refreshonly attribute to false
Expand Down

0 comments on commit 3e364df

Please sign in to comment.