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"
# 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]
# | 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.
# usage: ynh_add_logrotate_config [/path/to/log/file/or/folder]
#
# If no `--logfile` is provided, `/var/log/$app` will be used as default.
# `logfile` can point to a directory or a file.
# If not argument is provided, `/var/log/$app/*.log` is used as default.
#
# 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.
ynh_use_logrotate() {
ynh_add_logrotate_config() {
# ============ Argument parsing =============
local -A args_array=([l]=logfile= [u]=specific_user=)
local logfile
local specific_user
ynh_handle_getopts_args "$@"
logfile="${logfile:-}"
specific_user="${specific_user:-}"
# ===========================================
logfile="$1"
set -o noglob
if [[ -z "$logfile" ]]; then
@ -34,13 +27,10 @@ ynh_use_logrotate() {
for stuff in $logfile
do
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
local su_directive=""
if [[ -n "$specific_user" ]]; then
su_directive="su ${specific_user%/*} ${specific_user#*/}"
fi
local tempconf="$(mktemp)"
cat << EOF >$tempconf
$logfile {
@ -60,7 +50,6 @@ $logfile {
notifempty
# Keep old logs in the same dir
noolddir
$su_directive
}
EOF
@ -73,10 +62,7 @@ EOF
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"
mkdir -p "/var/log/$app"
chmod 750 "/var/log/$app"
}
# Remove the app's logrotate config.
@ -84,7 +70,7 @@ EOF
# usage: ynh_remove_logrotate
#
# Requires YunoHost version 2.6.4 or higher.
ynh_remove_logrotate() {
ynh_remove_logrotate_config() {
if [ -e "/etc/logrotate.d/$app" ]; then
rm "/etc/logrotate.d/$app"
fi