#!/bin/bash #================================================= # GENERIC START #================================================= # IMPORT GENERIC HELPERS #================================================= source _common.sh source ynh_install_ruby source /usr/share/yunohost/helpers #================================================= # LOAD SETTINGS #================================================= ynh_script_progression --message="Loading installation settings..." --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) is_public=$(ynh_app_setting_get --app=$app --key=is_public) final_path=$(ynh_app_setting_get --app=$app --key=final_path) db_name=$(ynh_app_setting_get --app=$app --key=db_name) db_user=$db_name db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd) port=$(ynh_app_setting_get --app=$app --key=port) install_extensions=$(ynh_app_setting_get --app=$app --key=install_extensions) access_domain=$(ynh_app_setting_get --app=$app --key=access_domain) mail=$(ynh_app_setting_get --app=$app --key=mail) #================================================= # 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..." --weight=1 # Fix is_public as a boolean value if [ "$is_public" = "Yes" ]; then ynh_app_setting_set --app=$app --key=is_public --value=1 is_public=1 elif [ "$is_public" = "No" ]; then ynh_app_setting_set --app=$app --key=is_public --value=0 is_public=0 fi # If db_name doesn't exist, create it if [ -z "$db_name" ]; then db_name=$(ynh_sanitize_dbid --db_name=$app) ynh_app_setting_set --app=$app --key=db_name --value=$db_name fi # If final_path doesn't exist, create it if [ -z "$final_path" ]; then final_path=/opt/yunohost/$app ynh_app_setting_set --app=$app --key=final_path --value=$final_path fi # If install_extensions doesn't exist, create it if [ -z "$install_extensions" ]; then install_extensions=0 ynh_app_setting_set --app=$app --key=install_extensions --value=$install_extensions fi # If access_domain doesn't exist, create it if [ -z "$access_domain" ]; then access_domain=$domain ynh_app_setting_set --app=$app --key=access_domain --value=$access_domain fi if [[ ! -d "$final_path/live/public/extensions/" || \ ! -d "$final_path/live/public/extensions/src/" ]] then if test -e "../sources/extra_files/app" then cp -a "../sources/extra_files/app/." "$final_path/live" fi fi #================================================= # 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 () { # 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 --action="stop" --log_path="/var/log/$app/$app.log" #================================================= # DOWNLOAD, CHECK AND UNPACK SOURCE #================================================= if [ "$upgrade_type" == "UPGRADE_APP" ] then ynh_script_progression --message="Upgrading source files..." --weight=1 # Backup files to keep tmpdir=$(mktemp -d) if [ -d "$final_path/live/.env" ] ; then cp -Rp "$final_path/live/.env" "$tmpdir" fi if [ -d "$final_path/live/log" ] ; then cp -Rp "$final_path/live/log" "$tmpdir" fi # Remove destination directory ynh_secure_remove --file="$final_path/live" # Download, check integrity, uncompress and patch the source from app.src mkdir -p "$final_path" ynh_setup_source --dest_dir="$final_path/live" if [ -d "$tmpdir/.env" ] ; then cp -Rp "$tmpdir/.env" "$final_path/live" fi if [ -d "$tmpdir/log" ] ; then cp -Rp "$tmpdir/log" "$final_path/live" fi fi #================================================= # NGINX CONFIGURATION #================================================= ynh_script_progression --message="Upgrading nginx web server configuration..." --weight=1 # Create a dedicated nginx config ynh_add_nginx_config "\ port \ access_domain \ " #================================================= # UPGRADE DEPENDENCIES #================================================= ynh_script_progression --message="Upgrading dependencies..." --weight=1 ynh_install_app_dependencies $pkg_dependencies #================================================= # INSTALL RUBY #================================================= ynh_script_progression --message="Installing Ruby...( This may take a while... )" --weight=100 #331 ynh_install_ruby --ruby_version=$RUBY_VERSION /opt/rbenv/versions/$RUBY_VERSION/bin/gem update --system --no-document #================================================= # CREATE DEDICATED USER #================================================= ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1 # Create a dedicated user (if not existing) ynh_system_user_create --username=$app --home_dir=$final_path #================================================= # SPECIFIC UPGRADE #================================================= #================================================= # MODIFY A CONFIG FILE #================================================= config_file="$final_path/live/.env" ynh_script_progression --message="Modifying a config file..." --weight=2 if ynh_compare_current_package_version --comparison lt --version "3.13.6~ynh4" then ynh_replace_string --match_string="SMTP_HOST=.*$" --replace_string="SMTP_HOST=localhost" --target_file="$config_file" ynh_replace_string --match_string="SMTP_PORT=.*$" --replace_string="SMTP_PORT=25" --target_file="$config_file" ynh_replace_string --match_string="SMTP_DOMAIN=.*$" --replace_string="SMTP_DOAMIN=localhost" --target_file="$config_file" fi ynh_replace_string --match_string="__MAIL__" --replace_string="$mail" --target_file="$final_path/live/app/mailers/application_mailer.rb" ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="$final_path/live/app/views/user_mailer/welcome.html.erb" ynh_replace_string --match_string="__PATH__" --replace_string="$path_url" --target_file="$final_path/live/app/views/user_mailer/welcome.html.erb" #================================================= # INSTALLING Standard Notes - Synicing Server #================================================= if [ "$upgrade_type" == "UPGRADE_APP" ] then ynh_script_progression --message="Installing Standard Notes - Synicing Server..." --weight=93 chown -R "$app": "$final_path" pushd "$final_path/live" exec_as "$app" env PATH=$PATH /opt/rbenv/versions/$RUBY_VERSION/bin/bundle config set path 'vendor/bundle' exec_as "$app" env PATH=$PATH /opt/rbenv/versions/$RUBY_VERSION/bin/bundle config set with 'development' exec_as "$app" env PATH=$PATH /opt/rbenv/versions/$RUBY_VERSION/bin/bundle install exec_as "$app" env PATH=$PATH RAILS_ENV=production /opt/rbenv/versions/$RUBY_VERSION/bin/bundle exec rails db:create db:migrate --quiet # exec_as "$app" env PATH=$PATH RAILS_ENV=production /opt/rbenv/versions/$RUBY_VERSION/bin/bundle exec rails assets:precompile --quiet popd fi #================================================= # INSTALLING Standard Notes - Extensions #================================================= ynh_script_progression --message="Reinstalling Standard Notes - Extensions..." --weight=1 if [ $path_url = "/" ] then path="" else path=$path_url fi ynh_replace_string --match_string="__DOMAIN__PATH__" --replace_string="$domain$path" --target_file="$final_path/live/public/extensions/repo.json" find "$final_path/live/public/extensions/src/" -name "*.json" -print0 | while read -d $'\0' file do ynh_replace_string --match_string="__DOMAIN__PATH__" --replace_string="$domain$path" --target_file="$file" done find "../conf/" -name "ext_*.src" -print0 | while read -d $'\0' file do basename=$(basename -as .src $file) ynh_setup_source --dest_dir="$final_path/live/public/extensions/src/${basename#'ext_'}" --source_id="$basename" done install_extensions=1 ynh_app_setting_set --app=$app --key=install_extensions --value=$install_extensions #================================================= # SETUP SYSTEMD #================================================= ynh_script_progression --message="Upgrading systemd configuration..." --weight=1 # Create a dedicated systemd config ynh_add_systemd_config --others_var="\ port \ RUBY_VERSION \ " #================================================= # STORE THE CONFIG FILE CHECKSUM #================================================= # Calculate and store the config file checksum into the app settings ynh_store_file_checksum --file="$config_file" ynh_store_file_checksum --file="/etc/nginx/conf.d/$domain.d/$app.conf" #================================================= # GENERIC FINALIZATION #================================================= #================================================= # SECURE FILES AND DIRECTORIES #================================================= # Set permissions to app files chown -R root: $final_path chown $app: $final_path mkdir -p "$final_path/live/log" chown -R $app: "$final_path/live/log/" mkdir -p "$final_path/live/tmp" chown -R $app: "$final_path/live/tmp/" mkdir -p "/var/log/$app" chown -R $app: "/var/log/$app" #================================================= # SETUP LOGROTATE #================================================= ynh_script_progression --message="Upgrading logrotate configuration..." --weight=1 # Use logrotate to manage application logfile(s) ynh_use_logrotate --logfile="$final_path/live/log/production.log" ynh_use_logrotate --logfile="/var/log/$app/$app.log" #================================================= # INTEGRATE SERVICE IN YUNOHOST #================================================= yunohost service add $app --description "Standard Notes - Syncing Server" --log "/var/log/$app/$app.log" #================================================= # SETUP FAIL2BAN #================================================= ynh_script_progression --message="Reconfiguring fail2ban..." --weight=1 # Create a dedicated fail2ban config touch "/var/log/$app/$app.log" ynh_add_fail2ban_config --use_template --others_var="\ domain \ path_url \ " #================================================= # SETUP SSOWAT #================================================= ynh_script_progression --message="Upgrading SSOwat configuration..." --weight=1 # Make app public if necessary or protect it if [ $is_public -eq 1 ] then # Create the visitors permission if needed if ! ynh_permission_exists --permission "main"; then ynh_permission_create --permission "main" --allowed "visitors" fi fi #================================================= # START SYSTEMD SERVICE #================================================= ynh_script_progression --message="Starting a systemd service..." --weight=1 # Start a systemd service ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log" #================================================= # RELOAD NGINX #================================================= ynh_script_progression --message="Reloading nginx web server..." --weight=1 ynh_systemd_action --service_name=nginx --action=reload #================================================= # WAITING FOR SERVICE #================================================= ynh_script_progression --message="Waiting for service..." --weight=1 is_service_ready #================================================= # SEND A README FOR THE ADMIN #================================================= ynh_script_progression --message="Sending a readme for the admin..." admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app" config_panel="You can find some specific configurations for this app by using the experimental __URL_TAG1__config-panel feature__URL_TAG2__$admin_panel/config-panel__URL_TAG3__" ynh_replace_string --match_string="__TYPE__" --replace_string="installed" --target_file="../conf/message" ynh_replace_string --match_string="__DOMAIN__" --replace_string="$domain" --target_file="../conf/message" ynh_replace_string --match_string="__PATH_URL__" --replace_string="$path_url" --target_file="../conf/message" ynh_replace_string --match_string="__CONFIG_PANEL__" --replace_string="$config_panel" --target_file="../conf/message" ynh_send_readme_to_admin --app_message="../conf/message" --type='upgrade' #================================================= # END OF SCRIPT #================================================= ynh_script_progression --message="Upgrade of $app completed" --last