diff --git a/scripts/_common.sh b/scripts/_common.sh index ecc6bb3..3c3e2d2 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -163,46 +163,126 @@ ynh_maintenance_mode_OFF () { # 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 +# Install Ruby Version Management # # [internal] # # usage: ynh_install_rbenv +# +# Requires YunoHost version 2.7.12 or higher. ynh_install_rbenv () { - echo "Installation of rbenv - ruby version management" >&2 - # Build an app.src for rbenv - mkdir -p "../conf" - echo "SOURCE_URL=https://github.com/rbenv/rbenv/archive/v1.1.2.tar.gz + 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 "$rbenv_install_dir" rbenv + # 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/v20200520.tar.gz -SOURCE_SUM=52be6908a94fbd4a94f5064e8b19d4a3baa4b773269c3884165518d83bcc8922" > "../conf/ruby-build.src" - # Download and extract ruby-build - ynh_setup_source "$rbenv_install_dir/plugins/ruby-build" ruby-build + # 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 - (cd $rbenv_install_dir - ./src/configure && make -C src) + # 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 -# Create shims directory if needed -if [ ! -d $rbenv_install_dir/shims ] ; then - mkdir $rbenv_install_dir/shims -fi + (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 } -# Install a specific version of ruby +# Load the version of Ruby for an app, and set variables. # -# ynh_install_ruby will install the version of ruby provided as argument by using rbenv. +# 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. # -# rbenv (ruby version management) stores the target ruby version in a .ruby_version file created in the target folder (using rbenv local ) -# It then uses that information for every ruby user that uses rbenv provided ruby command +# 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 + (cd $final_path + rbenv local $ruby_version) +} + +# 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 ) +# It then uses that information for every Ruby user that uses rbenv provided Ruby command # # 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) @@ -212,142 +292,141 @@ fi # When not possible (e.g. in systemd service definition), please use direct path # to rbenv shims (e.g. $RBENV_ROOT/shims/bundle) # -# usage: ynh_install_ruby ruby_version user +# usage: ynh_install_ruby --ruby_version=ruby_version # | arg: -v, --ruby_version= - Version of ruby to install. -# If possible, prefer to use major version number (e.g. 8 instead of 8.10.0). -# The crontab will handle the update of minor versions when needed. +# +# Requires YunoHost version 2.7.12 or higher. ynh_install_ruby () { - # Declare an array to define the options of this helper. - declare -Ar args_array=( [v]=ruby_version= ) - # Use rbenv, https://github.com/rbenv/rbenv to manage the ruby versions - local ruby_version - # Manage arguments with getopts - ynh_handle_getopts_args "$@" + # 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 "$@" - # Create $rbenv_install_dir - mkdir -p "$rbenv_install_dir/plugins/ruby-build" + # Store ruby_version into the config of this app + ynh_app_setting_set --app=$YNH_APP_INSTANCE_NAME --key=ruby_version --value=$ruby_version - # Load rbenv path in PATH - CLEAR_PATH="$rbenv_install_dir/bin:$PATH" + # Create $rbenv_install_dir if doesn't exist already + mkdir -p "$rbenv_install_dir/plugins/ruby-build" - # Remove /usr/local/bin in PATH in case of ruby prior installation - PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') + # Load rbenv path in PATH + local CLEAR_PATH="$rbenv_install_dir/bin:$PATH" - # Move an existing ruby binary, to avoid to block rbenv - test -x /usr/bin/ruby && mv /usr/bin/ruby /usr/bin/ruby_rbenv + # Remove /usr/local/bin in PATH in case of Ruby prior installation + PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') - # If rbenv is not previously setup, install it - if ! type rbenv > /dev/null 2>&1 - then - ynh_install_rbenv - elif dpkg --compare-versions "$($rbenv_install_dir/bin/rbenv --version | cut -d" " -f2)" lt "1.1.2" - then - ynh_install_rbenv - elif dpkg --compare-versions "$($rbenv_install_dir/plugins/ruby-build/bin/ruby-build --version | cut -d" " -f2)" lt "20200520" - then - ynh_install_rbenv - fi + # Move an existing Ruby binary, to avoid to block rbenv + test -x /usr/bin/ruby && mv /usr/bin/ruby /usr/bin/ruby_rbenv - # Restore /usr/local/bin in PATH (if needed) - PATH=$CLEAR_PATH + # If rbenv is not previously setup, install it + if ! type rbenv > /dev/null 2>&1 + then + 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 + fi - # And replace the old ruby binary - test -x /usr/bin/ruby_rbenv && mv /usr/bin/ruby_rbenv /usr/bin/ruby + # Restore /usr/local/bin in PATH + PATH=$CLEAR_PATH - # Install the requested version of ruby - CONFIGURE_OPTS="--disable-install-doc --with-jemalloc" MAKE_OPTS="-j2" rbenv install --skip-existing $ruby_version + # And replace the old Ruby binary + test -x /usr/bin/ruby_rbenv && mv /usr/bin/ruby_rbenv /usr/bin/ruby - # Store the ID of this app and the version of ruby requested for it - echo "$YNH_APP_ID:$ruby_version" | tee --append "$rbenv_install_dir/ynh_app_version" + # 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 - # Store ruby_version into the config of this app - ynh_app_setting_set $app ruby_version $ruby_version + # Remove app virtualenv + if `rbenv alias --list | grep --quiet "$YNH_APP_INSTANCE_NAME" 1>/dev/null 2>&1` + then + rbenv alias $YNH_APP_INSTANCE_NAME --remove + fi - # Set environment for ruby users - echo "#rbenv + # Create app virtualenv + rbenv alias $YNH_APP_INSTANCE_NAME $ruby_version + + # Cleanup Ruby versions + ynh_cleanup_ruby + + # Set environment for Ruby users + echo "#rbenv export RBENV_ROOT=$rbenv_install_dir export PATH=\"$rbenv_install_dir/bin:$PATH\" eval \"\$(rbenv init -)\" -#rbenv" > /etc/profile.d/rbenv.sh + #rbenv" > /etc/profile.d/rbenv.sh - # Load the right environment for the Installation - eval "$(rbenv init -)" - - (cd $final_path - rbenv local $ruby_version) + # Load the environment + eval "$(rbenv init -)" } -# Remove the version of ruby used by the app. +# Remove the version of Ruby used by the app. # -# This helper will check if another app uses the same version of ruby, -# if not, this version of ruby will be removed. -# If no other app uses ruby, rbenv will be also removed. +# This helper will also cleanup Ruby versions # # usage: ynh_remove_ruby ynh_remove_ruby () { - ruby_version=$(ynh_app_setting_get $app ruby_version) + local ruby_version=$(ynh_app_setting_get --app=$YNH_APP_INSTANCE_NAME --key=ruby_version) - # Remove the line for this app - sed --in-place "/$YNH_APP_ID:$ruby_version/d" "$rbenv_install_dir/ynh_app_version" + # Load rbenv path in PATH + local CLEAR_PATH="$rbenv_install_dir/bin:$PATH" - # If no other app uses this version of ruby, remove it. - if ! grep --quiet "$ruby_version" "$rbenv_install_dir/ynh_app_version" - then - $rbenv_install_dir/bin/rbenv uninstall --force $ruby_version - fi + # Remove /usr/local/bin in PATH in case of Ruby prior installation + PATH=$(echo $CLEAR_PATH | sed 's@/usr/local/bin:@@') - # Remove rbenv environment configuration - rm /etc/profile.d/rbenv.sh + rbenv alias $YNH_APP_INSTANCE_NAME --remove - # If no other app uses rbenv, remove rbenv and dedicated group - if [ ! -s "$rbenv_install_dir/ynh_app_version" ] - then - ynh_secure_remove "$rbenv_install_dir" - fi + # Remove the line for this app + ynh_app_setting_delete --app=$YNH_APP_INSTANCE_NAME --key=ruby_version + + # Cleanup Ruby versions + ynh_cleanup_ruby } -# Remove the version of ruby used by the app. +# Remove no more needed versions of Ruby used by the app. # -# This helper will check if another app uses the same version of ruby, -# if not, this version of ruby will be removed. -# If no other app uses ruby, rbenv will be also removed. +# 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_remove_ruby -ynh_remove_ruby () { - ruby_version=$(ynh_app_setting_get $app ruby_version) +# usage: ynh_cleanup_ruby +ynh_cleanup_ruby () { - # Remove the line for this app - sed --in-place "/$YNH_APP_ID:$ruby_version/d" "$rbenv_install_dir/ynh_app_version" + # 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 no other app uses this version of ruby, remove it. - if ! grep --quiet "$ruby_version" "$rbenv_install_dir/ynh_app_version" - then - $rbenv_install_dir/bin/rbenv uninstall --force $ruby_version - fi - - # Remove rbenv environment configuration - rm /etc/profile.d/rbenv.sh - - # If no other app uses rbenv, remove rbenv and dedicated group - if [ ! -s "$rbenv_install_dir/ynh_app_version" ] - then - ynh_secure_remove "$rbenv_install_dir" - fi -} - -# Returns true if upstream version is up to date -# -# This helper should be used to avoid an upgrade of the upstream version -# when it's not needed (but yet allowing to upgrade other parts of the -# YunoHost application (e.g. nginx conf) -# -# usage: ynh_is_upstream_up_to_date (returns a boolean) -ynh_is_upstream_up_to_date () { - local version=$(ynh_read_manifest "/etc/yunohost/apps/$YNH_APP_INSTANCE_NAME/manifest.json" "version" || echo 1.0) - version="${version/~ynh*/}" - local last_version=$(ynh_read_manifest "../manifest.json" "version" || echo 1.0) - last_version="${last_version/~ynh*/}" - [ "$version" = "$last_version" ] + # 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 } #================================================= diff --git a/scripts/change_url b/scripts/change_url index ce73aaf..60a3cd5 100644 --- a/scripts/change_url +++ b/scripts/change_url @@ -128,8 +128,9 @@ ynh_store_file_checksum --file="$discourse_config_file" ynh_psql_connect_as --user=$db_user --password=$db_pwd --database=$db_name <<< "UPDATE site_settings SET value = replace(value, '${old_path%/}/images/', '${new_path%/}/images/'); UPDATE site_settings SET value = '${new_path}' WHERE name='long_polling_base_url';" +ynh_use_ruby # Remap URLs in forum posts -exec_login_as $app RAILS_ENV=production bundle exec script/discourse remap ${old_path%/}/uploads ${new_path%/}/uploads <<< "YES +exec_login_as $app RAILS_ENV=production bin/bundle exec script/discourse remap ${old_path%/}/uploads ${new_path%/}/uploads <<< "YES # " rake_exec="exec_login_as $app RAILS_ENV=production bin/rake" diff --git a/scripts/install b/scripts/install index 7d6154f..318818e 100644 --- a/scripts/install +++ b/scripts/install @@ -64,6 +64,7 @@ ynh_app_setting_set --app=$app --key=admin --value=$admin ynh_script_progression --message="Installing dependencies..." ynh_install_app_dependencies $pkg_dependencies +ynh_install_ruby --ruby_version=$RUBY_VERSION #================================================= # CREATE A POSTGRESQL DATABASE @@ -103,13 +104,6 @@ ynh_setup_source --dest_dir="$final_path" mkdir -p "$final_path/plugins/discourse-ldap-auth" ynh_setup_source --dest_dir="$final_path/plugins/discourse-ldap-auth" --source_id=ldap-auth -#================================================= -# INSTALL RUBY -#================================================= -ynh_script_progression --message="Installing Ruby..." - -ynh_install_ruby --ruby_version=$RUBY_VERSION - #================================================= # NGINX CONFIGURATION #================================================= @@ -197,8 +191,9 @@ ynh_add_config --template="../conf/secrets.yml" --destination="$final_path/confi chown -R $app: $final_path pushd "$final_path" + ynh_use_ruby # Install bundler, a gems installer - gem install bundler + ynh_gem install bundler # Install without documentation exec_as $app echo "gem: --no-ri --no-rdoc" >> "$final_path/.gemrc" popd @@ -207,13 +202,13 @@ popd if [ -n "$(uname -m | grep arm)" ] ; then # Define the platform specifically to retrieve binaries # for libv8 because it currently doesn't compile on ARM devices - exec_login_as $app bundle config specific_platform arm-linux + exec_login_as $app bin/bundle config specific_platform arm-linux fi # Install dependencies -exec_login_as $app bundle config set path 'vendor/bundle' -exec_login_as $app bundle config set with 'development' -exec_login_as $app MAKEFLAGS=-j2 bundle install --jobs 2 +exec_login_as $app bin/bundle config set path 'vendor/bundle' +exec_login_as $app bin/bundle config set with 'development' +exec_login_as $app MAKEFLAGS=-j2 bin/bundle install --jobs 2 # On ARM architecture, replace bundled libpsl by system native libpsl # because the provided binary isn't compatible diff --git a/scripts/restore b/scripts/restore index 900c267..90af673 100644 --- a/scripts/restore +++ b/scripts/restore @@ -90,12 +90,6 @@ ynh_script_progression --message="Reinstalling dependencies..." # Define and install dependencies ynh_install_app_dependencies $pkg_dependencies - -#================================================= -# INSTALL RUBY -#================================================= -ynh_script_progression --message="Install Ruby..." - ynh_install_ruby --ruby_version=$RUBY_VERSION #================================================= @@ -104,7 +98,8 @@ ynh_install_ruby --ruby_version=$RUBY_VERSION ynh_script_progression --message="Reinstall Bundle Gem..." (cd "$final_path" -gem install bundler) +ynh_use_ruby +ynh_gem install bundler) #================================================= # RESTORE THE POSTGRESQL DATABASE diff --git a/scripts/upgrade b/scripts/upgrade index fbe60ae..e1cd224 100644 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -177,12 +177,6 @@ ynh_store_file_checksum --file="/etc/nginx/conf.d/$domain.d/$app.conf" ynh_script_progression --message="Upgrading dependencies..." ynh_install_app_dependencies $pkg_dependencies - -#================================================= -# INSTALL RUBY -#================================================= -ynh_script_progression --message="Installing Ruby..." - ynh_install_ruby --ruby_version=$RUBY_VERSION #================================================= @@ -273,8 +267,9 @@ then ( cd "$final_path" + ynh_use_ruby # Install bundler, a gems installer - gem install bundler + ynh_gem install bundler # Install without documentation exec_as $app echo "gem: --no-ri --no-rdoc" >> "$final_path/.gemrc" ) @@ -282,12 +277,12 @@ then if [ -n "$(uname -m | grep arm)" ] ; then # Define the platform specifically to retrieve binaries # for libv8 because it currently doesn't compile on ARM devices - exec_login_as $app bundle config specific_platform arm-linux + exec_login_as $app bin/bundle config specific_platform arm-linux fi # Install dependencies - exec_login_as $app bundle config set path 'vendor/bundle' - exec_login_as $app bundle config set with 'development' - exec_login_as $app MAKEFLAGS=-j2 bundle install --jobs 2 + exec_login_as $app bin/bundle config set path 'vendor/bundle' + exec_login_as $app bin/bundle config set with 'development' + exec_login_as $app MAKEFLAGS=-j2 bin/bundle install --jobs 2 # On ARM architecture, replace bundled libpsl by system native libpsl # because the provided binary isn't compatible