39 lines
1.0 KiB
Bash
Executable File
39 lines
1.0 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
# Remove certificates of hosts that both:
|
|
# - have been down for more than 30 days;
|
|
# - are no longer in the hosts file.
|
|
|
|
set -o errexit # (-e) exit immediately if any command has a non-zero exit status
|
|
set -o nounset # (-u) don't accept undefined variables
|
|
#set -o xtrace # for debugging
|
|
|
|
timestamp_start=$(date +%s)
|
|
|
|
# Go where this script is.
|
|
cd "$(dirname "$0")" || exit
|
|
|
|
# Go through certs of hosts that have been down for more than 30 days.
|
|
find certs -mtime +30 -type f -execdir sh -c '
|
|
cert_file="$1"
|
|
host=$(expr "$cert_file" : "^\.\/\(.*\)\:[0-9]*\.pem$")
|
|
port=$(expr "$cert_file" : "^\.\/.*\:\([0-9]*\)\.pem$")
|
|
|
|
# Append port if not default
|
|
if [ "$port" != 1965 ]; then
|
|
host="$host:$port"
|
|
fi
|
|
|
|
# If it is not in the hosts file, delete it.
|
|
if ! grep -xq "$host" ../hosts; then
|
|
echo "Pruning $host"
|
|
rm "$cert_file"
|
|
fi
|
|
' sh {} \;
|
|
|
|
timestamp_end=$(date +%s)
|
|
exec_time="$((timestamp_end - timestamp_start))"
|
|
exec_time_formatted="$(date -d "@$exec_time" --utc "+%H:%M:%S")"
|
|
|
|
echo "OK (duration: $exec_time_formatted)"
|