|
| 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