From 34e1085a9f94e8863fce2310a5d63aa8aaa52a9f Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Mon, 17 Apr 2017 14:12:15 +0200 Subject: [PATCH 1/3] Setup log rotation (closes #25) --- scripts/_common.sh | 58 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/backup | 3 +++ scripts/install | 3 +++ scripts/remove | 3 +++ scripts/restore | 3 +++ scripts/upgrade | 6 +++++ 6 files changed, 76 insertions(+) diff --git a/scripts/_common.sh b/scripts/_common.sh index 9a09472..5f85ebc 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -133,3 +133,61 @@ SECURE_REMOVE () { # Suppression de dossier avec vérification des variable return 1 fi } + +#================================================= +# FUTURE YUNOHOST HELPERS - TO BE REMOVED LATER +#================================================= + +# Use logrotate to manage the logfile +# +# usage: ynh_use_logrotate [logfile] +# | arg: logfile - absolute path of logfile +# +# 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. +ynh_use_logrotate () { + 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 + logfile=$1/.log # Else, uses the directory and all logfile into it. + fi + else + logfile="/var/log/${app}/.log" # Without argument, use a defaut directory in /var/log + fi + cat > ./${app}-logrotate << EOF # Build a config file for logrotate +$logfile { + # Rotate if the logfile exceeds 100Mo + size 100M + # Keep 12 old log maximum + rotate 12 + # Compress the logs with gzip + compress + # Compress the log at the next cycle. So keep always 2 non compressed logs + delaycompress + # Copy and truncate the log to allow to continue write on it. Instead of move the log. + copytruncate + # Do not do an error if the log is missing + missingok + # Not rotate if the log is empty + notifempty + # Keep old logs in the same dir + noolddir +} +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 +} + +# Remove the app's logrotate config. +# +# usage: ynh_remove_logrotate +ynh_remove_logrotate () { + if [ -e "/etc/logrotate.d/$app" ]; then + sudo rm "/etc/logrotate.d/$app" + fi +} \ No newline at end of file diff --git a/scripts/backup b/scripts/backup index 1231263..df8d966 100755 --- a/scripts/backup +++ b/scripts/backup @@ -43,3 +43,6 @@ mysqldump -u "$dbuser" -p"$dbpass" --no-create-db "$dbname" > ./db.sql # Copy the control file of the dependency package # FIXME: find a way to retrieve package name from _common.sh? dpkg-query -s nextcloud-deps > ./nextcloud-deps.control + +# Backup the logrotate configuration file +ynh_backup "/etc/logrotate.d/$app" "logrotate" \ No newline at end of file diff --git a/scripts/install b/scripts/install index 4897649..bc56e53 100755 --- a/scripts/install +++ b/scripts/install @@ -186,3 +186,6 @@ sed -i "s@#DESTDIR#@${DESTDIR}@g" ../conf/nextcloud.cron sudo cp ../conf/nextcloud.cron "$cron_path" sudo chmod 644 "$cron_path" _exec_occ background:cron + +# Setup log rotation +ynh_use_logrotate "/home/yunohost.app/nextcloud/data/nextcloud.log" \ No newline at end of file diff --git a/scripts/remove b/scripts/remove index 4e74409..7bae392 100755 --- a/scripts/remove +++ b/scripts/remove @@ -45,3 +45,6 @@ done # Remove the user account id "$app" >/dev/null 2>&1 \ && sudo deluser --quiet --remove-home "$app" >/dev/null + +# Remove logrotate configuration +ynh_remove_logrotate \ No newline at end of file diff --git a/scripts/restore b/scripts/restore index 2b285cc..579e9af 100755 --- a/scripts/restore +++ b/scripts/restore @@ -88,6 +88,9 @@ sudo cp -a ./conf/php-fpm.conf "$phpfpm_conf" # Restore cron job sudo cp -a ./conf/cron "/etc/cron.d/${app}" +# Restore logrotate configuration file +sudo cp -a ./logrotate /etc/logrotate.d/$app + # Reload services sudo service php5-fpm restart || true sudo service nginx reload || true diff --git a/scripts/upgrade b/scripts/upgrade index 2ebf4ef..22c169f 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -239,6 +239,12 @@ you don't see Nextcloud as installed." >&2 sudo chmod 644 "$cron_path" fi +# Setup log rotation +if [ ! -f "/etc/logrotate.d/$app" ]; then + # Don't change the logrotate conf file if already existing + # (the helper only appends the log file configuration) + ynh_use_logrotate "/home/yunohost.app/nextcloud/data/nextcloud.log" +fi # Warn about possible disabled apps echo "Note that if you've installed some third-parties Nextcloud applications, \ they are probably disabled and you'll have to manually activate them again." >&2 From 89a336801f7452b9037f6409f095ef5ef9efcb77 Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Mon, 17 Apr 2017 17:24:52 +0200 Subject: [PATCH 2/3] Fix temp directory rights on upgrade --- scripts/upgrade.d/upgrade.generic.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/upgrade.d/upgrade.generic.sh b/scripts/upgrade.d/upgrade.generic.sh index 4765259..c68b8ca 100755 --- a/scripts/upgrade.d/upgrade.generic.sh +++ b/scripts/upgrade.d/upgrade.generic.sh @@ -17,7 +17,9 @@ COMMON_UPGRADE () { # Retrieve new Nextcloud sources in a temporary directory TMPDIR=$(mktemp -d) - extract_nextcloud "$TMPDIR" # Télécharge nextcloud, vérifie sa somme de contrôle et le décompresse. + # Set temp folder ownership + sudo chown -R $app: "$TMPDIR" + extract_nextcloud "$TMPDIR" "$app" # Télécharge nextcloud, vérifie sa somme de contrôle et le décompresse. # Copy Nextcloud configuration file sed -i "s@#DOMAIN#@${domain}@g" ../conf/config.json From 5bb0a5430020324a6e037075fd0f2d258532b27a Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Mon, 17 Apr 2017 18:08:12 +0200 Subject: [PATCH 3/3] Revert "Fix temp directory rights on upgrade" This reverts commit 89a336801f7452b9037f6409f095ef5ef9efcb77. --- scripts/upgrade.d/upgrade.generic.sh | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/scripts/upgrade.d/upgrade.generic.sh b/scripts/upgrade.d/upgrade.generic.sh index c68b8ca..4765259 100755 --- a/scripts/upgrade.d/upgrade.generic.sh +++ b/scripts/upgrade.d/upgrade.generic.sh @@ -17,9 +17,7 @@ COMMON_UPGRADE () { # Retrieve new Nextcloud sources in a temporary directory TMPDIR=$(mktemp -d) - # Set temp folder ownership - sudo chown -R $app: "$TMPDIR" - extract_nextcloud "$TMPDIR" "$app" # Télécharge nextcloud, vérifie sa somme de contrôle et le décompresse. + extract_nextcloud "$TMPDIR" # Télécharge nextcloud, vérifie sa somme de contrôle et le décompresse. # Copy Nextcloud configuration file sed -i "s@#DOMAIN#@${domain}@g" ../conf/config.json