Skip to content

Instantly share code, notes, and snippets.

@jamesstout
Last active May 8, 2020 13:48
Show Gist options
  • Save jamesstout/c19c227d1ac7dd37108ecbbf5c52eea7 to your computer and use it in GitHub Desktop.
Save jamesstout/c19c227d1ac7dd37108ecbbf5c52eea7 to your computer and use it in GitHub Desktop.
docker-image-refresh.sh
#!/usr/bin/env bash
logExt="$(date +%Y-%m-%d).log"
LOG_FILE="/var/services/homes/james/logs/$(basename "$0").$logExt"
WORK_DIR=$(mktemp -d)
# check if tmp dir was created
if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
echo "Could not create temp dir"
exit 1
fi
# deletes the temp directory
function cleanup {
rm -rf "$WORK_DIR"
}
# register the cleanup function to be called on the EXIT signal
trap cleanup EXIT
# get list of all image:rev
# not mariadb|redis as they cause issues
# not |headphones|nzbget as not used
for image in $(docker ps --all | grep -Ev 'mariadb|redis|headphones|nzbget|ID' | awk '{ print $2 }' | grep -v '\d+'); do
echo "$image"
docker pull "$image" | tee -a "$LOG_FILE"
done
# if the docker pull retreived newer images
# get the names, and email me
if [ "$(grep -c newer "$LOG_FILE")" -gt 0 ];
then
# the awk is to remove leading spaces .. causes issues with mail as does the Status:, hence the cut
grep newer "$LOG_FILE" | cut -d ":" -f2- | awk '{$1=$1};1' > "$WORK_DIR"/report.txt
cat > "$WORK_DIR"/mail.txt <<EOF
To: [email protected]
From: [email protected]
Subject: docker image refresh
EOF
cat "$WORK_DIR"/mail.txt "$WORK_DIR"/report.txt | /usr/bin/ssmtp [email protected]
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment