2018-04-14 09:33:23 +02:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
|
# GENERIC START
|
|
|
|
|
#=================================================
|
|
|
|
|
# IMPORT GENERIC HELPERS
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
if [ ! -e _common.sh ]; then
|
|
|
|
|
# Fetch helpers file if not in current directory
|
|
|
|
|
cp ../settings/scripts/_common.sh ./_common.sh
|
|
|
|
|
chmod a+rx _common.sh
|
|
|
|
|
fi
|
|
|
|
|
source /usr/share/yunohost/helpers
|
2018-04-24 17:45:19 +02:00
|
|
|
|
source _common.sh
|
2018-04-14 09:33:23 +02:00
|
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
|
# MANAGE SCRIPT FAILURE
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
# Exit if an error occurs during the execution of the script
|
|
|
|
|
ynh_abort_if_errors
|
|
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
|
# LOAD SETTINGS
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
app=$YNH_APP_INSTANCE_NAME
|
|
|
|
|
|
|
|
|
|
domain=$(ynh_app_setting_get $app domain)
|
2018-12-03 09:25:46 +01:00
|
|
|
|
path_url=$(ynh_app_setting_get $app path)
|
2018-04-14 09:33:23 +02:00
|
|
|
|
final_path=$(ynh_app_setting_get $app final_path)
|
|
|
|
|
is_public=$(ynh_app_setting_get $app is_public)
|
|
|
|
|
admin=$(ynh_app_setting_get $app admin)
|
|
|
|
|
db_name=$(ynh_app_setting_get $app db_name)
|
|
|
|
|
db_pwd=$(ynh_app_setting_get $app db_pwd)
|
2018-07-01 10:06:14 +02:00
|
|
|
|
redis_db=$(ynh_app_setting_get $app redis_db)
|
2018-04-14 09:33:23 +02:00
|
|
|
|
|
2018-04-30 18:53:20 +02:00
|
|
|
|
# Check memory requirements
|
|
|
|
|
check_memory_requirements_upgrade
|
|
|
|
|
|
2018-06-03 17:21:50 +02:00
|
|
|
|
#=================================================
|
|
|
|
|
# ENABLE MAINTENANCE MODE
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
ynh_maintenance_mode_ON
|
|
|
|
|
|
2018-04-14 09:33:23 +02:00
|
|
|
|
#=================================================
|
|
|
|
|
# BACKUP BEFORE UPGRADE
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
# Stop services
|
2018-05-04 12:51:16 +02:00
|
|
|
|
systemctl stop $app
|
2018-04-14 09:33:23 +02:00
|
|
|
|
|
|
|
|
|
# Backup the current version of the app
|
|
|
|
|
if [[ $(ynh_app_setting_get $app disable_backup_before_upgrade) != '1' ]]
|
|
|
|
|
then
|
|
|
|
|
ynh_backup_before_upgrade
|
|
|
|
|
ynh_clean_setup () {
|
|
|
|
|
ynh_restore_upgradebackup
|
|
|
|
|
}
|
|
|
|
|
fi
|
|
|
|
|
#=================================================
|
|
|
|
|
# INSTALL DEPENDENCIES
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
ynh_install_app_dependencies "$pkg_dependencies"
|
|
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
|
# NGINX CONFIGURATION
|
|
|
|
|
#=================================================
|
|
|
|
|
|
2018-06-03 17:56:14 +02:00
|
|
|
|
# Create a dedicated nginx config
|
|
|
|
|
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
|
2018-06-03 17:56:14 +02:00
|
|
|
|
ynh_replace_string '$proxy_add_x_forwarded_for' '$http_your_original_ip_header' "/etc/nginx/conf.d/$domain.d/$app.conf"
|
2018-04-14 09:33:23 +02:00
|
|
|
|
fi
|
2018-06-03 17:56:14 +02:00
|
|
|
|
ynh_store_file_checksum "/etc/nginx/conf.d/$domain.d/$app.conf"
|
2018-04-14 09:33:23 +02:00
|
|
|
|
|
2018-04-29 11:28:38 +02:00
|
|
|
|
if ! ynh_is_upstream_up_to_date ; then
|
|
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
|
# DOWNLOAD, CHECK AND UNPACK SOURCE
|
|
|
|
|
#=================================================
|
2018-12-27 12:50:00 +01:00
|
|
|
|
# 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
|
2018-04-29 11:28:38 +02:00
|
|
|
|
ynh_app_setting_set $app final_path $final_path
|
2019-02-10 12:14:36 +01:00
|
|
|
|
# Backup files to keep
|
|
|
|
|
tmpdir=$(mktemp -d)
|
2019-07-02 21:40:10 +02:00
|
|
|
|
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
|
2019-02-10 12:14:36 +01:00
|
|
|
|
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 $final_path
|
2018-04-29 11:28:38 +02:00
|
|
|
|
# Download, check integrity, uncompress and patch the source from app.src
|
|
|
|
|
ynh_setup_source "$final_path"
|
2019-02-10 12:14:36 +01:00
|
|
|
|
# Restore previous files
|
2019-07-02 21:40:10 +02:00
|
|
|
|
if [ -d $tmpdir/uploads ] ; then
|
|
|
|
|
cp -Rp $tmpdir/uploads $final_path/public
|
|
|
|
|
fi
|
2019-02-10 12:14:36 +01:00
|
|
|
|
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
|
2019-06-25 21:22:10 +02:00
|
|
|
|
(
|
|
|
|
|
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
|
|
|
|
|
)
|
2019-02-10 12:14:36 +01:00
|
|
|
|
cp -Rp $tmpdir/log $final_path
|
|
|
|
|
cp -p $tmpdir/discourse.conf $final_path/config
|
|
|
|
|
|
2018-04-29 11:28:38 +02:00
|
|
|
|
# Install LDAP plugin
|
2019-02-10 12:14:36 +01:00
|
|
|
|
ynh_secure_remove "$final_path/plugins/discourse-ldap-auth"
|
2018-04-29 11:28:38 +02:00
|
|
|
|
mkdir -p "$final_path/plugins/discourse-ldap-auth"
|
|
|
|
|
ynh_setup_source "$final_path/plugins/discourse-ldap-auth" ldap-auth
|
|
|
|
|
|
2019-02-10 12:14:36 +01:00
|
|
|
|
#=================================================
|
|
|
|
|
# INSTALL RUBY
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
ynh_install_ruby $RUBY_VERSION
|
|
|
|
|
|
2018-04-29 11:28:38 +02:00
|
|
|
|
#=================================================
|
|
|
|
|
# SPECIFIC SETUP
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
|
# CONFIGURE DISCOURSE
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
# Make a backup of the original config file if modified
|
|
|
|
|
ynh_backup_if_checksum_is_different "$final_path/config/discourse.conf"
|
|
|
|
|
# Configure database
|
|
|
|
|
discourse_config_file="$final_path/config/discourse.conf"
|
|
|
|
|
cp $final_path/config/discourse_defaults.conf $discourse_config_file
|
|
|
|
|
ynh_replace_string "db_name = discourse" "db_name = $db_name" "$discourse_config_file"
|
|
|
|
|
ynh_replace_string "db_username = discourse" "db_username = $db_name" "$discourse_config_file"
|
|
|
|
|
ynh_replace_string "db_password =" "db_password = $db_pwd" "$discourse_config_file"
|
|
|
|
|
|
|
|
|
|
# Configure hostname
|
|
|
|
|
ynh_replace_string "hostname = \"www.example.com\"" "hostname = \"$domain\"" "$discourse_config_file"
|
|
|
|
|
ynh_replace_string "relative_url_root =" "relative_url_root = ${path_url%/}" "$discourse_config_file"
|
|
|
|
|
# Serve static assets (i.e. images, js, etc.)
|
|
|
|
|
ynh_replace_string "serve_static_assets = false" "serve_static_assets = true" "$discourse_config_file"
|
|
|
|
|
# Don't show miniprofiler
|
|
|
|
|
ynh_replace_string "load_mini_profiler = true" "load_mini_profiler = false" "$discourse_config_file"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Configure e-mail server
|
|
|
|
|
admin_mail=$(ynh_user_get_info "$admin" mail)
|
|
|
|
|
ynh_replace_string "developer_emails =" "developer_emails = $admin_mail" "$discourse_config_file"
|
|
|
|
|
ynh_replace_string "smtp_address =" "smtp_address = localhost" "$discourse_config_file"
|
|
|
|
|
ynh_replace_string "smtp_domain =" "smtp_domain = $domain" "$discourse_config_file"
|
|
|
|
|
ynh_replace_string "smtp_enable_start_tls = true" "smtp_enable_start_tls = false" "$discourse_config_file"
|
|
|
|
|
|
2018-07-01 10:06:14 +02:00
|
|
|
|
# Configure redis
|
|
|
|
|
ynh_replace_string "redis_db = 0" "redis_db = $redis_db" "$discourse_config_file"
|
|
|
|
|
|
2018-08-22 22:11:39 +02:00
|
|
|
|
# Don't notify on new versions (handled by the YunoHost package)
|
|
|
|
|
ynh_replace_string "new_version_emails = true" "new_version_emails = false" "$discourse_config_file"
|
2018-12-27 12:50:00 +01:00
|
|
|
|
|
2018-04-29 11:28:38 +02:00
|
|
|
|
# Calculate and store the config file checksum
|
|
|
|
|
ynh_store_file_checksum "$discourse_config_file"
|
|
|
|
|
|
|
|
|
|
# Make a backup of the original config file if modified
|
|
|
|
|
ynh_backup_if_checksum_is_different "$final_path/plugins/discourse-ldap-auth/config/settings.yml"
|
2019-02-10 12:14:36 +01:00
|
|
|
|
|
2018-04-29 11:28:38 +02:00
|
|
|
|
# Configure LDAP plugin
|
|
|
|
|
ldap_config_file="$final_path/plugins/discourse-ldap-auth/config/settings.yml"
|
|
|
|
|
ynh_replace_string "adfs.example.com" "localhost" "$ldap_config_file"
|
|
|
|
|
ynh_replace_string "dc=example,dc=com" "ou=users,dc=yunohost,dc=org" "$ldap_config_file"
|
|
|
|
|
ynh_replace_string "sAMAccountName" "uid" "$ldap_config_file"
|
|
|
|
|
ynh_store_file_checksum "$ldap_config_file"
|
|
|
|
|
|
2019-02-10 12:14:36 +01:00
|
|
|
|
# Disable svgo worker
|
|
|
|
|
echo "svgo: false" > $final_path/.image_optim.yml
|
|
|
|
|
|
2018-04-29 11:28:38 +02:00
|
|
|
|
#=================================================
|
2018-05-04 12:51:16 +02:00
|
|
|
|
# SETUP UNICORN, A RUBY SERVER
|
2018-04-29 11:28:38 +02:00
|
|
|
|
#=================================================
|
2018-05-04 12:51:16 +02:00
|
|
|
|
unicorn_config_file="$final_path/config/unicorn.conf.rb"
|
|
|
|
|
# Use socket connection
|
|
|
|
|
ynh_replace_string 'listen (ENV\["UNICORN_PORT"\] || 3000).to_i' '# listen (ENV["UNICORN_PORT"] || 3000).to_i' "$unicorn_config_file"
|
|
|
|
|
ynh_replace_string '# listen "#{discourse_path}/tmp/sockets/unicorn.sock"' 'listen "#{discourse_path}/tmp/sockets/unicorn.sock"' "$unicorn_config_file"
|
2018-04-29 11:28:38 +02:00
|
|
|
|
# Calculate and store the config file checksum
|
2018-05-04 12:51:16 +02:00
|
|
|
|
ynh_store_file_checksum "$unicorn_config_file"
|
2018-04-29 11:28:38 +02:00
|
|
|
|
|
|
|
|
|
# Set a secret value
|
|
|
|
|
cp ../conf/secrets.yml "$final_path/config/secrets.yml"
|
|
|
|
|
ynh_replace_string "__SECRET__" "$(ynh_string_random)" "$final_path/config/secrets.yml"
|
|
|
|
|
# Calculate and store the config file checksum
|
|
|
|
|
ynh_store_file_checksum "$final_path/config/secrets.yml"
|
|
|
|
|
|
|
|
|
|
# Set permissions to app files
|
|
|
|
|
chown -R $app: $final_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(cd "$final_path"
|
|
|
|
|
# Install bundler, a gems installer
|
|
|
|
|
gem install bundler
|
|
|
|
|
# Install without documentation
|
2019-02-10 12:14:36 +01:00
|
|
|
|
exec_as $app echo "gem: --no-ri --no-rdoc" >> "$final_path/.gemrc")
|
|
|
|
|
# Specific actions on ARM architecture
|
|
|
|
|
if [ -n "$(uname -m | grep arm)" ] ; then
|
|
|
|
|
# Define the platform specifically to retrieve binaries
|
|
|
|
|
# for libv8 because it currently doesn't compile on ARM devices
|
|
|
|
|
exec_login_as $app bundle config specific_platform arm-linux
|
|
|
|
|
fi
|
2018-04-29 11:28:38 +02:00
|
|
|
|
# Install dependencies
|
2019-02-10 12:14:36 +01:00
|
|
|
|
exec_login_as $app MAKEFLAGS=-j2 bundle install --jobs 2 --path vendor/bundle --with development
|
|
|
|
|
|
2018-04-29 11:28:38 +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
|
|
|
|
|
(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)
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
|
# PREPARE THE DATABASE
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
rake_exec="exec_login_as $app RAILS_ENV=production bin/rake"
|
|
|
|
|
$rake_exec db:migrate
|
|
|
|
|
$rake_exec assets:precompile
|
|
|
|
|
|
2019-02-10 12:14:36 +01:00
|
|
|
|
#=================================================
|
|
|
|
|
# CONFIGURE 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
|
|
|
|
|
(cd $final_path/plugins/discourse-ldap-auth/gems/${RUBY_VERSION}/gems/omniauth-ldap*/
|
|
|
|
|
patch -p1 < $YNH_CWD/../conf/ldap-auth-fix-subfolder.patch)
|
|
|
|
|
|
2018-04-29 11:28:38 +02:00
|
|
|
|
#=================================================
|
|
|
|
|
# GENERIC FINALIZATION
|
|
|
|
|
#=================================================
|
|
|
|
|
# SECURE FILES AND DIRECTORIES
|
|
|
|
|
#=================================================
|
|
|
|
|
|
2019-02-10 12:14:36 +01:00
|
|
|
|
# Add a pids and socket directory for the systemd script.
|
|
|
|
|
mkdir -p "$final_path/tmp/pids"
|
|
|
|
|
mkdir "$final_path/tmp/sockets"
|
|
|
|
|
|
2019-03-15 19:50:18 +01:00
|
|
|
|
# Create specific folders and links for subfolder compatibilityn
|
|
|
|
|
# (see: https://meta.discourse.org/t/subfolder-support-with-docker/30507)
|
|
|
|
|
(
|
|
|
|
|
cd $final_path
|
|
|
|
|
mkdir -p "public/forum"
|
|
|
|
|
cd public/forum && ln -s ../uploads && ln -s ../backups
|
|
|
|
|
)
|
|
|
|
|
|
2018-04-29 11:28:38 +02:00
|
|
|
|
# Set permissions to app files
|
|
|
|
|
chown -R $app: $final_path
|
2018-05-23 21:22:00 +02:00
|
|
|
|
# Restrict rights to log directory (needed by logrotate)
|
|
|
|
|
chmod g-w $final_path/log
|
2018-04-29 11:28:38 +02:00
|
|
|
|
fi
|
2018-04-14 09:33:23 +02:00
|
|
|
|
#=================================================
|
|
|
|
|
# SETUP SSOWAT
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
# If app is public, add url to SSOWat conf as skipped_uris
|
|
|
|
|
if [ $is_public -eq 1 ]; then
|
|
|
|
|
# unprotected_uris allows SSO credentials to be passed anyway.
|
|
|
|
|
ynh_app_setting_set "$app" skipped_uris "/"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
|
# RELOAD NGINX
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
systemctl reload nginx
|
|
|
|
|
|
|
|
|
|
#=================================================
|
2018-05-04 12:51:16 +02:00
|
|
|
|
# START UNICORN
|
2018-04-14 09:33:23 +02:00
|
|
|
|
#=================================================
|
|
|
|
|
|
2018-05-04 12:51:16 +02:00
|
|
|
|
if [ -n "$(uname -m | grep arm)" ] ; then
|
|
|
|
|
unicorn_workers=2
|
|
|
|
|
else
|
|
|
|
|
unicorn_workers=3
|
|
|
|
|
fi
|
|
|
|
|
# Wait for discourse to be fully started
|
2018-07-01 10:05:00 +02:00
|
|
|
|
ynh_check_starting "INFO -- : worker=$((unicorn_workers-1)) ready" "$final_path/log/unicorn.stderr.log" "240" "$app"
|
2018-06-03 17:21:50 +02:00
|
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
|
# DISABLE MAINTENANCE MODE
|
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
|
|
ynh_maintenance_mode_OFF
|