Skip to content

Commit d9985d8

Browse files
committed
MODULES-10763 Do not report apt-get update as a change
1 parent 0a23900 commit d9985d8

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

Diff for: manifests/update.pp

+14-5
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,22 @@
5656
} else {
5757
$_refresh = true
5858
}
59+
# We perform the update in an `unless` clause of the exec, and
60+
# return true only if the package cache file changed.
61+
# This ensures that Puppet does not report a change if the
62+
# update command had no effect. See MODULES-10763 for discussion.
63+
$apt_update_had_no_effect = epp(
64+
'apt/update_had_no_effect.sh.epp',
65+
'provider' => $apt::provider,
66+
'timeout' => $apt::_update['timeout'],
67+
'tries' => $apt::_update['tries'],
68+
)
5969
exec { 'apt_update':
60-
command => "${apt::provider} update",
70+
command => "echo ${apt::provider} successfully updated the package cache.",
6171
loglevel => $apt::_update['loglevel'],
62-
logoutput => 'on_failure',
72+
logoutput => true,
73+
provider => shell,
6374
refreshonly => $_refresh,
64-
timeout => $apt::_update['timeout'],
65-
tries => $apt::_update['tries'],
66-
try_sleep => 1,
75+
unless => $apt_update_had_no_effect,
6776
}
6877
}

Diff for: templates/update_had_no_effect.sh.epp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<%- |
2+
String $provider = 'apt',
3+
Integer $timeout = 30,
4+
Integer $tries = 1,
5+
| -%>
6+
7+
export PATH=/usr/bin:/bin:/usr/sbin:/sbin
8+
<%# Since `mktemp` might not be available, we choose a reasonable default. -%>
9+
TMPFILE="$(mktemp)" || TMPFILE=/tmp/.puppetlabs.apt.update_had_no_effect.sh
10+
<%# Try to prevent command injection by truncating immediately before using. -%>
11+
cat /dev/null > "$TMPFILE"
12+
<%# Retrieve the configured apt-cache directory. -%>
13+
apt-config shell DIR Dir::Cache > "$TMPFILE" && . "$TMPFILE"
14+
<%# Set a reasonable default in case `apt-config shell` didn't work. -%>
15+
[ "$DIR" ] || DIR='var/cache/apt'
16+
<%# Early exit if the cache directory doesn't exist. -%>
17+
cd "/$DIR" || exit 0
18+
<%# Try to prevent command injection by truncating immediately before using. -%>
19+
cat /dev/null > "$TMPFILE"
20+
<%# Retrieve the configured cache filename. -%>
21+
apt-config shell CUR DIR::Cache::pkgcache >"$TMPFILE" && . "$TMPFILE"
22+
<%# Set a reasonable default in case `apt-config shell` didn't work. -%>
23+
[ "$CUR" ] || CUR=pkgcache.bin
24+
<%# If the cache file doesn't exist, create it as an empty file. -%>
25+
[ -e "$CUR" ] || cat /dev/null > "$CUR"
26+
<%# Copy the cache file contents so we can detect changes. -%>
27+
cat "$CUR" > "$TMPFILE"
28+
<%# Loop for the configured number of tries. -%>
29+
TRIES=<%= $tries %>
30+
while true; do
31+
<%# Use the `timeout` command from GNU coretools if available. -%>
32+
if timeout 1 true; then
33+
timeout <%= $timeout %>m <%= $provider %> update && break
34+
else
35+
<%= $provider %> update && break
36+
fi
37+
<%# Exit if the number of configured tries has been reached. -%>
38+
[ $TRIES -le 1 ] && break
39+
<%# Emulate `try_sleep => 1` from the original `exec` resource -%>
40+
sleep 1
41+
<%# Decrement the loop count -%>
42+
TRIES=$(( TRIES - 1 ))
43+
done
44+
<%# Set the exit code to failure (1) presuming a change occurred. -%>
45+
EXITCODE=1
46+
<%# Guard against a missing package cache file. -%>
47+
[ -e "$CUR" ] || cat /dev/null > "$CUR"
48+
<%# Set the exit code to success (0) if no change occurred. -%>
49+
cmp "$CUR" "$TMPFILE" && EXITCODE=0
50+
<%# Clean up -%>
51+
rm -f "$TMPFILE"
52+
exit $EXITCODE

0 commit comments

Comments
 (0)