Files
cloudron-box/setup/container/docker_janitor
2015-10-14 23:21:03 -07:00

41 lines
1.0 KiB
Bash
Executable File

#!/bin/bash
set -eu -o pipefail
# this config matches the docker log-file configuration
# logrotate ignores daily/monthly etc with size directive
readonly logrotate_template=$(cat <<"EOF"
"$LOG_VOLUME/*" "$LOG_VOLUME/*/*" "$LOG_VOLUME/*/*/*" {
missingok
rotate 4
size 5M
nocompress
copytruncate
notifempty
create
su
maxage 7
}
EOF
)
readonly containers=$(docker ps -qa)
for container in $containers; do
echo "Cleaning up $container"
if logdir=$(docker inspect --format='{{index .Volumes "/var/log"}}' $container); then
echo -e "\tLogrotate files under $logdir"
logrotate_config=$(mktemp)
{ echo "$logrotate_template" | LOG_VOLUME="$logdir" envsubst; } > "${logrotate_config}"
logrotate "${logrotate_config}" # -f is not required since we are size based
find "${logdir}" -mindepth 3 -maxdepth 3 -type d -exec rm -rf {} + # since we logrotate only till depth 3
rm "${logrotate_config}"
fi
done