mirror of
https://github.com/YunoHost-Apps/lutim_ynh.git
synced 2024-09-03 19:36:24 +02:00
Update helpers
This commit is contained in:
parent
9ffeb7037f
commit
4f529c127b
5 changed files with 87 additions and 44 deletions
|
@ -212,15 +212,17 @@ EOF
|
||||||
|
|
||||||
# Start or restart a service and follow its booting
|
# Start or restart a service and follow its booting
|
||||||
#
|
#
|
||||||
# usage: ynh_check_starting "Line to match" [Log file] [Timeout]
|
# usage: ynh_check_starting "Line to match" [Log file] [Timeout] [Service name]
|
||||||
#
|
#
|
||||||
# | arg: Line to match - The line to find in the log to attest the service have finished to boot.
|
# | arg: Line to match - The line to find in the log to attest the service have finished to boot.
|
||||||
# | arg: Log file - The log file to watch
|
# | arg: Log file - The log file to watch
|
||||||
|
# | arg: Service name
|
||||||
# /var/log/$app/$app.log will be used if no other log is defined.
|
# /var/log/$app/$app.log will be used if no other log is defined.
|
||||||
# | arg: Timeout - The maximum time to wait before ending the watching. Defaut 300 seconds.
|
# | arg: Timeout - The maximum time to wait before ending the watching. Defaut 300 seconds.
|
||||||
ynh_check_starting () {
|
ynh_check_starting () {
|
||||||
local line_to_match="$1"
|
local line_to_match="$1"
|
||||||
local app_log="${2:-/var/log/$app/$app.log}"
|
local service_name="${4:-$app}"
|
||||||
|
local app_log="${2:-/var/log/$service_name/$service_name.log}"
|
||||||
local timeout=${3:-300}
|
local timeout=${3:-300}
|
||||||
|
|
||||||
ynh_clean_check_starting () {
|
ynh_clean_check_starting () {
|
||||||
|
@ -229,13 +231,14 @@ ynh_check_starting () {
|
||||||
ynh_secure_remove "$templog" 2>&1
|
ynh_secure_remove "$templog" 2>&1
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "Starting of $app" >&2
|
echo "Starting of $service_name" >&2
|
||||||
systemctl restart $app
|
systemctl stop $service_name
|
||||||
local templog="$(mktemp)"
|
local templog="$(mktemp)"
|
||||||
# Following the starting of the app in its log
|
# Following the starting of the app in its log
|
||||||
tail -f -n1 "$app_log" > "$templog" &
|
tail -F -n0 "$app_log" > "$templog" &
|
||||||
# Get the PID of the tail command
|
# Get the PID of the tail command
|
||||||
local pid_tail=$!
|
local pid_tail=$!
|
||||||
|
systemctl start $service_name
|
||||||
|
|
||||||
local i=0
|
local i=0
|
||||||
for i in `seq 1 $timeout`
|
for i in `seq 1 $timeout`
|
||||||
|
@ -243,7 +246,7 @@ ynh_check_starting () {
|
||||||
# Read the log until the sentence is found, that means the app finished to start. Or run until the timeout
|
# Read the log until the sentence is found, that means the app finished to start. Or run until the timeout
|
||||||
if grep --quiet "$line_to_match" "$templog"
|
if grep --quiet "$line_to_match" "$templog"
|
||||||
then
|
then
|
||||||
echo "The service $app has correctly started." >&2
|
echo "The service $service_name has correctly started." >&2
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
echo -n "." >&2
|
echo -n "." >&2
|
||||||
|
@ -251,7 +254,7 @@ ynh_check_starting () {
|
||||||
done
|
done
|
||||||
if [ $i -eq $timeout ]
|
if [ $i -eq $timeout ]
|
||||||
then
|
then
|
||||||
echo "The service $app didn't fully started before the timeout." >&2
|
echo "The service $service_name didn't fully started before the timeout." >&2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
|
@ -419,55 +422,56 @@ ynh_multimedia_addaccess () {
|
||||||
# | arg: max_retry - Maximum number of retries allowed before banning IP address - default: 3
|
# | arg: max_retry - Maximum number of retries allowed before banning IP address - default: 3
|
||||||
# | arg: ports - Ports blocked for a banned IP address - default: http,https
|
# | arg: ports - Ports blocked for a banned IP address - default: http,https
|
||||||
ynh_add_fail2ban_config () {
|
ynh_add_fail2ban_config () {
|
||||||
# Process parameters
|
# Process parameters
|
||||||
logpath=$1
|
logpath=$1
|
||||||
failregex=$2
|
failregex=$2
|
||||||
max_retry=${3:-3}
|
max_retry=${3:-3}
|
||||||
ports=${4:-http,https}
|
ports=${4:-http,https}
|
||||||
|
|
||||||
test -n "$logpath" || ynh_die "ynh_add_fail2ban_config expects a logfile path as first argument and received nothing."
|
test -n "$logpath" || ynh_die "ynh_add_fail2ban_config expects a logfile path as first argument and received nothing."
|
||||||
test -n "$failregex" || ynh_die "ynh_add_fail2ban_config expects a failure regex as second argument and received nothing."
|
test -n "$failregex" || ynh_die "ynh_add_fail2ban_config expects a failure regex as second argument and received nothing."
|
||||||
|
|
||||||
finalfail2banjailconf="/etc/fail2ban/jail.d/$app.conf"
|
finalfail2banjailconf="/etc/fail2ban/jail.d/$app.conf"
|
||||||
finalfail2banfilterconf="/etc/fail2ban/filter.d/$app.conf"
|
finalfail2banfilterconf="/etc/fail2ban/filter.d/$app.conf"
|
||||||
ynh_backup_if_checksum_is_different "$finalfail2banjailconf" 1
|
ynh_backup_if_checksum_is_different "$finalfail2banjailconf" 1
|
||||||
ynh_backup_if_checksum_is_different "$finalfail2banfilterconf" 1
|
ynh_backup_if_checksum_is_different "$finalfail2banfilterconf" 1
|
||||||
|
|
||||||
sudo tee $finalfail2banjailconf <<EOF
|
sudo tee $finalfail2banjailconf <<EOF
|
||||||
[$app]
|
[$app]
|
||||||
enabled = true
|
enabled = true
|
||||||
port = $ports
|
port = $ports
|
||||||
filter = $app
|
filter = $app
|
||||||
logpath = $logpath
|
logpath = $logpath
|
||||||
maxretry = $max_retry"
|
maxretry = $max_retry
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
sudo tee $finalfail2banfilterconf <<EOF
|
sudo tee $finalfail2banfilterconf <<EOF
|
||||||
[INCLUDES]
|
[INCLUDES]
|
||||||
before = common.conf
|
before = common.conf
|
||||||
[Definition]
|
[Definition]
|
||||||
failregex = $failregex
|
failregex = $failregex
|
||||||
ignoreregrex ="
|
ignoreregex =
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
ynh_store_file_checksum "$finalfail2banjailconf"
|
ynh_store_file_checksum "$finalfail2banjailconf"
|
||||||
ynh_store_file_checksum "$finalfail2banfilterconf"
|
ynh_store_file_checksum "$finalfail2banfilterconf"
|
||||||
|
|
||||||
sudo systemctl restart fail2ban
|
systemctl restart fail2ban
|
||||||
if local fail2ban_error="$(tail -n50 /var/log/fail2ban.log | grep "WARNING Command.*$app.*addfailregex")"
|
local fail2ban_error="$(journalctl -u fail2ban | tail -n50 | grep "WARNING.*$app.*")"
|
||||||
then
|
if [ -n "$fail2ban_error" ]
|
||||||
echo "[ERR] Fail2ban fail to load the jail for $app" >&2
|
then
|
||||||
echo "WARNING${fail2ban_error#*WARNING}" >&2
|
echo "[ERR] Fail2ban failed to load the jail for $app" >&2
|
||||||
fi
|
echo "WARNING${fail2ban_error#*WARNING}" >&2
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Remove the dedicated fail2ban config (jail and filter conf files)
|
# Remove the dedicated fail2ban config (jail and filter conf files)
|
||||||
#
|
#
|
||||||
# usage: ynh_remove_fail2ban_config
|
# usage: ynh_remove_fail2ban_config
|
||||||
ynh_remove_fail2ban_config () {
|
ynh_remove_fail2ban_config () {
|
||||||
ynh_secure_remove "/etc/fail2ban/jail.d/$app.conf"
|
ynh_secure_remove "/etc/fail2ban/jail.d/$app.conf"
|
||||||
ynh_secure_remove "/etc/fail2ban/filter.d/$app.conf"
|
ynh_secure_remove "/etc/fail2ban/filter.d/$app.conf"
|
||||||
sudo systemctl restart fail2ban
|
sudo systemctl restart fail2ban
|
||||||
}
|
}
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
|
@ -480,7 +484,7 @@ ynh_remove_fail2ban_config () {
|
||||||
ynh_read_manifest () {
|
ynh_read_manifest () {
|
||||||
manifest="$1"
|
manifest="$1"
|
||||||
key="$2"
|
key="$2"
|
||||||
python3 -c "import sys, json;print(json.load(open('$manifest'))['$key'])"
|
python3 -c "import sys, json;print(json.load(open('$manifest', encoding='utf-8'))['$key'])"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Exit without error if the package is up to date
|
# Exit without error if the package is up to date
|
||||||
|
@ -525,7 +529,7 @@ ynh_abort_if_up_to_date () {
|
||||||
# If you give the name of a YunoHost user, ynh_send_readme_to_admin will find its email adress for you
|
# If you give the name of a YunoHost user, ynh_send_readme_to_admin will find its email adress for you
|
||||||
# example: "root admin@domain user1 user2"
|
# example: "root admin@domain user1 user2"
|
||||||
ynh_send_readme_to_admin() {
|
ynh_send_readme_to_admin() {
|
||||||
local app_message="${1:-...No specific informations...}"
|
local app_message="${1:-...No specific information...}"
|
||||||
local recipients="${2:-root}"
|
local recipients="${2:-root}"
|
||||||
|
|
||||||
# Retrieve the email of users
|
# Retrieve the email of users
|
||||||
|
@ -556,7 +560,7 @@ ynh_send_readme_to_admin() {
|
||||||
|
|
||||||
local mail_message="This is an automated message from your beloved YunoHost server.
|
local mail_message="This is an automated message from your beloved YunoHost server.
|
||||||
|
|
||||||
Specific informations for the application $app.
|
Specific information for the application $app.
|
||||||
|
|
||||||
$app_message
|
$app_message
|
||||||
|
|
||||||
|
@ -569,6 +573,45 @@ $(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')"
|
||||||
echo "$mail_message" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "$mail_subject" "$recipients"
|
echo "$mail_message" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "$mail_subject" "$recipients"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#=================================================
|
||||||
|
|
||||||
|
# Reload (or other actions) a service and print a log in case of failure.
|
||||||
|
#
|
||||||
|
# usage: ynh_system_reload service_name [action]
|
||||||
|
# | arg: service_name - Name of the service to reload
|
||||||
|
# | arg: action - Action to perform with systemctl. Default: reload
|
||||||
|
ynh_system_reload () {
|
||||||
|
local service_name=$1
|
||||||
|
local action=${2:-reload}
|
||||||
|
|
||||||
|
# Reload, restart or start and print the log if the service fail to start or reload
|
||||||
|
systemctl $action $service_name || ( journalctl --lines=20 -u $service_name >&2 && false)
|
||||||
|
}
|
||||||
|
|
||||||
|
#=================================================
|
||||||
|
|
||||||
|
ynh_debian_release () {
|
||||||
|
lsb_release --codename --short
|
||||||
|
}
|
||||||
|
|
||||||
|
is_stretch () {
|
||||||
|
if [ "$(ynh_debian_release)" == "stretch" ]
|
||||||
|
then
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
is_jessie () {
|
||||||
|
if [ "$(ynh_debian_release)" == "jessie" ]
|
||||||
|
then
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
#============= FUTURE YUNOHOST HELPER ============
|
#============= FUTURE YUNOHOST HELPER ============
|
||||||
#=================================================
|
#=================================================
|
||||||
|
|
|
@ -127,7 +127,7 @@ ynh_replace_string "\(^ *prefix.*=> '\).*" "\1$new_path'," "$final_path/lutim.co
|
||||||
# RELOAD NGINX
|
# RELOAD NGINX
|
||||||
#=================================================
|
#=================================================
|
||||||
|
|
||||||
systemctl reload nginx
|
ynh_system_reload nginx
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
# START AND CHECK LUTIM BOOTING
|
# START AND CHECK LUTIM BOOTING
|
||||||
|
|
|
@ -185,7 +185,7 @@ fi
|
||||||
# RELOAD NGINX
|
# RELOAD NGINX
|
||||||
#=================================================
|
#=================================================
|
||||||
|
|
||||||
systemctl reload nginx
|
ynh_system_reload nginx
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
# CHECK LUTIM BOOTING
|
# CHECK LUTIM BOOTING
|
||||||
|
|
|
@ -122,7 +122,7 @@ ynh_restore_file "/etc/logrotate.d/$app"
|
||||||
# RELOAD NGINX
|
# RELOAD NGINX
|
||||||
#=================================================
|
#=================================================
|
||||||
|
|
||||||
systemctl reload nginx
|
ynh_system_reload nginx
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
# START AND CHECK LUTIM BOOTING
|
# START AND CHECK LUTIM BOOTING
|
||||||
|
|
|
@ -170,7 +170,7 @@ fi
|
||||||
# RELOAD NGINX
|
# RELOAD NGINX
|
||||||
#=================================================
|
#=================================================
|
||||||
|
|
||||||
systemctl reload nginx
|
ynh_system_reload nginx
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
# START AND CHECK LUTIM BOOTING
|
# START AND CHECK LUTIM BOOTING
|
||||||
|
|
Loading…
Reference in a new issue