mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
261 lines
9.7 KiB
Bash
261 lines
9.7 KiB
Bash
#!/bin/bash
|
|
|
|
# Manage a fail of the script
|
|
#
|
|
# [internal]
|
|
#
|
|
# usage:
|
|
# ynh_exit_properly is used only by the helper ynh_abort_if_errors.
|
|
# You should not use it directly.
|
|
# Instead, add to your script:
|
|
# ynh_clean_setup () {
|
|
# instructions...
|
|
# }
|
|
#
|
|
# This function provide a way to clean some residual of installation that not managed by remove script.
|
|
#
|
|
# It prints a warning to inform that the script was failed, and execute the ynh_clean_setup function if used in the app script
|
|
#
|
|
ynh_exit_properly () {
|
|
local exit_code=$?
|
|
if [ "$exit_code" -eq 0 ]; then
|
|
exit 0 # Exit without error if the script ended correctly
|
|
fi
|
|
|
|
trap '' EXIT # Ignore new exit signals
|
|
set +eu # Do not exit anymore if a command fail or if a variable is empty
|
|
|
|
echo -e "!!\n $app's script has encountered an error. Its execution was cancelled.\n!!" >&2
|
|
|
|
if type -t ynh_clean_setup > /dev/null; then # Check if the function exist in the app script.
|
|
ynh_clean_setup # Call the function to do specific cleaning for the app.
|
|
fi
|
|
|
|
ynh_die # Exit with error status
|
|
}
|
|
|
|
# Exits if an error occurs during the execution of the script.
|
|
#
|
|
# usage: ynh_abort_if_errors
|
|
#
|
|
# This configure the rest of the script execution such that, if an error occurs
|
|
# or if an empty variable is used, the execution of the script stops
|
|
# immediately and a call to `ynh_clean_setup` is triggered if it has been
|
|
# defined by your script.
|
|
#
|
|
ynh_abort_if_errors () {
|
|
set -eu # Exit if a command fail, and if a variable is used unset.
|
|
trap ynh_exit_properly EXIT # Capturing exit signals on shell script
|
|
}
|
|
|
|
# Fetch the Debian release codename
|
|
#
|
|
# usage: ynh_get_debian_release
|
|
# | ret: The Debian release codename (i.e. jessie, stretch, ...)
|
|
ynh_get_debian_release () {
|
|
echo $(lsb_release --codename --short)
|
|
}
|
|
|
|
# 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.
|
|
# WARNING: When using --line_match, you should always add `ynh_clean_check_starting` into your
|
|
# `ynh_clean_setup` at the beginning of the script. Otherwise, tail will not stop in case of failure
|
|
# of the script. The script will then hang forever.
|
|
# | 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 --unit=$service_name --follow --since=-0 --quiet > "$templog" &
|
|
# Get the PID of the journalctl command
|
|
local pid_tail=$!
|
|
else
|
|
# Read the specified log file
|
|
tail -F -n0 "$log_path" > "$templog" 2>&1 &
|
|
# Get the PID of the tail command
|
|
local pid_tail=$!
|
|
fi
|
|
fi
|
|
|
|
ynh_print_info --message="${action^} the service $service_name"
|
|
systemctl $action $service_name \
|
|
|| ( journalctl --no-pager --lines=$length -u $service_name >&2 \
|
|
; test -e "$log_path" && echo "--" >&2 && 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
|
|
ynh_print_info --message="The service $service_name has correctly started."
|
|
break
|
|
fi
|
|
if [ $i -eq 3 ]; then
|
|
echo -n "Please wait, the service $service_name is ${action}ing" >&2
|
|
fi
|
|
if [ $i -ge 3 ]; then
|
|
echo -n "." >&2
|
|
fi
|
|
sleep 1
|
|
done
|
|
if [ $i -ge 3 ]; then
|
|
echo "" >&2
|
|
fi
|
|
if [ $i -eq $timeout ]
|
|
then
|
|
ynh_print_warn --message="The service $service_name didn't fully started before the timeout."
|
|
ynh_print_warn --message="Please find here an extract of the end of the log of the service $service_name:"
|
|
journalctl --no-pager --lines=$length -u $service_name >&2
|
|
test -e "$log_path" && echo "--" >&2 && tail --lines=$length "$log_path" >&2
|
|
fi
|
|
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
|
|
}
|
|
|
|
# Read the value of a key in a ynh manifest file
|
|
#
|
|
# usage: ynh_read_manifest manifest key
|
|
# | arg: -m, --manifest= - Path of the manifest to read
|
|
# | arg: -k, --key= - Name of the key to find
|
|
ynh_read_manifest () {
|
|
# Declare an array to define the options of this helper.
|
|
declare -Ar args_array=( [m]=manifest= [k]=manifest_key= )
|
|
local manifest
|
|
local manifest_key
|
|
# Manage arguments with getopts
|
|
ynh_handle_getopts_args "$@"
|
|
|
|
if [ ! -e "$manifest" ]; then
|
|
# If the manifest isn't found, try the common place for backup and restore script.
|
|
manifest="../settings/manifest.json"
|
|
fi
|
|
|
|
jq ".$manifest_key" "$manifest" --raw-output
|
|
}
|
|
|
|
# Read the upstream version from the manifest
|
|
# The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
|
|
# For example : 4.3-2~ynh3
|
|
# This include the number before ~ynh
|
|
# In the last example it return 4.3-2
|
|
#
|
|
# usage: ynh_app_upstream_version [-m manifest]
|
|
# | arg: -m, --manifest= - Path of the manifest to read
|
|
ynh_app_upstream_version () {
|
|
declare -Ar args_array=( [m]=manifest= )
|
|
local manifest
|
|
# Manage arguments with getopts
|
|
ynh_handle_getopts_args "$@"
|
|
|
|
manifest="${manifest:-../manifest.json}"
|
|
version_key=$(ynh_read_manifest --manifest="$manifest" --manifest_key="version")
|
|
echo "${version_key/~ynh*/}"
|
|
}
|
|
|
|
# Read package version from the manifest
|
|
# The version number in the manifest is defined by <upstreamversion>~ynh<packageversion>
|
|
# For example : 4.3-2~ynh3
|
|
# This include the number after ~ynh
|
|
# In the last example it return 3
|
|
#
|
|
# usage: ynh_app_package_version [-m manifest]
|
|
# | arg: -m, --manifest= - Path of the manifest to read
|
|
ynh_app_package_version () {
|
|
declare -Ar args_array=( [m]=manifest= )
|
|
local manifest
|
|
# Manage arguments with getopts
|
|
ynh_handle_getopts_args "$@"
|
|
|
|
manifest="${manifest:-../manifest.json}"
|
|
version_key=$(ynh_read_manifest --manifest="$manifest" --manifest_key="version")
|
|
echo "${version_key/*~ynh/}"
|
|
}
|
|
|
|
# Checks the app version to upgrade with the existing app version and returns:
|
|
# - UPGRADE_APP if the upstream app version has changed
|
|
# - UPGRADE_PACKAGE if only the YunoHost package has changed
|
|
#
|
|
## It stops the current script without error if the package is up-to-date
|
|
#
|
|
# This helper should be used to avoid an upgrade of an app, or the upstream part
|
|
# of it, when it's not needed
|
|
#
|
|
# To force an upgrade, even if the package is up to date,
|
|
# you have to set the variable YNH_FORCE_UPGRADE before.
|
|
# example: sudo YNH_FORCE_UPGRADE=1 yunohost app upgrade MyApp
|
|
#
|
|
# usage: ynh_check_app_version_changed
|
|
ynh_check_app_version_changed () {
|
|
local force_upgrade=${YNH_FORCE_UPGRADE:-0}
|
|
local package_check=${PACKAGE_CHECK_EXEC:-0}
|
|
|
|
# By default, upstream app version has changed
|
|
local return_value="UPGRADE_APP"
|
|
|
|
local current_version=$(ynh_read_manifest --manifest="/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" --manifest_key="version" || echo 1.0)
|
|
local current_upstream_version="$(ynh_app_upstream_version --manifest="/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json")"
|
|
local update_version=$(ynh_read_manifest --manifest="../manifest.json" --manifest_key="version" || echo 1.0)
|
|
local update_upstream_version="$(ynh_app_upstream_version)"
|
|
|
|
if [ "$current_version" == "$update_version" ] ; then
|
|
# Complete versions are the same
|
|
if [ "$force_upgrade" != "0" ]
|
|
then
|
|
echo "Upgrade forced by YNH_FORCE_UPGRADE." >&2
|
|
unset YNH_FORCE_UPGRADE
|
|
elif [ "$package_check" != "0" ]
|
|
then
|
|
echo "Upgrade forced for package check." >&2
|
|
else
|
|
ynh_die "Up-to-date, nothing to do" 0
|
|
fi
|
|
elif [ "$current_upstream_version" == "$update_upstream_version" ] ; then
|
|
# Upstream versions are the same, only YunoHost package versions differ
|
|
return_value="UPGRADE_PACKAGE"
|
|
fi
|
|
echo $return_value
|
|
}
|