mirror of
https://github.com/YunoHost-Apps/restic_ynh.git
synced 2024-09-03 20:16:22 +02:00
247 lines
7.2 KiB
Bash
247 lines
7.2 KiB
Bash
#!/bin/bash
|
|
|
|
#=================================================
|
|
# COMMON VARIABLES
|
|
#=================================================
|
|
# App package root directory should be the parent folder
|
|
PKG_DIR=$(cd ../; pwd)
|
|
RESTIC_VERSION="0.10.0"
|
|
|
|
# Install restic if restic is not here
|
|
install_restic () {
|
|
architecture=$(uname -m)
|
|
arch=''
|
|
case $architecture in
|
|
i386|i686)
|
|
arch="386"
|
|
;;
|
|
x86_64)
|
|
arch=amd64
|
|
;;
|
|
armv*)
|
|
arch=arm
|
|
;;
|
|
*)
|
|
echo "Unsupported architecture \"$architecture\""
|
|
exit 1
|
|
;;
|
|
esac
|
|
wget https://github.com/restic/restic/releases/download/v${RESTIC_VERSION}/restic_${RESTIC_VERSION}_linux_${arch}.bz2 -O /tmp/restic.bz2 2>&1 >/dev/null
|
|
wget https://github.com/restic/restic/releases/download/v${RESTIC_VERSION}/SHA256SUMS -O /tmp/restic-sha256sums 2>&1 >/dev/null
|
|
expected_sum=$(grep restic_${RESTIC_VERSION}_linux_${arch}.bz2 /tmp/restic-sha256sums | awk '{print $1}')
|
|
sum=$(sha256sum /tmp/restic.bz2 | awk '{print $1}')
|
|
if [ "$sum" == "$expected_sum" ];then
|
|
pkill restic || true
|
|
bunzip2 /tmp/restic.bz2 -f -c > /usr/local/bin/restic
|
|
chmod +x /usr/local/bin/restic
|
|
else
|
|
echo -e "\e[91m \e[1m"
|
|
echo -e "\nDownloaded file does not match expected sha256 sum, aborting"
|
|
echo -e "\e[22m"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
#=================================================
|
|
# COMMON HELPERS
|
|
#=================================================
|
|
ynh_export () {
|
|
local ynh_arg=""
|
|
for var in $@;
|
|
do
|
|
ynh_arg=$(echo $var | awk '{print toupper($0)}')
|
|
if [ "$var" == "path_url" ]; then
|
|
ynh_arg="PATH"
|
|
fi
|
|
ynh_arg="YNH_APP_ARG_$ynh_arg"
|
|
export $var="${!ynh_arg}"
|
|
done
|
|
}
|
|
# Save listed var in YunoHost app settings
|
|
# usage: ynh_save_args VARNAME1 [VARNAME2 [...]]
|
|
ynh_save_args () {
|
|
for var in $@;
|
|
do
|
|
local setting_var="$var"
|
|
if [ "$var" == "path_url" ]; then
|
|
setting_var="path"
|
|
fi
|
|
ynh_app_setting_set $app $setting_var "${!var}"
|
|
done
|
|
}
|
|
|
|
# Render templates with Jinja2
|
|
#
|
|
# Attention : Variables should be exported before calling this helper to be
|
|
# accessible inside templates.
|
|
#
|
|
# usage: ynh_render_template some_template output_path
|
|
# | arg: some_template - Template file to be rendered
|
|
# | arg: output_path - The path where the output will be redirected to
|
|
ynh_render_template() {
|
|
local template_path=$1
|
|
local output_path=$2
|
|
# Taken from https://stackoverflow.com/a/35009576
|
|
python3 -c 'import os, sys, jinja2; sys.stdout.write(
|
|
jinja2.Template(sys.stdin.read()
|
|
).render(os.environ));' < $template_path > $output_path
|
|
}
|
|
|
|
ynh_configure () {
|
|
ynh_backup_if_checksum_is_different $2
|
|
ynh_render_template "${PKG_DIR}/conf/$1.j2" "$2"
|
|
ynh_store_file_checksum $2
|
|
}
|
|
|
|
# Remove any logs for all the following commands.
|
|
#
|
|
# usage: ynh_print_OFF
|
|
# WARNING: You should be careful with this helper, and never forgot to use ynh_print_ON as soon as possible to restore the logging.
|
|
ynh_print_OFF () {
|
|
set +x
|
|
}
|
|
|
|
# Restore the logging after ynh_print_OFF
|
|
#
|
|
# usage: ynh_print_ON
|
|
ynh_print_ON () {
|
|
set -x
|
|
# Print an echo only for the log, to be able to know that ynh_print_ON has been called.
|
|
echo ynh_print_ON > /dev/null
|
|
}
|
|
|
|
# Send an email to inform the administrator
|
|
#
|
|
# usage: ynh_send_readme_to_admin app_message [recipients]
|
|
# | arg: app_message - The message to send to the administrator.
|
|
# | arg: 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() {
|
|
local app_message="${1:-...No specific information...}"
|
|
local recipients="${2:-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"
|
|
}
|
|
|
|
# Read the value of a key in a ynh manifest file
|
|
#
|
|
# usage: ynh_read_manifest manifest key
|
|
# | arg: manifest - Path of the manifest to read
|
|
# | arg: key - Name of the key to find
|
|
ynh_read_manifest () {
|
|
manifest="$1"
|
|
key="$2"
|
|
python3 -c "import sys, json;print(json.load(open('$manifest', encoding='utf-8'))['$key'])"
|
|
}
|
|
|
|
|
|
# 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 "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" "version" || echo 1.0)
|
|
local current_upstream_version="${current_version/~ynh*/}"
|
|
local update_version=$(ynh_read_manifest "../manifest.json" "version" || echo 1.0)
|
|
local update_upstream_version="${update_version/~ynh*/}"
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|