From 6d3347948a51e81cf8f5cb5706e2efea29220a12 Mon Sep 17 00:00:00 2001 From: yalh76 Date: Fri, 1 Feb 2019 21:13:30 +0100 Subject: [PATCH] Adding ynh_systemd_action helper --- README.md | 2 +- conf/app.src | 4 +- manifest.json | 2 +- scripts/backup | 12 ++++- scripts/change_url | 31 ++++++++++--- scripts/detect_arch | 12 +++++ scripts/install | 27 ++++++++---- scripts/remove | 17 ++++++++ scripts/restore | 21 ++++++++- scripts/upgrade | 27 ++++++++++-- scripts/ynh_systemd_action | 89 ++++++++++++++++++++++++++++++++++++++ 11 files changed, 220 insertions(+), 24 deletions(-) create mode 100644 scripts/detect_arch create mode 100644 scripts/ynh_systemd_action diff --git a/README.md b/README.md index 41f3d59..fadd65a 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Can be run as Single User Blog or Multi User Instance. Each User can be limited from 1 to unlimited blogs. -**Shipped version:** v0.8.0 +**Shipped version:** v0.8.1 ## Licence diff --git a/conf/app.src b/conf/app.src index 5f55950..8c1512a 100644 --- a/conf/app.src +++ b/conf/app.src @@ -1,5 +1,5 @@ -SOURCE_URL=https://github.com/writeas/writefreely/releases/download/v0.8.0/writefreely_0.8.0_linux_amd64.tar.gz -SOURCE_SUM=ed1020692bdb96e3d3fc52605d089f26153edfb5dc2822b4da03fcc351c1e841 +SOURCE_URL=https://github.com/writeas/writefreely/releases/download/v0.8.1/writefreely_0.8.1_linux_amd64.tar.gz +SOURCE_SUM=97fe8d2098f86eaa4a294e1d5845dafe7079e5502070ba3929f78abfbf9be371 SOURCE_SUM_PRG=sha256sum SOURCE_FORMAT=tar.gz SOURCE_IN_SUBDIR=false diff --git a/manifest.json b/manifest.json index 872da58..67083ed 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "en": "Write Freely is free and open source software for starting a minimalist, federated blog — or an entire community.", "fr": "Write Freely est un logiciel gratuit et à code source ouvert permettant de créer un blog fédéré minimaliste ou une communauté entière." }, - "version": "0.8.0~ynh2", + "version": "0.8.1~ynh1", "url": "https://writefreely.org", "license": "free", "maintainer": { diff --git a/scripts/backup b/scripts/backup index fc5c764..5db9479 100755 --- a/scripts/backup +++ b/scripts/backup @@ -7,15 +7,25 @@ #================================================= source ../settings/scripts/_common.sh +source ../settings/scripts/detect_arch source /usr/share/yunohost/helpers +#================================================= +# MANAGE APPLICATION NOT COMPATIBLE WITH ARM +#================================================= + +if [ $architecture = "arm" ] +then + ynh_die "WriteFreely not available on Arm" +fi + #================================================= # MANAGE SCRIPT FAILURE #================================================= ynh_clean_setup () { ### Remove this function if there's nothing to clean before calling the remove script. - true + ynh_clean_check_starting } # Exit if an error occurs during the execution of the script ynh_abort_if_errors diff --git a/scripts/change_url b/scripts/change_url index 7f71444..ff6fa1b 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -6,9 +6,19 @@ # IMPORT GENERIC HELPERS #================================================= +source detect_arch source _common.sh source /usr/share/yunohost/helpers +#================================================= +# MANAGE APPLICATION NOT COMPATIBLE WITH ARM +#================================================= + +if [ $architecture = "arm" ] +then + ynh_die "WriteFreely not available on Arm" +fi + #================================================= # RETRIEVE ARGUMENTS #================================================= @@ -90,18 +100,27 @@ fi #================================================= # SPECIFIC MODIFICATIONS #================================================= -# ... +# STOP SERVICE #================================================= -# Stop writefreely for modification -yunohost service stop "$app" +ynh_systemd_action --action=stop --service_name=$app + +#================================================= +# MODIFY A CONFIG FILE +#================================================= ynh_replace_string "host =.*" "host =https://$new_domain$new_path" "$final_path/config.ini" -# Start writefreely after modification -yunohost service start $app +#================================================= +# START SERVICE +#================================================= -### Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script. +ynh_systemd_action --action=start --service_name=$app + +#================================================= +# STORE THE CONFIG FILE CHECKSUM +#================================================= +fy the checksum of a file, stored by `ynh_store_file_checksum` in the install script. ### And create a backup of this file if the checksum is different. So the file will be backed up if the admin had modified it. ynh_backup_if_checksum_is_different "$final_path/config.ini" # Recalculate and store the checksum of the file for the next upgrade. diff --git a/scripts/detect_arch b/scripts/detect_arch new file mode 100644 index 0000000..4e258d9 --- /dev/null +++ b/scripts/detect_arch @@ -0,0 +1,12 @@ +#!/bin/bash + +if [ -n "$(uname -m | grep 64)" ]; then + architecture="x86-64" +elif [ -n "$(uname -m | grep 86)" ]; then + architecture="i386" +elif [ -n "$(uname -m | grep arm)" ]; then + architecture="arm" +else + ynh_die "Unable to detect your achitecture, please open a bug describing \ + your hardware and the result of the command \"uname -m\"." 1 +fi diff --git a/scripts/install b/scripts/install index bca64e6..4686697 100755 --- a/scripts/install +++ b/scripts/install @@ -7,25 +7,30 @@ #================================================= source _common.sh +source detect_arch +source ynh_systemd_action source /usr/share/yunohost/helpers +#================================================= +# MANAGE APPLICATION NOT COMPATIBLE WITH ARM +#================================================= + +if [ $architecture = "arm" ] +then + ynh_die "WriteFreely not available on Arm" +fi + #================================================= # MANAGE SCRIPT FAILURE #================================================= ynh_clean_setup () { ### Remove this function if there's nothing to clean before calling the remove script. - true + ynh_clean_check_starting } # Exit if an error occurs during the execution of the script ynh_abort_if_errors -arch=$(uname -m) -if [[ $arch == arm* ]] -then - ynh_die "WriteFreely not available on Arm" -fi - #================================================= # RETRIEVE ARGUMENTS FROM THE MANIFEST #================================================= @@ -362,4 +367,10 @@ systemctl reload nginx # START SERVICE #================================================= -yunohost service start $app +ynh_systemd_action --action=start --service_name=$app + +#================================================= +# CLEAN TEMPORARY PROCESS AND FILE USED BY ynh_check_starting +#================================================= + +ynh_clean_check_starting diff --git a/scripts/remove b/scripts/remove index 79cb21c..df1d506 100755 --- a/scripts/remove +++ b/scripts/remove @@ -6,9 +6,20 @@ # IMPORT GENERIC HELPERS #================================================= +source detect_arch source _common.sh +source ynh_systemd_action source /usr/share/yunohost/helpers +#================================================= +# MANAGE APPLICATION NOT COMPATIBLE WITH ARM +#================================================= + +if [ $architecture = "arm" ] +then + ynh_die "WriteFreely not available on Arm" +fi + #================================================= # LOAD SETTINGS #================================================= @@ -116,3 +127,9 @@ ynh_secure_remove "/var/log/$app/" # Delete a system user ynh_system_user_delete $app + +#================================================= +# CLEAN TEMPORARY PROCESS AND FILE USED BY ynh_check_starting +#================================================= + +ynh_clean_check_starting diff --git a/scripts/restore b/scripts/restore index a427469..9eacdc0 100755 --- a/scripts/restore +++ b/scripts/restore @@ -7,15 +7,26 @@ #================================================= source ../settings/scripts/_common.sh +source ../settings/scripts/detect_arch +source ../settings/scripts/ynh_systemd_action source /usr/share/yunohost/helpers +#================================================= +# MANAGE APPLICATION NOT COMPATIBLE WITH ARM +#================================================= + +if [ $architecture = "arm" ] +then + ynh_die "WriteFreely not available on Arm" +fi + #================================================= # MANAGE SCRIPT FAILURE #================================================= ynh_clean_setup () { #### Remove this function if there's nothing to clean before calling the remove script. - true + ynh_clean_check_starting } # Exit if an error occurs during the execution of the script ynh_abort_if_errors @@ -129,4 +140,10 @@ systemctl reload nginx # START SERVICE #================================================= -yunohost service start $app +ynh_systemd_action --action=start --service_name=$app + +#================================================= +# CLEAN TEMPORARY PROCESS AND FILE USED BY ynh_check_starting +#================================================= + +ynh_clean_check_starting diff --git a/scripts/upgrade b/scripts/upgrade index ebdca4f..4158fff 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -7,8 +7,19 @@ #================================================= source _common.sh +source detect_arch +source ynh_systemd_action source /usr/share/yunohost/helpers +#================================================= +# MANAGE APPLICATION NOT COMPATIBLE WITH ARM +#================================================= + +if [ $architecture = "arm" ] +then + ynh_die "WriteFreely not available on Arm" +fi + #================================================= # LOAD SETTINGS #================================================= @@ -58,12 +69,16 @@ ynh_backup_before_upgrade ynh_clean_setup () { # restore it if the upgrade fails ynh_restore_upgradebackup + ynh_clean_check_starting } # Exit if an error occurs during the execution of the script ynh_abort_if_errors -# Stop writefreely for upgrade -yunohost service stop "$app" +#================================================= +# STOP SERVICE +#================================================= + +ynh_systemd_action --action=stop --service_name=$app #================================================= # CHECK THE PATH @@ -178,4 +193,10 @@ systemctl reload nginx # START SERVICE #================================================= -yunohost service start $app +ynh_systemd_action --action=start --service_name=$app + +#================================================= +# CLEAN TEMPORARY PROCESS AND FILE USED BY ynh_check_starting +#================================================= + +ynh_clean_check_starting diff --git a/scripts/ynh_systemd_action b/scripts/ynh_systemd_action new file mode 100644 index 0000000..6bed6be --- /dev/null +++ b/scripts/ynh_systemd_action @@ -0,0 +1,89 @@ +#!/bin/bash + +# Start (or other actions) a service, print a log in case of failure and optionnaly wait until the service is completely started +# +# usage: ynh_systemd_action [-n service_name] [-a action] [ [-l "line to match"] [-p log_path] [-t timeout] [-e length] ] +# | arg: -n, --service_name= - Name of the service to reload. Default : $app +# | arg: -a, --action= - Action to perform with systemctl. Default: start +# | arg: -l, --line_match= - Line to match - The line to find in the log to attest the service have finished to boot. +# If not defined it don't wait until the service is completely started. +# | arg: -p, --log_path= - Log file - Path to the log file. Default : /var/log/$app/$app.log +# | arg: -t, --timeout= - Timeout - The maximum time to wait before ending the watching. Default : 300 seconds. +# | arg: -e, --length= - Length of the error log : Default : 20 +ynh_systemd_action() { + # Declare an array to define the options of this helper. + declare -Ar args_array=( [n]=service_name= [a]=action= [l]=line_match= [p]=log_path= [t]=timeout= [e]=length= ) + local service_name + local action + local line_match + local length + local log_path + local timeout + + # Manage arguments with getopts + ynh_handle_getopts_args "$@" + + local service_name="${service_name:-$app}" + local action=${action:-start} + local log_path="${log_path:-/var/log/$service_name/$service_name.log}" + local length=${length:-20} + local timeout=${timeout:-300} + + # Start to read the log + if [[ -n "${line_match:-}" ]] + then + local templog="$(mktemp)" + # Following the starting of the app in its log + if [ "$log_path" == "systemd" ] ; then + # Read the systemd journal + journalctl -u $service_name -f --since=-45 > "$templog" & + else + # Read the specified log file + tail -F -n0 "$log_path" > "$templog" & + fi + # Get the PID of the tail command + local pid_tail=$! + fi + + echo "${action^} the service $service_name" >&2 + systemctl $action $service_name \ + || ( journalctl --lines=$length -u $service_name >&2 \ + ; test -n "$log_path" && echo "--" && tail --lines=$length "$log_path" >&2 \ + ; false ) + + # Start the timeout and try to find line_match + if [[ -n "${line_match:-}" ]] + then + local i=0 + for i in $(seq 1 $timeout) + do + # Read the log until the sentence is found, that means the app finished to start. Or run until the timeout + if grep --quiet "$line_match" "$templog" + then + echo "The service $service_name has correctly started." >&2 + break + fi + echo -n "." >&2 + sleep 1 + done + if [ $i -eq $timeout ] + then + echo "The service $service_name didn't fully started before the timeout." >&2 + journalctl --lines=$length -u $service_name >&2 + test -n "$log_path" && echo "--" && tail --lines=$length "$log_path" >&2 + fi + + echo "" + ynh_clean_check_starting + fi +} + +# Clean temporary process and file used by ynh_check_starting +# (usually used in ynh_clean_setup scripts) +# +# usage: ynh_clean_check_starting +ynh_clean_check_starting () { + # Stop the execution of tail. + kill -s 15 $pid_tail 2>&1 + ynh_secure_remove "$templog" 2>&1 +}