1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/acropolis_ynh.git synced 2024-09-03 18:06:22 +02:00
acropolis_ynh/scripts/upgrade

234 lines
8.6 KiB
Text
Raw Normal View History

2021-11-10 19:58:54 +01:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --weight=1
2021-11-10 19:58:54 +01:00
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
admin=$(ynh_app_setting_get --app=$app --key=admin)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
2022-07-15 05:14:36 +02:00
db_user=$(ynh_app_setting_get --app=$app --key=db_user)
db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)
2021-11-15 20:16:45 +01:00
port_web=$(ynh_app_setting_get --app=$app --key=port_web)
secret_key_base=$(ynh_app_setting_get --app=$app --key=secret_key_base)
2021-11-10 19:58:54 +01:00
#=================================================
# CHECK VERSION
#=================================================
2022-07-15 05:14:36 +02:00
ynh_script_progression --message="Checking version..."
2021-11-10 19:58:54 +01:00
upgrade_type=$(ynh_check_app_version_changed)
2022-07-15 05:14:36 +02:00
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=1
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
ynh_clean_check_starting
# Restore it if the upgrade fails
ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..." --weight=1
ynh_systemd_action --service_name=${app}-web --action="stop" --log_path=systemd --line_match="Stopped"
ynh_systemd_action --service_name=${app}-sidekiq --action="stop" --log_path=systemd --line_match="Stopped"
2021-11-15 20:16:45 +01:00
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."
# Cleaning legacy permissions
if ynh_legacy_permissions_exists; then
ynh_legacy_permissions_delete_all
ynh_app_setting_delete --app=$app --key=is_public
fi
# If port_web doesn't exist, create it, needed for old install
if [[ -z "$port_web" ]]; then
port_web=3000
ynh_app_setting_set --app=$app --key=port_web --value=$port_web
fi
# If db_pwd doesn't exist, create it, needed for old install
if [[ -z "$db_pwd" ]]; then
db_pwd=$(ynh_string_random)
ynh_app_setting_set --app=$app --key=db_pwd --value=$db_pwd
ynh_psql_test_if_first_run
sudo --login --user=postgres psql -c"ALTER user $app WITH PASSWORD '$db_pwd'" postgres
ynh_replace_string --match_string="DB_PASS=" --replace_string="DB_PASS=${db_pwd}" --target_file="$config"
fi
# If secret_key_base doesn't exist, retrieve it or create it
if [[ -z "$secret_key_base" ]]; then
secret_key_base=$(grep -oP "SECRET_KEY_BASE=\K\w+" $config)
if [[ -z "$secret_key_base" ]]; then
secret_key_base=$(ynh_string_random --length=128)
fi
ynh_app_setting_set --app=$app --key=secret_key_base --value="$secret_key_base"
fi
ynh_remove_extra_repo
2022-07-15 15:35:26 +02:00
ynh_app_setting_delete --app=$app --key=redis_namespace
2021-11-10 19:58:54 +01:00
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
2021-11-10 19:58:54 +01:00
# Create a dedicated user (if not existing)
ynh_system_user_create --username=$app --home_dir="$final_path"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Upgrading source files..." --weight=1
2021-11-10 19:58:54 +01:00
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path"
fi
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app:www-data "$final_path"
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading dependencies..." --weight=1
2021-11-10 19:58:54 +01:00
ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies
2022-07-15 05:14:36 +02:00
ynh_exec_warn_less ynh_install_ruby --ruby_version=$RUBY_VERSION
2021-11-10 19:58:54 +01:00
#=================================================
2022-07-15 05:14:36 +02:00
# NGINX CONFIGURATION
2021-11-10 19:58:54 +01:00
#=================================================
2022-07-15 05:14:36 +02:00
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=1
# Create a dedicated NGINX config
ynh_add_nginx_config
2021-11-15 20:16:45 +01:00
2022-07-15 05:14:36 +02:00
#=================================================
# SPECIFIC UPGRADE
2021-11-10 19:58:54 +01:00
#=================================================
2021-11-15 20:16:45 +01:00
# INSTALLING RUBY AND BUNDLER
#=================================================
ynh_script_progression --message="Installing Ruby..."
pushd "$final_path"
ynh_use_ruby
ynh_gem install bundler:1.17.3 --no-document
ynh_exec_as $app echo "gem: --no-ri --no-rdoc" >> "$final_path/.gemrc"
popd
2021-11-10 19:58:54 +01:00
#=================================================
# UPDATE A CONFIG FILE
#=================================================
ynh_script_progression --message="Updating a configuration file..." --weight=1
2021-11-10 19:58:54 +01:00
2021-11-15 20:16:45 +01:00
database_yml="$final_path/config/database.yml"
diaspora_toml="$final_path/config/diaspora.toml"
secret_key_base=$(ynh_string_random --length=128)
ynh_app_setting_set --app="$app" --key=secret_key_base --value="$secret_key_base"
2021-11-10 19:58:54 +01:00
2022-07-15 15:33:09 +02:00
ynh_add_config --template="../conf/database_yml.example" --destination="$database_yml"
ynh_add_config --template="../conf/diaspora_toml.example" --destination="$diaspora_toml"
2021-11-15 20:16:45 +01:00
chmod 400 "$database_yml"
chown $app:$app "$database_yml"
chmod 400 "$diaspora_toml"
chown $app:$app "$diaspora_toml"
2021-11-10 19:58:54 +01:00
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..." --weight=1
2021-11-10 19:58:54 +01:00
# Create a dedicated systemd config
ynh_add_systemd_config --service="$app-web" --template="acropolis-web.service"
ynh_add_systemd_config --service="$app-sidekiq" --template="acropolis-sidekiq.service"
2021-11-15 20:16:45 +01:00
2022-07-15 05:14:36 +02:00
#=================================================
# UPGRADE ACROPOLIS
#=================================================
2021-11-15 20:16:45 +01:00
ynh_script_progression --message="Upgrading acropolis..."
pushd "$final_path"
sudo -u $app $ynh_ruby_load_path bin/bundle config deployment 'true'
sudo -u $app $ynh_ruby_load_path bin/bundle config without 'development test'
sudo -u $app $ynh_ruby_load_path bin/bundle install -j$(getconf _NPROCESSORS_ONLN)
sudo -u $app RAILS_ENV=production $ynh_ruby_load_path bin/bundle exec rails assets:clean
sudo -u $app RAILS_ENV=production $ynh_ruby_load_path bin/bundle exec rails assets:precompile
sudo -u $app RAILS_ENV=production $ynh_ruby_load_path bin/bundle exec rails db:migrate
popd
2021-11-10 19:58:54 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Upgrading logrotate configuration..." --weight=1
2021-11-10 19:58:54 +01:00
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
2021-11-10 19:58:54 +01:00
2021-11-15 20:16:45 +01:00
yunohost service add "$app-web" --description="$app web service"
yunohost service add "$app-sidekiq" --description="$app sidekiq service"
2021-11-10 19:58:54 +01:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1
2021-11-10 19:58:54 +01:00
2021-11-15 20:16:45 +01:00
ynh_systemd_action --service_name=${app}-web --action="start" --log_path=systemd --line_match="Listening on"
ynh_systemd_action --service_name=${app}-sidekiq --action="start" --log_path=systemd --line_match="Schedules Loaded"
2021-11-10 19:58:54 +01:00
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
2021-11-10 19:58:54 +01:00
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --last