Implement ynh_systemd_action helper

This commit is contained in:
yalh76 2019-04-03 03:21:30 +02:00
parent 8ee6140dc9
commit de2622127a
5 changed files with 109 additions and 16 deletions

View file

@ -9,11 +9,15 @@
#Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers
source ../settings/scripts/ynh_systemd_action
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
ynh_clean_check_starting
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
@ -32,7 +36,7 @@ db_name=$(ynh_app_setting_get "$app" psql_db)
# STOP PEERTUBE FOR BACKUP
#=================================================
systemctl stop "$app"
ynh_systemd_action --service_name="$app" --action="stop"
#=================================================
# STANDARD BACKUP STEPS
@ -78,9 +82,7 @@ ynh_backup "/etc/systemd/system/$app.service"
# START PEERTUBE
#=================================================
systemctl start "$app"
# App needs time to start
sleep 30
ynh_systemd_action --service_name="$app" --action="start"
#=================================================
# END OF SCRIPT

View file

@ -9,11 +9,15 @@
source _common.sh
source /usr/share/yunohost/helpers
source ynh_add_secure_repos__3
source ynh_systemd_action
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
ynh_clean_check_starting
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
@ -227,9 +231,7 @@ systemctl reload nginx
#=================================================
ynh_print_info "Start service..."
systemctl enable "$app"
systemctl start "$app"
sleep 30
ynh_systemd_action --service_name="$app" --action="start"
#=================================================
# CHANGE PEERTUBE ADMIN PASSWORD AFTER INITIAL GEN

View file

@ -14,11 +14,15 @@ fi
source _common.sh
source /usr/share/yunohost/helpers
source ynh_add_secure_repos__3
source ynh_systemd_action
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
ynh_clean_check_starting
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
@ -159,10 +163,7 @@ systemctl reload nginx
#=================================================
ynh_print_info "Start service..."
systemctl enable "$app"
systemctl start "$app"
# App needs time to start
sleep 30
ynh_systemd_action --service_name="$app" --action="start"
#=================================================
# END OF SCRIPT

View file

@ -9,6 +9,7 @@
source _common.sh
source /usr/share/yunohost/helpers
source ynh_add_secure_repos__3
source ynh_systemd_action
#=================================================
# LOAD SETTINGS
@ -53,6 +54,7 @@ ynh_print_info "Backing up the app before upgrading (may take a while)..."
ynh_backup_before_upgrade
ynh_clean_setup () {
# restore it if the upgrade fails
ynh_clean_check_starting
ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
@ -62,7 +64,7 @@ ynh_abort_if_errors
# STOP PEERTUBE FOR UPGRADE
#=================================================
systemctl stop "$app"
ynh_systemd_action --service_name="$app" --action="stop"
#===================================================
# Add PostgreSQL extension for v1.0.0-beta.10.pre.1
@ -241,10 +243,7 @@ systemctl reload nginx
#=================================================
ynh_print_info "Start service..."
systemctl enable "$app"
systemctl start "$app"
# App needs time to start
sleep 30
ynh_systemd_action --service_name="$app" --action="start"
#=================================================
# END OF SCRIPT

View file

@ -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
}