1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/etherpad_mypads_ynh.git synced 2024-09-03 18:36:09 +02:00
etherpad_mypads_ynh/scripts/upgrade
Éric Gaspar 3a624fdfbd new path
2023-08-27 23:13:48 +02:00

360 lines
15 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --weight=20
export=$(ynh_app_setting_get --app=$app --key=export)
password=$(ynh_app_setting_get --app=$app --key=password)
mypads=$(ynh_app_setting_get --app=$app --key=mypads)
useldap=$(ynh_app_setting_get --app=$app --key=useldap)
abiword=$(ynh_app_setting_get --app=$app --key=abiword)
overwrite_settings=$(ynh_app_setting_get --app=$app --key=overwrite_settings)
overwrite_credentials=$(ynh_app_setting_get --app=$app --key=overwrite_credentials)
overwrite_nginx=$(ynh_app_setting_get --app=$app --key=overwrite_nginx)
overwrite_systemd=$(ynh_app_setting_get --app=$app --key=overwrite_systemd)
# Optional parameters from config-panel feature
pad_config_nocolors=$(ynh_app_setting_get --app=$app --key=pad_config_nocolors)
pad_config_showlinenumbers=$(ynh_app_setting_get --app=$app --key=pad_config_showlinenumbers)
pad_config_chatandusers=$(ynh_app_setting_get --app=$app --key=pad_config_chatandusers)
pad_config_alwaysshowchat=$(ynh_app_setting_get --app=$app --key=pad_config_alwaysshowchat)
pad_config_show_markdown=$(ynh_app_setting_get --app=$app --key=pad_config_show_markdown)
#=================================================
# CHECK VERSION
#=================================================
ynh_script_progression --message="Checking version..." --weight=1
# Wait for etherpad to be fully started
ynh_systemd_action --action=restart --line_match="You can access your Etherpad instance at" --log_path="/var/log/$app/etherpad.log" --timeout="120"
upgrade_type=$(ynh_check_app_version_changed)
#=================================================
# ACTIVATE MAINTENANCE MODE
#=================================================
ynh_script_progression --message="Activating maintenance mode..." --weight=2
ynh_maintenance_mode_ON
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# STOP SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Stopping a systemd service..." --weight=3
ynh_systemd_action --service_name=$app --action="stop"
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
ynh_script_progression --message="Ensuring downward compatibility..." --weight=2
# If abiword setting doesn't exist
if [ -z "$abiword" ]; then
abiword=0
ynh_app_setting_set --app=$app --key=abiword --value=$abiword
fi
# If abiword setting exists
if [ -n "$abiword" ]; then
if [ $abiword -eq 1 ]; then
export=abiword
fi
ynh_app_setting_set --app=$app --key=export --value=$export
ynh_app_setting_delete --app=$app --key=abiword
fi
# If export setting doesn't exist
if [ -z "$export" ]; then
export=none
ynh_app_setting_set --app=$app --key=export --value=$export
fi
# If mypads setting doesn't exist
if [ -z "$mypads" ]; then
mypads=1
ynh_app_setting_set --app=$app --key=mypads --value=$mypads
fi
# If useldap setting doesn't exist
if [ -z "$useldap" ]; then
useldap=0
ynh_app_setting_set --app=$app --key=useldap --value=$useldap
fi
# If path setting doesn't exist
if [ -z "$path" ]; then
path="/"
ynh_app_setting_set --app=$app --key=path --value=$path
fi
# If overwrite_settings doesn't exist, create it
if [ -z "$overwrite_settings" ]; then
overwrite_settings=1
ynh_app_setting_set --app=$app --key=overwrite_settings --value=$overwrite_settings
fi
# If overwrite_credentials doesn't exist, create it
if [ -z "$overwrite_credentials" ]; then
overwrite_credentials=1
ynh_app_setting_set --app=$app --key=overwrite_credentials --value=$overwrite_credentials
fi
# If overwrite_nginx doesn't exist, create it
if [ -z "$overwrite_nginx" ]; then
overwrite_nginx=1
ynh_app_setting_set --app=$app --key=overwrite_nginx --value=$overwrite_nginx
fi
# If overwrite_systemd doesn't exist, create it
if [ -z "$overwrite_systemd" ]; then
overwrite_systemd=1
ynh_app_setting_set --app=$app --key=overwrite_systemd --value=$overwrite_systemd
fi
# Support full Unicode in MySQL databases
ynh_mysql_connect_as --user=$db_user --password="$db_pwd" --database=$db_name \
<<< "ALTER DATABASE $db_name CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Upgrading source files..." --weight=4
# Download, check integrity, uncompress and patch the source from app.src
ynh_setup_source --dest_dir="$install_dir" --keep="settings.json credentials.json"
fi
chmod -R o-rwx "$install_dir"
chown -R $app:$app "$install_dir"
#=================================================
# UPGRADE DEPENDENCIES
#=================================================
ynh_script_progression --message="Upgrading dependencies..." --weight=5
ynh_install_nodejs --nodejs_version=$nodejs_version
ynh_use_nodejs
#=================================================
# NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Upgrading NGINX web server configuration..." --weight=2
# Overwrite the NGINX configuration only if it's allowed
if [ $overwrite_nginx -eq 1 ]
then
ynh_add_nginx_config
fi
#=================================================
# SPECIFIC UPGRADE
#=================================================
# HANDLE LOG FILES AND LOGROTATE
#=================================================
ynh_script_progression --message="Configuring log rotation..." --weight=1
# Create log directory
chown $app -R /var/log/$app
#=================================================
# CONFIGURE ETHERPAD
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ]
then
ynh_script_progression --message="Reconfiguring Etherpad..." --weight=3
# Overwrite the settings config file only if it's allowed
if [ $overwrite_settings -eq 1 ]
then
# Verify the checksum of a file, stored by `ynh_store_file_checksum` in the install script.
ynh_backup_if_checksum_is_different --file="$install_dir/settings.json"
cp ../conf/settings.json "$install_dir/settings.json"
ynh_replace_string --match_string="__PORT__" --replace_string="$port" --target_file="$install_dir/settings.json"
if [ "$export" = "abiword" ]
then
# Get abiword binary path
abiword_path=`which abiword`
# Set the path of Abiword into Etherpad config
ynh_replace_string --match_string="\"abiword\" : null" --replace_string="\"abiword\" : \"$abiword_path\"" --target_file="$install_dir/settings.json"
elif [ "$export" = "libreoffice" ]
then
# Get soffice binary path
soffice_path=`which soffice`
# Set the path of soffice into Etherpad config
ynh_replace_string --match_string="\"soffice\" : null" --replace_string="\"soffice\" : \"$soffice_path\"" --target_file="$install_dir/settings.json"
fi
if test -z "$language"; then
# If upgrading from a version which doesn't support translations, set language to English by default
language=en
ynh_app_setting_set --app=$app --key=language --value=$language
fi
ynh_replace_string --match_string="__LANGUAGE__" --replace_string="$language" --target_file="$install_dir/settings.json"
# Use LDAP for MyPads
if [ $mypads -eq 1 ] && [ $useldap -eq 1 ]
then
ynh_replace_string --match_string="//noldap" --replace_string="" --target_file="$install_dir/settings.json"
fi
# Optional parameters from config-panel feature
if [ -n "$pad_config_nocolors" ]; then
ynh_replace_string --match_string="\(\"noColors\" *: \).*," --replace_string="\1$pad_config_nocolors," --target_file="$install_dir/settings.json"
fi
if [ -n "$pad_config_showlinenumbers" ]; then
ynh_replace_string --match_string="\(\"showLineNumbers\" *: \).*," --replace_string="\1$pad_config_showlinenumbers," --target_file="$install_dir/settings.json"
fi
if [ -n "$pad_config_chatandusers" ]; then
ynh_replace_string --match_string="\(\"chatAndUsers\" *: \).*," --replace_string="\1$pad_config_chatandusers," --target_file="$install_dir/settings.json"
fi
if [ -n "$pad_config_alwaysshowchat" ]; then
ynh_replace_string --match_string="\(\"alwaysShowChat\" *: \).*," --replace_string="\1$pad_config_alwaysshowchat," --target_file="$install_dir/settings.json"
fi
if [ -n "$pad_config_show_markdown" ]; then
ynh_replace_string --match_string="\(\"ep_markdown_default\" *: \).*," --replace_string="\1$pad_config_show_markdown," --target_file="$install_dir/settings.json"
fi
# Recalculate and store the checksum of the file for the next upgrade.
ynh_store_file_checksum --file="$install_dir/settings.json"
fi
# Overwrite the credentials config file only if it's allowed
if [ $overwrite_credentials -eq 1 ]
then
ynh_add_config --template="../conf/credentials.json" --destination="$install_dir/credentials.json"
fi
fi
chmod 600 "$install_dir/settings.json"
chown $app:$app "$install_dir/settings.json"
chmod 600 "$install_dir/credentials.json"
chown $app:$app "$install_dir/credentials.json"
#=================================================
# SETUP SYSTEMD
#=================================================
ynh_script_progression --message="Upgrading systemd configuration..." --weight=2
# Create a dedicated systemd config
if [ $overwrite_systemd -eq 1 ]
then
ynh_add_systemd_config
fi
#=================================================
# INSTALL ETHERPAD'S PLUGINS
#=================================================
ynh_script_progression --message="Installing Etherpad plugins..." --weight=90
pushd "$install_dir"
# Add Left/Center/Right/Justify to lines of text in a pad
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install --no-save ep_align@${ep_align_version}
# Framapad - Adds author names to span titles
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install --no-save ep_author_hover@${ep_author_hover_version}
# Framapad - Adds comments on sidebar and link it to the text.
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install --no-save ep_comments_page@${ep_comments_page_version}
# Framapad - Displays paragraphs, sentences, words and characters counts.
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install --no-save ep_countable@${ep_countable_version}
# Framapad - Delete pads which were never edited
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install --no-save ep_delete_empty_pads@${ep_delete_empty_pads_version}
# Framapad - Apply colors to fonts
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install --no-save ep_font_color@${ep_font_color_version}
# Framapad - Adds heading support to Etherpad Lite.
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install --no-save ep_headings2@${ep_headings2_version}
# Framapad - Edit and Export as Markdown in Etherpad
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install --no-save ep_markdown@${ep_markdown_version}
if [ $mypads -eq 1 ]; then
# Framapad - Groups and private pads for Etherpad
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install --no-save ep_mypads@${mypads_version}
fi
# Framapad - Add support to do 'Spell checking'
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install --no-save ep_spellcheck@${ep_spellcheck_version}
# Framapad - Add support for Subscript and Superscript
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install --no-save ep_subscript_and_superscript@${ep_subscript_and_superscript_version}
# Framapad - User Pad Contents font size can be set in settings, this does not effect other peoples views
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH $ynh_npm install --no-save ep_font_size@${ep_font_size_version}
popd
#=================================================
# UPGRADE NPM MODULES
#=================================================
ynh_script_progression --message="Upgrading Etherpad..." --weight=60
pushd $install_dir
ynh_exec_warn_less ynh_exec_as $app env $ynh_node_load_PATH src/bin/installDeps.sh
popd
#=================================================
# ADD MYPADS LINK
#=================================================
if [ "$upgrade_type" == "UPGRADE_APP" ] && [ $mypads -eq 1 ]
then
# Find the /div just after the field to open a pad in order to add a link to MyPads plugin.
sed -i '157i<center><br><font size="4"><a href="./mypads/" style="text-decoration: none; color: #555">MyPads</a></font></center>' $install_dir/src/templates/index.html
fi
#=================================================
# GENERIC FINALIZATION
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_script_progression --message="Upgrading logrotate configuration..." --weight=2
# Use logrotate to manage app-specific logfile(s)
ynh_use_logrotate --non-append --specific_user=$app/$app
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
yunohost service add $app --description="Collaborative editor" --log="/var/log/$app/etherpad.log"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=9
ynh_systemd_action --service_name=$app --action=restart --line_match="You can access your Etherpad instance at" --log_path="/var/log/$app/etherpad.log" --timeout="120"
#=================================================
# UPGRADE FAIL2BAN
#=================================================
ynh_script_progression --message="Reconfiguring Fail2Ban..." --weight=8
# Create a dedicated Fail2Ban config
ynh_add_fail2ban_config --logpath="/var/log/nginx/$domain-access.log" --failregex="<HOST> .* .POST /mypads/api/auth/login HTTP/1.1. 400" --max_retry=5
#=================================================
# DEACTIVE MAINTENANCE MODE
#=================================================
ynh_script_progression --message="Disabling maintenance mode..." --weight=5
ynh_maintenance_mode_OFF
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Upgrade of $app completed" --last