Showing with 16 additions and 6 deletions.
  1. +5 −0 CHANGELOG.md
  2. +10 −5 files/metrics_tidy
  3. +1 −1 metadata.json
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Patch Release 6.4.1

## Changes:
- Standardize cleanup of temp files [#88](https://github.com/puppetlabs/puppetlabs-puppet_metrics_collector/pull/88)

# Minor Release 6.4.0

## Improvements
Expand Down
15 changes: 10 additions & 5 deletions files/metrics_tidy
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
fail() {
# Restore stdout by pointing it to fd 3 and send any errors to it
exec >&3
cat "$tmp"
rm "$tmp"
[[ -e $tmp ]] && cat "$tmp"

exit 1
}

cleanup() {
rm "$tmp"
for f in "${temp_files[@]}"; do
[[ -e $f ]] && rm -- "$f"
done
}

temp_files=()

# Clone, i.e. preserve, original stdout using fd 3.
exec 3>&1
# Send stderr and stdout to a temp file
tmp="$(mktemp)"
temp_files+=("$tmp")
exec &>"$tmp"

# Run the fail() method on error
Expand Down Expand Up @@ -61,11 +65,12 @@ find "$metrics_directory" -type f -ctime +"$retention_days" -delete
# Compress the remaining files in a Puppet service metrics directory.
# Store the list of files in a temp file so that `tar` and `rm` will operate on the same files
metrics_tmp="$(mktemp)"
temp_files+=("$metrics_tmp")
find "$metrics_directory" -type f -name "*json" >"$metrics_tmp"
tar --create --gzip --file "${metrics_directory}/${metrics_type}-$(date +%Y.%m.%d.%H.%M.%S).tar.gz" \
--files-from "$metrics_tmp"

# Cleanup the backed up json files so that we do not duplicate files in the tarballs.
# We can assume that the json files have no spaces as they are created by our scripts
xargs -a "$metrics_tmp" rm
rm "$metrics_tmp"
# Only run xargs if the file is >0 bytes
[[ -s $metrics_tmp ]] && xargs -a "$metrics_tmp" rm
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "puppetlabs-puppet_metrics_collector",
"version": "6.4.0",
"version": "6.4.1",
"author": "npwalker",
"summary": "A Puppet module for gathering metrics from PE components",
"license": "Apache-2.0",
Expand Down