mirror of
https://github.com/YunoHost-Apps/teampass_ynh.git
synced 2024-09-03 20:26:37 +02:00
285 lines
9.2 KiB
Bash
Executable file
285 lines
9.2 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
|
|
YNH_PHP_VERSION="7.3"
|
|
|
|
#=================================================
|
|
# BACKUP
|
|
#=================================================
|
|
|
|
#=================================================
|
|
# PACKAGE CHECK BYPASSING...
|
|
#=================================================
|
|
|
|
#=================================================
|
|
# BOOLEAN CONVERTER
|
|
#=================================================
|
|
|
|
bool_to_01 () {
|
|
local var="$1"
|
|
[ "$var" = "true" ] && var=1
|
|
[ "$var" = "false" ] && var=0
|
|
echo "$var"
|
|
}
|
|
|
|
bool_to_true_false () {
|
|
local var="$1"
|
|
[ "$var" = "1" ] && var=true
|
|
[ "$var" = "0" ] && var=false
|
|
echo "$var"
|
|
}
|
|
|
|
#=================================================
|
|
# EXPERIMENTAL HELPERS
|
|
#=================================================
|
|
|
|
# Start or restart a service and follow its booting
|
|
#
|
|
# usage: ynh_check_starting "Line to match" [Log file] [Timeout] [Service name]
|
|
#
|
|
# | arg: -m, --line_to_match= - Line to match - The line to find in the log to attest the service have finished to boot.
|
|
# | arg: -l, --app_log= - Log file - The log file to watch; specify "systemd" to read systemd journal for specified service
|
|
# /var/log/$app/$app.log will be used if no other log is defined.
|
|
# | arg: -t, --timeout= - Timeout - The maximum time to wait before ending the watching. Defaut 300 seconds.
|
|
# | arg: -n, --service_name= - Service name
|
|
|
|
ynh_check_starting () {
|
|
# Declare an array to define the options of this helper.
|
|
declare -Ar args_array=( [m]=line_to_match= [l]=app_log= [t]=timeout= [n]=service_name= )
|
|
local line_to_match
|
|
local app_log
|
|
local timeout
|
|
local service_name
|
|
# Manage arguments with getopts
|
|
ynh_handle_getopts_args "$@"
|
|
local app_log="${app_log:-/var/log/$service_name/$service_name.log}"
|
|
local timeout=${timeout:-300}
|
|
local service_name="${service_name:-$app}"
|
|
|
|
echo "Starting of $service_name" >&2
|
|
systemctl stop $service_name
|
|
local templog="$(mktemp)"
|
|
# Following the starting of the app in its log
|
|
if [ "$app_log" == "systemd" ] ; then
|
|
# Read the systemd journal
|
|
journalctl -u $service_name -f --since=-45 > "$templog" &
|
|
else
|
|
# Read the specified log file
|
|
tail -F -n0 "$app_log" > "$templog" &
|
|
fi
|
|
# Get the PID of the last command
|
|
local pid_tail=$!
|
|
systemctl start $service_name
|
|
|
|
local i=0
|
|
for i in `seq 1 $timeout`
|
|
do
|
|
# Read the log until the sentence is found, which means the app finished starting. Or run until the timeout.
|
|
if grep --quiet "$line_to_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 start before the timeout." >&2
|
|
fi
|
|
|
|
echo ""
|
|
ynh_clean_check_starting
|
|
}
|
|
|
|
|
|
#=================================================
|
|
|
|
|
|
# Move a directory in yunohost.multimedia, and replace by a symbolic link
|
|
#
|
|
# usage: ynh_multimedia_movefolder "Source directory" "Destination directory"
|
|
#
|
|
# | arg: -s, --source_dir= - Source directory - The real directory which contains your medias.
|
|
# It will be moved to "Destination directory"
|
|
# A symbolic link will replace it.
|
|
# | arg: -d, --dest_dir= - Destination directory - The new name and place of the directory, relative to "/home/yunohost.multimedia"
|
|
ynh_multimedia_movefolder () {
|
|
# Declare an array to define the options of this helper.
|
|
declare -Ar args_array=( [s]=source_dir= [d]=dest_dir= )
|
|
local source_dir
|
|
local dest_dir
|
|
# Manage arguments with getopts
|
|
ynh_handle_getopts_args "$@"
|
|
|
|
./yunohost.multimedia-master/script/ynh_media_addfolder.sh --inv --source="$source_dir" --dest="$dest_dir"
|
|
}
|
|
|
|
|
|
#=================================================
|
|
|
|
# Send an email to inform the administrator
|
|
#
|
|
# usage: ynh_send_readme_to_admin app_message [recipients]
|
|
# | arg: -m --app_message= - The message to send to the administrator.
|
|
# | arg: -r, --recipients= - The recipients of this email. Use spaces to separate multiples recipients. - default: root
|
|
# example: "root admin@domain"
|
|
# 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"
|
|
ynh_send_readme_to_admin() {
|
|
# Declare an array to define the options of this helper.
|
|
declare -Ar args_array=( [m]=app_message= [r]=recipients= )
|
|
local app_message
|
|
local recipients
|
|
# Manage arguments with getopts
|
|
ynh_handle_getopts_args "$@"
|
|
local app_message="${app_message:-...No specific information...}"
|
|
local recipients="${recipients:-root}"
|
|
|
|
# Retrieve the email of users
|
|
find_mails () {
|
|
local list_mails="$1"
|
|
local mail
|
|
local recipients=" "
|
|
# Read each mail in argument
|
|
for mail in $list_mails
|
|
do
|
|
# Keep root or a real email address as it is
|
|
if [ "$mail" = "root" ] || echo "$mail" | grep --quiet "@"
|
|
then
|
|
recipients="$recipients $mail"
|
|
else
|
|
# But replace an user name without a domain after by its email
|
|
if mail=$(ynh_user_get_info "$mail" "mail" 2> /dev/null)
|
|
then
|
|
recipients="$recipients $mail"
|
|
fi
|
|
fi
|
|
done
|
|
echo "$recipients"
|
|
}
|
|
recipients=$(find_mails "$recipients")
|
|
|
|
local mail_subject="☁️🆈🅽🅷☁️: \`$app\` was just installed!"
|
|
|
|
local mail_message="This is an automated message from your beloved YunoHost server.
|
|
|
|
Specific information for the application $app.
|
|
|
|
$app_message
|
|
|
|
---
|
|
Automatic diagnosis data from YunoHost
|
|
|
|
$(yunohost tools diagnosis | grep -B 100 "services:" | sed '/services:/d')"
|
|
|
|
# Define binary to use for mail command
|
|
if [ -e /usr/bin/bsd-mailx ]
|
|
then
|
|
local mail_bin=/usr/bin/bsd-mailx
|
|
else
|
|
local mail_bin=/usr/bin/mail.mailutils
|
|
fi
|
|
|
|
# Send the email to the recipients
|
|
echo "$mail_message" | $mail_bin -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: -n, --service_name= - Name of the service to reload
|
|
# | arg: -a, --action= - Action to perform with systemctl. Default: reload
|
|
ynh_system_reload () {
|
|
# Declare an array to define the options of this helper.
|
|
declare -Ar args_array=( [n]=service_name= [a]=action= )
|
|
local service_name
|
|
local action
|
|
# Manage arguments with getopts
|
|
ynh_handle_getopts_args "$@"
|
|
local action=${action:-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_maintenance_mode_ON () {
|
|
# Load value of $path_url and $domain from the config if their not set
|
|
if [ -z $path_url ]; then
|
|
path_url=$(ynh_app_setting_get $app path)
|
|
fi
|
|
if [ -z $domain ]; then
|
|
domain=$(ynh_app_setting_get $app domain)
|
|
fi
|
|
|
|
# Create an html to serve as maintenance notice
|
|
echo "<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="refresh" content="3">
|
|
<title>Your app $app is currently under maintenance!</title>
|
|
<style>
|
|
body {
|
|
width: 70em;
|
|
margin: 0 auto;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>Your app $app is currently under maintenance!</h1>
|
|
<p>This app has been put under maintenance by your administrator at $(date)</p>
|
|
<p>Please wait until the maintenance operation is done. This page will be reloaded as soon as your app will be back.</p>
|
|
|
|
</body>
|
|
</html>" > "/var/www/html/maintenance.$app.html"
|
|
|
|
# Create a new nginx config file to redirect all access to the app to the maintenance notice instead.
|
|
echo "# All request to the app will be redirected to ${path_url}_maintenance and fall on the maintenance notice
|
|
rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/? redirect;
|
|
# Use another location, to not be in conflict with the original config file
|
|
location ${path_url}_maintenance/ {
|
|
alias /var/www/html/ ;
|
|
|
|
try_files maintenance.$app.html =503;
|
|
|
|
# Include SSOWAT user panel.
|
|
include conf.d/yunohost_panel.conf.inc;
|
|
}" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
|
|
|
|
# The current config file will redirect all requests to the root of the app.
|
|
# To keep the full path, we can use the following rewrite rule:
|
|
# rewrite ^${path_url}/(.*)$ ${path_url}_maintenance/\$1? redirect;
|
|
# The difference will be in the $1 at the end, which keep the following queries.
|
|
# But, if it works perfectly for a html request, there's an issue with any php files.
|
|
# This files are treated as simple files, and will be downloaded by the browser.
|
|
# Would be really be nice to be able to fix that issue. So that, when the page is reloaded after the maintenance, the user will be redirected to the real page he was.
|
|
|
|
systemctl reload nginx
|
|
}
|
|
|
|
ynh_maintenance_mode_OFF () {
|
|
# Load value of $path_url and $domain from the config if their not set
|
|
if [ -z $path_url ]; then
|
|
path_url=$(ynh_app_setting_get $app path)
|
|
fi
|
|
if [ -z $domain ]; then
|
|
domain=$(ynh_app_setting_get $app domain)
|
|
fi
|
|
|
|
# Rewrite the nginx config file to redirect from ${path_url}_maintenance to the real url of the app.
|
|
echo "rewrite ^${path_url}_maintenance/(.*)$ ${path_url}/\$1 redirect;" > "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
|
|
systemctl reload nginx
|
|
|
|
# Sleep 4 seconds to let the browser reload the pages and redirect the user to the app.
|
|
sleep 4
|
|
|
|
# Then remove the temporary files used for the maintenance.
|
|
rm "/var/www/html/maintenance.$app.html"
|
|
rm "/etc/nginx/conf.d/$domain.d/maintenance.$app.conf"
|
|
|
|
systemctl reload nginx
|
|
}
|