From 390835e96adf042dafa0df26012d8c16fecd2ec7 Mon Sep 17 00:00:00 2001 From: Maniack Crudelis Date: Mon, 24 Jul 2017 02:50:48 +0200 Subject: [PATCH] [fix] Logrotate append (#328) * [fix] Logrotate append * Typo fix by JimboJoe --- data/helpers.d/backend | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/data/helpers.d/backend b/data/helpers.d/backend index dbb739aaf..edd8ad7f4 100644 --- a/data/helpers.d/backend +++ b/data/helpers.d/backend @@ -1,16 +1,26 @@ # Use logrotate to manage the logfile # -# usage: ynh_use_logrotate [logfile] +# usage: ynh_use_logrotate [logfile] [--non-append] # | arg: logfile - absolute path of logfile +# | option: --non-append - Replace the config file instead of appending this new config. # # 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. # /parentdir/logdir/ # /parentdir/logdir/logfile.log # -# It's possible to use this helper several times, each config will added to same logrotate config file. +# It's possible to use this helper several times, each config will be added to the same logrotate config file. +# Unless you use the option --non-append ynh_use_logrotate () { - if [ "$#" -gt 0 ]; then + local customtee="tee -a" + if [ $# -gt 0 ] && [ "$1" == "--non-append" ]; then + customtee="tee" + # Destroy this argument for the next command. + shift + elif [ $# -gt 1 ] && [ "$2" == "--non-append" ]; then + customtee="tee" + fi + if [ $# -gt 0 ]; then if [ "$(echo ${1##*.})" == "log" ]; then # Keep only the extension to check if it's a logfile logfile=$1 # In this case, focus logrotate on the logfile else @@ -40,7 +50,7 @@ $logfile { } EOF sudo mkdir -p $(dirname "$logfile") # Create the log directory, if not exist - cat ${app}-logrotate | sudo tee -a /etc/logrotate.d/$app > /dev/null # Append this config to the others for this app. If a config file already exist + cat ${app}-logrotate | sudo $customtee /etc/logrotate.d/$app > /dev/null # Append this config to the existing config file, or replace the whole config file (depending on $customtee) } # Remove the app's logrotate config.