2019-03-19 23:33:34 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2021-02-24 06:35:45 +01:00
|
|
|
rbenv_version=1.1.2
|
2021-03-06 01:10:38 +01:00
|
|
|
ruby_build_version=20201225
|
2021-02-24 06:35:45 +01:00
|
|
|
rbenv_aliases_version=1.1.0
|
2019-03-19 23:33:34 +01:00
|
|
|
rbenv_install_dir="/opt/rbenv"
|
2021-03-06 01:10:38 +01:00
|
|
|
ruby_version_path="$rbenv_install_dir/versions"
|
2019-03-19 23:33:34 +01:00
|
|
|
# RBENV_ROOT is the directory of rbenv, it needs to be loaded as a environment variable.
|
|
|
|
export RBENV_ROOT="$rbenv_install_dir"
|
|
|
|
|
2021-02-24 06:35:45 +01:00
|
|
|
# Install Ruby Version Management
|
2019-03-19 23:33:34 +01:00
|
|
|
#
|
|
|
|
# [internal]
|
|
|
|
#
|
|
|
|
# usage: ynh_install_rbenv
|
2021-03-06 01:10:38 +01:00
|
|
|
#
|
|
|
|
# Requires YunoHost version 2.7.12 or higher.
|
2019-03-19 23:33:34 +01:00
|
|
|
ynh_install_rbenv () {
|
2021-03-06 01:10:38 +01:00
|
|
|
ynh_print_info --message="Installation of rbenv - Ruby Version Management - rbenv-${rbenv_version}/ruby-build-${ruby_build_version}"
|
2021-02-24 06:35:45 +01:00
|
|
|
|
2021-03-06 01:10:38 +01:00
|
|
|
# Build an app.src for rbenv
|
|
|
|
mkdir -p "../conf"
|
|
|
|
echo "SOURCE_URL=https://github.com/rbenv/rbenv/archive/v${rbenv_version}.tar.gz
|
2019-10-07 22:54:13 +02:00
|
|
|
SOURCE_SUM=80ad89ffe04c0b481503bd375f05c212bbc7d44ef5f5e649e0acdf25eba86736" > "../conf/rbenv.src"
|
2021-03-06 01:10:38 +01:00
|
|
|
# Download and extract rbenv
|
|
|
|
ynh_setup_source --dest_dir="$rbenv_install_dir" --source_id=rbenv
|
2019-03-19 23:33:34 +01:00
|
|
|
|
2021-03-06 01:10:38 +01:00
|
|
|
# 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
|
2021-01-07 20:33:33 +01:00
|
|
|
SOURCE_SUM=54cae123c2758e7714c66aca7ef8bc7f29cda8583891191ceb3053c6d098ecf1" > "../conf/ruby-build.src"
|
2021-03-06 01:10:38 +01:00
|
|
|
# Download and extract ruby-build
|
|
|
|
ynh_setup_source --dest_dir="$rbenv_install_dir/plugins/ruby-build" --source_id=ruby-build
|
2021-02-24 06:35:45 +01:00
|
|
|
|
2021-03-06 01:10:38 +01:00
|
|
|
# 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
|
2021-02-24 06:35:45 +01:00
|
|
|
SOURCE_SUM=12e89bc4499e85d8babac2b02bc8b66ceb0aa3f8047b26728a3eca8a6030273d" > "../conf/rbenv-aliases.src"
|
2021-03-06 01:10:38 +01:00
|
|
|
# Download and extract rbenv-aliases
|
|
|
|
ynh_setup_source --dest_dir="$rbenv_install_dir/plugins/rbenv-aliases" --source_id=rbenv-aliases
|
2019-03-19 23:33:34 +01:00
|
|
|
|
2021-03-06 01:10:38 +01:00
|
|
|
(cd $rbenv_install_dir
|
|
|
|
./src/configure && make -C src)
|
2019-03-19 23:33:34 +01:00
|
|
|
|
2021-03-06 01:10:38 +01:00
|
|
|
# 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 () {
|
2021-03-06 01:37:12 +01:00
|
|
|
ruby_version=$(ynh_app_setting_get --app=$app --key=ruby_version)
|
2021-03-08 21:38:00 +01:00
|
|
|
|
2021-03-06 01:10:38 +01:00
|
|
|
# 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
|
2021-03-06 01:52:48 +01:00
|
|
|
ynh_ruby_load_path="PATH=$PATH"
|
2021-03-06 01:10:38 +01:00
|
|
|
|
|
|
|
# Sets the local application-specific Ruby version
|
|
|
|
(cd $final_path
|
|
|
|
rbenv local $ruby_version)
|
2019-03-19 23:33:34 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 06:35:45 +01:00
|
|
|
# Install a specific version of Ruby
|
2019-03-19 23:33:34 +01:00
|
|
|
#
|
2021-02-24 06:35:45 +01:00
|
|
|
# ynh_install_ruby will install the version of Ruby provided as argument by using rbenv.
|
2019-03-19 23:33:34 +01:00
|
|
|
#
|
2021-02-24 06:35:45 +01:00
|
|
|
# rbenv (Ruby Version Management) stores the target Ruby version in a .ruby_version file created in the target folder (using rbenv local <version>)
|
|
|
|
# It then uses that information for every Ruby user that uses rbenv provided Ruby command
|
2019-03-19 23:33:34 +01:00
|
|
|
#
|
|
|
|
# This helper creates a /etc/profile.d/rbenv.sh that configures PATH environment for rbenv
|
|
|
|
# for every LOGIN user, hence your user must have a defined shell (as opposed to /usr/sbin/nologin)
|
|
|
|
#
|
|
|
|
# Don't forget to execute ruby-dependent command in a login environment
|
|
|
|
# (e.g. sudo --login option)
|
|
|
|
# When not possible (e.g. in systemd service definition), please use direct path
|
|
|
|
# to rbenv shims (e.g. $RBENV_ROOT/shims/bundle)
|
|
|
|
#
|
2021-02-24 06:35:45 +01:00
|
|
|
# usage: ynh_install_ruby --ruby_version=ruby_version
|
2019-03-19 23:33:34 +01:00
|
|
|
# | arg: -v, --ruby_version= - Version of ruby to install.
|
2021-03-06 01:10:38 +01:00
|
|
|
#
|
|
|
|
# Requires YunoHost version 2.7.12 or higher.
|
2019-03-19 23:33:34 +01:00
|
|
|
ynh_install_ruby () {
|
2021-03-06 01:10:38 +01:00
|
|
|
# Declare an array to define the options of this helper.
|
|
|
|
local legacy_args=v
|
|
|
|
local -A args_array=( [v]=ruby_version= )
|
|
|
|
local ruby_version
|
|
|
|
# Manage arguments with getopts
|
|
|
|
ynh_handle_getopts_args "$@"
|
|
|
|
|
|
|
|
# Store ruby_version into the config of this app
|
|
|
|
ynh_app_setting_set --app=$YNH_APP_INSTANCE_NAME --key=ruby_version --value=$ruby_version
|
|
|
|
|
|
|
|
# Create $rbenv_install_dir if doesn't exist already
|
|
|
|
mkdir -p "$rbenv_install_dir/plugins/ruby-build"
|
|
|
|
|
|
|
|
# Load rbenv path in PATH
|
|
|
|
local CLEAR_PATH="$rbenv_install_dir/bin:$PATH"
|
|
|
|
|
|
|
|
# Remove /usr/local/bin in PATH in case of Ruby prior installation
|
|
|
|
PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@')
|
|
|
|
|
|
|
|
# Move an existing Ruby binary, to avoid to block rbenv
|
|
|
|
test -x /usr/bin/ruby && mv /usr/bin/ruby /usr/bin/ruby_rbenv
|
|
|
|
|
|
|
|
# If rbenv is not previously setup, install it
|
|
|
|
if ! type rbenv > /dev/null 2>&1
|
2021-02-24 06:35:45 +01:00
|
|
|
then
|
2021-03-06 01:10:38 +01:00
|
|
|
ynh_install_rbenv
|
|
|
|
elif dpkg --compare-versions "$($rbenv_install_dir/bin/rbenv --version | cut -d" " -f2)" lt $rbenv_version
|
|
|
|
then
|
|
|
|
ynh_install_rbenv
|
|
|
|
elif dpkg --compare-versions "$($rbenv_install_dir/plugins/ruby-build/bin/ruby-build --version | cut -d" " -f2)" lt $ruby_build_version
|
|
|
|
then
|
|
|
|
ynh_install_rbenv
|
2021-02-24 06:35:45 +01:00
|
|
|
fi
|
2019-03-19 23:33:34 +01:00
|
|
|
|
2021-03-06 01:10:38 +01:00
|
|
|
# Restore /usr/local/bin in PATH
|
|
|
|
PATH=$CLEAR_PATH
|
2021-02-24 06:35:45 +01:00
|
|
|
|
2021-03-06 01:10:38 +01:00
|
|
|
# And replace the old Ruby binary
|
|
|
|
test -x /usr/bin/ruby_rbenv && mv /usr/bin/ruby_rbenv /usr/bin/ruby
|
|
|
|
|
|
|
|
# Install the requested version of Ruby
|
|
|
|
ynh_print_info --message="Installation of Ruby-"$ruby_version
|
|
|
|
CONFIGURE_OPTS="--disable-install-doc --with-jemalloc" MAKE_OPTS="-j2" rbenv install --skip-existing $ruby_version
|
|
|
|
|
|
|
|
# Remove app virtualenv
|
|
|
|
if `rbenv alias --list | grep --quiet "$YNH_APP_INSTANCE_NAME" 1>/dev/null 2>&1`
|
2021-02-24 06:35:45 +01:00
|
|
|
then
|
2021-03-06 01:10:38 +01:00
|
|
|
rbenv alias $YNH_APP_INSTANCE_NAME --remove
|
2021-02-24 06:35:45 +01:00
|
|
|
fi
|
|
|
|
|
2021-03-06 01:10:38 +01:00
|
|
|
# Create app virtualenv
|
|
|
|
rbenv alias $YNH_APP_INSTANCE_NAME $ruby_version
|
|
|
|
|
|
|
|
# Cleanup Ruby versions
|
|
|
|
ynh_cleanup_ruby
|
|
|
|
|
|
|
|
# Set environment for Ruby users
|
|
|
|
echo "#rbenv
|
2019-03-19 23:33:34 +01:00
|
|
|
export RBENV_ROOT=$rbenv_install_dir
|
|
|
|
export PATH=\"$rbenv_install_dir/bin:$PATH\"
|
|
|
|
eval \"\$(rbenv init -)\"
|
2021-03-06 01:10:38 +01:00
|
|
|
#rbenv" > /etc/profile.d/rbenv.sh
|
2019-03-19 23:33:34 +01:00
|
|
|
|
2021-03-06 01:10:38 +01:00
|
|
|
# Load the environment
|
|
|
|
eval "$(rbenv init -)"
|
2019-03-19 23:33:34 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 06:35:45 +01:00
|
|
|
# Remove the version of Ruby used by the app.
|
2019-03-19 23:33:34 +01:00
|
|
|
#
|
2021-03-06 01:10:38 +01:00
|
|
|
# This helper will also cleanup Ruby versions
|
2019-03-19 23:33:34 +01:00
|
|
|
#
|
|
|
|
# usage: ynh_remove_ruby
|
|
|
|
ynh_remove_ruby () {
|
2021-03-06 01:10:38 +01:00
|
|
|
local ruby_version=$(ynh_app_setting_get --app=$YNH_APP_INSTANCE_NAME --key=ruby_version)
|
|
|
|
|
|
|
|
# Load rbenv path in PATH
|
|
|
|
local CLEAR_PATH="$rbenv_install_dir/bin:$PATH"
|
|
|
|
|
|
|
|
# Remove /usr/local/bin in PATH in case of Ruby prior installation
|
|
|
|
PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@')
|
|
|
|
|
|
|
|
rbenv alias $YNH_APP_INSTANCE_NAME --remove
|
|
|
|
|
|
|
|
# Remove the line for this app
|
|
|
|
ynh_app_setting_delete --app=$YNH_APP_INSTANCE_NAME --key=ruby_version
|
|
|
|
|
|
|
|
# Cleanup Ruby versions
|
|
|
|
ynh_cleanup_ruby
|
2019-03-19 23:33:34 +01:00
|
|
|
}
|
2021-03-06 01:10:38 +01:00
|
|
|
|
|
|
|
# Remove no more needed versions of Ruby used by the app.
|
|
|
|
#
|
|
|
|
# This helper will check what Ruby version are no more required,
|
|
|
|
# and uninstall them
|
|
|
|
# If no app uses Ruby, rbenv will be also removed.
|
|
|
|
#
|
|
|
|
# usage: ynh_cleanup_ruby
|
|
|
|
ynh_cleanup_ruby () {
|
|
|
|
|
|
|
|
# List required Ruby version
|
|
|
|
local installed_apps=$(yunohost app list | grep -oP 'id: \K.*$')
|
|
|
|
local required_ruby_versions=""
|
|
|
|
for installed_app in $installed_apps
|
|
|
|
do
|
|
|
|
local installed_app_ruby_version=$(yunohost app setting $installed_app ruby_version)
|
|
|
|
if [[ $installed_app_ruby_version ]]
|
|
|
|
then
|
|
|
|
required_ruby_versions="${installed_app_ruby_version}\n${required_ruby_versions}"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# Remove no more needed Ruby version
|
|
|
|
local installed_ruby_versions=$(rbenv versions --bare --skip-aliases | grep -Ev '/')
|
|
|
|
for installed_ruby_version in $installed_ruby_versions
|
|
|
|
do
|
|
|
|
if ! `echo ${required_ruby_versions} | grep "${installed_ruby_version}" 1>/dev/null 2>&1`
|
|
|
|
then
|
|
|
|
ynh_print_info --message="Removing of Ruby-"$installed_ruby_version
|
|
|
|
$rbenv_install_dir/bin/rbenv uninstall --force $installed_ruby_version
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# If none Ruby version is required
|
|
|
|
if [[ ! $required_ruby_versions ]]
|
|
|
|
then
|
|
|
|
# Remove rbenv environment configuration
|
|
|
|
ynh_print_info --message="Removing of rbenv-"$rbenv_version
|
|
|
|
ynh_secure_remove --file="$rbenv_install_dir"
|
|
|
|
rm /etc/profile.d/rbenv.sh
|
|
|
|
fi
|
2021-03-07 18:44:44 +01:00
|
|
|
}
|