2017-04-10 02:55:10 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
2018-04-06 17:58:23 +00:00
|
|
|
source _common.sh
|
2019-08-11 13:58:52 +00:00
|
|
|
source ynh_add_swap
|
2019-04-06 14:08:48 +00:00
|
|
|
source /usr/share/yunohost/helpers
|
2017-04-10 02:55:10 +00:00
|
|
|
|
2024-02-24 22:28:27 +00:00
|
|
|
config="$install_dir/live/.env.production"
|
2022-01-07 00:25:26 +00:00
|
|
|
|
|
|
|
|
#=================================================
|
2024-05-27 12:19:32 +00:00
|
|
|
# STOP SYSTEMD SERVICE
|
|
|
|
|
#=================================================
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_script_progression "Stopping $app's systemd service..."
|
2024-05-27 12:19:32 +00:00
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
if ynh_app_upgrading_from_version_before 4.2.8~ynh2; then
|
2024-05-27 12:19:32 +00:00
|
|
|
# Workaround for pre-packagingv2 versions
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_systemctl --service=${app}-web --action="stop" --log_path="systemd" --wait_until="Goodbye"
|
|
|
|
|
ynh_systemctl --service=${app}-sidekiq --action="stop" --log_path="systemd" --wait_until="Bye"
|
|
|
|
|
ynh_systemctl --service=${app}-streaming --action="stop" --log_path="systemd"
|
2024-05-27 12:19:32 +00:00
|
|
|
else
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_systemctl --service=${app}-web --action="stop" --log_path=/var/log/$app/$app-web.log --wait_until="Goodbye"
|
|
|
|
|
ynh_systemctl --service=${app}-sidekiq --action="stop" --log_path=/var/log/$app/$app-sidekiq.log --wait_until="Bye"
|
|
|
|
|
ynh_systemctl --service=${app}-streaming --action="stop" --log_path=/var/log/$app/$app-streaming.log
|
2024-05-27 12:19:32 +00:00
|
|
|
fi
|
|
|
|
|
|
2018-05-01 18:45:29 +00:00
|
|
|
#=================================================
|
|
|
|
|
# ENSURE DOWNWARD COMPATIBILITY
|
|
|
|
|
#=================================================
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_script_progression "Ensuring downward compatibility..."
|
2017-04-10 02:55:10 +00:00
|
|
|
|
2024-02-24 22:28:27 +00:00
|
|
|
# Set `service` settings to support `yunohost app shell` command
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_app_setting_set_default --key=service --value="$app-web.service"
|
2021-05-16 16:55:19 +00:00
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
if ynh_app_upgrading_from_version_before 4.2.8~ynh2; then
|
2024-05-27 11:17:03 +00:00
|
|
|
# Rename legacy database
|
|
|
|
|
if ynh_psql_database_exists --database="${app}_production"; then
|
|
|
|
|
db_name=$(ynh_sanitize_dbid --db_name="$app")
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_app_setting_set --key="db_name" --value="$db_name"
|
2024-05-27 11:17:03 +00:00
|
|
|
|
|
|
|
|
# Remove the newly created db by resources, and rename legacy db
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_psql_db_shell <<< "DROP DATABASE IF EXISTS $db_name;"
|
|
|
|
|
ynh_psql_db_shell <<< "ALTER DATABASE ${app}_production RENAME TO $db_name;"
|
2024-05-27 11:17:03 +00:00
|
|
|
fi
|
|
|
|
|
# Same with user
|
|
|
|
|
if ynh_psql_user_exists --user="${app}_production"; then
|
|
|
|
|
db_user="$db_name"
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_app_setting_set --key="db_user" --value="$db_user"
|
2024-05-27 11:17:03 +00:00
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_psql_db_shell <<< "DROP USER IF EXISTS $db_user;"
|
|
|
|
|
ynh_psql_db_shell <<< "ALTER USER ${app}_production RENAME $db_user;"
|
2024-05-27 11:17:03 +00:00
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
2019-03-18 03:22:38 +00:00
|
|
|
#=================================================
|
2024-02-24 22:28:27 +00:00
|
|
|
# ADD SWAP IF NEEDED
|
2019-03-18 03:22:38 +00:00
|
|
|
#=================================================
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_script_progression "Adding swap if needed..."
|
2019-03-18 03:22:38 +00:00
|
|
|
|
2024-02-24 22:28:27 +00:00
|
|
|
total_memory=$(ynh_get_ram --total)
|
|
|
|
|
swap_needed=0
|
2020-05-26 11:50:52 +00:00
|
|
|
|
2024-02-24 22:28:27 +00:00
|
|
|
if [ $total_memory -lt $memory_needed ]; then
|
|
|
|
|
# Need a minimum of 8Go of memory
|
|
|
|
|
swap_needed=$(($memory_needed - $total_memory))
|
2019-05-15 10:18:39 +00:00
|
|
|
fi
|
2018-06-20 09:54:30 +00:00
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_script_progression "Adding $swap_needed Mo to swap..."
|
2024-02-24 22:28:27 +00:00
|
|
|
ynh_add_swap --size=$swap_needed
|
2021-04-09 22:18:53 +00:00
|
|
|
|
2019-03-18 03:22:38 +00:00
|
|
|
#=================================================
|
|
|
|
|
# UPGRADE DEPENDENCIES
|
|
|
|
|
#=================================================
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_script_progression "Upgrading Ruby and NodeJS..."
|
2019-03-18 03:22:38 +00:00
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_ruby_install
|
|
|
|
|
ynh_nodejs_install
|
2022-09-19 23:57:06 +00:00
|
|
|
|
2019-03-18 03:22:38 +00:00
|
|
|
#=================================================
|
2024-02-24 22:28:27 +00:00
|
|
|
# "REBUILD" THE APP (DEPLOY NEW SOURCES, RERUN NPM BUILD...)
|
2019-08-11 13:58:52 +00:00
|
|
|
#=================================================
|
2024-02-24 22:28:27 +00:00
|
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
2019-08-11 13:58:52 +00:00
|
|
|
#=================================================
|
|
|
|
|
|
2025-08-21 11:50:05 +00:00
|
|
|
ynh_script_progression "Upgrading source files..."
|
2019-08-11 13:58:52 +00:00
|
|
|
|
2025-08-21 11:50:05 +00:00
|
|
|
# Download, check integrity, uncompress and patch the source from manifest.toml
|
|
|
|
|
ynh_setup_source --dest_dir="$install_dir/live" --full_replace --keep="public/system .env.production"
|
2025-08-24 20:11:54 +00:00
|
|
|
# Download redis migration script
|
|
|
|
|
ynh_setup_source --source_id=redis_migration --dest_dir="$install_dir/live"
|
2025-08-21 11:50:05 +00:00
|
|
|
|
|
|
|
|
chmod -R 775 "$install_dir"
|
|
|
|
|
chmod o-rwx "$install_dir"
|
|
|
|
|
chown -R $app:www-data "$install_dir"
|
2019-08-11 13:58:52 +00:00
|
|
|
|
2019-03-21 01:58:33 +00:00
|
|
|
#=================================================
|
2024-02-24 22:28:27 +00:00
|
|
|
# BUILD ASSETS
|
2019-03-21 01:58:33 +00:00
|
|
|
#=================================================
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_script_progression "Building assets..."
|
2019-03-21 01:58:33 +00:00
|
|
|
|
2024-02-24 22:28:27 +00:00
|
|
|
pushd "$install_dir/live"
|
2025-08-21 08:42:04 +00:00
|
|
|
gem update --system
|
|
|
|
|
gem install bundler --no-document
|
2025-08-21 12:08:33 +00:00
|
|
|
ynh_hide_warnings ynh_exec_as_app $ld_preload bin/bundle config deployment 'true'
|
|
|
|
|
ynh_hide_warnings ynh_exec_as_app $ld_preload bin/bundle config without 'development test'
|
|
|
|
|
ynh_hide_warnings ynh_exec_as_app $ld_preload bin/bundle config set force_ruby_platform true --quiet
|
|
|
|
|
ynh_hide_warnings ynh_exec_as_app $ld_preload bin/bundle install -j$(getconf _NPROCESSORS_ONLN)
|
2025-08-21 08:42:04 +00:00
|
|
|
|
2025-08-21 12:08:33 +00:00
|
|
|
env corepack enable
|
2025-08-21 08:42:04 +00:00
|
|
|
echo Y | ynh_hide_warnings ynh_exec_as_app yarn workspaces focus --production
|
|
|
|
|
ynh_hide_warnings ynh_exec_as_app yarn install --immutable
|
2020-05-18 23:20:45 +00:00
|
|
|
popd
|
2019-03-21 01:58:33 +00:00
|
|
|
|
2024-05-27 10:28:15 +00:00
|
|
|
chown -R "$app:www-data" "$install_dir/live/public"
|
|
|
|
|
|
2019-03-23 00:54:06 +00:00
|
|
|
#=================================================
|
2024-02-24 22:28:27 +00:00
|
|
|
# REAPPLY SYSTEM CONFIGURATIONS
|
2019-03-23 00:54:06 +00:00
|
|
|
#=================================================
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_script_progression "Upgrading system configurations related to $app..."
|
2021-04-10 18:28:06 +00:00
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_config_add_nginx
|
2021-04-10 18:28:06 +00:00
|
|
|
|
|
|
|
|
# Create a dedicated systemd config
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_config_add_systemd --service="$app-web" --template="mastodon-web.service"
|
2024-02-24 22:28:27 +00:00
|
|
|
yunohost service add "$app-web" --description="$app web service"
|
|
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_config_add_systemd --service="$app-sidekiq" --template="mastodon-sidekiq.service"
|
2024-02-24 22:28:27 +00:00
|
|
|
yunohost service add "$app-sidekiq" --description="$app sidekiq service"
|
|
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_config_add_systemd --service="$app-streaming" --template="mastodon-streaming.service"
|
2024-02-24 22:28:27 +00:00
|
|
|
yunohost service add "$app-streaming" --description="$app streaming service"
|
2021-04-10 18:28:06 +00:00
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_config_add --template="cron" --destination="/etc/cron.d/$app"
|
2019-03-21 01:07:49 +00:00
|
|
|
|
2024-02-24 22:28:27 +00:00
|
|
|
# Use logrotate to manage app-specific logfile(s)
|
|
|
|
|
mkdir -p /var/log/$app
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_config_add_logrotate
|
2018-12-26 05:55:29 +00:00
|
|
|
|
2018-06-12 16:32:43 +00:00
|
|
|
#=================================================
|
2024-02-24 22:28:27 +00:00
|
|
|
# RECONFIGURE THE APP (UPDATE CONF, APPLY MIGRATIONS...)
|
2018-06-12 16:32:43 +00:00
|
|
|
#=================================================
|
2024-02-24 22:28:27 +00:00
|
|
|
# UPDATE A CONFIG FILE
|
2018-06-12 16:32:43 +00:00
|
|
|
#=================================================
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_script_progression "Updating a config file..."
|
2017-04-20 14:35:44 +00:00
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
if ynh_app_upgrading_from_version_before 4.3.2~ynh1; then
|
2024-12-28 00:29:20 +00:00
|
|
|
pushd "$install_dir/live"
|
2025-08-21 08:42:04 +00:00
|
|
|
|
2024-12-28 00:29:20 +00:00
|
|
|
# Generate active record encryption
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_hide_warnings ynh_exec_as_app RAILS_ENV=production $ld_preload bin/bundle exec rails db:encryption:init > active_record_encryption.txt
|
2024-12-28 00:29:20 +00:00
|
|
|
active_record_encryption_deterministic_key=$(grep -oP "ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY=\K.+" "$install_dir/live/active_record_encryption.txt")
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_app_setting_set --key=active_record_encryption_deterministic_key --value="$active_record_encryption_deterministic_key"
|
2024-12-28 00:29:20 +00:00
|
|
|
active_record_encryption_key_derivation_salt=$(grep -oP "ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT=\K.+" "$install_dir/live/active_record_encryption.txt")
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_app_setting_set --key=active_record_encryption_key_derivation_salt --value="$active_record_encryption_key_derivation_salt"
|
2024-12-28 00:29:20 +00:00
|
|
|
active_record_encryption_primary_key=$(grep -oP "ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY=\K.+" "$install_dir/live/active_record_encryption.txt")
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_app_setting_set --key=active_record_encryption_primary_key --value="$active_record_encryption_primary_key"
|
|
|
|
|
ynh_safe_rm "$install_dir/live/active_record_encryption.txt"
|
2024-12-28 00:29:20 +00:00
|
|
|
popd
|
|
|
|
|
fi
|
|
|
|
|
|
2024-02-24 22:28:27 +00:00
|
|
|
language="$(echo $language | head -c 2)"
|
|
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_config_add --template=".env.production.sample" --destination="$config"
|
2019-03-18 03:22:38 +00:00
|
|
|
|
2019-05-10 14:34:57 +00:00
|
|
|
#=================================================
|
2024-02-24 22:28:27 +00:00
|
|
|
# APPLY MIGRATIONS
|
2019-05-10 14:34:57 +00:00
|
|
|
#=================================================
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_script_progression "Applying migrations..."
|
2019-05-10 14:34:57 +00:00
|
|
|
|
2024-02-24 22:28:27 +00:00
|
|
|
pushd "$install_dir/live"
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_hide_warnings ynh_exec_as_app RAILS_ENV=production $ld_preload bin/bundle exec rails db:migrate --quiet
|
|
|
|
|
ynh_hide_warnings ynh_exec_as_app RAILS_ENV=production $ld_preload bin/bundle exec rails assets:precompile --quiet
|
2025-08-23 20:44:57 +00:00
|
|
|
|
|
|
|
|
# Apply redis namespace migration (https://github.com/mastodon/redis_namespace_migration)
|
2025-08-24 20:11:54 +00:00
|
|
|
ynh_hide_warings ynh_exec_as_app RAILS_ENV=production COREPACK_ENABLE_DOWNLOAD_PROMPT=0 $ld_preload bin/rails runner rename.rb
|
2025-08-23 20:44:57 +00:00
|
|
|
redis_namespace="$(ynh_app_setting_get --app=$app --key=redis_namespace)"
|
|
|
|
|
if [ $redis_namespace ]; then
|
|
|
|
|
if [ ynh_app_setting_get --app=$app --key=es_enabled == "true" ] && [ ynh_app_setting_get --app=$app --key=es_prefix == "" ]; then
|
|
|
|
|
ynh_app_setting_set --app=$app --key=es_prefix --value=$redis_namespace
|
|
|
|
|
else
|
|
|
|
|
ynh_app_setting_delete --app=$app --key=redis_namespace
|
|
|
|
|
fi
|
|
|
|
|
fi
|
2024-02-24 22:28:27 +00:00
|
|
|
popd
|
2019-05-10 14:34:57 +00:00
|
|
|
|
2019-03-18 03:22:38 +00:00
|
|
|
#=================================================
|
2024-02-24 22:28:27 +00:00
|
|
|
# START SYSTEMD SERVICE
|
2019-03-18 03:22:38 +00:00
|
|
|
#=================================================
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_script_progression "Starting $app's systemd service..."
|
2019-03-18 03:22:38 +00:00
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_systemctl --service=${app}-web --action="restart" --log_path=/var/log/$app/$app-web.log --wait_until="Listening on"
|
|
|
|
|
ynh_systemctl --service=${app}-sidekiq --action="start" --log_path=/var/log/$app/$app-sidekiq.log --wait_until="Schedules Loaded"
|
|
|
|
|
ynh_systemctl --service=${app}-streaming --action="start" --log_path=/var/log/$app/$app-streaming.log --wait_until="Streaming API now listening"
|
2019-03-18 03:22:38 +00:00
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
|
# END OF SCRIPT
|
|
|
|
|
#=================================================
|
|
|
|
|
|
2025-08-21 08:42:04 +00:00
|
|
|
ynh_script_progression "Upgrade of $app completed"
|