2020-01-18 10:28:47 +01:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
2021-05-01 21:37:41 +02:00
source ynh_install_ruby__2
2021-03-06 21:39:49 +01:00
source ynh_redis
2020-01-18 10:28:47 +01:00
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Loading installation settings..." --weight=1
2020-01-18 10:28:47 +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)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
2020-12-24 12:11:42 +01:00
db_user=$db_name
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
port=$(ynh_app_setting_get --app=$app --key=port)
2021-01-22 20:41:41 +01:00
access_domain=$(ynh_app_setting_get --app=$app --key=access_domain)
2021-02-06 01:44:31 +01:00
mail=$(ynh_app_setting_get --app=$app --key=mail)
2021-03-06 21:39:49 +01:00
redis_db=$(ynh_app_setting_get --app=$app --key=redis_db)
config_file="$final_path/live/.env"
2020-01-18 10:28:47 +01:00
#=================================================
# 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)
2021-02-06 21:41:33 +01: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 () {
# restore it if the upgrade fails
ynh_restore_upgradebackup
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
2020-01-18 10:28:47 +01:00
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
2020-01-18 10:28:47 +01:00
2021-03-14 18:51:33 +01:00
# Cleaning legacy permissions
if ynh_legacy_permissions_exists; then
ynh_legacy_permissions_delete_all
ynh_app_setting_delete --app=$app --key=is_public
2020-01-18 10:28:47 +01:00
fi
# If db_name doesn't exist, create it
if [ -z "$db_name" ]; then
2020-12-27 23:18:55 +01:00
db_name=$(ynh_sanitize_dbid --db_name=$app)
ynh_app_setting_set --app=$app --key=db_name --value=$db_name
2020-01-18 10:28:47 +01:00
fi
# If final_path doesn't exist, create it
if [ -z "$final_path" ]; then
2020-12-27 23:18:55 +01:00
final_path=/opt/yunohost/$app
ynh_app_setting_set --app=$app --key=final_path --value=$final_path
2020-01-18 10:28:47 +01:00
fi
2021-01-22 20:41:41 +01:00
# 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
2021-02-06 20:01:03 +01:00
# If mail doesn't exist, create it
if [ -z "$mail" ]; then
mail="$app@$domain"
ynh_app_setting_set --app=$app --key=mail --value=$mail
fi
2021-03-06 21:39:49 +01:00
# If redis_db doesn't exist, create it
if [ -z "$redis_db" ]; then
redis_db=$(ynh_redis_get_free_db)
ynh_app_setting_set --app=$app --key=redis_db --value="$redis_db"
fi
2021-02-06 20:01:03 +01:00
if ynh_compare_current_package_version --comparison lt --version "3.13.6~ynh4"
then
2021-03-06 21:39:49 +01:00
# Add variables to .env config file
2021-02-06 20:01:03 +01:00
echo -e "\
2021-03-06 21:39:49 +01:00
\n# Public file server\
\n# Empty is disabled\
\n# Any value is enabled\
\nRAILS_SERVE_STATIC_FILES=\
" >> "$config_file"
2021-02-06 20:01:03 +01:00
# Apply Patch
if [ -f "$YNH_CWD/../sources/patches/app-01-add-mail.patch" ]
then
pushd "$final_path/live"
patch --strip=1 < "$YNH_CWD/../sources/patches/app-01-add-mail.patch"
popd
fi
fi
2021-03-06 21:39:49 +01:00
if ynh_compare_current_package_version --comparison lt --version "3.20.4~ynh1"
then
# Add variables to .env config file
echo -e "\
\n# (Optional) Redis Cache for ephemeral sessions\
\nREDIS_URL=redis://cache:6379\
\n\
\n# (Optional) Change URLs to Internal DNS\
\n#INTERNAL_DNS_REROUTE_ENABLED=false\
\n\
\n# (Optional) Auth Proxy JWT Secret\
\n#AUTH_JWT_SECRET=changeme123\
\n\
\n# (Optional) User Management Server - registration emails, subscriptions etc.\
\n#USER_MANAGEMENT_SERVER=\
" >> "$config_file"
# Change variables to .env config file
ynh_replace_string \
--match_string="RAILS_LOG_LEVEL=.*$" \
--replace_string='# Log Level options: "INFO" | "DEBUG" | "INFO" | "WARN" | "ERROR" | "FATAL"\nRAILS_LOG_LEVEL=INFO' \
--target_file="$config_file"
ynh_replace_string --match_string="REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_file"
fi
2020-01-18 10:28:47 +01:00
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Stopping a systemd service..." --weight=1
2020-01-18 10:28:47 +01:00
ynh_systemd_action --service_name=$app --action="stop" --log_path="/var/log/$app/$app.log"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
2021-02-07 00:02:09 +01:00
ynh_script_progression --message="Upgrading source files..." --weight=1
2020-01-18 10:28:47 +01:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
2020-12-27 23:18:55 +01:00
# Backup files to keep
tmpdir=$(mktemp -d)
2021-02-07 23:05:13 +01:00
if [ -f "$final_path/live/.env" ] ; then
2021-02-06 02:44:33 +01:00
cp -Rp "$final_path/live/.env" "$tmpdir"
fi
if [ -d "$final_path/live/log" ] ; then
cp -Rp "$final_path/live/log" "$tmpdir"
2020-12-27 23:18:55 +01:00
fi
# Remove destination directory
2021-02-06 02:44:33 +01:00
ynh_secure_remove --file="$final_path/live"
2020-12-27 23:18:55 +01:00
# Download, check integrity, uncompress and patch the source from app.src
2021-02-06 02:44:33 +01:00
mkdir -p "$final_path"
2020-12-27 23:18:55 +01:00
ynh_setup_source --dest_dir="$final_path/live"
2021-02-07 23:05:13 +01:00
if [ -f "$tmpdir/.env" ] ; then
2021-02-06 02:44:33 +01:00
cp -Rp "$tmpdir/.env" "$final_path/live"
fi
if [ -d "$tmpdir/log" ] ; then
cp -Rp "$tmpdir/log" "$final_path/live"
2020-12-27 23:18:55 +01:00
fi
2020-01-18 10:28:47 +01:00
fi
2021-02-07 00:02:09 +01:00
if [ "$upgrade_type" == "UPGRADE_PACKAGE" ]
then
ynh_secure_remove --file="$final_path/live/public/extensions"
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
2020-01-18 10:28:47 +01:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Upgrading nginx web server configuration..." --weight=1
2020-01-18 10:28:47 +01:00
# Create a dedicated nginx config
2021-05-01 20:11:59 +02:00
ynh_add_nginx_config
2020-12-20 14:22:36 +01:00
2020-01-18 10:28:47 +01:00
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Upgrading dependencies..." --weight=1
2020-01-18 10:28:47 +01:00
ynh_install_app_dependencies $pkg_dependencies
2021-05-01 21:37:41 +02:00
ynh_install_ruby --ruby_version=$RUBY_VERSION
2020-01-18 10:28:47 +01:00
2020-12-20 14:22:36 +01:00
#=================================================
# INSTALL RUBY
#=================================================
2020-12-27 23:18:55 +01:00
ynh_script_progression --message="Installing Ruby...( This may take a while... )" --weight=100 #331
2020-12-20 14:22:36 +01:00
2021-05-01 21:37:41 +02:00
pushd "$final_path/live"
ynh_use_ruby
ynh_gem update --system --no-document
ynh_gem install bundler --no-document
popd
2020-12-20 14:22:36 +01:00
2020-01-18 10:28:47 +01:00
#=================================================
# CREATE DEDICATED USER
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Making sure dedicated system user exists..." --weight=1
2020-01-18 10:28:47 +01:00
# Create a dedicated user (if not existing)
2020-12-20 14:22:36 +01:00
ynh_system_user_create --username=$app --home_dir=$final_path
2020-01-18 10:28:47 +01:00
#=================================================
# SPECIFIC UPGRADE
#=================================================
#=================================================
2020-12-20 14:22:36 +01:00
# MODIFY A CONFIG FILE
2020-01-18 10:28:47 +01:00
#=================================================
2020-12-24 12:11:42 +01:00
config_file="$final_path/live/.env"
2021-01-24 11:36:36 +01:00
ynh_script_progression --message="Modifying a config file..." --weight=2
2021-02-06 02:44:33 +01:00
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"
2021-02-06 20:01:03 +01:00
ynh_replace_string --match_string="SMTP_USERNAME=.*$" --replace_string="#&" --target_file="$config_file"
ynh_replace_string --match_string="SMTP_PASSWORD=.*$" --replace_string="#&" --target_file="$config_file"
ynh_replace_string --match_string="SMTP_DOMAIN=.*$" --replace_string="SMTP_DOMAIN=localhost" --target_file="$config_file"
ynh_replace_string --match_string="SMTP_DOMAIN=.*$" --replace_string="&\n# Enable STARTTLS\n# Empty is disabled\n# Any value is enabled\nSMTP_STARTTLS=" --target_file="$config_file"
ynh_replace_string --match_string="RAILS_SERVE_STATIC_FILES=.*$" --replace_string="RAILS_SERVE_STATIC_FILES=true" --target_file="$config_file"
2021-02-06 02:44:33 +01:00
fi
2021-02-06 01:44:31 +01:00
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"
2021-01-24 11:36:36 +01:00
2020-01-18 10:28:47 +01:00
#=================================================
2021-05-01 21:48:58 +02:00
# INSTALLING Standard Notes - Syncing Server
2020-01-18 10:28:47 +01:00
#=================================================
2020-12-20 14:22:36 +01:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
2021-05-01 21:48:58 +02:00
ynh_script_progression --message="Installing Standard Notes - Syncing Server..." --weight=93
2020-12-27 23:18:55 +01:00
2021-03-20 13:48:42 +01:00
chown -R $app: "$final_path"
2020-12-27 23:18:55 +01:00
pushd "$final_path/live"
2021-05-01 21:37:41 +02:00
exec_as $app $ynh_ruby_load_path bin/bundle config set --local path 'vendor/bundle'
exec_as $app $ynh_ruby_load_path bin/bundle config set with 'development'
exec_as $app $ynh_ruby_load_path bin/bundle install -j$(getconf _NPROCESSORS_ONLN)
exec_as $app RAILS_ENV=production $ynh_ruby_load_path bin/bundle exec rails db:migrate --quiet
exec_as $app RAILS_ENV=production $ynh_ruby_load_path bin/bundle exec rails db:seed --quiet
# exec_as $app RAILS_ENV=production $ynh_ruby_load_path bin/bundle exec rails assets:precompile --quiet
2020-12-27 23:18:55 +01:00
popd
2020-12-20 14:22:36 +01:00
fi
2020-01-18 10:28:47 +01:00
2021-01-08 19:44:37 +01:00
#=================================================
# INSTALLING Standard Notes - Extensions
#=================================================
2021-02-07 00:02:09 +01:00
ynh_script_progression --message="Installing Standard Notes - Extensions..." --weight=1
2021-02-06 02:44:33 +01:00
if [ $path_url = "/" ]
2021-01-08 19:44:37 +01:00
then
2021-02-06 02:44:33 +01:00
path=""
else
path=$path_url
fi
2021-01-08 19:44:37 +01:00
2021-02-06 02:44:33 +01:00
ynh_replace_string --match_string="__DOMAIN__PATH__" --replace_string="$domain$path" --target_file="$final_path/live/public/extensions/repo.json"
2021-01-14 20:32:33 +01:00
2021-02-06 02:44:33 +01:00
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
2021-01-14 20:32:33 +01:00
2021-02-06 02:44:33 +01:00
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
2021-01-30 12:57:24 +01:00
2020-01-18 10:28:47 +01:00
#=================================================
# SETUP SYSTEMD
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Upgrading systemd configuration..." --weight=1
2020-01-18 10:28:47 +01:00
# Create a dedicated systemd config
2021-05-01 20:11:59 +02:00
ynh_add_systemd_config
2020-01-18 10:28:47 +01:00
#=================================================
2020-12-20 14:22:36 +01:00
# STORE THE CONFIG FILE CHECKSUM
2020-01-18 10:28:47 +01:00
#=================================================
2020-12-20 14:22:36 +01:00
# 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"
2020-01-18 10:28:47 +01:00
2020-12-20 14:22:36 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
2020-01-18 10:28:47 +01:00
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
2020-12-20 14:22:36 +01:00
# Set permissions to app files
2020-01-18 10:28:47 +01:00
chown -R root: $final_path
2020-12-20 14:22:36 +01:00
chown $app: $final_path
2020-12-27 23:18:55 +01:00
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/"
2020-12-20 14:22:36 +01:00
2020-12-27 23:18:55 +01:00
mkdir -p "/var/log/$app"
chown -R $app: "/var/log/$app"
2020-12-20 14:22:36 +01:00
#=================================================
# SETUP LOGROTATE
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Upgrading logrotate configuration..." --weight=1
2020-12-20 14:22:36 +01:00
# 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"
2020-12-24 12:11:42 +01:00
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
yunohost service add $app --description "Standard Notes - Syncing Server" --log "/var/log/$app/$app.log"
2020-12-20 14:22:36 +01:00
#=================================================
# SETUP FAIL2BAN
#=================================================
2021-01-02 19:42:20 +01:00
ynh_script_progression --message="Reconfiguring fail2ban..." --weight=1
2020-12-20 14:22:36 +01:00
# Create a dedicated fail2ban config
2021-01-02 19:42:20 +01:00
touch "/var/log/$app/$app.log"
2021-05-01 20:11:59 +02:00
ynh_add_fail2ban_config --use_template
2020-01-18 10:28:47 +01:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Starting a systemd service..." --weight=1
2020-01-18 10:28:47 +01:00
2020-12-20 14:22:36 +01:00
# Start a systemd service
2020-01-18 10:28:47 +01:00
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app/$app.log"
#=================================================
# RELOAD NGINX
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Reloading nginx web server..." --weight=1
2020-01-18 10:28:47 +01:00
ynh_systemd_action --service_name=nginx --action=reload
2021-01-15 19:58:23 +01:00
#=================================================
2021-01-25 13:08:34 +01:00
# WAITING FOR SERVICE
#=================================================
ynh_script_progression --message="Waiting for service..." --weight=1
is_service_ready
#=================================================
2021-01-15 19:58:23 +01:00
# 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"
2021-01-22 23:47:08 +01:00
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__"
2021-02-10 22:14:53 +01:00
admin_mail_info="\
New Feature ( 3.13.6~ynh4 ):\n\
Now Standard Notes extensions are hosted with this package.\n\
Only new users will get an e-mail with install instructions.\n\
With the current SMTP settings you can not send e-mails to all e-mail providers. For example GMail will probably not recive this e-mails.\n\
Please setup the SMTP account in this file: \"$final_path/live/.env\" under \"#Mailer settings\" if you want to fix this.\n\
2021-02-10 22:53:12 +01:00
Please setup the Access-Domain for the extensions in the __URL_TAG1__config-panel__URL_TAG2__$admin_panel/config-panel__URL_TAG3__\
"
2021-01-15 19:58:23 +01:00
2021-02-10 22:53:12 +01:00
echo -e "\
Standard Notes - Syncing Server was successfully upgraded.\n\
2021-05-01 21:48:58 +02:00
Please configure the Standard Notes web app or mobile app to use this syncing server: https://$domain$path_url/\n\
2021-02-10 22:53:12 +01:00
$config_panel\n\n\
$admin_mail_info\
" > message
2021-01-15 19:58:23 +01:00
2021-02-10 22:53:12 +01:00
ynh_send_readme_to_admin --app_message="message" --type='upgrade'
2021-01-15 19:58:23 +01:00
2020-01-18 10:28:47 +01:00
#=================================================
# END OF SCRIPT
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Upgrade of $app completed" --last