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
2018-09-30 18:11:11 +02:00

351 lines
12 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source _common.sh
source /usr/share/yunohost/helpers
# Load common variables for all scripts.
source _variables
#=================================================
# LOAD SETTINGS
#=================================================
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get $app domain)
path_url=$(ynh_app_setting_get $app path)
admin=$(ynh_app_setting_get $app admin)
language=$(ynh_app_setting_get $app language)
is_public=$(ynh_app_setting_get $app is_public)
final_path=$(ynh_app_setting_get $app final_path)
port=$(ynh_app_setting_get $app port)
export=$(ynh_app_setting_get $app export)
db_name=$(ynh_app_setting_get $app db_name)
mypads=$(ynh_app_setting_get $app mypads)
useldap=$(ynh_app_setting_get $app useldap)
overwrite_settings=$(ynh_app_setting_get $app overwrite_settings)
overwrite_credentials=$(ynh_app_setting_get $app overwrite_credentials)
overwrite_nginx=$(ynh_app_setting_get $app overwrite_nginx)
overwrite_systemd=$(ynh_app_setting_get $app overwrite_systemd)
# Optional parameters from config-panel feature
pad_config_nocolors=$(ynh_app_setting_get $app pad_config_nocolors)
pad_config_showlinenumbers=$(ynh_app_setting_get $app pad_config_showlinenumbers)
pad_config_chatandusers=$(ynh_app_setting_get $app pad_config_chatandusers)
pad_config_alwaysshowchat=$(ynh_app_setting_get $app pad_config_alwaysshowchat)
pad_config_show_markdown=$(ynh_app_setting_get $app pad_config_show_markdown)
#=================================================
# CHECK VERSION
#=================================================
# Wait for etherpad to be fully started
ynh_check_starting "You can access your Etherpad instance at" "/var/log/$app/etherpad.log" "120"
ynh_abort_if_up_to_date
#=================================================
# ENSURE DOWNWARD COMPATIBILITY
#=================================================
if [ "$is_public" = "Yes" ]; then
ynh_app_setting_set $app is_public 1 # Convert is_public to boolean
is_public=1
elif [ "$is_public" = "No" ]; then
ynh_app_setting_set $app is_public 0
is_public=0
fi
if [ -z "$db_name" ]; then # If db_name setting doesn't exist
db_name=$(ynh_sanitize_dbid $app)
ynh_app_setting_set $app db_name $db_name
fi
if [ -z "$abiword" ]; then # If abiword setting doesn't exist
abiword=0
ynh_app_setting_set $app abiword $abiword
fi
if [ -n "$abiword" ]; then # If abiword setting exists
if [ $abiword -eq 1 ]; then
export=abiword
fi
ynh_app_setting_set $app export $export
ynh_app_setting_delete $app abiword
fi
if [ -z "$export" ]; then # If export setting doesn't exist
export=none
ynh_app_setting_set $app export $export
fi
if [ -z "$mypads" ]; then # If mypads setting doesn't exist
mypads=1
ynh_app_setting_set $app mypads $mypads
fi
if [ -z "$useldap" ]; then # If useldap setting doesn't exist
useldap=0
ynh_app_setting_set $app useldap $useldap
fi
if [ -z "$path_url" ]; then # If path_url setting doesn't exist
path_url="/"
ynh_app_setting_set $app path $path_url
fi
# If overwrite_settings doesn't exist, create it
if [ -z "$overwrite_settings" ]; then
overwrite_settings=1
ynh_app_setting_set $app overwrite_settings $overwrite_settings
fi
# If overwrite_credentials doesn't exist, create it
if [ -z "$overwrite_credentials" ]; then
overwrite_credentials=1
ynh_app_setting_set $app overwrite_credentials $overwrite_credentials
fi
# If overwrite_nginx doesn't exist, create it
if [ -z "$overwrite_nginx" ]; then
overwrite_nginx=1
ynh_app_setting_set $app overwrite_nginx $overwrite_nginx
fi
# If overwrite_systemd doesn't exist, create it
if [ -z "$overwrite_systemd" ]; then
overwrite_systemd=1
ynh_app_setting_set $app overwrite_systemd $overwrite_systemd
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
# Backup the current version of the app
ynh_backup_before_upgrade
ynh_clean_setup () {
# Cleanup installatioNettoyage des résidus d'installation non pris en charge par le script remove.
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
#=================================================
# ACTIVATE MAINTENANCE MODE
#=================================================
ynh_maintenance_mode_ON
#=================================================
# STOP ETHERPAD
#=================================================
ynh_system_reload $app stop
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
ynh_setup_source "$final_path" # Download, check integrity and uncompress the source from app.src
#=================================================
# NGINX CONFIGURATION
#=================================================
# Overwrite the nginx configuration only if it's allowed
if [ $overwrite_nginx -eq 1 ]
then
if [ "$path_url" != "/" ]
then
ynh_replace_string "^#sub_path_only" "" "../conf/nginx.conf"
fi
ynh_replace_string "__PATH__/" "${path_url%/}/" "../conf/nginx.conf"
ynh_add_nginx_config
fi
#=================================================
# UPGRADE NODEJS
#=================================================
# Remove the old nvm helper.
if [ -d /opt/nvm ]
then
ynh_secure_remove "/opt/nvm"
sed --in-place "/NVM_DIR/d" /root/.bashrc
fi
ynh_install_nodejs $nodejs_version
#=================================================
# UPGRADE NPM MODULES
#=================================================
# Update the main modules of etherpad
(cd "$final_path/src"
npm cache clean
ynh_exec_warn_less npm update)
# Then update the additionnal modules
(cd "$final_path"
npm cache clean
while read node_module
do
echo "Update $node_module"
ynh_exec_warn_less npm install --upgrade $node_module || true
done <<< "$(ls -1 "$final_path/node_modules" | grep "^ep_")")
#=================================================
# SPECIFIC UPGRADE
#=================================================
# CONFIGURE ETHERPAD
#=================================================
# Overwrite the settings config file only if it's allowed
if [ $overwrite_settings -eq 1 ]
then
ynh_backup_if_checksum_is_different "$final_path/settings.json" # Verify the checksum and backup the file if it's different
cp ../conf/settings.json "$final_path/settings.json"
ynh_replace_string "__PORT__" "$port" "$final_path/settings.json"
if [ "$export" = "abiword" ]
then
abiword_path=`which abiword` # Get abiword binary path
ynh_replace_string "\"abiword\" : null" "\"abiword\" : \"$abiword_path\"" "$final_path/settings.json" # Renseigne l'emplacement de abiword dans la config de etherpad
elif [ "$export" = "libreoffice" ]
then
soffice_path=`which soffice` # Get soffice binary path
ynh_replace_string "\"soffice\" : null" "\"soffice\" : \"$soffice_path\"" "$final_path/settings.json" # Renseigne l'emplacement de abiword dans la config de etherpad
fi
if test -z $language; then
language=en # If upgrading from a version which doesn't support translations, set language to English by default
ynh_app_setting_set $app language $language
fi
ynh_replace_string "__LANGUAGE__" "$language" "$final_path/settings.json"
# Use ldap for mypads
if [ $mypads -eq 1 ] && [ $useldap -eq 1 ]
then
ynh_replace_string "//noldap\(.*\)" "\1 //useldap" "$final_path/settings.json"
fi
# Optional parameters from config-panel feature
if [ -n "$pad_config_nocolors" ]; then
ynh_replace_string "\(\"noColors\" *: \).*," "\1$pad_config_nocolors," "$final_path/settings.json"
fi
if [ -n "$pad_config_showlinenumbers" ]; then
ynh_replace_string "\(\"showLineNumbers\" *: \).*," "\1$pad_config_showlinenumbers," "$final_path/settings.json"
fi
if [ -n "$pad_config_chatandusers" ]; then
ynh_replace_string "\(\"chatAndUsers\" *: \).*," "\1$pad_config_chatandusers," "$final_path/settings.json"
fi
if [ -n "$pad_config_alwaysshowchat" ]; then
ynh_replace_string "\(\"alwaysShowChat\" *: \).*," "\1$pad_config_alwaysshowchat," "$final_path/settings.json"
fi
if [ -n "$pad_config_show_markdown" ]; then
ynh_replace_string "\(\"ep_markdown_default\" *: \).*," "\1$pad_config_show_markdown," "$final_path/settings.json"
fi
ynh_store_file_checksum "$final_path/settings.json" # Recalculate and store the config file checksum into the app settings
fi
# Overwrite the credentials config file only if it's allowed
if [ $overwrite_credentials -eq 1 ]
then
ynh_backup_if_checksum_is_different "$final_path/credentials.json" # Verify the checksum and backup the file if it's different
cp ../conf/credentials.json "$final_path/credentials.json"
ynh_replace_string "__DB_USER__" "$app" "$final_path/credentials.json"
db_pwd=$(ynh_app_setting_get $app mysqlpwd)
ynh_print_OFF; password=$(ynh_app_setting_get $app password); ynh_print_ON
ynh_replace_string "__DB_PWD__" "$db_pwd" "$final_path/credentials.json"
ynh_replace_string "__ADMIN__" "$admin" "$final_path/credentials.json"
ynh_print_OFF; ynh_replace_special_string "__PASSWD__" "$password" "$final_path/credentials.json"; ynh_print_ON
ynh_store_file_checksum "$final_path/credentials.json" # Recalculate and store the config file checksum into the app settings
fi
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_system_user_create $app $final_path # Create the dedicated user, if it doesn't exist
#=================================================
# SECURING FILES AND DIRECTORIES
#=================================================
# Set files ownership to etherpad
chown -R $app: $final_path
chmod 600 "$final_path/credentials.json" # Restrict access to credentials.json
chown $app -R /var/log/$app/etherpad.log
#=================================================
# UPGRADE FAIL2BAN
#=================================================
ynh_add_fail2ban_config "/var/log/nginx/$domain-access.log" "<HOST> .* \"POST /mypads/api/auth/login HTTP/1.1\" 400" 5
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_use_logrotate --non-append
#=================================================
# SETUP SYSTEMD
#=================================================
# Overwrite the systemd configuration only if it's allowed
if [ $overwrite_systemd -eq 1 ]
then
ynh_replace_string "__ENV_PATH__" "$PATH" "../conf/systemd.service"
ynh_add_systemd_config
fi
#=================================================
# SOME HACKS
#=================================================
if [ $mypads -eq 1 ]
then
mod_line=$(grep -nA5 "index.createOpenPad" $final_path/src/templates/index.html | grep "</div>" | cut -d '-' -f 1) # Recherche le /div situé sous le champs d'ouverture de pad.
sed -i "$mod_line s@div>@&\n\t<center><br><font size="5"><a href="./mypads/">Mypads</a></font></center>@" $final_path/src/templates/index.html # Pour ajouter un lien vers le plugin mypads depuis la page d'Etherpad.
fi
#=================================================
# SETUP SSOWAT
#=================================================
# Make app public if necessary
if [ $is_public -eq 1 ]; then
ynh_app_setting_set $app skipped_uris "/"
else
ynh_app_setting_set $app skipped_uris "/admin" # etherpad admin page doesn't support SSO...
fi
#=================================================
# RELOAD NGINX
#=================================================
ynh_system_reload nginx
#=================================================
# CHECK ETHERPAD STARTING
#=================================================
# Wait for etherpad to be fully started
ynh_check_starting "You can access your Etherpad instance at" "/var/log/$app/etherpad.log" "120"
#=================================================
# DEACTIVE MAINTENANCE MODE
#=================================================
ynh_maintenance_mode_OFF