garage_ynh/scripts/_common.sh

128 lines
3.3 KiB
Bash
Raw Normal View History

2017-06-05 13:04:03 +02:00
#!/bin/bash
2019-02-10 15:02:38 +01:00
#=================================================
2019-03-05 23:17:15 +01:00
# COMMON VARIABLES
2019-02-10 15:02:38 +01:00
#=================================================
2022-11-29 14:38:41 +01:00
pkg_dependencies_virtualisation="qemu-utils e2fsprogs"
2019-03-05 23:11:52 +01:00
2019-03-05 23:17:15 +01:00
#=================================================
# PERSONAL HELPERS
#=================================================
2022-07-27 10:00:43 +02:00
GARAGE_VERSION="0.8.0"
2022-07-27 10:00:43 +02:00
2022-07-30 14:02:57 +02:00
get_ip() {
curl ip.me
}
2022-07-27 10:00:43 +02:00
# inspired by restic helper
install_garage () {
architecture=$(uname -m)
arch=''
case $architecture in
i386|i686)
arch="i686"
;;
x86_64)
arch=x86_64
;;
armv*)
arch=armv6l
;;
aarch64)
arch=aarch64
;;
*)
echo
ynh_die --message="Unsupported architecture \"$architecture\""
;;
esac
2022-10-06 09:46:45 +02:00
wget https://garagehq.deuxfleurs.fr/_releases/v$GARAGE_VERSION/$arch-unknown-linux-musl/garage -O garage 2>&1 >/dev/null
chmod +x garage
2022-07-27 10:00:43 +02:00
}
garage_connect() {
local command="$1"
local peer="$2"
# connect to cluster
2022-11-29 14:38:41 +01:00
$command node connect "$peer"
sleep 2
# wait until layout is updated
2022-12-23 10:56:23 +01:00
local i=0
2022-11-29 14:38:41 +01:00
until $command layout show 2>/dev/null | grep "${peer:0:15}"; do
2022-12-23 10:56:23 +01:00
i=$(( i + 1 ))
if [ $i -gt 30 ]
then
2022-12-23 11:36:28 +01:00
ynh_die --message="unable to get layout from remote peer"
2022-12-23 10:56:23 +01:00
fi
sleep 1
done
2022-09-12 13:18:58 +02:00
}
2022-09-12 13:18:58 +02:00
apply_layout() {
2022-10-06 09:46:45 +02:00
garage_command=$1
$garage_command layout show 2>/dev/null
local layout_version=$($garage_command layout show 2>/dev/null | grep -Po -- "(?<=--version).*" | head -1 | xargs)
if [ "$layout_version" != "" ]
2022-09-12 13:18:58 +02:00
then
$garage_command layout apply --version $layout_version
else
ynh_print_warn --message="unable to apply layout. No enough nodes"
2022-10-06 09:46:45 +02:00
return 0
2022-09-12 13:18:58 +02:00
fi
}
2019-02-10 15:02:38 +01:00
#=================================================
# EXPERIMENTAL HELPERS
#=================================================
2022-07-30 14:02:57 +02:00
ynh_send_readme_to_admin() {
2022-10-06 09:46:45 +02:00
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.
2022-07-30 14:02:57 +02:00
Specific information for the application $app.
2022-10-06 09:46:45 +02:00
$app_message"
# 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"
2022-07-30 14:02:57 +02:00
}
2019-02-10 15:02:38 +01:00
#=================================================
# FUTURE OFFICIAL HELPERS
2019-02-10 15:02:38 +01:00
#=================================================