mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Refactor ynh_use_logrotate madness
This commit is contained in:
parent
4ce101b5c6
commit
308ed0e174
1 changed files with 45 additions and 58 deletions
|
@ -1,82 +1,60 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
FIRST_CALL_TO_LOGROTATE="true"
|
||||||
|
|
||||||
# Use logrotate to manage the logfile
|
# Use logrotate to manage the logfile
|
||||||
#
|
#
|
||||||
# usage: ynh_use_logrotate [--logfile=/log/file] [--nonappend] [--specific_user=user/group]
|
# usage: ynh_use_logrotate [--logfile=/log/file] [--specific_user=user/group]
|
||||||
# | arg: -l, --logfile= - absolute path of logfile
|
# | arg: -l, --logfile= - absolute path of logfile
|
||||||
# | arg: -n, --nonappend - (optional) Replace the config file instead of appending this new config.
|
|
||||||
# | arg: -u, --specific_user= - run logrotate as the specified user and group. If not specified logrotate is runned as root.
|
# | arg: -u, --specific_user= - run logrotate as the specified user and group. If not specified logrotate is runned as root.
|
||||||
#
|
#
|
||||||
# If no `--logfile` is provided, `/var/log/$app` will be used as default.
|
# If no `--logfile` is provided, `/var/log/$app` will be used as default.
|
||||||
# `logfile` can point to a directory or a file.
|
# `logfile` can point to a directory or a file.
|
||||||
#
|
#
|
||||||
# It's possible to use this helper multiple times, each config will be added to
|
|
||||||
# the same logrotate config file. Unless you use the option `--non-append`
|
|
||||||
#
|
|
||||||
# Requires YunoHost version 2.6.4 or higher.
|
# Requires YunoHost version 2.6.4 or higher.
|
||||||
# Requires YunoHost version 3.2.0 or higher for the argument `--specific_user`
|
|
||||||
ynh_use_logrotate() {
|
ynh_use_logrotate() {
|
||||||
|
|
||||||
# Stupid patch to remplace --non-append by --nonappend
|
# Stupid patch to ignore legacy --non-append and --nonappend
|
||||||
# Because for some reason --non-append was supposed to be legacy
|
# which was never properly understood and improperly used and kind of bullshit
|
||||||
# (why is it legacy ? Idk maybe because getopts cant parse args with - in their names..)
|
|
||||||
# but there was no good communication about this, and now --non-append
|
|
||||||
# is still the most-used option, yet it was parsed with batshit stupid code
|
|
||||||
# So instead this loops over the positional args, and replace --non-append
|
|
||||||
# with --nonappend so it's transperent for the rest of the function...
|
|
||||||
local all_args=( ${@} )
|
local all_args=( ${@} )
|
||||||
for I in $(seq 0 $(($# - 1)))
|
for I in $(seq 0 $(($# - 1)))
|
||||||
do
|
do
|
||||||
if [[ "${all_args[$I]}" == "--non-append" ]]
|
if [[ "${all_args[$I]}" == "--non-append" ]] || [[ "${all_args[$I]}" == "--nonappend" ]]
|
||||||
then
|
then
|
||||||
all_args[$I]="--nonappend"
|
unset all_args[$I]
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
set -- "${all_args[@]}"
|
set -- "${all_args[@]}"
|
||||||
|
|
||||||
# Declare an array to define the options of this helper.
|
# Argument parsing
|
||||||
local legacy_args=lnu
|
local legacy_args=lu
|
||||||
local -A args_array=([l]=logfile= [n]=nonappend [u]=specific_user=)
|
local -A args_array=([l]=logfile= [u]=specific_user=)
|
||||||
local logfile
|
local logfile
|
||||||
local nonappend
|
|
||||||
local specific_user
|
local specific_user
|
||||||
# Manage arguments with getopts
|
|
||||||
ynh_handle_getopts_args "$@"
|
ynh_handle_getopts_args "$@"
|
||||||
logfile="${logfile:-}"
|
logfile="${logfile:-}"
|
||||||
nonappend="${nonappend:-0}"
|
|
||||||
specific_user="${specific_user:-}"
|
specific_user="${specific_user:-}"
|
||||||
|
|
||||||
# LEGACY CODE - PRE GETOPTS
|
set -o noglob
|
||||||
if [ $# -gt 0 ] && [ "$(echo ${1:0:1})" != "-" ]; then
|
if [[ -z "$logfile" ]]; then
|
||||||
# If the given logfile parameter already exists as a file, or if it ends up with ".log",
|
logfile="/var/log/${app}/*.log"
|
||||||
# we just want to manage a single file
|
elif [[ "${logfile##*.}" != "log" ]] && [[ "${logfile##*.}" != "txt" ]]; then
|
||||||
if [ -f "$1" ] || [ "$(echo ${1##*.})" == "log" ]; then
|
logfile="$logfile/*.log"
|
||||||
local logfile=$1
|
|
||||||
# Otherwise we assume we want to manage a directory and all its .log file inside
|
|
||||||
else
|
|
||||||
local logfile=$1/*.log
|
|
||||||
fi
|
fi
|
||||||
fi
|
set +o noglob
|
||||||
# LEGACY CODE
|
|
||||||
|
for stuff in $logfile
|
||||||
|
do
|
||||||
|
mkdir --parents $(dirname "$stuff")
|
||||||
|
done
|
||||||
|
|
||||||
local customtee="tee --append"
|
|
||||||
if [ "$nonappend" -eq 1 ]; then
|
|
||||||
customtee="tee"
|
|
||||||
fi
|
|
||||||
if [ -n "$logfile" ]; then
|
|
||||||
if [ ! -f "$1" ] && [ "$(echo ${logfile##*.})" != "log" ]; then # Keep only the extension to check if it's a logfile
|
|
||||||
local logfile="$logfile/*.log" # Else, uses the directory and all logfile into it.
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
logfile="/var/log/${app}/*.log" # Without argument, use a defaut directory in /var/log
|
|
||||||
fi
|
|
||||||
local su_directive=""
|
local su_directive=""
|
||||||
if [[ -n $specific_user ]]; then
|
if [[ -n "$specific_user" ]]; then
|
||||||
su_directive=" # Run logorotate as specific user - group
|
su_directive="su ${specific_user%/*} ${specific_user#*/}"
|
||||||
su ${specific_user%/*} ${specific_user#*/}"
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
cat >./${app}-logrotate <<EOF # Build a config file for logrotate
|
local tempconf="$(mktemp)"
|
||||||
|
cat << EOF >$tempconf
|
||||||
$logfile {
|
$logfile {
|
||||||
# Rotate if the logfile exceeds 100Mo
|
# Rotate if the logfile exceeds 100Mo
|
||||||
size 100M
|
size 100M
|
||||||
|
@ -86,20 +64,29 @@ $logfile {
|
||||||
compress
|
compress
|
||||||
# Compress the log at the next cycle. So keep always 2 non compressed logs
|
# Compress the log at the next cycle. So keep always 2 non compressed logs
|
||||||
delaycompress
|
delaycompress
|
||||||
# Copy and truncate the log to allow to continue write on it. Instead of move the log.
|
# Copy and truncate the log to allow to continue write on it. Instead of moving the log.
|
||||||
copytruncate
|
copytruncate
|
||||||
# Do not do an error if the log is missing
|
# Do not trigger an error if the log is missing
|
||||||
missingok
|
missingok
|
||||||
# Not rotate if the log is empty
|
# Do not rotate if the log is empty
|
||||||
notifempty
|
notifempty
|
||||||
# Keep old logs in the same dir
|
# Keep old logs in the same dir
|
||||||
noolddir
|
noolddir
|
||||||
$su_directive
|
$su_directive
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
mkdir --parents $(dirname "$logfile") # Create the log directory, if not exist
|
|
||||||
cat ${app}-logrotate | $customtee /etc/logrotate.d/$app >/dev/null # Append this config to the existing config file, or replace the whole config file (depending on $customtee)
|
if [[ "$FIRST_CALL_TO_LOGROTATE" == "true" ]]
|
||||||
chmod 644 "/etc/logrotate.d/$app" # Make sure permissions are correct (otherwise the config file could be ignored and the corresponding logs never rotated)
|
then
|
||||||
|
cat $tempconf > /etc/logrotate.d/$app
|
||||||
|
else
|
||||||
|
cat $tempconf >> /etc/logrotate.d/$app
|
||||||
|
fi
|
||||||
|
|
||||||
|
FIRST_CALL_TO_LOGROTATE="false"
|
||||||
|
|
||||||
|
# Make sure permissions are correct (otherwise the config file could be ignored and the corresponding logs never rotated)
|
||||||
|
chmod 644 "/etc/logrotate.d/$app"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Remove the app's logrotate config.
|
# Remove the app's logrotate config.
|
||||||
|
|
Loading…
Add table
Reference in a new issue