1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/etherpad_mypads_ynh.git synced 2024-09-03 18:36:09 +02:00

Update and purge _common.sh

This commit is contained in:
Maniack Crudelis 2018-02-18 23:32:40 +01:00
parent f7e7ea9735
commit d62ad8cfa6
2 changed files with 43 additions and 210 deletions

View file

@ -22,14 +22,6 @@ CHECK_SIZE () { # Vérifie avant chaque backup que l'espace est suffisant
fi fi
} }
#=================================================
# PACKAGE CHECK BYPASSING...
#=================================================
IS_PACKAGE_CHECK () { # Détermine une exécution en conteneur (Non testé)
return $(uname -n | grep -c 'pchecker_lxc')
}
#================================================= #=================================================
# EXPERIMENTAL HELPERS # EXPERIMENTAL HELPERS
#================================================= #=================================================
@ -212,15 +204,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 +223,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 +238,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 +246,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 ""
@ -272,14 +267,6 @@ ynh_print_info () {
ynh_print_log "[INFO] ${1}" ynh_print_log "[INFO] ${1}"
} }
# Print a warning on stderr
#
# usage: ynh_print_warn "Text to print"
# | arg: text - The text to print
ynh_print_warn () {
ynh_print_log "[WARN] ${1}" >&2
}
# Print a error on stderr # Print a error on stderr
# #
# usage: ynh_print_err "Text to print" # usage: ynh_print_err "Text to print"
@ -288,61 +275,6 @@ ynh_print_err () {
ynh_print_log "[ERR] ${1}" >&2 ynh_print_log "[ERR] ${1}" >&2
} }
# Execute a command and print the result as an error
#
# usage: ynh_exec_err command to execute
# usage: ynh_exec_err "command to execute | following command"
# In case of use of pipes, you have to use double quotes. Otherwise, this helper will be executed with the first command, then be send to the next pipe.
#
# | arg: command - command to execute
ynh_exec_err () {
ynh_print_err "$(eval $@)"
}
# Execute a command and print the result as a warning
#
# usage: ynh_exec_warn command to execute
# usage: ynh_exec_warn "command to execute | following command"
# In case of use of pipes, you have to use double quotes. Otherwise, this helper will be executed with the first command, then be send to the next pipe.
#
# | arg: command - command to execute
ynh_exec_warn () {
ynh_print_warn "$(eval $@)"
}
# Execute a command and force the result to be printed on stdout
#
# usage: ynh_exec_warn_less command to execute
# usage: ynh_exec_warn_less "command to execute | following command"
# In case of use of pipes, you have to use double quotes. Otherwise, this helper will be executed with the first command, then be send to the next pipe.
#
# | arg: command - command to execute
ynh_exec_warn_less () {
eval $@ 2>&1
}
# Execute a command and redirect stdout in /dev/null
#
# usage: ynh_exec_quiet command to execute
# usage: ynh_exec_quiet "command to execute | following command"
# In case of use of pipes, you have to use double quotes. Otherwise, this helper will be executed with the first command, then be send to the next pipe.
#
# | arg: command - command to execute
ynh_exec_quiet () {
eval $@ > /dev/null
}
# Execute a command and redirect stdout and stderr in /dev/null
#
# usage: ynh_exec_fully_quiet command to execute
# usage: ynh_exec_fully_quiet "command to execute | following command"
# In case of use of pipes, you have to use double quotes. Otherwise, this helper will be executed with the first command, then be send to the next pipe.
#
# | arg: command - command to execute
ynh_exec_fully_quiet () {
eval $@ > /dev/null 2>&1
}
# Remove any logs for all the following commands. # Remove any logs for all the following commands.
# #
# usage: ynh_print_OFF # usage: ynh_print_OFF
@ -362,55 +294,6 @@ ynh_print_ON () {
#================================================= #=================================================
# Install or update the main directory yunohost.multimedia
#
# usage: ynh_multimedia_build_main_dir
ynh_multimedia_build_main_dir () {
wget -nv https://github.com/YunoHost-Apps/yunohost.multimedia/archive/master.zip 2>&1
unzip -q master.zip
./yunohost.multimedia-master/script/ynh_media_build.sh
}
# Add a directory in yunohost.multimedia
# This "directory" will be a symbolic link to a existing directory.
#
# usage: ynh_multimedia_addfolder "Source directory" "Destination directory"
#
# | arg: Source directory - The real directory which contains your medias.
# | arg: Destination directory - The name and the place of the symbolic link, relative to "/home/yunohost.multimedia"
ynh_multimedia_addfolder () {
local source_dir="$1"
local dest_dir="$2"
./yunohost.multimedia-master/script/ynh_media_addfolder.sh --source="$source_dir" --dest="$dest_dir"
}
# Move a directory in yunohost.multimedia, and replace by a symbolic link
#
# usage: ynh_multimedia_movefolder "Source directory" "Destination directory"
#
# | arg: Source directory - The real directory which contains your medias.
# It will be moved to "Destination directory"
# A symbolic link will replace it.
# | arg: Destination directory - The new name and place of the directory, relative to "/home/yunohost.multimedia"
ynh_multimedia_movefolder () {
local source_dir="$1"
local dest_dir="$2"
./yunohost.multimedia-master/script/ynh_media_addfolder.sh --inv --source="$source_dir" --dest="$dest_dir"
}
# Allow an user to have an write authorisation in multimedia directories
#
# usage: ynh_multimedia_addaccess user_name
#
# | arg: user_name - The name of the user which gain this access.
ynh_multimedia_addaccess () {
local user_name=$1
groupadd -f multimedia
usermod -a -G multimedia $user_name
}
#=================================================
# Create a dedicated fail2ban config (jail and filter conf files) # Create a dedicated fail2ban config (jail and filter conf files)
# #
# usage: ynh_add_fail2ban_config log_file filter [max_retry [ports]] # usage: ynh_add_fail2ban_config log_file filter [max_retry [ports]]
@ -439,7 +322,7 @@ 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
@ -447,16 +330,17 @@ EOF
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.*")"
if [ -n "$fail2ban_error" ]
then then
echo "[ERR] Fail2ban fail to load the jail for $app" >&2 echo "[ERR] Fail2ban failed to load the jail for $app" >&2
echo "WARNING${fail2ban_error#*WARNING}" >&2 echo "WARNING${fail2ban_error#*WARNING}" >&2
fi fi
} }
@ -525,7 +409,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 +440,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

View file

@ -1,51 +0,0 @@
#!/bin/bash
# https://github.com/YunoHost/yunohost/pull/394
# Substitute/replace a string (or expression) by another in a file
#
# usage: ynh_replace_string match_string replace_string target_file
# | arg: match_string - String to be searched and replaced in the file
# | arg: replace_string - String that will replace matches
# | arg: target_file - File in which the string will be replaced.
#
# As this helper is based on sed command, regular expressions and
# references to sub-expressions can be used
# (see sed manual page for more information)
ynh_replace_string () {
local delimit=@
local match_string=$1
local replace_string=$2
local workfile=$3
# Escape the delimiter if it's in the string.
match_string=${match_string//${delimit}/"\\${delimit}"}
replace_string=${replace_string//${delimit}/"\\${delimit}"}
sudo sed --in-place "s${delimit}${match_string}${delimit}${replace_string}${delimit}g" "$workfile"
}
# Substitute/replace a password by another in a file
#
# usage: ynh_replace_password_string match_string replace_string target_file
# | arg: match_string - String to be searched and replaced in the file
# | arg: replace_string - String that will replace matches
# | arg: target_file - File in which the string will be replaced.
#
# This helper will use ynh_replace_string, but as you can use special
# characters, you can't use some regular expressions and sub-expressions.
ynh_replace_password_string () {
local match_string=$1
local replace_string=$2
local workfile=$3
# Escape any backslash to preserve them as simple backslash.
match_string=${match_string//\\/"\\\\"}
replace_string=${replace_string//\\/"\\\\"}
# Escape the & character, who has a special function in sed.
match_string=${match_string//&/"\&"}
replace_string=${replace_string//&/"\&"}
ynh_replace_string "$match_string" "$replace_string" "$workfile"
}