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
2017-03-19 18:26:09 +01:00

186 lines
7.2 KiB
Bash

#!/bin/bash
#=================================================
# GENERIC STARTING
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source .fonctions
source /usr/share/yunohost/helpers
#=================================================
# LOAD SETTINGS
#=================================================
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get $app domain)
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)
abiword=$(ynh_app_setting_get $app abiword)
db_name=$(ynh_app_setting_get $app db_name)
#=================================================
# FIX OLD THINGS
#=================================================
if [ "$is_public" = "Yes" ]; then
ynh_app_setting_set $app is_public 1 # Fixe is_public en booléen
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 # Si db_name n'est pas renseigné dans app setting
db_name=$(ynh_make_valid_dbid $app)
ynh_app_setting_set $app db_name $db_name
fi
#=================================================
# BACKUP BEFORE UPGRADE THEN ACTIVE TRAP
#=================================================
BACKUP_BEFORE_UPGRADE # Backup the current version of the app
ynh_clean_setup () {
BACKUP_FAIL_UPGRADE # restore it if the upgrade fails
}
ynh_abort_if_errors # Active trap pour arrêter le script si une erreur est détectée.
#=================================================
# STANDARD UPGRADE STEPS
#=================================================
# DOWNLOAD, CHECK AND UNPACK SOURCE
#=================================================
SETUP_SOURCE # Télécharge la source, décompresse et copie dans $final_path
#=================================================
# NGINX CONFIGURATION
#=================================================
# Copie le fichier de config nginx
sudo cp ../conf/nginx.conf /etc/nginx/conf.d/$domain.d/$app.conf
# Modifie les variables dans le fichier de configuration nginx
sudo sed -i "s@__PORT__@$port@g" /etc/nginx/conf.d/$domain.d/$app.conf
#=================================================
# SPECIFIC UPGRADE
#=================================================
# CONFIGURE ETHERPAD
#=================================================
# Copie ou modification d'un fichier de config
CHECK_MD5_CONFIG "settings.json" "$final_path/settings.json" # Créé un backup du fichier de config si il a été modifié.
CHECK_MD5_CONFIG "credentials.json" "$final_path/credentials.json" # Créé un backup du fichier de config si il a été modifié.
sudo cp ../conf/settings.json "$final_path/settings.json"
sudo cp ../conf/credentials.json "$final_path/credentials.json"
sudo sed -i "s/__PORT__/$port/g" "$final_path/settings.json"
sudo sed -i "s/__DB_USER__/$app/g" "$final_path/credentials.json"
db_pwd=$(ynh_app_setting_get $app mysqlpwd)
password=$(ynh_app_setting_get $app password)
sudo sed -i "s/__DB_PWD__/$db_pwd/g" "$final_path/credentials.json"
sudo sed -i "s/__ADMIN__/$admin/g" "$final_path/credentials.json"
sudo sed -i "s/__PASSWD__/$password/g" "$final_path/credentials.json"
if [ "$abiword" -eq 1 ]
then
abiword_path=`which abiword` # Récupère l'emplacement de l'exécutable de abiword
sudo sed -i "s@\"abiword\" : null@\"abiword\" : \"$abiword_path\"@" "$final_path/settings.json" # Renseigne l'emplacement de abiword dans la config de etherpad
fi
if test -z $language; then
language=en # En cas d'upgrade d'une version ne gérant pas la langue, la langue est anglais par défaut
ynh_app_setting_set $app language $language
fi
sudo sed -i "s/__LANGUAGE__/$language/g" "$final_path/settings.json"
STORE_MD5_CONFIG "settings.json" "$final_path/settings.json" # Réenregistre la somme de contrôle du fichier de config
STORE_MD5_CONFIG "credentials.json" "$final_path/credentials.json" # Réenregistre la somme de contrôle du fichier de config
#=================================================
# CREATE DEDICATED USER
#=================================================
ynh_system_user_create $app # Create the dedicated user, if not exist
#=================================================
# SECURING FILES AND DIRECTORIES
#=================================================
# Les fichiers appartiennent à etherpad
sudo chown -R $app: $final_path
sudo chmod 600 "$final_path/credentials.json" # Restreint l'accès à credentials.json
sudo chown $app -R /var/log/$app/etherpad.log
#=================================================
# SETUP LOGROTATE
#=================================================
ynh_use_logrotate
#=================================================
# SETUP SYSTEMD
#=================================================
# Mise en place du script systemd
sudo systemctl stop $app
sudo cp ../conf/etherpad.service /etc/systemd/system/$app.service
sudo chown root: /etc/systemd/system/$app.service
sudo sed -i "s@__FINALPATH__@$final_path/@g" /etc/systemd/system/$app.service
sudo sed -i "s@__APP__@$app@g" /etc/systemd/system/$app.service
## Démarrage auto du service
sudo systemctl enable $app
#=================================================
# SOME HACKS
#=================================================
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.
sudo 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.
#=================================================
# START ETHERPAD IN BACKGROUND
#=================================================
# Démarre etherpad
WARNING echo "Démarrage d'etherpad"
tempfile="$(mktemp)"
tail -f -n1 /var/log/$app/etherpad.log > "$tempfile" & # Suit le démarrage dans le log
PID_TAIL=$! # Récupère le PID de la commande tail, qui est passée en arrière plan.
sudo systemctl start $app # Démarre etherpad. Le démarrage est fait le plus tôt possible, car il est très long...
#=================================================
# 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" # La page d'admin etherpad ne supporte pas le sso...
fi
#=================================================
# RELOAD NGINX
#=================================================
sudo systemctl reload nginx
#=================================================
# CHECK ETHERPAD STARTING
#=================================================
for i in `seq 1 60`
do # La boucle attend le démarrage d'etherpad. Ou 1 minute. Cette boucle évite simplement un 502 au début, car le démarrage est long...
if grep -q "You can access your Etherpad instance at" "$tempfile"; then
WARNING echo "Le service $app a démarré correctement."
break # Si le log annonce le démarrage d'etherpad, sort de la boucle.
fi
WARNING echo -n "."
sleep 1
done
echo ""
QUIET kill -s 15 $PID_TAIL # Arrête l'exécution de tail.
sudo rm "$tempfile"