Merge pull request #511 from Josue-T/patch-10

Add su directive as option for logroate
This commit is contained in:
Bram 2018-09-06 00:14:06 +02:00 committed by GitHub
commit c3d8da7f07
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,8 +1,9 @@
# Use logrotate to manage the logfile # Use logrotate to manage the logfile
# #
# usage: ynh_use_logrotate [logfile] [--non-append] # usage: ynh_use_logrotate [logfile] [--non-append|--append] [specific_user/specific_group]
# | arg: logfile - absolute path of logfile # | arg: logfile - absolute path of logfile
# | arg: --non-append - (Option) Replace the config file instead of appending this new config. # | arg: --non-append - (Option) Replace the config file instead of appending this new config.
# | arg: specific_user : run logrotate as the specified user and group. If not specified logrotate is runned as root.
# #
# If no argument provided, a standard directory will be use. /var/log/${app} # If no argument provided, a standard directory will be use. /var/log/${app}
# You can provide a path with the directory only or with the logfile. # You can provide a path with the directory only or with the logfile.
@ -13,6 +14,7 @@
# Unless you use the option --non-append # Unless you use the option --non-append
ynh_use_logrotate () { ynh_use_logrotate () {
local customtee="tee -a" local customtee="tee -a"
local user_group="${3:-}"
if [ $# -gt 0 ] && [ "$1" == "--non-append" ]; then if [ $# -gt 0 ] && [ "$1" == "--non-append" ]; then
customtee="tee" customtee="tee"
# Destroy this argument for the next command. # Destroy this argument for the next command.
@ -29,6 +31,12 @@ ynh_use_logrotate () {
else else
local logfile="/var/log/${app}/*.log" # Without argument, use a defaut directory in /var/log local logfile="/var/log/${app}/*.log" # Without argument, use a defaut directory in /var/log
fi fi
local su_directive=""
if [[ -n $user_group ]]; then
su_directive=" # Run logorotate as specific user - group
su ${user_group%/*} ${user_group#*/}"
fi
cat > ./${app}-logrotate << EOF # Build a config file for logrotate cat > ./${app}-logrotate << EOF # Build a config file for logrotate
$logfile { $logfile {
# Rotate if the logfile exceeds 100Mo # Rotate if the logfile exceeds 100Mo
@ -47,6 +55,7 @@ $logfile {
notifempty notifempty
# Keep old logs in the same dir # Keep old logs in the same dir
noolddir noolddir
$su_directive
} }
EOF EOF
sudo mkdir -p $(dirname "$logfile") # Create the log directory, if not exist sudo mkdir -p $(dirname "$logfile") # Create the log directory, if not exist