mirror of
https://framagit.org/YunoHost-Apps/gitea_ynh.git
synced 2024-11-24 12:01:35 +01:00
Use ynh_systemd_action to control service start
This commit is contained in:
parent
fb15a9c489
commit
0e108f1261
@ -50,5 +50,4 @@ config_nginx
|
|||||||
config_gitea
|
config_gitea
|
||||||
|
|
||||||
# RELOAD services
|
# RELOAD services
|
||||||
ynh_check_starting "INFO] Listen: http://0.0.0.0:" "/var/log/$app/gitea.log"
|
ynh_systemd_action -l "Serving \[::\]:$port with pid" -p "systemd" -a restart
|
||||||
sleep 1
|
|
||||||
|
@ -9,53 +9,91 @@ ynh_delete_file_checksum () {
|
|||||||
ynh_app_setting_delete $app $checksum_setting_name
|
ynh_app_setting_delete $app $checksum_setting_name
|
||||||
}
|
}
|
||||||
|
|
||||||
# Start or restart a service and follow its booting
|
# Start (or other actions) a service, print a log in case of failure and optionnaly wait until the service is completely started
|
||||||
#
|
#
|
||||||
# usage: ynh_check_starting "Line to match" [Log file] [Timeout] [Service name]
|
# 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: Line to match - The line to find in the log to attest the service have finished to boot.
|
# | arg: -a, --action= - Action to perform with systemctl. Default: start
|
||||||
# | arg: Log file - The log file to watch
|
# | arg: -l, --line_match= - Line to match - The line to find in the log to attest the service have finished to boot.
|
||||||
# | arg: Service name
|
# If not defined it don't wait until the service is completely started.
|
||||||
# /var/log/$app/$app.log will be used if no other log is defined.
|
# | arg: -p, --log_path= - Log file - Path to the log file. Default : /var/log/$app/$app.log
|
||||||
# | arg: Timeout - The maximum time to wait before ending the watching. Defaut 300 seconds.
|
# | arg: -t, --timeout= - Timeout - The maximum time to wait before ending the watching. Default : 300 seconds.
|
||||||
ynh_check_starting () {
|
# | arg: -e, --length= - Length of the error log : Default : 20
|
||||||
local line_to_match="$1"
|
ynh_systemd_action() {
|
||||||
local service_name="${4:-$app}"
|
# Declare an array to define the options of this helper.
|
||||||
local app_log="${2:-/var/log/$service_name/$service_name.log}"
|
declare -Ar args_array=( [n]=service_name= [a]=action= [l]=line_match= [p]=log_path= [t]=timeout= [e]=length= )
|
||||||
local timeout=${3:-300}
|
local service_name
|
||||||
|
local action
|
||||||
|
local line_match
|
||||||
|
local length
|
||||||
|
local log_path
|
||||||
|
local timeout
|
||||||
|
|
||||||
ynh_clean_check_starting () {
|
# Manage arguments with getopts
|
||||||
# Stop the execution of tail.
|
ynh_handle_getopts_args "$@"
|
||||||
kill -s 15 $pid_tail 2>&1
|
|
||||||
ynh_secure_remove "$templog" 2>&1
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Starting of $service_name" >&2
|
local service_name="${service_name:-$app}"
|
||||||
systemctl stop $service_name
|
local action=${action:-start}
|
||||||
local templog="$(mktemp)"
|
local log_path="${log_path:-/var/log/$service_name/$service_name.log}"
|
||||||
# Following the starting of the app in its log
|
local length=${length:-20}
|
||||||
tail -F -n0 "$app_log" > "$templog" &
|
local timeout=${timeout:-300}
|
||||||
# Get the PID of the tail command
|
|
||||||
local pid_tail=$!
|
|
||||||
systemctl start $service_name
|
|
||||||
|
|
||||||
local i=0
|
# Start to read the log
|
||||||
for i in `seq 1 $timeout`
|
if [[ -n "${line_match:-}" ]]
|
||||||
do
|
then
|
||||||
# Read the log until the sentence is found, that means the app finished to start. Or run until the timeout
|
local templog="$(mktemp)"
|
||||||
if grep --quiet "$line_to_match" "$templog"
|
# Following the starting of the app in its log
|
||||||
then
|
if [ "$log_path" == "systemd" ] ; then
|
||||||
echo "The service $service_name has correctly started." >&2
|
# Read the systemd journal
|
||||||
break
|
journalctl --unit=$service_name --follow --since=-0 --quiet > "$templog" &
|
||||||
fi
|
else
|
||||||
echo -n "." >&2
|
# Read the specified log file
|
||||||
sleep 1
|
tail -F -n0 "$log_path" > "$templog" &
|
||||||
done
|
fi
|
||||||
if [ $i -eq $timeout ]
|
# Get the PID of the tail command
|
||||||
then
|
local pid_tail=$!
|
||||||
echo "The service $service_name didn't fully started before the timeout." >&2
|
fi
|
||||||
fi
|
|
||||||
|
|
||||||
echo ""
|
echo "${action^} the service $service_name" >&2
|
||||||
ynh_clean_check_starting
|
systemctl $action $service_name \
|
||||||
|
|| ( journalctl --lines=$length -u $service_name >&2 \
|
||||||
|
; test -e "$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
|
||||||
|
echo "Please find here an extract of the end of the log of the service $service_name:"
|
||||||
|
journalctl --lines=$length -u $service_name >&2
|
||||||
|
test -e "$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
|
||||||
}
|
}
|
@ -113,6 +113,4 @@ yunohost service add "$app" --log "/var/log/$app/$app.log"
|
|||||||
ynh_use_logrotate "/var/log/$app"
|
ynh_use_logrotate "/var/log/$app"
|
||||||
|
|
||||||
# Reload services
|
# Reload services
|
||||||
# ynh_check_starting "Serving [::]:$port with pid" "/var/log/$app/gitea.log"
|
ynh_systemd_action -l "Serving \[::\]:$port with pid" -p "systemd"
|
||||||
sleep 20
|
|
||||||
systemctl start gitea
|
|
||||||
|
@ -19,6 +19,7 @@ domain=$(ynh_app_setting_get "$app" domain)
|
|||||||
path_url=$(ynh_app_setting_get "$app" path)
|
path_url=$(ynh_app_setting_get "$app" path)
|
||||||
dbpass=$(ynh_app_setting_get "$app" mysqlpwd)
|
dbpass=$(ynh_app_setting_get "$app" mysqlpwd)
|
||||||
admin=$(ynh_app_setting_get "$app" adminusername)
|
admin=$(ynh_app_setting_get "$app" adminusername)
|
||||||
|
port=$(ynh_app_setting_get "$app" web_port)
|
||||||
|
|
||||||
# Check domain/path availability with app helper
|
# Check domain/path availability with app helper
|
||||||
ynh_webpath_available $domain $path_url || ynh_die "$domain is not available as domain, please use an other domain."
|
ynh_webpath_available $domain $path_url || ynh_die "$domain is not available as domain, please use an other domain."
|
||||||
@ -68,5 +69,4 @@ yunohost service add "$app" --log /var/log/"$app"/"$app".log
|
|||||||
|
|
||||||
# Reload services
|
# Reload services
|
||||||
systemctl reload nginx.service
|
systemctl reload nginx.service
|
||||||
sleep 20
|
ynh_systemd_action -l "Serving \[::\]:$port with pid" -p "systemd"
|
||||||
systemctl start gitea
|
|
||||||
|
@ -110,6 +110,4 @@ set_permission
|
|||||||
set_access_settings
|
set_access_settings
|
||||||
|
|
||||||
# Reload services
|
# Reload services
|
||||||
# ynh_check_starting "INFO] Listen: http://0.0.0.0:" "/var/log/$app/gitea.log"
|
ynh_systemd_action -l "Serving \[::\]:$port with pid" -p "systemd"
|
||||||
sleep 20
|
|
||||||
systemctl restart gitea
|
|
||||||
|
Loading…
Reference in New Issue
Block a user