#!/bin/bash #================================================= # COMMON VARIABLES #================================================= pkg_dependencies="g++ libjemalloc1|libjemalloc2 libjemalloc-dev zlib1g-dev libreadline-dev libpq-dev libssl-dev libyaml-dev libcurl4-openssl-dev libapr1-dev libxslt1-dev libxml2-dev vim imagemagick postgresql postgresql-server-dev-all postgresql-contrib optipng jhead jpegoptim gifsicle brotli" RUBY_VERSION="2.7.1" #================================================= # PERSONAL HELPERS #================================================= # Execute a command as another user with login # (hence in user home dir, with prior loading of .profile, etc.) # usage: exec_login_as USER COMMAND [ARG ...] exec_login_as() { local user=$1 shift 1 exec_as $user --login "$@" } # Execute a command as another user # usage: exec_as USER COMMAND [ARG ...] exec_as() { local user=$1 shift 1 if [[ $user = $(whoami) ]]; then eval "$@" else sudo -u "$user" "$@" fi } # Returns true if a swap partition is enabled, false otherwise # usage: is_swap_present is_swap_present() { [ $(awk '/^SwapTotal:/{print $2}' /proc/meminfo) -gt 0 ] } # Returns true if swappiness higher than 50 # usage: is_swappiness_sufficient is_swappiness_sufficient() { [ $(cat /proc/sys/vm/swappiness) -gt 50 ] } # Returns true if specified free memory is available (RAM + swap) # usage: is_memory_available MEMORY (in bytes) is_memory_available() { 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 ] } # Checks discourse install memory requirements # terminates installation if requirements not met check_memory_requirements() { if ! is_swap_present ; then ynh_die --message="You must have a swap partition in order to install and use this application" elif ! is_swappiness_sufficient ; then ynh_die --message="Your swappiness must be higher than 50; please see https://en.wikipedia.org/wiki/Swappiness" elif ! is_memory_available 1000000 ; then ynh_die --message="You must have a minimum of 1Gb available memory (RAM+swap) for the installation" fi } # 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() { if ! is_memory_available 400000 ; then ynh_die --message="You must have a minimum of 400Mb available memory (RAM+swap) for the upgrade" fi } 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 "
This app has been put under maintenance by your administrator at $(date)
Please wait until the maintenance operation is done. This page will be reloaded as soon as your app will be back.
" > "/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 } #================================================= # EXPERIMENTAL HELPERS #================================================= #================================================= # FUTURE OFFICIAL HELPERS #================================================= #================================================= # RUBY HELPER #================================================= rbenv_version=1.1.2 ruby_build_version=20201225 rbenv_aliases_version=1.1.0 rbenv_install_dir="/opt/rbenv" ruby_version_path="$rbenv_install_dir/versions" # RBENV_ROOT is the directory of rbenv, it needs to be loaded as a environment variable. export RBENV_ROOT="$rbenv_install_dir" # Install Ruby Version Management # # [internal] # # usage: ynh_install_rbenv # # Requires YunoHost version 2.7.12 or higher. ynh_install_rbenv () { ynh_print_info --message="Installation of rbenv - Ruby Version Management - rbenv-${rbenv_version}/ruby-build-${ruby_build_version}" # Build an app.src for rbenv mkdir -p "../conf" echo "SOURCE_URL=https://github.com/rbenv/rbenv/archive/v${rbenv_version}.tar.gz SOURCE_SUM=80ad89ffe04c0b481503bd375f05c212bbc7d44ef5f5e649e0acdf25eba86736" > "../conf/rbenv.src" # Download and extract rbenv ynh_setup_source --dest_dir="$rbenv_install_dir" --source_id=rbenv # Build an app.src for ruby-build mkdir -p "../conf" echo "SOURCE_URL=https://github.com/rbenv/ruby-build/archive/v${ruby_build_version}.tar.gz SOURCE_SUM=54cae123c2758e7714c66aca7ef8bc7f29cda8583891191ceb3053c6d098ecf1" > "../conf/ruby-build.src" # Download and extract ruby-build ynh_setup_source --dest_dir="$rbenv_install_dir/plugins/ruby-build" --source_id=ruby-build # Build an app.src for rbenv-aliases mkdir -p "../conf" echo "SOURCE_URL=https://github.com/tpope/rbenv-aliases/archive/v${rbenv_aliases_version}.tar.gz SOURCE_SUM=12e89bc4499e85d8babac2b02bc8b66ceb0aa3f8047b26728a3eca8a6030273d" > "../conf/rbenv-aliases.src" # Download and extract rbenv-aliases ynh_setup_source --dest_dir="$rbenv_install_dir/plugins/rbenv-aliases" --source_id=rbenv-aliases (cd $rbenv_install_dir ./src/configure && make -C src) # Create shims directory if needed if [ ! -d $rbenv_install_dir/shims ] ; then mkdir $rbenv_install_dir/shims fi } # Load the version of Ruby for an app, and set variables. # # ynh_use_ruby has to be used in any app scripts before using Ruby for the first time. # This helper will provide alias and variables to use in your scripts. # # To use gem or Ruby, use the alias `ynh_gem` and `ynh_ruby` # Those alias will use the correct version installed for the app # For example: use `ynh_gem install` instead of `gem install` # # With `sudo` or `ynh_exec_as`, use instead the fallback variables `$ynh_gem` and `$ynh_ruby` # And propagate $PATH to sudo with $ynh_ruby_load_path # Exemple: `ynh_exec_as $app $ynh_ruby_load_path $ynh_gem install` # # $PATH contains the path of the requested version of Ruby. # However, $PATH is duplicated into $ruby_path to outlast any manipulation of $PATH # You can use the variable `$ynh_ruby_load_path` to quickly load your Ruby version # in $PATH for an usage into a separate script. # Exemple: $ynh_ruby_load_path $final_path/script_that_use_gem.sh` # # # Finally, to start a Ruby service with the correct version, 2 solutions # Either the app is dependent of Ruby or gem, but does not called it directly. # In such situation, you need to load PATH # `Environment="__YNH_RUBY_LOAD_ENV_PATH__"` # `ExecStart=__FINALPATH__/my_app` # You will replace __YNH_RUBY_LOAD_ENV_PATH__ with $ynh_ruby_load_path # # Or Ruby start the app directly, then you don't need to load the PATH variable # `ExecStart=__YNH_RUBY__ my_app run` # You will replace __YNH_RUBY__ with $ynh_ruby # # # one other variable is also available # - $ruby_path: The absolute path to Ruby binaries for the chosen version. # # usage: ynh_use_ruby # # Requires YunoHost version 2.7.12 or higher. ynh_use_ruby () { ruby_version=$(ynh_app_setting_get --app=$app --key=ruby_version) # Get the absolute path of this version of Ruby ruby_path="$ruby_version_path/$YNH_APP_INSTANCE_NAME/bin" # Allow alias to be used into bash script shopt -s expand_aliases # Create an alias for the specific version of Ruby and a variable as fallback ynh_ruby="$ruby_path/ruby" alias ynh_ruby="$ynh_ruby" # And gem ynh_gem="$ruby_path/gem" alias ynh_gem="$ynh_gem" # Load the path of this version of Ruby in $PATH if [[ :$PATH: != *":$ruby_path"* ]]; then PATH="$ruby_path:$PATH" fi # Create an alias to easily load the PATH ynh_ruby_load_path="PATH=$PATH" # Sets the local application-specific Ruby version pushd $final_path rbenv local $ruby_version popd } # Install a specific version of Ruby # # ynh_install_ruby will install the version of Ruby provided as argument by using rbenv. # # rbenv (Ruby Version Management) stores the target Ruby version in a .ruby_version file created in the target folder (using rbenv local