helpers2.1: simplify the logrotate mess: rename to ynh_add/remove_logrotate_config, use positional args, --specific_user is useless, we just need to make sure the parent dir has no +w on group...

This commit is contained in:
Alexandre Aubin 2024-06-11 01:07:03 +02:00
parent 480366d5a1
commit c5815fb1ef

View file

@ -2,26 +2,19 @@
FIRST_CALL_TO_LOGROTATE="true" FIRST_CALL_TO_LOGROTATE="true"
# Use logrotate to manage the logfile # Add a logrotate configuration to manage log files / log directory
# #
# usage: ynh_use_logrotate [--logfile=/log/file] [--specific_user=user/group] # usage: ynh_add_logrotate_config [/path/to/log/file/or/folder]
# | arg: -l, --logfile= - absolute path of logfile
# | 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 not argument is provided, `/var/log/$app/*.log` is used as default.
# `logfile` can point to a directory or a file. #
# The configuration is autogenerated by YunoHost
# (ie it doesnt come from a specific app template like nginx or systemd conf)
# #
# Requires YunoHost version 2.6.4 or higher. # Requires YunoHost version 2.6.4 or higher.
ynh_use_logrotate() { ynh_add_logrotate_config() {
# ============ Argument parsing ============= logfile="$1"
local -A args_array=([l]=logfile= [u]=specific_user=)
local logfile
local specific_user
ynh_handle_getopts_args "$@"
logfile="${logfile:-}"
specific_user="${specific_user:-}"
# ===========================================
set -o noglob set -o noglob
if [[ -z "$logfile" ]]; then if [[ -z "$logfile" ]]; then
@ -34,13 +27,10 @@ ynh_use_logrotate() {
for stuff in $logfile for stuff in $logfile
do do
mkdir --parents $(dirname "$stuff") mkdir --parents $(dirname "$stuff")
# Make sure the permissions of the parent dir are correct (otherwise the config file could be ignored and the corresponding logs never rotated)
chmod 750 $(dirname "$stuff")
done done
local su_directive=""
if [[ -n "$specific_user" ]]; then
su_directive="su ${specific_user%/*} ${specific_user#*/}"
fi
local tempconf="$(mktemp)" local tempconf="$(mktemp)"
cat << EOF >$tempconf cat << EOF >$tempconf
$logfile { $logfile {
@ -60,7 +50,6 @@ $logfile {
notifempty notifempty
# Keep old logs in the same dir # Keep old logs in the same dir
noolddir noolddir
$su_directive
} }
EOF EOF
@ -73,10 +62,7 @@ EOF
FIRST_CALL_TO_LOGROTATE="false" 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" chmod 644 "/etc/logrotate.d/$app"
mkdir -p "/var/log/$app"
chmod 750 "/var/log/$app"
} }
# Remove the app's logrotate config. # Remove the app's logrotate config.
@ -84,7 +70,7 @@ EOF
# usage: ynh_remove_logrotate # usage: ynh_remove_logrotate
# #
# Requires YunoHost version 2.6.4 or higher. # Requires YunoHost version 2.6.4 or higher.
ynh_remove_logrotate() { ynh_remove_logrotate_config() {
if [ -e "/etc/logrotate.d/$app" ]; then if [ -e "/etc/logrotate.d/$app" ]; then
rm "/etc/logrotate.d/$app" rm "/etc/logrotate.d/$app"
fi fi