#!/bin/bash #================================================= # GENERIC START #================================================= # IMPORT GENERIC HELPERS #================================================= source _common.sh source /usr/share/yunohost/helpers #================================================= # LOAD SETTINGS #================================================= ynh_script_progression --message="Loading installation settings..." --time --weight=1 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) language=$(ynh_app_setting_get --app=$app --key=language) db_name=$(ynh_app_setting_get --app=$app --key=db_name) redis_namespace=$(ynh_app_setting_get --app=$app --key=db_name) db_user=$(ynh_sanitize_dbid --db_name=$app) db_pwd=$(ynh_app_setting_get --app=$app --key=db_pwd) port_web=$(ynh_app_setting_get --app=$app --key=port_web) secret_key_base=$(ynh_app_setting_get --app=$app --key=secret_key_base) #================================================= # CHECK VERSION #================================================= ### This helper will compare the version of the currently installed app and the version of the upstream package. ### $upgrade_type can have 2 different values ### - UPGRADE_APP if the upstream app version has changed ### - UPGRADE_PACKAGE if only the YunoHost package has changed ### ynh_check_app_version_changed will stop the upgrade if the app is up to date. ### UPGRADE_APP should be used to upgrade the core app only if there's an upgrade to do. upgrade_type=$(ynh_check_app_version_changed) #================================================= # 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 redis_namespace doesn't exist, create it if [[ -z "$redis_namespace" ]]; then redis_namespace=${app}_production ynh_app_setting_set --app=$app --key=redis_namespace --value=$redis_namespace 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 #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP #================================================= ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --time --weight=1 # Backup the current version of the app ynh_backup_before_upgrade ynh_clean_setup () { # 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..." --time --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" #================================================= # CREATE DEDICATED USER #================================================= ynh_script_progression --message="Making sure dedicated system user exists..." --time --weight=1 # 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..." --time --weight=1 # Download, check integrity, uncompress and patch the source from app.src ynh_setup_source --dest_dir="$final_path" fi # FIXME: this should be managed by the core in the future # Here, as a packager, you may have to tweak the ownerhsip/permissions # such that the appropriate users (e.g. maybe www-data) can access # files in some cases. # But FOR THE LOVE OF GOD, do not allow r/x for "others" on the entire folder - # this will be treated as a security issue. chmod 750 "$final_path" chmod -R o-rwx "$final_path" chown -R $app:www-data "$final_path" #================================================= # NGINX CONFIGURATION #================================================= ynh_script_progression --message="Upgrading NGINX web server configuration..." --time --weight=1 # Create a dedicated NGINX config ynh_add_nginx_config 'web' #================================================= # UPGRADE DEPENDENCIES #================================================= ynh_script_progression --message="Upgrading dependencies..." --time --weight=1 ynh_install_app_dependencies $pkg_dependencies ynh_install_ruby --ruby_version=$RUBY_VERSION #================================================= # SPECIFIC UPGRADE #================================================= #================================================= # 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 #================================================= # UPDATE A CONFIG FILE #================================================= ynh_script_progression --message="Updating a configuration file..." --time --weight=1 database_yml="$final_path/config/database.yml" diaspora_toml="$final_path/config/diaspora.toml" language="$(echo $language | head -c 2)" redis_namespace=${app}_production ynh_app_setting_set --app="$app" --key=redis_namespace --value="$redis_namespace" secret_key_base=$(ynh_string_random --length=128) ynh_app_setting_set --app="$app" --key=secret_key_base --value="$secret_key_base" ynh_add_config --template="$database_yml.example" --destination="$database_yml" ynh_add_config --template="$diaspora_toml.example" --destination="$diaspora_toml" ynh_replace_string --match_string="#certificate_authorities = \"/etc/ssl/" --replace_string="certificate_authorities = \"/etc/ssl/" --target_file=$diaspora_toml ynh_replace_string --match_string="username: \"postgres\"" --replace_string="username: \"$db_user\"" --target_file=$database_yml ynh_replace_string --match_string="password: \"postgres\"" --replace_string="password: \"$db_pwd\"" --target_file=$database_yml chmod 400 "$database_yml" chown $app:$app "$database_yml" chmod 400 "$diaspora_toml" chown $app:$app "$diaspora_toml" #================================================= # SETUP SYSTEMD #================================================= ynh_script_progression --message="Upgrading systemd configuration..." --time --weight=1 # Create a dedicated systemd config ynh_add_systemd_config --service="$app-web" --template="glitchsoc-web.service" ynh_add_systemd_config --service="$app-sidekiq" --template="glitchsoc-sidekiq.service" 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 #================================================= # GENERIC FINALIZATION #================================================= # SETUP LOGROTATE #================================================= ynh_script_progression --message="Upgrading logrotate configuration..." --time --weight=1 # 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..." --time --weight=1 yunohost service add "$app-web" --description="$app web service" yunohost service add "$app-sidekiq" --description="$app sidekiq service" #================================================= # START SYSTEMD SERVICE #================================================= ynh_script_progression --message="Starting a systemd service..." --time --weight=1 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" #================================================= # RELOAD NGINX #================================================= ynh_script_progression --message="Reloading NGINX web server..." --time --weight=1 ynh_systemd_action --service_name=nginx --action=reload #================================================= # END OF SCRIPT #================================================= ynh_script_progression --message="Upgrade of $app completed" --time --last