159 lines
5.6 KiB
Bash
Executable File
159 lines
5.6 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# A simple wrapper script to rotate the service's logs. Creates a little bit
|
|
# crafted logrotate configuration (if it doesn't exist yet) and calls the
|
|
# standard logrotate.
|
|
#
|
|
# Uses the copytruncate utility which must be available on path.
|
|
# Doesn't rotate logs for stopped services.
|
|
#
|
|
# Author: Kovács Zoltán <kovacs.zoltan@smartfront.hu>
|
|
# Kovács Zoltán <kovacsz@marcusconsulting.hu>
|
|
# License: GNU/GPL v3+ (https://www.gnu.org/licenses/gpl-3.0.en.html)
|
|
# 2023-06-18 v1.0
|
|
# new: forked from the "Smartfront's DOCKER_skeleton" repository.
|
|
# 2021-09-14 v0.2
|
|
# add: Rotates the web logs (if any) as well.
|
|
# 2021-09-02 v0.1 Initial release
|
|
|
|
# Accepted environment variables and their defaults.
|
|
PAR_BASEDIR=${PAR_BASEDIR:-""} # Service's base folder
|
|
PAR_CONFDIR=${PAR_CONFDIR:-""} # Folder containing configs
|
|
PAR_LOGDIR=${PAR_LOGDIR:-""} # Folder containing logs
|
|
|
|
# Other initialisations.
|
|
CONFDIR="configs" # Folder containing configs
|
|
LOGDIR="logs" # Folder containing logs
|
|
YMLFILE="docker-compose.yml"
|
|
|
|
# Messages.
|
|
MSG_MISSINGDEP="Fatal: missing dependency"
|
|
|
|
# Basic environment settings.
|
|
LANG=C
|
|
LC_ALL=C
|
|
# We need also the sbin directories.
|
|
if ! [[ "$PATH" =~ '/sbin:' ]]; then
|
|
PATH="$PATH:/usr/local/sbin:/usr/sbin:/sbin"; fi
|
|
|
|
# Checks the dependencies.
|
|
TR=$(which tr 2>/dev/null)
|
|
if [ -z "$TR" ]; then echo "$MSG_MISSINGDEP tr."; exit 1 ; fi
|
|
for item in basename date dirname docker-compose logrotate readlink
|
|
do
|
|
if [ -n "$(which $item)" ]
|
|
then export $(echo $item | "$TR" '[:lower:]' '[:upper:]' | "$TR" '-' '_')=$(which $item)
|
|
else echo "$MSG_MISSINGDEP $item." >&2; exit 1; fi
|
|
done
|
|
# All dependencies are available via "$THECOMMAND" (upper case) call.
|
|
|
|
# Where I'm?
|
|
# https://gist.github.com/TheMengzor/968e5ea87e99d9c41782
|
|
SOURCE="${BASH_SOURCE[0]}"
|
|
while [ -h "$SOURCE" ]; do
|
|
# resolve $SOURCE until the file is no longer a symlink
|
|
SCRPATH="$( cd -P "$( "$DIRNAME" "$SOURCE" )" && pwd )" #"
|
|
SOURCE="$("$READLINK" "$SOURCE")"
|
|
# if $SOURCE was a relative symlink, we need to resolve it
|
|
# relative to the path where the symlink file was located
|
|
[[ $SOURCE != /* ]] && SOURCE="$SCRPATH/$SOURCE"
|
|
done; SCRPATH="$( cd -P "$( "$DIRNAME" "$SOURCE" )" && pwd )" #"
|
|
SCRFILE="$("$BASENAME" "$(test -L "$0" && "$READLINK" "$0" || echo "$0")")" #"
|
|
|
|
# Searches the base folder, containing a docker-compose.yml file.
|
|
# Called from the base folder (./)?
|
|
BASE_DIR="$PAR_BASEDIR"
|
|
TEST_DIR="$SCRPATH"
|
|
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
|
|
# Called from ./tools?
|
|
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
|
|
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
|
|
# Called from ./tools/*.d?
|
|
TEST_DIR="$("$DIRNAME" "$TEST_DIR")"
|
|
[[ -z "$BASE_DIR" ]] && [[ -r "$TEST_DIR/$YMLFILE" ]] && BASE_DIR="$TEST_DIR"
|
|
# On failure gives it up here.
|
|
if [ -z "$BASE_DIR" -o ! -r "$BASE_DIR/$YMLFILE" ]; then
|
|
echo "$MSG_MISSINGYML" >&2; exit 1
|
|
fi
|
|
|
|
# Sets the absolute paths.
|
|
CONFDIR="${PAR_CONFDIR:-$BASE_DIR/$CONFDIR}"
|
|
CONFFILE="$CONFDIR/.${SCRFILE%.*}.conf"
|
|
STATEFILE="$CONFDIR/.${SCRFILE%.*}.state"
|
|
LOGDIR="${PAR_LOGDIR:-$BASE_DIR/$LOGDIR}"
|
|
|
|
# Doesn't rotate logs for stopped services.
|
|
[[ -z "$(cd "$BASE_DIR"; "$DOCKER_COMPOSE" ps --services --filter "status=running")" ]] \
|
|
&& exit 0
|
|
|
|
# Locates the worker script.
|
|
WORKERSCRIPT="$SCRPATH/copytruncate"
|
|
[[ ! -x "$WORKERSCRIPT" ]] && WORKERSCRIPT="$(which copytruncate)"
|
|
[[ ! -x "$WORKERSCRIPT" ]] \
|
|
&& echo -e "$MSG_MISSINGDEP $WORKERSCRIPT." >&2 \
|
|
&& exit 1
|
|
|
|
# Creates the configuration if it doesn't exist yet.
|
|
if [ ! -e "$CONFFILE" ]; then
|
|
cat > "$CONFFILE" << EOF
|
|
$LOGDIR/*.log {
|
|
missingok
|
|
daily
|
|
rotate 30
|
|
# Must be consistent with prerotate script's settings!
|
|
dateext
|
|
dateyesterday
|
|
dateformat %Y-%m-%d.
|
|
extension log
|
|
compress
|
|
# We'll use our own copytruncate script, because:
|
|
# * we haven't permission to change ownership, so the built-in copytruncate
|
|
# method would fail (this is a bug in logrotate, I think);
|
|
# * we haven't permission to reload the service, so the create new log method
|
|
# doesn't work - the service would still write to the already rotated file.
|
|
# The custom script:
|
|
# * copytruncates files having (by default) .log extesion and compresses the copy
|
|
# (and returns with 1 exit code, thus, the logrotate will skip this file).
|
|
# * does nothing with files having any other extensions - e.g .1, .2 and so on
|
|
# (and returns with 0 exit code, thus, the logrotate can process this file);
|
|
prerotate
|
|
$WORKERSCRIPT \$1
|
|
endscript
|
|
# Only if the prerotate script didn't process it yet.
|
|
copytruncate
|
|
}
|
|
|
|
$LOGDIR/web/*.log {
|
|
missingok
|
|
daily
|
|
rotate 60
|
|
# Must be consistent with prerotate script's settings!
|
|
dateext
|
|
dateyesterday
|
|
dateformat %Y-%m-%d.
|
|
extension log
|
|
compress
|
|
# We'll use our own copytruncate script, because:
|
|
# * we haven't permission to change ownership, so the built-in copytruncate
|
|
# method would fail (this is a bug in logrotate, I think);
|
|
# * we haven't permission to reload the service, so the create new log method
|
|
# doesn't work - the service would still write to the already rotated file.
|
|
# The custom script:
|
|
# * copytruncates files having (by default) .log extesion and compresses the copy
|
|
# (and returns with 1 exit code, thus, the logrotate will skip this file).
|
|
# * does nothing with files having any other extensions - e.g .1, .2 and so on
|
|
# (and returns with 0 exit code, thus, the logrotate can process this file);
|
|
prerotate
|
|
$WORKERSCRIPT \$1
|
|
endscript
|
|
# Only if the prerotate script didn't process it yet.
|
|
copytruncate
|
|
}
|
|
EOF
|
|
fi
|
|
|
|
# Rotates the logs.
|
|
"$LOGROTATE" -s "$STATEFILE" "$CONFFILE"
|
|
|
|
# That's all, Folks! :)
|