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

387 lines
13 KiB
Text
Raw Normal View History

2018-04-14 09:33:23 +02:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
2018-04-14 09:33:23 +02:00
#=================================================
# LOAD SETTINGS
#=================================================
2020-05-30 17:05:03 +02:00
ynh_script_progression --message="Loading installation settings..."
2018-04-14 09:33:23 +02: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-05-29 17:36:13 +02:00
db_user=$db_name
db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)
redis_db=$(ynh_app_setting_get --app=$app --key=redis_db)
unicorn_workers=$(ynh_app_setting_get --app=$app --key=unicorn_workers)
2018-04-14 09:33:23 +02:00
if [ ${PACKAGE_CHECK_EXEC:-0} -eq 1 ]; then
:
else
# Check memory requirements
check_memory_requirements_upgrade
fi
2018-04-14 09:33:23 +02:00
#=================================================
# CHECK VERSION
2018-04-14 09:33:23 +02:00
#=================================================
2020-05-30 17:05:03 +02:00
ynh_script_progression --message="Checking version..."
upgrade_type=$(ynh_check_app_version_changed)
2018-04-14 09:33:23 +02:00
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
2020-05-30 17:05:03 +02:00
ynh_script_progression --message="Backing up the app before upgrading (may take a while)..."
2018-04-14 09:33:23 +02:00
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
2021-03-07 01:39:02 +01:00
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
#=================================================
# ENABLE MAINTENANCE MODE
#=================================================
2020-05-30 17:05:03 +02:00
ynh_script_progression --message="Enabling maintenance mode..."
ynh_maintenance_mode_ON
2018-04-14 09:33:23 +02:00
#=================================================
# STANDARD UPGRADE STEPS
2018-04-14 09:33:23 +02:00
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
2020-05-30 17:05:03 +02:00
ynh_script_progression --message="Stopping a systemd service..."
2018-04-14 09:33:23 +02:00
ynh_systemd_action --service_name=$app --action="stop" --log_path="$final_path/log/unicorn.stderr.log"
2022-05-29 17:36:13 +02:00
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..."
# If unicorn_workers doesn't exist, create it
if [ -z "$unicorn_workers" ]
then
# We assume for the moment that ARM devices are only dual core, so
# we restrict the number of workers to 2 (the default is 3)
if [ -n "$(uname -m | grep arm)" ]
then
unicorn_workers=2
else
unicorn_workers=3
fi
ynh_app_setting_set --app=$app --key=unicorn_workers --value=$unicorn_workers
fi
# Cleaning legacy permissions
if ynh_legacy_permissions_exists; then
ynh_legacy_permissions_delete_all
ynh_app_setting_delete --app=$app --key=is_public
fi
2021-04-10 01:48:01 +02:00
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_script_progression --message="Making sure dedicated system user exists..."
# Create a dedicated user (if not existing)
ynh_system_user_create --username=$app --home_dir="$final_path" --use_shell
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
2020-05-30 17:32:33 +02:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Upgrading source files..."
# Specific actions on ARM architecture
if [ -n "$(uname -m | grep arm)" ] ; then
# Unapply commit cf9b4a789b855b5199e98a13424e409854a8e848 that breaks ARM
# compatibility by pointing to a recent libv8 version
# This is due to this libv8 issue (https://github.com/cowboyd/libv8/issues/261)
# that prevents it from being compiled on ARM hence no binary gem is available yet
cp ../sources/patches_arm/* ../sources/patches
fi
# Backup files to keep
tmpdir=$(mktemp -d)
cp -Rp $final_path/plugins $final_path/config/discourse.conf $tmpdir
if [ -d $final_path/public/uploads ] ; then
cp -Rp $final_path/public/uploads $tmpdir
fi
if [ -d $final_path/public/backups ] ; then
cp -Rp $final_path/public/backups $tmpdir
fi
if [ -d $final_path/log ] ; then
cp -Rp $final_path/log $tmpdir
fi
# Remove destination directory
ynh_secure_remove --file=$final_path
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$final_path"
# Restore previous files
if [ -d $tmpdir/uploads ] ; then
cp -Rp $tmpdir/uploads $final_path/public
fi
if [ -d $tmpdir/backups ] ; then
cp -Rp $tmpdir/backups $final_path/public
fi
if [ -d $tmpdir/log ] ; then
cp -Rp $tmpdir/log $final_path
fi
(
cd $tmpdir/plugins/
for discourse_plugin_dir in */
do
# Only copy plugins not included in Discourse archive
if [ ! -d "$final_path/plugins/$discourse_plugin_dir" ]
then
cp -a "$discourse_plugin_dir" "$final_path/plugins/$discourse_plugin_dir"
fi
done
)
cp -Rp $tmpdir/log $final_path
2020-05-30 17:32:33 +02:00
cp -p $tmpdir/discourse.conf $final_path/config
2020-05-30 17:32:33 +02:00
# Install LDAP plugin
ynh_secure_remove --file="$final_path/plugins/discourse-ldap-auth"
mkdir -p "$final_path/plugins/discourse-ldap-auth"
ynh_setup_source --dest_dir="$final_path/plugins/discourse-ldap-auth" --source_id=ldap-auth
fi
2018-04-14 09:33:23 +02:00
2021-04-17 18:31:03 +02:00
chmod 750 "$final_path"
2021-04-10 01:48:01 +02:00
chmod -R o-rwx "$final_path"
2021-04-14 20:12:05 +02:00
chown -R $app:www-data "$final_path"
2021-04-10 01:48:01 +02:00
2018-04-14 09:33:23 +02:00
#=================================================
# NGINX CONFIGURATION
#=================================================
2021-03-07 01:39:02 +01:00
ynh_script_progression --message="Upgrading NGINX web server configuration..."
2018-04-14 09:33:23 +02:00
2021-03-07 01:39:02 +01:00
# Create a dedicated NGINX config
2018-06-03 17:56:14 +02:00
ynh_add_nginx_config
2018-04-14 09:33:23 +02:00
# Reference: https://meta.discourse.org/t/subfolder-support-with-docker/30507?u=falco&source_topic_id=54191
if [ "$path_url" != "/" ] ; then
2021-03-07 01:47:27 +01:00
ynh_replace_string --match_string='$proxy_add_x_forwarded_for' --replace_string='$http_your_original_ip_header' --target_file="/etc/nginx/conf.d/$domain.d/$app.conf"
2018-04-14 09:33:23 +02:00
fi
ynh_store_file_checksum --file="/etc/nginx/conf.d/$domain.d/$app.conf"
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
2020-05-30 17:05:03 +02:00
ynh_script_progression --message="Upgrading dependencies..."
2022-05-29 17:36:13 +02:00
ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies $build_pkg_dependencies
ynh_exec_warn_less ynh_install_ruby --ruby_version=$ruby_version
2021-03-11 02:35:15 +01:00
ynh_use_ruby
#=================================================
# SPECIFIC UPGRADE
#=================================================
2022-05-29 17:36:13 +02:00
# UPDATE A CONFIG FILE
#=================================================
2020-05-30 17:32:33 +02:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
2022-05-29 17:36:13 +02:00
ynh_script_progression --message="Updating a config file..."
2020-05-30 17:32:33 +02:00
2022-06-02 23:48:32 +02:00
admin_mail=$(ynh_user_get_info --username=$admin --key=mail)
2022-05-29 17:36:13 +02:00
ynh_add_config --template="../conf/discourse_defaults.conf" --destination="$final_path/config/discourse.conf"
2020-05-30 17:32:33 +02:00
2022-05-29 17:36:13 +02:00
ynh_add_config --template="../conf/settings.yml" --destination="$final_path/plugins/discourse-ldap-auth/config/settings.yml"
2020-05-30 17:32:33 +02:00
# Disable svgo worker
echo "svgo: false" > $final_path/.image_optim.yml
fi
#=================================================
# SETUP UNICORN, A RUBY SERVER
#=================================================
2020-05-30 17:32:33 +02:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Setting up Unicorn..."
unicorn_config_file="$final_path/config/unicorn.conf.rb"
# Make a backup of the original config file if modified
ynh_backup_if_checksum_is_different "$unicorn_config_file"
2021-03-07 01:55:03 +01:00
# Calculate and store the config file checksum
2020-05-30 17:32:33 +02:00
ynh_store_file_checksum --file="$unicorn_config_file"
2021-03-07 01:55:03 +01:00
secret="$(ynh_string_random)"
ynh_add_config --template="../conf/secrets.yml" --destination="$final_path/config/secrets.yml"
2020-05-30 17:32:33 +02:00
# Set permissions to app files
2021-04-17 18:31:03 +02:00
chmod 750 "$final_path"
2021-04-15 02:35:10 +02:00
chmod -R o-rwx "$final_path"
chown -R $app:www-data "$final_path"
2020-05-30 17:32:33 +02:00
pushd "$final_path"
2020-05-30 17:32:33 +02:00
# Install bundler, a gems installer
2021-03-08 21:38:47 +01:00
ynh_gem install bundler
2020-05-30 17:32:33 +02:00
# Install without documentation
2021-07-20 19:36:31 +02:00
ynh_exec_as $app echo "gem: --no-ri --no-rdoc" >> "$final_path/.gemrc"
popd
2020-05-30 17:32:33 +02:00
# Specific actions on ARM architecture
if [ -n "$(uname -m | grep arm)" ] ; then
2021-03-07 01:47:27 +01:00
# Define the platform specifically to retrieve binaries
# for libv8 because it currently doesn't compile on ARM devices
2021-03-08 21:38:47 +01:00
exec_login_as $app bin/bundle config specific_platform arm-linux
2020-05-30 17:32:33 +02:00
fi
# Install dependencies
2021-03-08 21:38:47 +01:00
exec_login_as $app bin/bundle config set path 'vendor/bundle'
exec_login_as $app bin/bundle config set with 'development'
exec_login_as $app MAKEFLAGS=-j2 bin/bundle install --jobs 2
2020-05-30 17:32:33 +02:00
# On ARM architecture, replace bundled libpsl by system native libpsl
# because the provided binary isn't compatible
if [ -n "$(uname -m | grep arm)" ] ; then
2021-03-07 01:47:27 +01:00
(
cd $final_path/vendor/bundle/ruby/*/gems/mini_suffix-*/vendor
rm libpsl.so
ln -s $(ldconfig -p | grep libpsl | awk 'END {print $NF}') libpsl.so
)
2020-05-30 17:32:33 +02:00
fi
fi
2018-04-14 09:33:23 +02:00
#=================================================
# PREPARE THE DATABASE
2018-04-14 09:33:23 +02:00
#=================================================
2020-05-30 17:32:33 +02:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Preparing the database..."
2022-05-29 17:36:13 +02:00
ynh_exec_warn_less exec_login_as $app RAILS_ENV=production bin/rake db:migrate
ynh_exec_warn_less exec_login_as $app RAILS_ENV=production bin/rake assets:precompile
2020-05-30 17:32:33 +02:00
fi
2022-06-03 01:08:53 +02:00
ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies
ynh_package_autoremove
#=================================================
# CONFIGURE PLUGINS
#=================================================
2020-05-30 17:32:33 +02:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Configuring plugins..."
# Patch ldap-auth plugin dependency (omniauth-ldap) to fix it when using domain subfolder
# (Can only do that now because we are patching dependencies which have just been downloaded)
# Patch applied: https://github.com/omniauth/omniauth-ldap/pull/16
(
2022-05-29 17:36:13 +02:00
cd $final_path/plugins/discourse-ldap-auth/gems/${ruby_version}/gems/omniauth-ldap*/
2020-05-30 17:32:33 +02:00
patch -p1 < $YNH_CWD/../conf/ldap-auth-fix-subfolder.patch
)
fi
#=================================================
# SETUP SYSTEMD
#=================================================
2021-03-07 01:39:02 +01:00
2020-05-30 17:32:33 +02:00
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Configuring a systemd service..."
2020-05-30 17:32:33 +02:00
additional_env="UNICORN_WORKERS=$unicorn_workers"
2022-05-29 17:36:13 +02:00
libjemalloc="$(ldconfig -p | grep libjemalloc | awk 'END {print $NF}')"
2020-05-30 17:32:33 +02:00
ynh_add_systemd_config
fi
2018-04-14 09:33:23 +02:00
#=================================================
# GENERIC FINALIZATION
#=================================================
# SECURE FILES AND DIRECTORIES
#=================================================
2020-05-30 17:05:03 +02:00
ynh_script_progression --message="Securing files and directories..."
# Add a pids and socket directory for the systemd script.
mkdir -p "$final_path/tmp/pids"
mkdir -p "$final_path/tmp/sockets"
# Create specific folders and links for subfolder compatibilityn
# (see: https://meta.discourse.org/t/subfolder-support-with-docker/30507)
(
2020-05-30 17:32:33 +02:00
cd $final_path
mkdir -p "public/forum"
cd public/forum
if [ ! -L ./uploads ]; then
ln -s ../uploads
fi
if [ ! -L ./backups ]; then
ln -s ../backups
fi
)
# Set permissions to app files
2021-04-17 18:31:03 +02:00
chmod 750 "$final_path"
2021-04-15 02:35:10 +02:00
chmod -R o-rwx "$final_path"
chown -R $app:www-data "$final_path"
#=================================================
2021-03-07 01:39:02 +01:00
# SETUP LOGROTATE
2018-04-14 09:33:23 +02:00
#=================================================
2021-03-07 01:39:02 +01:00
ynh_script_progression --message="Upgrading logrotate configuration..."
2018-04-14 09:33:23 +02:00
2021-03-07 01:39:02 +01:00
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --logfile="$final_path/log/unicorn.stderr.log"
ynh_use_logrotate --logfile="$final_path/log/unicorn.stdout.log"
ynh_use_logrotate --logfile="$final_path/log/production.log"
2018-04-14 09:33:23 +02:00
#=================================================
2020-12-03 19:30:38 +01:00
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..."
yunohost service add $app --log "$final_path/log/unicorn.stderr.log" "$final_path/log/unicorn.stdout.log" "$final_path/log/production.log"
2020-12-03 19:30:38 +01:00
#=================================================
# START SYSTEMD SERVICE
2018-04-14 09:33:23 +02:00
#=================================================
2020-05-30 17:05:03 +02:00
ynh_script_progression --message="Starting a systemd service..."
2018-04-14 09:33:23 +02:00
ynh_systemd_action --service_name=$app --action="start" --log_path="$final_path/log/unicorn.stderr.log" --line_match="INFO -- : worker=$((unicorn_workers-1)) ready"
2018-06-03 17:21:50 +02:00
#=================================================
# DISABLE MAINTENANCE MODE
#=================================================
2020-05-30 17:05:03 +02:00
ynh_script_progression --message="Disabling maintenance mode..."
2018-06-03 17:21:50 +02:00
ynh_maintenance_mode_OFF
#=================================================
# RELOAD NGINX
#=================================================
2021-03-07 01:39:02 +01:00
ynh_script_progression --message="Reloading NGINX web server..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
2020-05-30 17:05:03 +02:00
ynh_script_progression --message="Upgrade of $app completed"