2018-04-02 16:36:02 +02:00
#!/bin/bash
2020-04-27 13:41:00 +02:00
#=================================================
# COMMON VARIABLES
#=================================================
2024-07-01 10:38:12 +02:00
ruby_version = "3.2"
2022-07-27 04:15:08 +02:00
2024-05-16 09:06:32 +02:00
nodejs_version = "20"
2018-04-02 16:36:02 +02:00
2024-01-26 22:16:29 +01:00
libjemalloc = " $( ldconfig -p | grep libjemalloc | awk 'END {print $NF}' ) "
2020-04-27 13:41:00 +02:00
#=================================================
# PERSONAL HELPERS
#=================================================
2024-08-30 11:22:21 +02:00
_exec_as_app_with_ruby_node( ) {
ynh_exec_as " $app " -E env PATH = " $nodejs_path : $ruby_path : $PATH " " $@ "
}
2018-04-30 18:53:20 +02:00
# Returns true if a swap partition is enabled, false otherwise
# usage: is_swap_present
is_swap_present( ) {
2024-08-29 12:52:07 +02:00
[ $( awk '/^SwapTotal:/{print $2}' /proc/meminfo) -gt 0 ]
2018-04-30 18:53:20 +02:00
}
2023-09-25 14:51:10 +02:00
# Returns true if swappiness higher than 10
2018-04-30 18:53:20 +02:00
# usage: is_swappiness_sufficient
is_swappiness_sufficient( ) {
2024-08-29 12:52:07 +02:00
[ $( cat /proc/sys/vm/swappiness) -gt 10 ]
2018-04-30 18:53:20 +02:00
}
# Returns true if specified free memory is available (RAM + swap)
# usage: is_memory_available MEMORY (in bytes)
is_memory_available( ) {
2024-08-29 12:52:07 +02:00
local needed_memory = $1
local freemem = " $( awk '/^MemAvailable:/{print $2}' /proc/meminfo) "
local freeswap = " $( awk '/^SwapFree:/{print $2}' /proc/meminfo) "
[ $(( $freemem + $freeswap )) -gt $needed_memory ]
2018-04-30 18:53:20 +02:00
}
# Checks discourse install memory requirements
# terminates installation if requirements not met
check_memory_requirements( ) {
2024-08-29 12:52:07 +02:00
if ! is_swap_present ; then
ynh_print_warn --message= "You must have a swap partition in order to install and use this application"
elif ! is_swappiness_sufficient ; then
ynh_print_warn --message= "Your swappiness must be higher than 10; please see https://en.wikipedia.org/wiki/Swappiness"
elif ! is_memory_available 1000000 ; then
ynh_print_warn --message= "You must have a minimum of 1Gb available memory (RAM+swap) for the installation"
fi
2018-04-30 18:53:20 +02:00
}
# Checks discourse upgrade memory requirements
# Less requirements as the software is already installed and running
# terminates upgrade if requirements not met
check_memory_requirements_upgrade( ) {
2024-08-29 12:52:07 +02:00
if ! is_memory_available 400000 ; then
ynh_die --message= "You must have a minimum of 400Mb available memory (RAM+swap) for the upgrade"
fi
2018-04-30 18:53:20 +02:00
}
2018-04-02 16:36:02 +02:00
2024-08-30 13:41:05 +02:00
tools_prefix = " $install_dir /dependencies "
2024-08-29 13:56:11 +02:00
install_imagemagick( ) {
2024-08-30 13:41:05 +02:00
# See https://github.com/discourse/discourse_docker/blob/main/image/base/install-imagemagick
2024-08-29 13:56:11 +02:00
ynh_setup_source --source_id= "imagemagickv7" --dest_dir= " $install_dir /imagemagick_source "
2024-08-30 13:41:05 +02:00
mkdir -p " $tools_prefix "
chown -R " $app : $app " " $install_dir /imagemagick_source " " $tools_prefix "
2024-08-29 13:56:11 +02:00
pushd " $install_dir /imagemagick_source "
2024-08-30 13:41:05 +02:00
ynh_exec_as " $app " CFLAGS = " -O2 -I $tools_prefix /include -Wno-deprecated-declarations " \
2024-08-29 13:56:11 +02:00
./configure \
2024-08-30 13:41:05 +02:00
--prefix= " $tools_prefix " \
2024-08-29 13:56:11 +02:00
--enable-static \
--enable-bounds-checking \
--enable-hdri \
--enable-hugepages \
--with-threads \
--with-modules \
--with-quantum-depth= 16 \
--without-magick-plus-plus \
--with-bzlib \
--with-zlib \
--without-autotrace \
--with-freetype \
--with-jpeg \
--without-lcms \
--with-lzma \
--with-png \
--with-tiff \
--with-heic \
--with-rsvg \
--with-webp
ynh_exec_as " $app " make all -j" $( nproc) "
ynh_exec_as " $app " LIBTOOLFLAGS = -Wnone make install
popd
}
2024-03-19 21:33:15 +01:00
ynh_maintenance_mode_ON ( ) {
2024-08-29 12:52:07 +02:00
# Create an html to serve as maintenance notice
echo " <!DOCTYPE html>
2024-03-19 21:33:15 +01:00
<html>
<head>
<meta http-equiv= "refresh" content = "3" >
<title>Your app $app is currently under maintenance!</title>
<style>
2024-08-29 12:52:07 +02:00
body {
width: 70em;
margin: 0 auto;
}
2024-03-19 21:33:15 +01:00
</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"
2024-08-29 12:52:07 +02:00
# 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 } _maintenance and fall on the maintenance notice
2024-03-19 21:33:15 +01:00
rewrite ^${ path } /( .*) $ ${ path } _maintenance/? redirect;
# Use another location, to not be in conflict with the original config file
location ${ path } _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"
2024-08-29 12:52:07 +02:00
# 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}/(.*)$ ${path}_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.
2024-03-19 21:33:15 +01:00
2024-08-29 12:52:07 +02:00
systemctl reload nginx
2024-03-19 21:33:15 +01:00
}
ynh_maintenance_mode_OFF ( ) {
2024-08-29 12:52:07 +02:00
# Rewrite the nginx config file to redirect from ${path}_maintenance to the real url of the app.
echo " rewrite ^ ${ path } _maintenance/(.*) $ ${ path } /\$1 redirect; " > " /etc/nginx/conf.d/ $domain .d/maintenance. $app .conf "
systemctl reload nginx
2024-03-19 21:33:15 +01:00
2024-08-29 12:52:07 +02:00
# Sleep 4 seconds to let the browser reload the pages and redirect the user to the app.
sleep 4
2024-03-19 21:33:15 +01:00
2024-08-29 12:52:07 +02:00
# 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 "
2024-03-19 21:33:15 +01:00
2024-08-29 12:52:07 +02:00
systemctl reload nginx
2024-03-19 21:33:15 +01:00
}
2018-04-02 16:36:02 +02:00
2020-04-27 13:41:00 +02:00
#=================================================
# EXPERIMENTAL HELPERS
#=================================================
2018-04-02 16:36:02 +02:00
2020-04-27 13:41:00 +02:00
#=================================================
# FUTURE OFFICIAL HELPERS
#=================================================