Skip to content

Commit 3e364df

Browse files
authored
Merge pull request #1159 from webcompas/main
Support "hourly" and Integer as value for update frequency (fixes #1157)
2 parents 278a343 + 86826d2 commit 3e364df

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ If you raise the priority through the `pin` parameter to 500, normal policy goes
119119

120120
### Update the list of packages
121121

122-
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':
122+
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:
123123

124124
```puppet
125125
class { 'apt':

REFERENCE.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,11 @@ Options:
168168
`apt-get update` runs regardless of this value.
169169
Valid options:
170170
'always' (at every Puppet run);
171-
daily' (if the value of `apt_update_last_success` is less than current epoch time minus 86400);
171+
'hourly' (if the value of `apt_update_last_success` is less than current epoch time minus 3600);
172+
'daily' (if the value of `apt_update_last_success` is less than current epoch time minus 86400);
172173
'weekly' (if the value of `apt_update_last_success` is less than current epoch time minus 604800);
173174
'reluctantly' (only if the exec resource `apt_update` is notified).
175+
Integer (if the value of `apt_update_last_success` is less than current epoch time minus provided Integer value);
174176
Default: 'reluctantly'.
175177
* **:loglevel** `Integer`: Specifies the log level of logs outputted to the console. Default: undef.
176178
* **:timeout** `Integer`: Specifies how long to wait for the update to complete before canceling it. Valid options: an integer, in seconds. Default: undef.

manifests/init.pp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@
4141
# `apt-get update` runs regardless of this value.
4242
# Valid options:
4343
# 'always' (at every Puppet run);
44-
# daily' (if the value of `apt_update_last_success` is less than current epoch time minus 86400);
44+
# 'hourly' (if the value of `apt_update_last_success` is less than current epoch time minus 3600);
45+
# 'daily' (if the value of `apt_update_last_success` is less than current epoch time minus 86400);
4546
# 'weekly' (if the value of `apt_update_last_success` is less than current epoch time minus 604800);
47+
# Integer (if the value of `apt_update_last_success` is less than current epoch time minus provided Integer value);
4648
# 'reluctantly' (only if the exec resource `apt_update` is notified).
4749
# Default: 'reluctantly'.
4850
#
@@ -193,7 +195,7 @@
193195

194196
if $update['frequency'] {
195197
assert_type(
196-
Enum['always','daily','weekly','reluctantly'],
198+
Variant[Enum['always','hourly','daily','weekly','reluctantly'],Integer[60]],
197199
$update['frequency'],
198200
)
199201
}

manifests/update.pp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,36 @@
1414
'always': {
1515
$_kick_apt = true
1616
}
17+
Integer[60]:{
18+
#compare current date with the apt_update_last_success fact to determine
19+
#if we should kick apt_update.
20+
$int_threshold = (Integer(Timestamp().strftime('%s')) - Integer($apt::_update['frequency']))
21+
if $facts['apt_update_last_success'] {
22+
if $facts['apt_update_last_success'] + 0 < $int_threshold {
23+
$_kick_apt = true
24+
} else {
25+
$_kick_apt = false
26+
}
27+
} else {
28+
#if apt-get update has not successfully run, we should kick apt_update
29+
$_kick_apt = true
30+
}
31+
}
32+
'hourly':{
33+
#compare current date with the apt_update_last_success fact to determine
34+
#if we should kick apt_update.
35+
$hourly_threshold = (Integer(Timestamp().strftime('%s')) - 3600)
36+
if $facts['apt_update_last_success'] {
37+
if $facts['apt_update_last_success'] + 0 < $hourly_threshold {
38+
$_kick_apt = true
39+
} else {
40+
$_kick_apt = false
41+
}
42+
} else {
43+
#if apt-get update has not successfully run, we should kick apt_update
44+
$_kick_apt = true
45+
}
46+
}
1747
'daily': {
1848
#compare current date with the apt_update_last_success fact to determine
1949
#if we should kick apt_update.

spec/classes/apt_update_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
end
153153
end
154154

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

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

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

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

0 commit comments

Comments
 (0)