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

366 lines
14 KiB
Text
Raw Normal View History

2020-01-18 10:28:47 +01:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
2020-12-20 14:22:36 +01:00
source ynh_install_ruby
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)
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)
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
install_extensions=$(ynh_app_setting_get --app=$app --key=install_extensions)
access_domain=$(ynh_app_setting_get --app=$app --key=access_domain)
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)
#=================================================
# 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
# Fix is_public as a boolean value
if [ "$is_public" = "Yes" ]; then
2020-12-27 23:18:55 +01:00
ynh_app_setting_set --app=$app --key=is_public --value=1
is_public=1
2020-01-18 10:28:47 +01:00
elif [ "$is_public" = "No" ]; then
2020-12-27 23:18:55 +01:00
ynh_app_setting_set --app=$app --key=is_public --value=0
is_public=0
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 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/"
fi
fi
2020-01-18 10:28:47 +01:00
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." --weight=1
2020-01-18 10:28:47 +01:00
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
2020-12-27 23:18:55 +01:00
# restore it if the upgrade fails
ynh_restore_upgradebackup
2020-01-18 10:28:47 +01:00
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# 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
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
2020-12-27 23:18:55 +01:00
ynh_script_progression --message="Upgrading source files..." --weight=1
# Backup files to keep
tmpdir=$(mktemp -d)
if [ -d $final_path/live/log ] ; then
cp -Rp $final_path/live/log $tmpdir
fi
# Remove destination directory
ynh_secure_remove --file=$final_path
# 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/log ] ; then
cp -Rp $tmpdir/log $final_path/live
fi
2020-01-18 10:28:47 +01:00
fi
#=================================================
# 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-01-22 20:41:41 +01:00
ynh_add_nginx_config "\
port \
acess_domain
"
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
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
ynh_install_ruby --ruby_version=$RUBY_VERSION
/opt/rbenv/versions/$RUBY_VERSION/bin/gem update --system --no-document
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"
2020-12-20 14:22:36 +01:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
2020-12-27 23:18:55 +01:00
ynh_script_progression --message="Modifying a config file..." --weight=2
cp -f ../conf/env.sample $config_file
ynh_replace_string --match_string="EXPOSED_PORT=3000" --replace_string="EXPOSED_PORT=$port" --target_file="$config_file"
secret_key=$(ynh_string_random --length=48 | base64)
ynh_replace_string --match_string="SECRET_KEY_BASE=changeme123" --replace_string="SECRET_KEY_BASE=$secret_key" --target_file="$config_file"
pseudo_key=$(ynh_string_random --length=48 | base64)
ynh_replace_string --match_string="PSEUDO_KEY_PARAMS_KEY=changeme456" --replace_string="PSEUDO_KEY_PARAMS_KEY=$pseudo_key" --target_file="$config_file"
ynh_replace_string --match_string="RAILS_ENV=development" --replace_string="RAILS_ENV=production" --target_file="$config_file"
ynh_replace_string --match_string="DB_DATABASE=standard_notes_db" --replace_string="DB_DATABASE=$db_name" --target_file="$config_file"
ynh_replace_string --match_string="DB_USERNAME=std_notes_user" --replace_string="DB_USERNAME=$db_user" --target_file="$config_file"
ynh_replace_string --match_string="DB_PASSWORD=changeme123" --replace_string="DB_PASSWORD=$db_pwd" --target_file="$config_file"
ynh_replace_string --match_string="RAILS_RELATIVE_URL_ROOT=/" --replace_string="RAILS_RELATIVE_URL_ROOT=$path_url" --target_file="$config_file"
2020-12-20 14:22:36 +01:00
fi
2020-01-18 10:28:47 +01:00
#=================================================
2020-12-20 14:22:36 +01:00
# INSTALLING Standard Notes - Synicing Server
2020-01-18 10:28:47 +01:00
#=================================================
2020-12-20 14:22:36 +01:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
2020-12-27 23:18:55 +01:00
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
2021-01-14 20:32:33 +01:00
# exec_as "$app" env PATH=$PATH RAILS_ENV=production /opt/rbenv/versions/$RUBY_VERSION/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-01-16 23:04:32 +01:00
if [ $install_extensions -eq 1 ]
2021-01-08 19:44:37 +01:00
then
2021-01-14 20:32:33 +01:00
ynh_script_progression --message="Reinstalling Standard Notes - Extensions..." --weight=1
2021-01-08 19:44:37 +01:00
2021-01-14 20:32:33 +01:00
ynh_replace_string --match_string="^RAILS_SERVE_STATIC_FILES=.*$" --replace_string="RAILS_SERVE_STATIC_FILES=true" --target_file="$config_file"
if [ $path_url = "/" ]
then
path=""
else
path=$path_url
fi
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
2021-01-08 19:44:37 +01:00
fi
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
2020-12-27 23:18:55 +01:00
ynh_add_systemd_config --others_var="\
port \
RUBY_VERSION \
"
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"
ynh_add_fail2ban_config --use_template --others_var="\
2021-01-16 23:04:32 +01:00
domain \
path_url \
"
2020-01-18 10:28:47 +01:00
#=================================================
# SETUP SSOWAT
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Upgrading SSOwat configuration..." --weight=1
2020-12-20 14:22:36 +01:00
# Make app public if necessary or protect it
2020-01-18 10:28:47 +01:00
if [ $is_public -eq 1 ]
then
2020-12-27 23:18:55 +01:00
# Create the visitors permission if needed
if ! ynh_permission_exists --permission "main"; then
ynh_permission_create --permission "main" --allowed "visitors"
fi
2020-01-18 10:28:47 +01:00
fi
#=================================================
# 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"
2021-01-07 17:41:34 +01:00
sleep 15
2020-01-18 10:28:47 +01:00
#=================================================
# 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
#=================================================
# SEND A README FOR THE ADMIN
#=================================================
ynh_script_progression --message="Sending a readme for the admin..."
2021-01-16 23:04:32 +01:00
message_extensions="To install extensions open this page: https://$domain$path_url/extensions/\n\
2021-01-15 19:58:23 +01:00
If you want to change the url of the syncing server all extensions from here have to be reinstalled be the users."
2021-01-16 23:04:32 +01:00
message_no_extensions="You have no extensions installed.\n\
2021-01-15 19:58:23 +01:00
You can configure this app by using the config-panel"
admin_panel="https://$(grep portal_domain /etc/ssowat/conf.json | cut -d'"' -f4)/yunohost/admin/#/apps/$app"
2021-01-16 23:04:32 +01:00
action="You can find some specific actions for this app by using the experimental __URL_TAG1__action feature__URL_TAG2__$admin_panel/actions__URL_TAG3__"
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-01-15 19:58:23 +01:00
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"
2021-01-16 23:04:32 +01:00
if [ $install_extensions -eq 1 ]
2021-01-15 19:58:23 +01:00
then
ynh_replace_string --match_string="__EXTENSIONS__" --replace_string="$message_extensions" --target_file="../conf/message"
else
ynh_replace_string --match_string="__EXTENSIONS__" --replace_string="$message_no_extensions" --target_file="../conf/message"
fi
2021-01-16 23:04:32 +01:00
ynh_replace_string --match_string="__ACTION__" --replace_string="$action" --target_file="../conf/message"
2021-01-22 23:47:08 +01:00
ynh_replace_string --match_string="__CONFIG_PANEL__" --replace_string="$config_panel" --target_file="../conf/message"
2021-01-15 19:58:23 +01:00
ynh_send_readme_to_admin --app_message="../conf/message" --type='upgrade'
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