2017-04-10 04:55:10 +02:00
#!/bin/bash
2018-05-01 20:45:29 +02:00
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2017-04-10 04:55:10 +02:00
2018-04-06 19:58:23 +02:00
source _common.sh
2019-08-11 15:58:52 +02:00
source ynh_add_swap
2019-04-06 16:08:48 +02:00
source /usr/share/yunohost/helpers
2017-04-10 04:55:10 +02:00
2019-05-15 12:18:39 +02:00
upgrade_type=$(ynh_check_app_version_changed)
2024-02-24 23:28:27 +01:00
config="$install_dir/live/.env.production"
2022-01-07 01:25:26 +01:00
#=================================================
2024-05-27 14:19:32 +02:00
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..." --weight=1
if ynh_compare_current_package_version --comparison lt --version "4.2.8~ynh2"; then
# Workaround for pre-packagingv2 versions
ynh_systemd_action --service_name=${app}-web --action="stop" --log_path="systemd" --line_match="Goodbye"
ynh_systemd_action --service_name=${app}-sidekiq --action="stop" --log_path="systemd" --line_match="Bye"
ynh_systemd_action --service_name=${app}-streaming --action="stop" --log_path="systemd"
else
ynh_systemd_action --service_name=${app}-web --action="stop" --log_path=/var/log/$app/$app-web.log --line_match="Goodbye"
ynh_systemd_action --service_name=${app}-sidekiq --action="stop" --log_path=/var/log/$app/$app-sidekiq.log --line_match="Bye"
ynh_systemd_action --service_name=${app}-streaming --action="stop" --log_path=/var/log/$app/$app-streaming.log
fi
2018-05-01 20:45:29 +02:00
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
2022-09-20 01:57:06 +02:00
ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
2017-04-10 04:55:10 +02:00
2024-02-24 23:28:27 +01:00
# Set `service` settings to support `yunohost app shell` command
if [[ -z "${service:-}" ]]; then
service="$app-web.service"
ynh_app_setting_set --app="$app" --key=service --value="$service"
2021-05-16 18:55:19 +02:00
fi
2024-05-27 13:17:03 +02:00
# Remove previous added repository
2019-05-14 21:04:05 +02:00
ynh_remove_extra_repo
2024-05-27 13:17:03 +02:00
if ynh_compare_current_package_version --comparison lt --version "4.2.8~ynh2"; then
# Rename legacy database
if ynh_psql_database_exists --database="${app}_production"; then
db_name=$(ynh_sanitize_dbid --db_name="$app")
ynh_app_setting_set --app="$app" --key="db_name" --value="$db_name"
# Remove the newly created db by resources, and rename legacy db
ynh_psql_execute_as_root --sql="DROP DATABASE IF EXISTS ${app};"
ynh_psql_execute_as_root --sql="ALTER DATABASE ${app}_production RENAME TO $db_name;"
fi
# Same with user
if ynh_psql_user_exists --user="${app}_production"; then
db_user="$db_name"
ynh_app_setting_set --app="$app" --key="db_user" --value="$db_user"
ynh_psql_execute_as_root --sql="DROP USER IF EXISTS ${app};"
ynh_psql_execute_as_root --sql="ALTER USER ${app}_production RENAME $db_name;"
fi
fi
2019-03-18 04:22:38 +01:00
#=================================================
2024-02-24 23:28:27 +01:00
# ADD SWAP IF NEEDED
2019-03-18 04:22:38 +01:00
#=================================================
2024-02-24 23:28:27 +01:00
ynh_script_progression --message="Adding swap if needed..." --weight=1
2019-03-18 04:22:38 +01:00
2024-02-24 23:28:27 +01:00
total_memory=$(ynh_get_ram --total)
swap_needed=0
2020-05-26 13:50:52 +02:00
2024-02-24 23:28:27 +01:00
if [ $total_memory -lt $memory_needed ]; then
# Need a minimum of 8Go of memory
swap_needed=$(($memory_needed - $total_memory))
2019-05-15 12:18:39 +02:00
fi
2018-06-20 11:54:30 +02:00
2024-02-24 23:28:27 +01:00
ynh_script_progression --message="Adding $swap_needed Mo to swap..."
ynh_add_swap --size=$swap_needed
2021-04-10 00:18:53 +02:00
2019-03-18 04:22:38 +01:00
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
2024-02-24 23:28:27 +01:00
ynh_script_progression --message="Upgrading Ruby and NodeJS..." --weight=1
2019-03-18 04:22:38 +01:00
2022-05-29 15:54:35 +02:00
ynh_exec_warn_less ynh_install_ruby --ruby_version=$ruby_version
ynh_exec_warn_less ynh_install_nodejs --nodejs_version=$nodejs_version
2022-09-20 01:57:06 +02:00
2019-03-18 04:22:38 +01:00
#=================================================
2024-02-24 23:28:27 +01:00
# "REBUILD" THE APP (DEPLOY NEW SOURCES, RERUN NPM BUILD...)
2019-08-11 15:58:52 +02:00
#=================================================
2024-02-24 23:28:27 +01:00
# DOWNLOAD, CHECK AND UNPACK SOURCE
2019-08-11 15:58:52 +02:00
#=================================================
2024-02-24 23:28:27 +01:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Upgrading source files..." --weight=1
2019-08-11 15:58:52 +02:00
2024-02-24 23:28:27 +01:00
# Download Mastodon
ynh_setup_source --dest_dir="$install_dir/live" --keep="public/system/"
2019-08-11 15:58:52 +02:00
2024-02-24 23:28:27 +01:00
chmod 750 "$install_dir"
chmod -R o-rwx "$install_dir"
chown -R $app:www-data "$install_dir"
fi
2019-08-11 15:58:52 +02:00
2019-03-21 02:58:33 +01:00
#=================================================
2024-02-24 23:28:27 +01:00
# BUILD ASSETS
2019-03-21 02:58:33 +01:00
#=================================================
2024-02-24 23:28:27 +01:00
ynh_script_progression --message="Building assets..." --weight=1
2019-03-21 02:58:33 +01:00
2024-02-24 23:28:27 +01:00
pushd "$install_dir/live"
2021-03-06 01:10:38 +01:00
ynh_use_ruby
2021-03-08 00:57:36 +01:00
ynh_gem update --system
2021-03-07 20:17:05 +01:00
ynh_gem install bundler --no-document
2022-05-29 15:54:35 +02:00
ynh_exec_as $app $ynh_ruby_load_path $ld_preload bin/bundle config deployment 'true'
ynh_exec_as $app $ynh_ruby_load_path $ld_preload bin/bundle config without 'development test'
ynh_exec_as $app $ynh_ruby_load_path $ld_preload bin/bundle config set force_ruby_platform true --quiet
ynh_exec_as $app $ynh_ruby_load_path $ld_preload bin/bundle install -j$(getconf _NPROCESSORS_ONLN)
ynh_use_nodejs
2024-02-24 23:28:27 +01:00
ynh_exec_as $app $ynh_node_load_PATH yarn install --pure-lockfile --production --network-timeout 600000
2022-05-29 15:54:35 +02:00
ynh_exec_warn_less ynh_exec_as $app RAILS_ENV=production $ynh_ruby_load_path $ld_preload bin/bundle exec rails assets:clean
ynh_exec_warn_less ynh_exec_as $app RAILS_ENV=production $ynh_ruby_load_path $ld_preload bin/bundle exec rails assets:precompile
2020-05-19 01:20:45 +02:00
popd
2019-03-21 02:58:33 +01:00
2024-05-27 12:28:15 +02:00
chown "$app:www-data" "$install_dir"
chown -R "$app:www-data" "$install_dir/live/public"
2019-03-23 01:54:06 +01:00
#=================================================
2024-02-24 23:28:27 +01:00
# REAPPLY SYSTEM CONFIGURATIONS
2019-03-23 01:54:06 +01:00
#=================================================
2024-02-24 23:28:27 +01:00
ynh_script_progression --message="Upgrading system configurations related to $app..." --weight=1
2021-04-10 20:28:06 +02:00
2024-02-24 23:28:27 +01:00
ynh_add_nginx_config
2021-04-10 20:28:06 +02:00
# Create a dedicated systemd config
2021-05-10 15:52:05 +02:00
ynh_add_systemd_config --service="$app-web" --template="mastodon-web.service"
2024-02-24 23:28:27 +01:00
yunohost service add "$app-web" --description="$app web service"
2021-05-10 15:52:05 +02:00
ynh_add_systemd_config --service="$app-sidekiq" --template="mastodon-sidekiq.service"
2024-02-24 23:28:27 +01:00
yunohost service add "$app-sidekiq" --description="$app sidekiq service"
2021-05-10 15:52:05 +02:00
ynh_add_systemd_config --service="$app-streaming" --template="mastodon-streaming.service"
2024-02-24 23:28:27 +01:00
yunohost service add "$app-streaming" --description="$app streaming service"
2021-04-10 20:28:06 +02:00
2024-02-24 23:28:27 +01:00
ynh_add_config --template="cron" --destination="/etc/cron.d/$app"
2019-03-21 02:07:49 +01:00
2024-02-24 23:28:27 +01:00
# Use logrotate to manage app-specific logfile(s)
mkdir -p /var/log/$app
chown $app:$app /var/log/$app
ynh_use_logrotate --non-append
2018-12-26 06:55:29 +01:00
2018-06-12 18:32:43 +02:00
#=================================================
2024-02-24 23:28:27 +01:00
# RECONFIGURE THE APP (UPDATE CONF, APPLY MIGRATIONS...)
2018-06-12 18:32:43 +02:00
#=================================================
2024-02-24 23:28:27 +01:00
# UPDATE A CONFIG FILE
2018-06-12 18:32:43 +02:00
#=================================================
2024-02-24 23:28:27 +01:00
ynh_script_progression --message="Updating a config file..." --weight=1
2017-04-20 16:35:44 +02:00
2024-02-24 23:28:27 +01:00
language="$(echo $language | head -c 2)"
ynh_add_config --template=".env.production.sample" --destination="$config"
chmod 400 "$config"
chown $app:$app "$config"
2019-03-18 04:22:38 +01:00
2019-05-10 16:34:57 +02:00
#=================================================
2024-02-24 23:28:27 +01:00
# APPLY MIGRATIONS
2019-05-10 16:34:57 +02:00
#=================================================
2024-02-24 23:28:27 +01:00
ynh_script_progression --message="Applying migrations..." --weight=1
2019-05-10 16:34:57 +02:00
2024-02-24 23:28:27 +01:00
pushd "$install_dir/live"
ynh_use_ruby
ynh_exec_warn_less ynh_exec_as $app RAILS_ENV=production $ynh_ruby_load_path $ld_preload bin/bundle exec rails db:migrate
ynh_exec_warn_less ynh_exec_as $app RAILS_ENV=production $ynh_ruby_load_path $ld_preload bin/tootctl cache clear
popd
2019-05-10 16:34:57 +02:00
2019-03-18 04:22:38 +01:00
#=================================================
2024-02-24 23:28:27 +01:00
# START SYSTEMD SERVICE
2019-03-18 04:22:38 +01:00
#=================================================
2024-02-24 23:28:27 +01:00
ynh_script_progression --message="Starting a systemd service..." --weight=1
2019-03-18 04:22:38 +01:00
2024-05-27 12:28:33 +02:00
ynh_systemd_action --service_name=${app}-web --action="restart" --log_path=/var/log/$app/$app-web.log --line_match="Listening on"
2024-02-24 23:28:27 +01:00
ynh_systemd_action --service_name=${app}-sidekiq --action="start" --log_path=/var/log/$app/$app-sidekiq.log --line_match="Schedules Loaded"
ynh_systemd_action --service_name=${app}-streaming --action="start" --log_path=/var/log/$app/$app-streaming.log --line_match="Streaming API now listening"
2019-03-18 04:22:38 +01:00
#=================================================
# END OF SCRIPT
#=================================================
2022-09-20 01:57:06 +02:00
ynh_script_progression --message="Upgrade of $app completed" --last