Skip to content

Commit 73f1e75

Browse files
committed
MODULES-10763 Do not report apt-get update as a change
1 parent 6ab5dc4 commit 73f1e75

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

manifests/update.pp

+14-2
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,25 @@
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,
6475
timeout => $apt::_update['timeout'],
6576
tries => $apt::_update['tries'],
6677
try_sleep => 1,
78+
unless => $apt_update_had_no_effect,
6779
}
6880
}

templates/update_had_no_effect.sh.epp

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

0 commit comments

Comments
 (0)