mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
Use getopts for ynh_use_logrotate
This commit is contained in:
parent
cedaaa9087
commit
16e00fb7e6
1 changed files with 38 additions and 12 deletions
|
@ -1,9 +1,11 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Use logrotate to manage the logfile
|
||||
#
|
||||
# usage: ynh_use_logrotate [logfile] [--non-append|--append] [specific_user/specific_group]
|
||||
# | arg: logfile - absolute path of logfile
|
||||
# | 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.
|
||||
# usage: ynh_use_logrotate [--logfile=/log/file] [--nonappend] [--specific_user=user/group]
|
||||
# | arg: -l, --logfile= - absolute path of logfile
|
||||
# | arg: -n, --nonappend - (Option) 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.
|
||||
#
|
||||
# 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.
|
||||
|
@ -13,28 +15,52 @@
|
|||
# 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 () {
|
||||
local customtee="tee -a"
|
||||
local user_group="${3:-}"
|
||||
# Declare an array to define the options of this helper.
|
||||
declare -Ar args_array=( [l]=logfile= [n]=nonappend [u]=specific_user= [y]=non [a]=append )
|
||||
# [y]=non [a]=append are only for legacy purpose, to not fail on the old option '--non-append'
|
||||
local logfile
|
||||
local nonappend
|
||||
local specific_user
|
||||
# Manage arguments with getopts
|
||||
ynh_handle_getopts_args "$@"
|
||||
local logfile="${logfile:-}"
|
||||
local nonappend="${nonappend:-0}"
|
||||
local specific_user="${specific_user:-}"
|
||||
|
||||
# LEGACY CODE - PRE GETOPTS
|
||||
if [ $# -gt 0 ] && [ "$1" == "--non-append" ]; then
|
||||
customtee="tee"
|
||||
nonappend=1
|
||||
# Destroy this argument for the next command.
|
||||
shift
|
||||
elif [ $# -gt 1 ] && [ "$2" == "--non-append" ]; then
|
||||
customtee="tee"
|
||||
nonappend=1
|
||||
fi
|
||||
if [ $# -gt 0 ]; then
|
||||
|
||||
if [ $# -gt 0 ] && [ "$(echo ${1:0:1})" != "-" ]; then
|
||||
if [ "$(echo ${1##*.})" == "log" ]; then # Keep only the extension to check if it's a logfile
|
||||
local logfile=$1 # In this case, focus logrotate on the logfile
|
||||
else
|
||||
local logfile=$1/*.log # Else, uses the directory and all logfile into it.
|
||||
fi
|
||||
fi
|
||||
# LEGACY CODE
|
||||
|
||||
local customtee="tee -a"
|
||||
if [ "$nonappend" -eq 1 ]; then
|
||||
customtee="tee"
|
||||
fi
|
||||
if [ -n "$logfile" ]
|
||||
then
|
||||
if [ "$(echo ${logfile##*.})" != "log" ]; then # Keep only the extension to check if it's a logfile
|
||||
local logfile="$1/*.log" # Else, uses the directory and all logfile into it.
|
||||
fi
|
||||
else
|
||||
local logfile="/var/log/${app}/*.log" # Without argument, use a defaut directory in /var/log
|
||||
logfile="/var/log/${app}/*.log" # Without argument, use a defaut directory in /var/log
|
||||
fi
|
||||
local su_directive=""
|
||||
if [[ -n $user_group ]]; then
|
||||
if [[ -n $specific_user ]]; then
|
||||
su_directive=" # Run logorotate as specific user - group
|
||||
su ${user_group%/*} ${user_group#*/}"
|
||||
su ${specific_user%/*} ${specific_user#*/}"
|
||||
fi
|
||||
|
||||
cat > ./${app}-logrotate << EOF # Build a config file for logrotate
|
||||
|
|
Loading…
Add table
Reference in a new issue